본문 바로가기

알고리즘 문제풀이/leetcode142

[leetcode 347] Top K Frequent Elements Given a non-empty array of integers, return the k most frequent elements. 주어진 배열에서 가장 자주 등장하는 요소 k번째까지 리턴하기 Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1 Output: [1] Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must be better than O(n log n), where n is the array's size. It's guarante.. 2020. 10. 18.
[leetcode 32] Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. 가장 긴 well formed parentheses 찾기 Example 1: Input: s = "(()" Output: 2 Explanation: The longest valid parentheses substring is "()". Example 2: Input: s = ")()())" Output: 4 Explanation: The longest valid parentheses substring is "()()". Example 3: Input: s = "" Out.. 2020. 10. 17.
[leetcode 33] Search in Rotated Sorted Array You are given an integer array nums sorted in ascending order, and an integer target. Suppose that nums is rotated at some pivot unknown to you beforehand (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). If target is found in the array return its index, otherwise, return -1. 본래 오름차순인 배열이 특정 위치를 중심으로 회전되어 있다. 주어진 target의 위치를 찾아라. Example 1: Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4.. 2020. 10. 17.
[leetcode 21] Merge Two Sorted Lists Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists. 오름차순순으로 리스트를 합치자. Example 1: Input: l1 = [1,2,4], l2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2: Input: l1 = [], l2 = [] Output: [] Example 3: Input: l1 = [], l2 = [0] Output: [0] 풀이: -포인터만 잘 활용한다면 쉬운 문제이다. - 난이도: easy /** * Definition for singly-lin.. 2020. 10. 17.