diff --git a/contains-duplicate/mandoolala.py b/contains-duplicate/mandoolala.py new file mode 100644 index 000000000..ff90b6981 --- /dev/null +++ b/contains-duplicate/mandoolala.py @@ -0,0 +1,23 @@ +""" +https://leetcode.com/problems/contains-duplicate/ +""" +from typing import List + +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + ''' + [Complexity] + Time: O(n) + Space: O(n) + ''' + return (len(nums) != len(set(nums))) + + ''' + alternative: + nums_set = set() + for num in nums: + if num in nums_set: + return True + nums_set.add(num) + return False + ''' diff --git a/house-robber/mandoolala.py b/house-robber/mandoolala.py new file mode 100644 index 000000000..86120f611 --- /dev/null +++ b/house-robber/mandoolala.py @@ -0,0 +1,25 @@ +from typing import List + +class Solution: + def rob(self, nums: List[int]) -> int: + ''' + [Complexity] + Time: O(n) + Space: O(n) + ''' + cnt = len(nums) + + if cnt == 1: + return nums[0] + if cnt == 2: + return max(nums[0], nums[1]) + + dp = [0] * cnt + dp[0] = nums[0] + dp[1] = max(nums[0], nums[1]) + + for i in range(2, cnt): + # skip: dp[i-1] + # rob: dp[i-2]+nums[i] + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]) + return max(dp) diff --git a/longest-consecutive-sequence/mandoolala.py b/longest-consecutive-sequence/mandoolala.py new file mode 100644 index 000000000..47fcee207 --- /dev/null +++ b/longest-consecutive-sequence/mandoolala.py @@ -0,0 +1,14 @@ +from typing import List + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + num_set = set(nums) + longest = 0 + for num in num_set: + if num - 1 in num_set: + continue + length = 1 + while num + length in num_set: + length += 1 + longest = max(length, longest) + return longest diff --git a/top-k-frequent-elements/mandoolala.py b/top-k-frequent-elements/mandoolala.py new file mode 100644 index 000000000..8584c0d45 --- /dev/null +++ b/top-k-frequent-elements/mandoolala.py @@ -0,0 +1,40 @@ +from typing import List + + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + ''' + Using (min)heap + [Complexity] + Time: O(log n) + Space: O(n+k) + ''' + from heapq import heappush, heappop + counter = {} + for num in nums: + counter[num] = counter.get(num, 0) + 1 + heap = [] + for num, freq in counter.items(): + heappush(heap, (freq, num)) + if len(heap) > k: + heappop(heap) + return [num for _, num in heap] + ''' + Using hash table + [Complexity] + Time: O(n log n) + Space: O(n) + ''' + ''' + countDict = {} + for num in nums: + if num in countDict: + countDict[num] += 1 + else: + countDict[num] = 1 + sortedDictList = sorted(countDict.items(), key=lambda item: item[1], reverse=True) + freqElements = [] + for i in range(k): + freqElements.append(sortedDictList[i][0]) + return freqElements + ''' diff --git a/two-sum/mandoolala.py b/two-sum/mandoolala.py new file mode 100644 index 000000000..81fa801b1 --- /dev/null +++ b/two-sum/mandoolala.py @@ -0,0 +1,27 @@ +from typing import List + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + # return indices of two numbers such that they add up to target + ''' + [Complexity] + Time: O(n) + Space: O(1) + ''' + nums_dict = {} + for idx, num in enumerate(nums): + remaining = target - num + if remaining in nums_dict: + return [idx, nums_dict[remaining]] + nums_dict[num] = idx + ''' + [Complexity] + Time: O(n^2) + Space: O(1) + + for i in range(0,len(nums)-1): + for j in range(i+1, len(nums)): + sum = nums[i] + nums[j] + if sum == target: + return [i, j] + '''