Skip to content

Commit f23034f

Browse files
authored
Merge pull request #2511 from hyeri0903/main
[hyeri0903] WEEK 06 Solutions
2 parents bee65e5 + a196931 commit f23034f

9 files changed

Lines changed: 265 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
'''
4+
1.๋ฌธ์ œ: ๊ฐ€์žฅ ๋งŽ์€ ์–‘์˜ ๋ฌผ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” max value return
5+
2.์กฐ๊ฑด
6+
- n: ๋†’์ด๋ฅผ ์˜๋ฏธ, ์ตœ์†Œ = 5, ์ตœ๋Œ€ = 10^5
7+
- ์›์†Œ๊ฐ’ ์ตœ์†Œ = 0, ์ตœ๋Œ€ = 10^4
8+
3.ํ’€์ด
9+
- ๋†’์ด๋Š” height[i], height[j] ์ค‘์— ์ž‘์€ ๊ฐ’, ๊ฐ€๋กœ๋Š” abs(i-j)
10+
- output = ๋†’์ด x ๊ฐ€๋กœ
11+
-> 2์ค‘ loop ๋Š” O(n^2) ๋กœ TLE ๋ฐœ์ƒ.
12+
-> two pointer ๋กœ O(n) ์œผ๋กœ ํ•ด๊ฒฐ!
13+
'''
14+
15+
n = len(height)
16+
maxArea = 0
17+
18+
left = 0
19+
right = n-1
20+
21+
while left < right:
22+
curArea = abs(right-left) * min(height[left], height[right])
23+
maxArea = max(curArea, maxArea)
24+
25+
#height ์ด ๋‚ฎ์€์ชฝ pointer update
26+
if height[left] < height[right]:
27+
left += 1
28+
else:
29+
right -= 1
30+
31+
return maxArea
32+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class TrieNode:
2+
def __init__(self):
3+
self.children = {}
4+
self.is_end = False
5+
6+
class WordDictionary:
7+
''' ๋„ˆ๋ฌด ์–ด๋ ค์›Œ์ˆด ํ’€์ด ๋ดค์–ด์š”..ใ… ใ… '''
8+
9+
def __init__(self):
10+
self.root = TrieNode()
11+
12+
def addWord(self, word: str) -> None:
13+
node = self.root
14+
15+
for ch in word:
16+
#node ์— ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด TrieNode ์ƒ์„ฑ
17+
if ch not in node.children:
18+
node.children[ch] = TrieNode()
19+
#node ์— children ์ €์žฅ
20+
node = node.children[ch]
21+
node.is_end = True
22+
23+
def dfs(self, node, word, i):
24+
if i == len(word):
25+
return node.is_end
26+
27+
#์ผ๋ฐ˜ ๋ฌธ์ž์ผ ๊ฒฝ์šฐ
28+
ch = word[i]
29+
if ch != '.':
30+
if ch not in node.children:
31+
return False
32+
return self.dfs(node.children[ch], word, i + 1)
33+
#.์ด ํฌํ•จ๋œ ๊ฒฝ์šฐ -> ๋ชจ๋“  ๊ฒฝ์šฐ ํƒ์ƒ‰
34+
for child in node.children.values():
35+
if self.dfs(child, word, i + 1):
36+
return True
37+
return False
38+
39+
def search(self, word: str) -> bool:
40+
return self.dfs(self.root, word, 0)
41+
42+
43+
44+
# Your WordDictionary object will be instantiated and called as such:
45+
# obj = WordDictionary()
46+
# obj.addWord(word)
47+
# param_2 = obj.search(word)

โ€Žencode-and-decode-strings/hyeri0903.pyโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class Solution:
22
"""
33
@param: strs: a list of strings
44
@return: encodes a list of strings to a single string.
5+
๋ชจ๋ฅด๊ฒ ์–ด์„œ ํ•ด์„ค์˜ ํž˜์„ ๋นŒ๋ ธ์–ด์š”..ใ… 
56
"""
67
def encode(self, strs: List[str]):
78
text = ""
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def lengthOfLIS(self, nums: List[int]) -> int:
3+
'''
4+
๋ชจ๋ฅด๊ฒ ์–ด์„œ ํ’€์ด๋ดค์Šต๋‹ˆ๋‹ค ใ…œใ…œ
5+
1.problem: ์ฆ๊ฐ€ํ•˜๋Š” ๊ฐ€์žฅ ๊ธด subsequence length return (์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด)
6+
2.์กฐ๊ฑด
7+
- nums array ๊ธธ์ด ์ตœ์†Œ 1 , ์ตœ๋Œ€ 2500
8+
- ์›์†Œ ๊ฐ’ ์Œ์ˆ˜ ๊ฐ€๋Šฅ
9+
3.ํ’€์ด
10+
- dp : time complexity O(n^2)
11+
dp[i] = i๋ฒˆ์งธ ์›์†Œ๋ฅผ ๋งˆ์ง€๋ง‰์œผ๋กœํ•˜๋Š” LIS
12+
dp[i] max(dp[i], dp[j]+1) ๋‚˜ ๋ณด๋‹ค ์ž‘์€ ์• ๋“ค ์ค‘ ๊ฐ€์žฅ ๊ธด LIS + 1
13+
'''
14+
15+
n = len(nums)
16+
dp = [1] * (n)
17+
18+
for i in range(n):
19+
for j in range(i):
20+
#์•ž ์ˆซ์ž nums[j]๊ฐ€ ์ง€๊ธˆ nums[i]๋ณด๋‹ค ๋” ์ž‘์€ ๊ฒฝ์šฐ dp[i] update
21+
if nums[j] < nums[i]:
22+
dp[i] = max(dp[i], dp[j] + 1)
23+
return max(dp)
24+
25+
26+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def lengthOfLongestSubstring(self, s: str) -> int:
3+
left = 0
4+
max_len = 0
5+
visited = set()
6+
7+
for i in range(len(s)):
8+
while s[i] in visited:
9+
visited.remove(s[left])
10+
left += 1
11+
12+
visited.add(s[i])
13+
max_len = max(max_len, i - left + 1)
14+
15+
return max_len
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def numIslands(self, grid: List[List[str]]) -> int:
3+
'''
4+
m = ์„ธ๋กœ(์—ด), n = ๊ฐ€๋กœ(ํ–‰)
5+
solution: dfs
6+
'''
7+
m = len(grid)
8+
n = len(grid[0])
9+
visited = [ [0] * n for _ in range(m)]
10+
count = 0
11+
12+
def dfs(i, j):
13+
#๋ฒ”์œ„ ๋ฒ—์–ด๋‚˜๋ฉด return
14+
if i < 0 or i >= m or j < 0 or j >= n or grid[i][j] == "0":
15+
return
16+
17+
if visited[i][j] == 1:
18+
return
19+
20+
visited[i][j] = 1 #๋ฐฉ๋ฌธํ‘œ์‹œ
21+
22+
dfs(i+1, j)
23+
dfs(i-1, j)
24+
dfs(i, j+1)
25+
dfs(i, j-1)
26+
27+
for i in range(m):
28+
for j in range(n):
29+
if grid[i][j] == "1" and visited[i][j] == 0:
30+
dfs(i,j)
31+
count += 1
32+
return count
33+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
8+
9+
if head is None:
10+
return None
11+
12+
st = []
13+
cur = head
14+
15+
while cur:
16+
st.append(cur.val)
17+
cur = cur.next
18+
19+
dummy = ListNode(0)
20+
cur = dummy
21+
22+
while st:
23+
cur_value = st.pop()
24+
cur.next = ListNode(cur_value)
25+
cur = cur.next
26+
27+
return dummy.next
28+
29+
30+
31+

โ€Žspiral-matrix/hyeri0903.pyโ€Ž

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution:
2+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
3+
'''
4+
1.๋ฌธ์ œ: ๋‚˜์„ ํ˜•์œผ๋กœ numbers return
5+
2.์กฐ๊ฑด
6+
- m, n ๊ธธ์ด ์ตœ์†Œ = 1. ์ตœ๋Œ€ = 10
7+
- ์›์†Œ ๊ฐ’ ์ตœ์†Œ = -100, ์ตœ๋Œ€ = 100
8+
3.ํ’€์ด
9+
- ๋งˆ์ง€๋ง‰ ์ปฌ๋Ÿผ์— ์˜ค๋ฉด index j change
10+
'''
11+
12+
if len(matrix) == 1 and len(matrix[0]) == 1:
13+
return [matrix[0][0]]
14+
15+
left = 0
16+
right = len(matrix[0]) - 1
17+
top = 0
18+
bottom = len(matrix) - 1
19+
result = []
20+
21+
while top <= bottom and left <= right:
22+
#left -> right
23+
for i in range(left, right + 1):
24+
result.append(matrix[top][i])
25+
top += 1
26+
27+
#top -> bottom
28+
for i in range(top, bottom + 1):
29+
result.append(matrix[i][right])
30+
right -= 1
31+
32+
#right -> left
33+
if top <= bottom:
34+
for i in range(right, left - 1, -1):
35+
result.append(matrix[bottom][i])
36+
bottom -= 1
37+
38+
#bottom -> top
39+
if left <= right:
40+
for i in range(bottom, top - 1, -1):
41+
result.append(matrix[i][left])
42+
left += 1
43+
return result
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
'''
4+
๋ฌธ์ œ: string s ์— ๋Œ€ํ•ด์„œ valid parentheses ์ด๋ฉด true ์•„๋‹ˆ๋ฉด false
5+
conditions
6+
- open brackets must be closed by the same type
7+
- // must be closed in the correct order
8+
- s ์ตœ์†Œ ๊ธธ์ด = 1, ์ตœ๋Œ€ 10^4
9+
solution
10+
- open brackets -> st array ์— ์ €์žฅ, close brackets -> st.pop -> check valid
11+
- ๋งˆ์ง€๋ง‰์— st array length ๊ฐ€ 1 ์ด์ƒ์ด๋ฉด return False
12+
13+
- time complexity: O(n)
14+
- space complexity: O(n)
15+
'''
16+
17+
st = []
18+
19+
for i in range(len(s)):
20+
if s[i] == '(' or s[i] == '{' or s[i] == '[':
21+
st.append(s[i])
22+
else:
23+
# check if st is empty
24+
if len(st) <= 0:
25+
return False
26+
cur = st.pop()
27+
if s[i] == ')' and cur != '(':
28+
return False
29+
if s[i] == '}' and cur != '{':
30+
return False
31+
if s[i] == ']' and cur != '[':
32+
return False
33+
if len(st) > 0:
34+
return False
35+
return True
36+
37+

0 commit comments

Comments
ย (0)