Given a non-empty array of integers, return the k most frequent elements.
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:
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
# Time complexity: O(n)
# Space complexity: O(n)
bag = {}
for elt in nums:
if elt not in bag:
bag[elt] = 1
else:
bag[elt] += 1
# Index of array = frequencies
freq = [[] for i in range(len(nums) + 1)]
for key, val in bag.items():
freq[val].append(key)
res = []
i, n = 0, len(freq)
while i < n and k != 0:
if len(freq[n-1-i]) > 0:
res += freq[n-1-i]
k -= len(freq[n-1-i])
i += 1
return res
class Solution:
import heapq
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
# N = # of elt in nums
# k = size of heap.
# Time complexity: O(Nlog(k)).
# Space complexity: O(N+k)
# 1. build hash map : character and how often it appears
# O(N) time
hashmap = {}
for elt in nums:
if elt in hashmap:
hashmap[elt] += 1
else:
hashmap[elt] = 1
# 2-3. build heap of top k frequent elements and
# convert it into an output array
# O(N log k) time
return heapq.nlargest(k, hashmap.keys(), key=hashmap.get)
class Solution:
def partition(self, L, frequency, lo, hi, k):
if len(L) == k:
return L
else:
pivot, i, j = lo, lo, hi
while i < j:
while i < hi and frequency[L[i]] <= frequency[L[pivot]]:
i += 1
while j > lo and frequency[L[j]] >= frequency[L[pivot]]:
j -= 1
if i < j:
L[i], L[j] = L[j], L[i]
L[pivot], L[j] = L[j], L[pivot]
rightPart = L[pivot+1:]
return self.partition(rightPart, frequency, 0, len(rightPart)-1, k)
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
# N = # of elt in nums
# k = size of heap.
# Time complexity: O(N).
# Space complexity: O(N)
# 1. build hash map : character and how often it appears
# and array of unique elements.
# O(N) time
hashmap = {}
unq_elts = []
for elt in nums:
if elt in hashmap:
hashmap[elt] += 1
else:
hashmap[elt] = 1
unq_elts.append(elt)
# 2. Choose a random pivot and put it in its place in a sorted array
rightPart = self.partition(unq_elts, hashmap, 0, len(unq_elts)-1, k)
# 3. Return the k most frequent elements from the right part array.
return rightPart
https://leetcode.com/problems/find-k-closest-elements/ Naive def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]: L = sorted([(abs(elt - x), elt) for elt in arr], key=lambda tup: tup[0]) return sorted([tup[1] for tup in L[:k]]) Opti
Sep 23, 2022Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum element in the stack. Example: MinStack minStack = new MinStack();
Jun 27, 2022Given a string containing just the characters $($, $)$, ${$, $}$, $[$ and $]$, determine if the input string is valid. An input string is valid if: 1. Open brackets must be closed by the same type of brackets. 2. Open brackets must be closed in the correct order. Note that an empty string is also considered valid. Example 1:
Jun 27, 2022Solution 1 Time complexity: O(n³) Space complexity: O(n) class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: n = len(nums) if n < 3: return []
Apr 23, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up