Skip to content

Commit ced8e07

Browse files
authored
Merge pull request #625 from Chaedie/main
[Chaedie] Week 1
2 parents b901b4e + dd26a7e commit ced8e07

File tree

5 files changed

+231
-0
lines changed

5 files changed

+231
-0
lines changed

โ€Žcontains-duplicate/Chaedie.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
'''
4+
ํ’€์ด:
5+
์ค‘๋ณต๋œ ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ์ฐพ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
6+
7+
hash set ์œผ๋กœ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ณ 
8+
๊ธฐ์กด nums ์˜ ๊ธธ์ด์™€ ์ค‘๋ณต ์ œ๊ฑฐ๋œ nums_set ์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™์€์ง€ return ํ–ˆ์Šต๋‹ˆ๋‹ค.
9+
10+
์‹œ๊ฐ„ ๋ณต์žก๋„:
11+
O(n) - has set ์„ ๋งŒ๋“œ๋Š” ์‹œ๊ฐ„
12+
13+
๊ณต๊ฐ„ ๋ณต์žก๋„:
14+
O(n) - n๊ฐœ์˜ ์š”์†Œ๋ฅผ set์— ๋‹ด๊ธฐ ๋•Œ๋ฌธ
15+
'''
16+
17+
18+
class Solution:
19+
def containsDuplicate(self, nums: List[int]) -> bool:
20+
nums_set = set(nums)
21+
return len(nums_set) != len(nums)

โ€Žhouse-robber/Chaedie.py

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
'''
2+
Solution:
3+
์Šค์Šค๋กœ ํ’€์ง€ ๋ชปํ•ด ํ•™์Šต๋งŒ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.
4+
๋‹ค์Œ ๊ธฐํšŒ์— ๋‹ค์‹œ ํ’€์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
5+
'''
6+
class Solution:
7+
def rob(self, nums: List[int]) -> int:
8+
prev, curr = 0, 0
9+
for num in nums:
10+
prev, curr = curr, max(num + prev, curr)
11+
12+
return curr
13+
14+
15+
# class Solution:
16+
# '''
17+
# 4. ๋‹ฌ๋ ˆ์˜ ์ฝ”๋“œ ํ’€์ด - DP, prev, curr
18+
# O(n) time
19+
# O(1) space
20+
# '''
21+
22+
# # ๋‹ฌ๋ ˆ์˜ ์ฝ”๋“œ ํ’€์ด - DP, prev, cur
23+
24+
# def rob(self, nums: List[int]) -> int:
25+
# prev, curr = 0, 0
26+
# for num in nums:
27+
# prev, curr = curr, max(num + prev, curr)
28+
29+
# return curr
30+
31+
# '''
32+
# 3. ๋‹ฌ๋ ˆ์˜ ์ฝ”๋“œ ํ’€์ด - DP
33+
# [1,2,3,1]
34+
# [1, 2, 3, 1]
35+
# DP:[0, 1, 2, 4, 4]
36+
# MAX(1 + DP[2], DP[1]) = MAX(1 + 2, 4) = 4
37+
# '''
38+
# # ๋‹ฌ๋ ˆ์˜ ์ฝ”๋“œ ํ’€์ด - DP
39+
# # def rob(self, nums: List[int]) -> int:
40+
# # dp = [0] * (len(nums) + 1)
41+
# # dp[1] = nums[0]
42+
# # for n in range(2,len(nums) + 1):
43+
# # dp[n] = max(nums[n - 1] + dp[n - 2], dp[n - 1])
44+
# # return dp[-1]
45+
# '''
46+
# 2. ๋‹ฌ๋ ˆ์˜ ์ฝ”๋“œ ํ’€์ด - ์žฌ๊ท€, ๋ฉ”๋ชจ์ด์ œ์ด์…˜
47+
# time: O(n)
48+
# space: O(n)
49+
# '''
50+
# # ๋‹ฌ๋ ˆ์˜ ์ฝ”๋“œ ํ’€์ด - ์žฌ๊ท€, ๋ฉ”๋ชจ์ด์ œ์ด์…˜
51+
# # def rob(self, nums: List[int]) -> int:
52+
# # memo = {}
53+
54+
# # def dfs(start):
55+
# # if start in memo:
56+
# # return memo[start]
57+
# # if not start < len(nums):
58+
# # memo[start] = 0
59+
# # else:
60+
# # memo[start] = max(nums[start] + dfs(start + 2), dfs(start + 1))
61+
# # return memo[start]
62+
# # return dfs(0)
63+
# '''
64+
# 1. ๋‹ฌ๋ ˆ์˜ ์ฝ”๋“œ ํ’€์ด - ์žฌ๊ท€
65+
# time: O(2^n)
66+
# space: O(n)
67+
68+
# F([1,2,3,1]) => MAX(1 + F([3,1], f([2,3,1])))
69+
# F([3,1]) => MAX(3 + F([]), F([1]))
70+
# F([]) => 0
71+
# F([1]) => MAX(1 + F([]), F([])) => MAX(1 + 0, 0) => 1
72+
# F([]) => 0
73+
# F([]) => 0
74+
# F([2,3,1]) => MAX(2 + F([1]), F([3,1]))
75+
# F([1]) => MAX(1 + F([]), F([])) => MAX(1 + 0, 0) => 1
76+
# F([]) => 0
77+
# F([]) => 0
78+
# F([3,1]) => MAX(3 + F([]), F([1]))
79+
# F([]) => 0
80+
# F([1]) => MAX(1 + F([]), F([])) => MAX(1 + 0, 0) => 1
81+
# F([]) => 0
82+
# F([]) => 0
83+
# ์žฌ๊ท€๊ฐ€ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ๋‹ค.
84+
# ๋ฉ”๋ชจ์ด์ œ์ด์…˜์œผ๋กœ ๊ธฐ์–ตํ•ด๋‘๋ฉด ๋ฐ˜๋ณต์€ ์Šคํ‚ตํ•  ์ˆ˜ ์žˆ๋‹ค.
85+
# '''
86+
# # ๋‹ฌ๋ ˆ์˜ ์ฝ”๋“œ ํ’€์ด - ์žฌ๊ท€
87+
# # def rob(self, nums: List[int]) -> int:
88+
89+
# # def dfs(start):
90+
# # if not start < len(nums):
91+
# # return 0
92+
# # return max(nums[start] + dfs(start + 2), dfs(start + 1))
93+
# # return dfs(0)
94+
95+
# # neetcode ํ’€์ด - DP, ์ดํ•ด์•ˆ๋จ...
96+
# # def rob(self, nums: List[int]) -> int:
97+
# # rob1, rob2 = 0, 0
98+
# # # [rob1, rob2, n, n+1, ...]
99+
# # for n in nums:
100+
# # temp = max(n + rob1, rob2)
101+
# # rob1 = rob2
102+
# # rob2 = temp
103+
# # return rob2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Solution:
3+
0. i will use prev, cur pointers in for loop,
4+
so i have to get rid of edge case when len of nums is at most 1
5+
1. use hash set for remove duplicate elements
6+
2. sort list
7+
3. iterate sorted_nums and use two pointers (prev, cur)
8+
compare prev and cur and count if the difference is 1
9+
4. use max method, memorize maxCount
10+
5. return maxCount
11+
12+
Time Complexity:
13+
1. remove duplicate by hash set -> O(n)
14+
2. sort the list -> O(n log n)
15+
3. iterate sorted_nums -> O(n)
16+
17+
so time complexity of this solution will be O(n log n)
18+
19+
Space Complexity:
20+
1. set() -> O(n)
21+
2. sorted_nums -> O(n)
22+
3. count , maxCount -> O(1)
23+
24+
space complexity of this solution will be O(n)
25+
"""
26+
27+
28+
class Solution:
29+
def longestConsecutive(self, nums: List[int]) -> int:
30+
if len(nums) <= 1:
31+
return len(nums)
32+
33+
sorted_nums = sorted(list(set(nums)))
34+
35+
count = 1
36+
maxCount = 1
37+
for i in range(1, len(sorted_nums)):
38+
prev = sorted_nums[i - 1]
39+
cur = sorted_nums[i]
40+
if prev + 1 == cur:
41+
count += 1
42+
maxCount = max(count, maxCount)
43+
else:
44+
count = 1
45+
46+
return maxCount
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Solution:
3+
1. make a dictionary of (index, Frequency)
4+
2. sort the items of dictionary by Frequency
5+
3. return list of Top k Frequent Elements
6+
7+
Time Complexity:
8+
1. iterate nums for counting -> O(n)
9+
2. sort -> O(n log n)
10+
3. iterate list for making return value -> O(n)
11+
12+
So Time complexity of this solution is O(n log n)
13+
14+
Space Complexity:
15+
1. dictionary for counting frequency of nums -> O(n)
16+
2. Timsort's space overhead -> O(n)
17+
3. sorted List -> O(n)
18+
19+
Space complexity of this solution is O(n)
20+
"""
21+
22+
23+
class Solution:
24+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
25+
counts = defaultdict(int)
26+
for num in nums:
27+
counts[num] += 1
28+
29+
result = sorted(counts.items(), key=lambda x: x[1], reverse=True)
30+
return list(map(lambda x: x[0], result[:k]))

โ€Žvalid-palindrome/Chaedie.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
ํ’€์ด:
3+
1) lower case๋กœ ๋ณ€ํ˜•ํ•ฉ๋‹ˆ๋‹ค.
4+
2) alpha numeric ์ธ ๋ฌธ์ž๋งŒ string ๋ฐฐ์—ด์— ๋‹ด์•„์ค๋‹ˆ๋‹ค.
5+
3) string ๋ฐฐ์—ด๊ณผ string ๋ฐฐ์—ด์˜ ์—ญ์ˆœ ๋ฐฐ์—ด์„ ๋น„๊ตํ•ด์„œ return ํ•ฉ๋‹ˆ๋‹ค.
6+
7+
์‹œ๊ฐ„ ๋ณต์žก๋„:
8+
1) s.lower() -> O(n) - ๊ฐ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ lower case๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.
9+
2) for char in s: -> O(n) - ๊ฐ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด isalnum() ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
10+
3) string.append(char) -> O(1)
11+
4) string[::-1] -> O(n) - ๊ฐ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค.
12+
13+
๊ฒฐ๋ก : O(4n) ์ด๋ฏ€๋กœ O(n) ์ž…๋‹ˆ๋‹ค.
14+
15+
๊ณต๊ฐ„ ๋ณต์žก๋„:
16+
1) string ๋ฐฐ์—ด - O(n)
17+
2) string[::-1] - O(n)
18+
19+
๊ฒฐ๋ก : O(n) ์ž…๋‹ˆ๋‹ค.
20+
"""
21+
22+
23+
class Solution:
24+
def isPalindrome(self, s: str) -> bool:
25+
string = []
26+
s = s.lower()
27+
for char in s:
28+
if char.isalnum():
29+
string.append(char)
30+
31+
return string == string[::-1]

0 commit comments

Comments
ย (0)