Skip to content

Commit 457ffe5

Browse files
authored
Merge pull request #2381 from ppxyn1/main
[ppxyn1] WEEK 01 solutions
2 parents 0ac22f3 + 7db8b65 commit 457ffe5

4 files changed

Lines changed: 49 additions & 29 deletions

File tree

contains-duplicate/ppxyn1.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# idea: Hash
22

3+
# Ans 1
34
class Solution:
45
def containsDuplicate(self, nums: List[int]) -> bool:
56
count_dict={}
@@ -12,8 +13,10 @@ def containsDuplicate(self, nums: List[int]) -> bool:
1213
return False
1314

1415

15-
'''
16-
Trial and error
17-
Printing I/O inside the loop may cause Output Limit Exceeded
18-
'''
16+
# Ans 2
17+
# Time Complexity: O(n) (Set)
18+
class Solution:
19+
def containsDuplicate(self, nums: List[int]) -> bool:
20+
return len(nums) != len(set(nums))
21+
1922

longest-consecutive-sequence/ppxyn1.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# idea: Hash
2+
# start after checking left neightbour
3+
# Time complexity = O(n)
24
class Solution:
35
def longestConsecutive(self, nums: List[int]) -> int:
46
if not nums:
@@ -8,7 +10,7 @@ def longestConsecutive(self, nums: List[int]) -> int:
810
max_len = 1
911

1012
for num in num_set:
11-
if num - 1 not in num_set:
13+
if num - 1 not in num_set:
1214
current = num
1315
tmp = 1
1416
while current + 1 in num_set:

top-k-frequent-elements/ppxyn1.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# idea: dictonary
22

3+
# Ans 1
34
class Solution:
45
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
56
count_dict = {}
@@ -15,7 +16,19 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]:
1516
ans.append(sorted_items[i][0])
1617
return ans
1718

18-
'''
19-
Similar way : Using Counter() function
20-
'''
19+
20+
21+
# With Couter
22+
# Time Complexity: O(nlog(n))
23+
24+
from collections import Counter
25+
class Solution:
26+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
27+
count_dict = Counter(nums)
28+
count_dict = sorted(count_dict.items(), key=lambda x: x[1], reverse=True)
29+
ans = []
30+
for i in range(k):
31+
ans.append(count_dict[i][0])
32+
return ans
33+
2134

two-sum/ppxyn1.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# idea: For each number n in nums, check if (target - n) exists in the remaining elements.
22

3+
# Ans 1
34
class Solution:
45
def twoSum(self, nums: List[int], target: int) -> List[int]:
56
for idx, num in enumerate(nums):
@@ -8,27 +9,28 @@ def twoSum(self, nums: List[int], target: int) -> List[int]:
89
return [idx, nums.index(required_num, idx+1)]
910

1011

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-
'''
1812

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
13+
# Ans 2
14+
# idea : Two-pointer
15+
# Time Complexity : O(n log n)
16+
17+
class Solution:
18+
def twoSum(self, nums: List[int], target: int) -> List[int]:
19+
# Keep original indices !
20+
new_nums = [(idx, num) for idx, num in enumerate(nums)]
21+
new_nums.sort(key=lambda x: x[1])
22+
23+
left, right = 0, len(new_nums)-1
2324

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
25+
while left < right:
26+
idx_left, idx_right = new_nums[left][0], new_nums[right][0]
27+
val = new_nums[left][1] + new_nums[right][1]
28+
if val == target:
29+
return [idx_left, idx_right]
30+
elif val < target:
31+
left +=1
32+
else:
33+
right -=1
34+
return []
35+
3436

0 commit comments

Comments
 (0)