Skip to content

Commit 831b097

Browse files
authored
Merge pull request #1758 from rivkode/main
[rivkode] WEEK 02 solutions
2 parents e5c0669 + 3b67b3b commit 831b097

File tree

4 files changed

+220
-68
lines changed

4 files changed

+220
-68
lines changed

3sum/rivkode.py

Lines changed: 92 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
from typing import List, Tuple, Set
22

3+
class Solution(object):
4+
def threeSum(self, nums):
5+
result = set()
6+
v_set = {}
7+
8+
for i, v in enumerate(nums):
9+
v_set[v] = i
10+
11+
n = len(nums)
12+
for i in range(n):
13+
for j in range(i + 1, n):
14+
v = nums[i]
15+
w = nums[j]
16+
target = -(v + w)
17+
if target in v_set:
18+
k = v_set[target]
19+
if k != i and k != j:
20+
triplet = tuple(sorted([v, w, target]))
21+
result.add(triplet)
22+
23+
return list(result)
24+
25+
if __name__ == "__main__":
26+
solution = Solution()
27+
nums = [-1,0,1,2,-1,-4]
28+
result = solution.threeSum(nums)
29+
print(result)
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
# previous version
40+
341
# threeSum3()
442
# Time Complexity O(n ^ 2)
543
# - when sorting by sorted function(TimSort) for each string it takes O(nlogn)
@@ -8,58 +46,60 @@
846
# - when sorting takes O(1)
947

1048

11-
class Solution:
12-
def threeSum(self, nums: List[int]) -> list[tuple[int, ...]]:
13-
14-
triplets = set()
15-
for i in range(len(nums) - 2):
16-
for j in range(i + 1, len(nums)):
17-
for k in range(j + 1, len(nums)):
18-
if nums[i] + nums[j] + nums[k] == 0:
19-
triplet = [nums[i], nums[j], nums[k]]
20-
triplets.add(tuple(sorted(triplet)))
21-
22-
return list(triplets)
23-
24-
def threeSum2(self, nums: List[int]) -> list[tuple[int, ...]]:
25-
triplets = set()
26-
27-
for i in range(len(nums)):
28-
seen = set()
29-
for j in range(i + 1, len(nums)):
30-
res = -(nums[i] + nums[j])
31-
if res in seen:
32-
triplet = [nums[i], nums[j], res]
33-
triplets.add(tuple(sorted(triplet)))
34-
seen.add(nums[j])
35-
36-
return list(triplets)
37-
38-
def threeSum3(self, nums: List[int]) -> list(tuple[int, ...]):
39-
triplets = set()
40-
nums.sort()
41-
42-
for i in range(len(nums) - 2):
43-
l = i + 1
44-
r = len(nums) - 1
45-
46-
while l < r:
47-
res = nums[l] + nums[r] + nums[i]
48-
if res > 0:
49-
r -= 1
50-
elif res < 0:
51-
l += 1
52-
elif res == 0:
53-
triple = (nums[l], nums[r], nums[i])
54-
triplets.add(triple)
55-
l, r = l + 1, r - 1
56-
else:
57-
raise Exception
58-
return list(triplets)
49+
# class Solution:
50+
# def threeSum(self, nums: List[int]) -> list[tuple[int, ...]]:
5951

60-
if __name__ == "__main__":
61-
nums = [-1,0,1,2,-1,-4]
52+
# triplets = set()
53+
# for i in range(len(nums) - 2):
54+
# for j in range(i + 1, len(nums)):
55+
# for k in range(j + 1, len(nums)):
56+
# if nums[i] + nums[j] + nums[k] == 0:
57+
# triplet = [nums[i], nums[j], nums[k]]
58+
# triplets.add(tuple(sorted(triplet)))
59+
60+
# return list(triplets)
61+
62+
# def threeSum2(self, nums: List[int]) -> list[tuple[int, ...]]:
63+
# triplets = set()
64+
65+
# for i in range(len(nums)):
66+
# seen = set()
67+
# for j in range(i + 1, len(nums)):
68+
# res = -(nums[i] + nums[j])
69+
# if res in seen:
70+
# triplet = [nums[i], nums[j], res]
71+
# triplets.add(tuple(sorted(triplet)))
72+
# seen.add(nums[j])
73+
74+
# return list(triplets)
75+
76+
# def threeSum3(self, nums: List[int]) -> list(tuple[int, ...]):
77+
# triplets = set()
78+
# nums.sort()
79+
80+
# for i in range(len(nums) - 2):
81+
# l = i + 1
82+
# r = len(nums) - 1
83+
84+
# while l < r:
85+
# res = nums[l] + nums[r] + nums[i]
86+
# if res > 0:
87+
# r -= 1
88+
# elif res < 0:
89+
# l += 1
90+
# elif res == 0:
91+
# triple = (nums[l], nums[r], nums[i])
92+
# triplets.add(triple)
93+
# l, r = l + 1, r - 1
94+
# else:
95+
# raise Exception
96+
# return list(triplets)
97+
98+
# if __name__ == "__main__":
99+
# nums = [-1,0,1,2,-1,-4]
100+
101+
102+
# solution = Solution()
103+
# solution.threeSum3(nums)
62104

63105

64-
solution = Solution()
65-
solution.threeSum3(nums)

climbing-stairs/rivkode.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
11
# Time Complexity O(n)
22
# - traversing for loop takes O(n)
3+
# - same with 피보나치
34
# Space Complexity O(n)
45
# - appending for loop it takes O(n)
56

67
class Solution:
7-
def climbStairs(self, n: int) -> int:
8-
stage = [1, 2, 3]
98

10-
for i in range(3, 45):
11-
value = stage[i - 1] + stage[i - 2]
12-
stage.append(value)
9+
def climbStairs(self, n):
10+
"""
11+
:type n: int
12+
:rtype: int
13+
"""
1314

14-
return stage[n - 1]
15+
map = {1:1, 2:2}
16+
17+
18+
for i in range(3, n + 1):
19+
map[i] = map[i-1] + map[i-2]
20+
21+
return map[n]
22+
23+
24+
# def climbStairs(self, n: int) -> int:
25+
# stage = [1, 2, 3]
26+
27+
# for i in range(3, 45):
28+
# value = stage[i - 1] + stage[i - 2]
29+
# stage.append(value)
30+
31+
# return stage[n - 1]
1532

1633
if __name__ == "__main__":
1734
solution = Solution()
1835
result = solution.climbStairs(5)
1936
print(result)
2037

38+
39+
40+
41+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution(object):
2+
def productExceptSelf(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[int]
6+
"""
7+
8+
all_multi = 1
9+
zero_count = 0
10+
result = []
11+
12+
for v in nums:
13+
if v == 0:
14+
zero_count += 1
15+
continue
16+
all_multi *= v
17+
all_multi = int(all_multi)
18+
19+
if zero_count == 0:
20+
for v in nums:
21+
if v == 0:
22+
result.append(all_multi)
23+
else:
24+
cur = all_multi / v
25+
result.append(int(cur))
26+
elif zero_count == 1:
27+
for v in nums:
28+
if v == 0:
29+
result.append(all_multi)
30+
else:
31+
result.append(0)
32+
33+
if zero_count >= 2:
34+
return [0 for _ in range(len(nums))]
35+
36+
return result
37+
38+
39+
if __name__ == "__main__":
40+
solution = Solution()
41+
# nums = [1,2,3,4]
42+
# nums = [-1,1,0,-3,3]
43+
nums = [1,2,3,4,0,0]
44+
result = solution.productExceptSelf(nums)
45+
print(result)
46+
47+
48+
49+

valid-anagram/rivkode.py

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,68 @@
11
from typing import List
22

33
# Time Complexity O(n log n)
4-
# - when sorting by sorted function(TimSort) for each string it takes O(nlogn)
5-
# - traversing for loop takes O(n)
4+
# - check len first
5+
# - make set for one of list for each values
6+
# - if there's a duplication, then value += 1. This is a number of count.
7+
# - fetch value using set.get() method.
8+
# - minus 1 and check if it is less than 0 which means it's invalid. return false directly.
69
# Space Complexity O(n)
710
# - when sorting takes O(n)
811

912
class Solution:
1013
def isAnagram(self, s: str, t: str) -> bool:
11-
s_sorted = sorted(s)
12-
t_sorted = sorted(t)
1314

14-
for i in range(len(s_sorted)):
15-
if s_sorted[i] != t_sorted[i]:
16-
return False
17-
return True
15+
"""
16+
:type s: str
17+
:type t: str
18+
:rtype: bool
19+
"""
20+
s_list = list(s)
21+
t_list = list(t)
22+
23+
if len(s_list) != len(t_list):
24+
return False
25+
26+
t_set = {}
27+
for t_val in t_list:
28+
cur = t_set.get(t_val, 0)
29+
cur += 1
30+
t_set[t_val] = cur
31+
32+
33+
flag = True
34+
35+
for s_val in s_list:
36+
if t_set.get(s_val):
37+
cur = t_set.get(s_val)
38+
cur -= 1
39+
t_set[s_val] = cur
40+
if cur < 0:
41+
flag = False
42+
break
43+
else:
44+
flag = False
45+
break
46+
47+
return flag
48+
49+
# previous logic
50+
# s_sorted = sorted(s)
51+
# t_sorted = sorted(t)
52+
53+
# for i in range(len(s_sorted)):
54+
# if s_sorted[i] != t_sorted[i]:
55+
# return False
56+
# return True
57+
1858

1959
if __name__ == "__main__":
2060
solution = Solution()
2161

22-
s = "nagaram"
23-
t = "anagram"
62+
s = "a"
63+
t = "ab"
2464

2565
result = solution.isAnagram(s, t)
2666
print(result)
67+
68+

0 commit comments

Comments
 (0)