본문 바로가기

배열16

[leetcode 215] Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: 5 Example 2: Input: [3,2,3,1,2,4,5,5,6] and k = 4 Output: 4 Note: You may assume k is always valid, 1 ≤ k ≤ array's length. 문제풀이: - k번째 큰 수를 찾는다. 중복된 수도 존재! - 소팅한 후에 뒤에서 부터 숫자를 찾는다. class Solution { public:.. 2020. 11. 4.
[leetcode 169] Majority Element Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Example 1: Input: [3,2,3] Output: 3 Example 2: Input: [2,2,1,1,1,2,2] Output: 2 문제 풀이: 난이도 easy 처음에 맵을 써서 풀었는데 그것보다 더 쉬운 방법이 있었다...! 소팅한 다음 중간에 위치한 값을 리턴하면 됐다.. class Solution .. 2020. 11. 1.
[leetcode 238] Product of Array Except Self Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Example: Input: [1,2,3,4] Output: [24,12,8,6] Constraint: It's guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer. Note: Please solve it without division and.. 2020. 10. 31.
[leetcode 121] Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. Note that you cannot sell a stock before you buy one. Example 1: Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day .. 2020. 10. 22.