File tree 5 files changed +129
-0
lines changed
longest-consecutive-sequence
5 files changed +129
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ https://leetcode.com/problems/contains-duplicate/
3
+ """
4
+ from typing import List
5
+
6
+ class Solution :
7
+ def containsDuplicate (self , nums : List [int ]) -> bool :
8
+ '''
9
+ [Complexity]
10
+ Time: O(n)
11
+ Space: O(n)
12
+ '''
13
+ return (len (nums ) != len (set (nums )))
14
+
15
+ '''
16
+ alternative:
17
+ nums_set = set()
18
+ for num in nums:
19
+ if num in nums_set:
20
+ return True
21
+ nums_set.add(num)
22
+ return False
23
+ '''
Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+ class Solution :
4
+ def rob (self , nums : List [int ]) -> int :
5
+ '''
6
+ [Complexity]
7
+ Time: O(n)
8
+ Space: O(n)
9
+ '''
10
+ cnt = len (nums )
11
+
12
+ if cnt == 1 :
13
+ return nums [0 ]
14
+ if cnt == 2 :
15
+ return max (nums [0 ], nums [1 ])
16
+
17
+ dp = [0 ] * cnt
18
+ dp [0 ] = nums [0 ]
19
+ dp [1 ] = max (nums [0 ], nums [1 ])
20
+
21
+ for i in range (2 , cnt ):
22
+ # skip: dp[i-1]
23
+ # rob: dp[i-2]+nums[i]
24
+ dp [i ] = max (dp [i - 1 ], dp [i - 2 ] + nums [i ])
25
+ return max (dp )
Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+ class Solution :
4
+ def longestConsecutive (self , nums : List [int ]) -> int :
5
+ num_set = set (nums )
6
+ longest = 0
7
+ for num in num_set :
8
+ if num - 1 in num_set :
9
+ continue
10
+ length = 1
11
+ while num + length in num_set :
12
+ length += 1
13
+ longest = max (length , longest )
14
+ return longest
Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+
4
+ class Solution :
5
+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
6
+ '''
7
+ Using (min)heap
8
+ [Complexity]
9
+ Time: O(log n)
10
+ Space: O(n+k)
11
+ '''
12
+ from heapq import heappush , heappop
13
+ counter = {}
14
+ for num in nums :
15
+ counter [num ] = counter .get (num , 0 ) + 1
16
+ heap = []
17
+ for num , freq in counter .items ():
18
+ heappush (heap , (freq , num ))
19
+ if len (heap ) > k :
20
+ heappop (heap )
21
+ return [num for _ , num in heap ]
22
+ '''
23
+ Using hash table
24
+ [Complexity]
25
+ Time: O(n log n)
26
+ Space: O(n)
27
+ '''
28
+ '''
29
+ countDict = {}
30
+ for num in nums:
31
+ if num in countDict:
32
+ countDict[num] += 1
33
+ else:
34
+ countDict[num] = 1
35
+ sortedDictList = sorted(countDict.items(), key=lambda item: item[1], reverse=True)
36
+ freqElements = []
37
+ for i in range(k):
38
+ freqElements.append(sortedDictList[i][0])
39
+ return freqElements
40
+ '''
Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+ class Solution :
4
+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
5
+ # return indices of two numbers such that they add up to target
6
+ '''
7
+ [Complexity]
8
+ Time: O(n)
9
+ Space: O(1)
10
+ '''
11
+ nums_dict = {}
12
+ for idx , num in enumerate (nums ):
13
+ remaining = target - num
14
+ if remaining in nums_dict :
15
+ return [idx , nums_dict [remaining ]]
16
+ nums_dict [num ] = idx
17
+ '''
18
+ [Complexity]
19
+ Time: O(n^2)
20
+ Space: O(1)
21
+
22
+ for i in range(0,len(nums)-1):
23
+ for j in range(i+1, len(nums)):
24
+ sum = nums[i] + nums[j]
25
+ if sum == target:
26
+ return [i, j]
27
+ '''
You can’t perform that action at this time.
0 commit comments