File tree Expand file tree Collapse file tree 5 files changed +146
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +146
-0
lines changed Original file line number Diff line number Diff line change 1+ # idea: Hash
2+
3+ class Solution :
4+ def containsDuplicate (self , nums : List [int ]) -> bool :
5+ count_dict = {}
6+ for i in range (len (nums )):
7+ # print(count_dict)
8+ if nums [i ] in count_dict .keys ():
9+ return True
10+ else :
11+ count_dict [nums [i ]] = 1
12+ return False
13+
14+
15+ '''
16+ Trial and error
17+ Printing I/O inside the loop may cause Output Limit Exceeded
18+ '''
19+
Original file line number Diff line number Diff line change 1+ # idea: DP Top-down / Bottom-up
2+ # DP was comp up on my head, but it is not easy for me to utilse recursive function.
3+
4+
5+ class Solution :
6+ def rob (self , nums : List [int ]) -> int :
7+ length = len (nums )
8+ if length == 0 :
9+ return 0
10+ if length == 1 :
11+ return nums [0 ]
12+
13+ lst = [0 ]* length
14+ lst [0 ] = nums [0 ]
15+ lst [1 ] = max (nums [0 ], nums [1 ])
16+
17+ for i in range (2 , length ):
18+ lst [i ] = max (lst [i - 1 ], lst [i - 2 ] + nums [i ])
19+
20+ return lst [- 1 ]
21+
Original file line number Diff line number Diff line change 1+ # idea: Hash
2+ class Solution :
3+ def longestConsecutive (self , nums : List [int ]) -> int :
4+ if not nums :
5+ return 0
6+
7+ num_set = set (nums )
8+ max_len = 1
9+
10+ for num in num_set :
11+ if num - 1 not in num_set :
12+ current = num
13+ tmp = 1
14+ while current + 1 in num_set :
15+ current += 1
16+ tmp += 1
17+ if tmp > max_len :
18+ max_len = tmp
19+ return max_len
20+
21+
22+
23+
24+
25+ '''
26+ Trial and error
27+
28+ The code below was passed on Leecode since their Constraints was 0 <= nums.length <= 10^5.
29+ But I realised that I missed another constraint they mentioned, which is "You must write an algorithm that runs in O(n) time."
30+ '''
31+ # class Solution:
32+ # def longestConsecutive(self, nums: List[int]) -> int:
33+ # if len(nums)==0:
34+ # return 0
35+ # sorted_nums = sorted(list(set(nums)))
36+ # # sorted_nums = sorted(nums) # duplicate
37+ # max_len = 1
38+ # tmp = 1
39+
40+ # for i in range(1, len(sorted_nums)):
41+ # if sorted_nums[i] == sorted_nums[i - 1] + 1:
42+ # tmp += 1
43+ # else:
44+ # max_len = max(max_len, tmp)
45+ # tmp = 1
46+ # ans = max(max_len, tmp)
47+ # return ans
48+
49+
50+
51+
Original file line number Diff line number Diff line change 1+ # idea: dictonary
2+
3+ class Solution :
4+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
5+ count_dict = {}
6+ ans = []
7+ for idx , val in enumerate (nums ):
8+ if val not in count_dict :
9+ count_dict [val ] = 1
10+ else :
11+ count_dict [val ] += 1
12+ sorted_items = sorted (count_dict .items (), key = lambda x : x [1 ], reverse = True ) #sorted return list / dict.items() is tuple
13+ # print(sorted_items)
14+ for i in range (k ):
15+ ans .append (sorted_items [i ][0 ])
16+ return ans
17+
18+ '''
19+ Similar way : Using Counter() function
20+ '''
21+
Original file line number Diff line number Diff line change 1+ # idea: For each number n in nums, check if (target - n) exists in the remaining elements.
2+
3+ class Solution :
4+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
5+ for idx , num in enumerate (nums ):
6+ required_num = target - num
7+ if required_num in nums [idx + 1 :]:
8+ return [idx , nums .index (required_num , idx + 1 )]
9+
10+
11+ '''
12+ Trial and error
13+ idea : two pointer
14+ I struggled to handle the indices of the original array after sorting it.
15+ The code below fails when negative numbers are involved.
16+ I realized it would be tricky to solve this problem with the two-pointer.
17+ '''
18+
19+ # class Solution:
20+ # def twoSum(self, nums: List[int], target: int) -> List[int]:
21+ # sorted_nums = sorted(nums)
22+ # left, right = 0, len(nums) - 1
23+
24+ # while left < right:
25+ # s = sorted_nums[left] + sorted_nums[right]
26+ # if s == target:
27+ # left_idx = nums.index(sorted_nums[left])
28+ # right_idx = nums.index(sorted_nums[right], left_idx + 1)
29+ # return [left_idx, right_idx]
30+ # elif s < target:
31+ # left += 1
32+ # else:
33+ # right -= 1
34+
You can’t perform that action at this time.
0 commit comments