Skip to content

Commit

Permalink
Merge pull request #914 from sungjinwi/main
Browse files Browse the repository at this point in the history
[suwi] Week06
  • Loading branch information
SamTheKorean authored Jan 19, 2025
2 parents b9206f9 + 19e2f54 commit e3aa241
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
26 changes: 26 additions & 0 deletions container-with-most-water/sungjinwi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
ํ’€์ด :
left, right ์ปค์„œ๋ฅผ ์–‘ ๋์— ๋†“๊ณ  ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ˜๋Œ€ํŽธ์œผ๋กœ ์ด๋™์‹œํ‚ค๋ฉฐ ๋ฌผ ์–‘ ๋น„๊ต
๋‘˜ ์ค‘ ๋” ๋†’์€ ๋†’์ด๋ฅผ ์ด๋™์‹œํ‚ค๋ฉด ๋ฌด์กฐ๊ฑด ๋ฌผ์˜ ์–‘์ด ์ค„์–ด๋“ค๊ธฐ ๋•Œ๋ฌธ์— ๋” ๋‚ฎ๊ฑฐ๋‚˜ ๊ฐ™์€ ์ปค์„œ๋ฅผ ์ด๋™์‹œํ‚ค๋ฉฐ ์—…๋ฐ์ดํŠธ
๋‘˜์ด ๋งŒ๋‚˜๋ฉด ๋น„๊ต ์ข…๋ฃŒ
len(height) : N
TC : O(N)
l, r์˜ ์ด๋™์ด ์ „์ฒด height ๊ฐœ์ˆ˜๋งŒํผ ์ผ์–ด๋‚˜๋ฏ€๋กœ
SC : O(1)
"""

class Solution:
def maxArea(self, height: List[int]) -> int:
l = 0
r = len(height) - 1
max_area = 0
while (l != r) :
cur_area = (r - l) * min(height[l], height[r])
max_area = max(cur_area, max_area)
if (height[l] >= height[r]) :
r -= 1
else :
l += 1
return max_area
25 changes: 25 additions & 0 deletions longest-increasing-subsequence/sungjinwi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
ํ’€์ด :
ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ์ˆซ์ž๋กœ ๋๋‚˜๋Š” LIS์˜ ๊ธธ์ด๋ฅผ dp๋ฐฐ์—ด์— ์ €์žฅ
nums[i] ์™€ ๊ทธ ์ดํ›„์— ์˜ค๋Š” nums[j]๋ฅผ ๋น„๊ตํ•ด nums[j]๊ฐ€ ํฌ๊ณ 
i๊นŒ์ง€์˜ LIS ๊ธธ์ด + 1 > j๊นŒ์ง€์˜ LIS๊ธธ์ด์ด๋ฉด j๊นŒ์ง€์˜ LIS๊ธธ์ด ์—…๋ฐ์ดํŠธ
์—…๋ฐ์ดํŠธ ํ•  ๋•Œ๋งˆ๋‹ค max๊ธธ์ด์™€ ๋น„๊ตํ•ด์„œ ์ตœ๋Œ€๊ธธ์ด ์—…๋ฐ์ดํŠธ
nums์˜ ๊ธธ์ด N
TC : O(N^2)
for๋ฌธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด๋ฉด N-1 + N-2 + ... + 1 = (N-1)N/2 ์ด๋ฏ€๋กœ N^2/2 -> O(N^2)
SC : O(N)
dp์˜ ๊ธธ์ด๋Š” nums์˜ ๊ธธ์ด์— ๋น„๋ก€ํ•˜๋ฏ€๋กœ
"""

class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
dp = [1] * len(nums)
max_LIS = 1
for i in range(len(nums) - 1) :
for j in range(i + 1, len(nums)) :
if nums[i] < nums[j] and dp[i] + 1 > dp[j] :
dp[j] = dp[i] + 1
max_LIS = max(max_LIS, dp[j])
return max_LIS
42 changes: 42 additions & 0 deletions spiral-matrix/sungjinwi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
ํ’€์ด :
์˜ค๋ฅธ์ชฝ, ์•„๋ž˜์ชฝ์œผ๋กœ ์ด๋™ํ•  ๋• ๊ฐ๊ฐ column๊ณผ row๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฏ€๋กœ direction 1
์™ผ์ชฝ, ์œ„์ชฝ์€ ๊ฐ์†Œํ•˜๋ฏ€๋กœ direction -1
๋กœ ์„ค์ •ํ•˜๊ณ  n_row ๋˜๋Š” n_col๋งŒํผ direction์„ ์ด๋™ํ•˜๋ฉด์„œ ans์— ๋‹ด์•„์ค€๋‹ค
๊ฐ for๋ฌธ์ด ๋๋‚  ๋•Œ๋งˆ๋‹ค n_row ๋˜๋Š” n_col์„ ๊ฐ์†Œ์‹œ์ผœ ์ฃผ๊ณ 
๋‘˜ ์ค‘ ํ•˜๋‚˜๊ฐ€ 0์ด ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์† ์ง„ํ–‰
- col์ด -1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ matrix[0][0]๋ถ€ํ„ฐ appendํ•  ์ˆ˜ ์žˆ๋„๋ก ์œ ์˜
- n_cols, n_rows ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ 0์ด๋˜๋„ ๋๋‚จ์— ์œ ์˜
TC : O(M * N)
matrix ์ „์ฒด๋ฅผ ํ•œ๋ฒˆ์”ฉ ์ˆœํšŒํ•ด์•ผํ•˜๋ฏ€๋กœ
SC : O(1)
returnํ•  ๋ฆฌ์ŠคํŠธ ์™ธ์—๋Š” ์ถ”๊ฐ€์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์ด ์ƒ์ˆ˜๊ฐœ(5๊ฐœ)์ด๋ฏ€๋กœ
"""

class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
ans = []
n_rows = len(matrix)
n_cols = len(matrix[0])

row = 0
col = -1
direction = 1

while n_cols and n_rows :
for _ in range(n_cols):
col += direction
ans.append(matrix[row][col])
n_rows -= 1

for _ in range(n_rows):
row += direction
ans.append(matrix[row][col])
n_cols -= 1

direction *= -1

return ans
30 changes: 30 additions & 0 deletions valid-parentheses/sungjinwi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
ํ’€์ด :
stack๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„
๊ด„ํ˜ธ์˜ ํŽ˜์–ด๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ์˜ key, value๋ฅผ ์ด์šฉํ•ด ๋งค์น˜์‹œํ‚จ๋‹ค
์—ฌ๋Š” ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚˜๋ฉด stack์— push, ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚˜๋ฉด stack์˜ ๋งˆ์ง€๋ง‰ ๊ด„ํ˜ธ์˜ pair์ผ ๋•Œ pop, ์•„๋‹ˆ๋ฉด return False
๋ฌธ์ž์—ด ์ „์ฒด๋ฅผ ์ˆœํšŒํ–ˆ์„ ๋•Œ stack์— ๋‚จ์•„์žˆ์œผ๋ฉด ์•ˆ ๋‹ซํžŒ ๊ด„ํ˜ธ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ return False
s์˜ ๊ธธ์ด N
TC : O(N)
s์— ๋Œ€ํ•ด ํ•œ๋ฒˆ for๋ฌธ
SC : O(N)
stack์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋Š” s์˜ ๊ธธ์ด์™€ ๋น„๋ก€
"""

class Solution:
def isValid(self, s: str) -> bool:
stack = []
pair = dict(zip('({[',')}]'))
for paren in s :
if paren in pair :
stack.append(paren)
elif not stack :
return False
elif pair[stack.pop()] != paren :
return False
if not stack :
return True
else :
return False

0 comments on commit e3aa241

Please sign in to comment.