Skip to content

[jungsiroo] Week2 #708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions 3sum/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
# Naive Solution
# Tc : O(n^3) / Sc : O(nC_3)

n = len(nums)
"""
for i in range(n-2):
for j in range(i+1,n-1):
for k in range(j+1, n):
if nums[i]+nums[j]+nums[k] == 0:
ret.add(tuple(sorted([nums[i], nums[j], nums[k]])))

ret = [x for x in ret]
return ret
"""

# Better Solution
# two-sum question with fixed num (traversal in for loop)
# Tc : O(n^2) / Sc : O(n)
ret = []
nums.sort()

for i in range(n):
if i>0 and nums[i] == nums[i-1]:
continue
j, k = i+1, n-1

while j < k:
sum_ = nums[i] + nums[j] + nums[k]
if sum_ < 0 : j += 1
elif sum_ > 0 : k -= 1
else:
ret.append([nums[i], nums[j], nums[k]])
j += 1

while nums[j] == nums[j-1] and j<k:
j += 1

return ret

18 changes: 18 additions & 0 deletions climbing-stairs/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def climbStairs(self, n: int) -> int:
"""
dynamic programming
dp[0] = 1
dp[1] = 1
dp[2] = dp[0] + dp[1] = 2
dp[3] = dp[1] + dp[2] = 3
...

Tc = O(n) / Sc = O(n)
"""

dp = [1 for _ in range(n+1)]
Copy link
Member

@DaleSeo DaleSeo Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

계단에 오르는 방법의 개수를 굳이 1번째 칸부터 n번째 칸까지 모두 저장할 필요가 있을까요? 현재 답안도 훌륭하지만 아직 마감까지 시간이 좀 남아있으니 혹시 시간되시면 고민해보시면 좋을 것 같습니다 :)

for i in range(2, n+1):
dp[i] = dp[i-2] + dp[i-1]
return dp[n]

26 changes: 26 additions & 0 deletions decode-ways/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution:
def numDecodings(self, s: str) -> int:
"""
dp 이용 : 가능한 경우의 수를 구해놓고 그 뒤에 붙을 수 있는가?
ex) 1234
(1,2) -> (1,2,3)
(12) -> (12,3)
(1) -> (1,23)

Tc : O(n) / Sc : O(n)
"""
if s[0] == '0': return 0

n = len(s)
dp = [0]*(n+1)
dp[0] = dp[1] = 1

for i in range(2, n+1):
one = int(s[i-1])
two = int(s[i-2:i])

if 1<=one<=9: dp[i] += dp[i-1]
if 10<=two<=26 : dp[i] += dp[i-2]

return dp[n]

30 changes: 30 additions & 0 deletions valid-anagram/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False

# sort and compare
# Tc : O(nlogn) / Sc : O(n)(Because of python timsort)
"""
s = sorted(s)
t = sorted(t)
for s_char, t_char in zip(s, t):
if s_char != t_char:
return False
return True
"""

# dictionary to count letters
# Tc : O(n) / Sc : O(n)
letters_cnt = dict()
INF = int(1e6)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오, 보통 이럴 때 float("inf")를 많이 사용하시는 것 같은데 신기하네요 ㅎㅎ

for s_char in s:
letters_cnt[s_char] = letters_cnt.get(s_char,0)+1

for t_char in t:
letters_cnt[t_char] = letters_cnt.get(t_char,INF)-1
if letters_cnt[t_char] < 0:
return False

return sum(letters_cnt.values()) == 0

Loading