Skip to content

Commit 0e2b7c0

Browse files
authored
Merge pull request #1793 from jongwanra/main
[jongwanra] WEEK 03 solutions
2 parents 5afbe53 + 6d46aaf commit 0e2b7c0

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

โ€Žcombination-sum/jongwanra.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
[Problem]
3+
https://leetcode.com/problems/combination-sum/
4+
5+
candidates: unique array of integers
6+
return a list of all unique combinations of candidates == target
7+
any order
8+
9+
ํ•˜๋‚˜์˜ ์ˆซ์ž๋Š” candidates์—์„œ ๋ฌด์ œํ•œ์œผ๋กœ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค.
10+
๋‘ ์กฐํ•ฉ์ด ์„œ๋กœ ๋‹ค๋ฅด๋‹ค๊ณ  ๊ฐ„์ฃผ๋˜๋Š” ์กฐ๊ฑด์€, ์„ ํƒ๋œ ์ˆซ์ž ์ค‘ ์ ์–ด๋„ ํ•˜๋‚˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฅผ ๋•Œ์ด๋‹ค.
11+
[Brainstorming]
12+
DFS๋ฅผ ์ด์šฉํ•ด์„œ Combination์„ ๋งŒ๋“ ๋‹ค.
13+
์ข…๋ฃŒ์กฐ๊ฑด: target == sum || target > sum
14+
15+
[Plan]
16+
1. candidates๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
17+
2. DFS
18+
19+
[Complexity]
20+
N -> candidates.length
21+
M -> approximately target divided by the smallest candidate. -> target / min(candidates)
22+
Time: O(N^M)
23+
Space: O(N + M)
24+
- ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ: ๊นŠ์ด๋Š” ์ตœ๋Œ€ target / min(candidates)
25+
- chosen: ์žฌ๊ท€ ์Šคํƒ ๊นŠ์ด ๋งŒํผ ๊ณต๊ฐ„ ์ฐจ์ง€
26+
- cache: ์ตœ์•…์˜ ๊ฒฝ์šฐ answer์™€ ๊ฐ™์€ ๊ฐœ์ˆ˜์˜ ์กฐํ•ฉ ์ €์žฅ -> O(number of combinations)
27+
"""
28+
29+
from typing import List
30+
31+
32+
class Solution:
33+
def combinationSum1(self, candidates: List[int], target: int) -> List[List[int]]:
34+
cache = set()
35+
answer = []
36+
chosen = []
37+
38+
def dfs(sum: int) -> None:
39+
nonlocal target, candidates, cache, answer, chosen
40+
print(chosen)
41+
if sum > target:
42+
return
43+
if sum == target:
44+
copied_chosen = chosen[:]
45+
copied_chosen.sort()
46+
47+
cache_key = tuple(copied_chosen)
48+
if cache_key in cache:
49+
# print(f"already exists {cache_key} in cache")
50+
return
51+
cache.add(cache_key)
52+
answer.append(copied_chosen)
53+
54+
for candidate in candidates:
55+
chosen.append(candidate)
56+
dfs(sum + candidate)
57+
chosen.pop()
58+
59+
dfs(0)
60+
return answer
61+
62+
"""
63+
์ค‘๋ณต ์กฐํ•ฉ ๋ฐฉ์ง€ another solution
64+
ref: https://www.algodale.com/problems/combination-sum/
65+
66+
[Complexity]
67+
N -> candidates.length
68+
M -> target / min(candidates)
69+
Time: O(N^M)
70+
Space: O(M)
71+
"""
72+
73+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
74+
answer = []
75+
combi = []
76+
77+
def dfs(sum: int, start: int) -> None:
78+
nonlocal target, answer, combi, candidates
79+
80+
if sum > target:
81+
return
82+
if sum == target:
83+
answer.append(combi[:])
84+
return
85+
86+
for index in range(start, len(candidates)):
87+
candidate = candidates[index]
88+
combi.append(candidate)
89+
dfs(sum + candidate, index)
90+
combi.pop()
91+
92+
dfs(0, 0)
93+
return answer
94+
95+
96+
sol = Solution()
97+
# print(sol.combinationSum([2,3,6,7], 7))
98+
print(sol.combinationSum([2, 3, 5], 8))
99+
# print(sol.combinationSum([2], 1))
100+

โ€Žnumber-of-1-bits/jongwanra.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
[Problem]
3+
https://leetcode.com/problems/number-of-1-bits/description/
4+
5+
์–‘์ˆ˜ n์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด์ง„๋ฒ•์—์„œ 1๋กœ ์„ค์ •๋œ ๋น„ํŠธ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด๋ผ.
6+
7+
8+
[Plan]
9+
1. ์ฃผ์–ด์ง„ ์–‘์ˆ˜๋ฅผ ์ด์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
10+
2. for-loop์„ ์ˆœํšŒํ•˜๋ฉฐ 1์˜ ๊ฐœ์ˆ˜๋ฅผ countingํ•œ๋‹ค.
11+
12+
[Complexity]
13+
N: bin(n).length - 2
14+
Time: O(N)
15+
Space = O(N)
16+
"""
17+
class Solution:
18+
def hammingWeight(self, n: int) -> int:
19+
binary = bin(n)
20+
output = 0
21+
for index in range(2, len(binary)):
22+
if binary[index] == '1':
23+
output += 1
24+
return output
25+
"""
26+
ref: https://www.algodale.com/problems/number-of-1-bits/
27+
[Complexity]
28+
Time: O(log n)
29+
Space: O(1)
30+
"""
31+
class AnotherSolution:
32+
def hammingWeight(self, n: int) -> int:
33+
count = 0
34+
while n:
35+
quotient, remainder = divmod(n, 2)
36+
print(f"n = {n} quotient={quotient}, remainder={remainder}")
37+
count += remainder
38+
n = quotient
39+
return count
40+
41+
sol = AnotherSolution()
42+
print(sol.hammingWeight(11) == 3)
43+
print(sol.hammingWeight(128) == 1)
44+
print(sol.hammingWeight(2147483645) == 30)
45+

โ€Žvalid-palindrome/jongwanra.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
[Problem]
3+
https://leetcode.com/problems/valid-palindrome/description/
4+
5+
๋ชจ๋“  ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์•ŒํŒŒ๋ฒณ๊ณผ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋“ค์„ ์ „๋ถ€ ์ œ๊ฑฐํ•˜ํ•œ ์ดํ›„์— ์•ž์—์„œ ๋ถ€ํ„ฐ ์ผ์œผ๋‚˜ ๋’ค์—์„œ ๋ถ€ํ„ฐ ์ฝ๋‚˜ ๋™์ผํ•˜๊ฒŒ ์ฝํžŒ๋‹ค๋ฉด, ๊ทธ ๋ฌธ์žฅ์€ ํšŒ๋ฌธ์ž…๋‹ˆ๋‹ค.
6+
์˜์ˆซ์ž ๋ฌธ์ž๋“ค์€ ์•ŒํŒŒ๋ฒณ๊ณผ ์ˆซ์ž๋“ค์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
7+
8+
[Brainstorming]
9+
leftPosition๊ณผ rightPosition์„ ๋‘๊ณ , ๋น„๊ตํ•˜๋ฉด์„œ ์•„๋‹ ๊ฒฝ์šฐ false๋ฅผ returnํ•œ๋‹ค. => O(s.length)
10+
11+
[Complexity]
12+
N: s.length
13+
Time: O(1/2 * N) => O(N)
14+
Space: O(1)
15+
"""
16+
17+
class Solution:
18+
def isPalindrome(self, s:str)-> bool:
19+
leftPos, rightPos = 0, len(s) - 1
20+
while leftPos < rightPos:
21+
while not s[leftPos].isalnum() and leftPos < rightPos:
22+
leftPos += 1
23+
while not s[rightPos].isalnum() and leftPos < rightPos:
24+
rightPos -= 1
25+
26+
if s[leftPos].upper() != s[rightPos].upper():
27+
return False
28+
leftPos += 1
29+
rightPos -= 1
30+
return True
31+
32+
sol = Solution()
33+
print(sol.isPalindrome("A man, a plan, a canal: Panama") == True)
34+
print(sol.isPalindrome("race a car") == False)
35+
print(sol.isPalindrome(" ") == True)
36+
print(sol.isPalindrome("0P") == False)
37+
print(sol.isPalindrome("a") == True)

0 commit comments

Comments
ย (0)