-
-
Notifications
You must be signed in to change notification settings - Fork 339
[hyeri0903] WEEK 06 Solutions #2511
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
Changes from all commits
e3571de
d31ada6
182e596
b942db6
e75b852
aba52c3
b5b3c4c
202b2de
a25dc2f
a196931
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| class Solution: | ||
| def maxArea(self, height: List[int]) -> int: | ||
| ''' | ||
| 1.문제: 가장 많은 양의 물을 저장할 수 있는 max value return | ||
| 2.조건 | ||
| - n: 높이를 의미, 최소 = 5, 최대 = 10^5 | ||
| - 원소값 최소 = 0, 최대 = 10^4 | ||
| 3.풀이 | ||
| - 높이는 height[i], height[j] 중에 작은 값, 가로는 abs(i-j) | ||
| - output = 높이 x 가로 | ||
| -> 2중 loop 는 O(n^2) 로 TLE 발생. | ||
| -> two pointer 로 O(n) 으로 해결! | ||
| ''' | ||
|
|
||
| n = len(height) | ||
| maxArea = 0 | ||
|
|
||
| left = 0 | ||
| right = n-1 | ||
|
|
||
| while left < right: | ||
| curArea = abs(right-left) * min(height[left], height[right]) | ||
| maxArea = max(curArea, maxArea) | ||
|
|
||
| #height 이 낮은쪽 pointer update | ||
| if height[left] < height[right]: | ||
| left += 1 | ||
| else: | ||
| right -= 1 | ||
|
|
||
| return maxArea | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| class TrieNode: | ||
| def __init__(self): | ||
| self.children = {} | ||
| self.is_end = False | ||
|
|
||
| class WordDictionary: | ||
| ''' 너무 어려워숴 풀이 봤어요..ㅠㅠ''' | ||
|
|
||
| def __init__(self): | ||
| self.root = TrieNode() | ||
|
|
||
| def addWord(self, word: str) -> None: | ||
| node = self.root | ||
|
|
||
| for ch in word: | ||
| #node 에 존재하지 않으면 TrieNode 생성 | ||
| if ch not in node.children: | ||
| node.children[ch] = TrieNode() | ||
| #node 에 children 저장 | ||
| node = node.children[ch] | ||
| node.is_end = True | ||
|
|
||
| def dfs(self, node, word, i): | ||
| if i == len(word): | ||
| return node.is_end | ||
|
|
||
| #일반 문자일 경우 | ||
| ch = word[i] | ||
| if ch != '.': | ||
| if ch not in node.children: | ||
| return False | ||
| return self.dfs(node.children[ch], word, i + 1) | ||
| #.이 포함된 경우 -> 모든 경우 탐색 | ||
| for child in node.children.values(): | ||
| if self.dfs(child, word, i + 1): | ||
| return True | ||
| return False | ||
|
|
||
| def search(self, word: str) -> bool: | ||
| return self.dfs(self.root, word, 0) | ||
|
|
||
|
|
||
|
|
||
| # Your WordDictionary object will be instantiated and called as such: | ||
| # obj = WordDictionary() | ||
| # obj.addWord(word) | ||
| # param_2 = obj.search(word) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| ''' | ||
| 모르겠어서 풀이봤습니다 ㅜㅜ | ||
| 1.problem: 증가하는 가장 긴 subsequence length return (최장 증가 부분 수열) | ||
| 2.조건 | ||
| - nums array 길이 최소 1 , 최대 2500 | ||
| - 원소 값 음수 가능 | ||
| 3.풀이 | ||
| - dp : time complexity O(n^2) | ||
| dp[i] = i번째 원소를 마지막으로하는 LIS | ||
| dp[i] max(dp[i], dp[j]+1) 나 보다 작은 애들 중 가장 긴 LIS + 1 | ||
| ''' | ||
|
|
||
| n = len(nums) | ||
| dp = [1] * (n) | ||
|
|
||
| for i in range(n): | ||
| for j in range(i): | ||
| #앞 숫자 nums[j]가 지금 nums[i]보다 더 작은 경우 dp[i] update | ||
| if nums[j] < nums[i]: | ||
| dp[i] = max(dp[i], dp[j] + 1) | ||
| return max(dp) | ||
|
|
||
|
|
||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| left = 0 | ||
| max_len = 0 | ||
| visited = set() | ||
|
|
||
| for i in range(len(s)): | ||
| while s[i] in visited: | ||
| visited.remove(s[left]) | ||
| left += 1 | ||
|
|
||
| visited.add(s[i]) | ||
| max_len = max(max_len, i - left + 1) | ||
|
|
||
| return max_len |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| class Solution: | ||
| def numIslands(self, grid: List[List[str]]) -> int: | ||
| ''' | ||
| m = 세로(열), n = 가로(행) | ||
| solution: dfs | ||
| ''' | ||
| m = len(grid) | ||
| n = len(grid[0]) | ||
| visited = [ [0] * n for _ in range(m)] | ||
| count = 0 | ||
|
|
||
| def dfs(i, j): | ||
| #범위 벗어나면 return | ||
| if i < 0 or i >= m or j < 0 or j >= n or grid[i][j] == "0": | ||
| return | ||
|
|
||
| if visited[i][j] == 1: | ||
| return | ||
|
|
||
| visited[i][j] = 1 #방문표시 | ||
|
|
||
| dfs(i+1, j) | ||
| dfs(i-1, j) | ||
| dfs(i, j+1) | ||
| dfs(i, j-1) | ||
|
|
||
| for i in range(m): | ||
| for j in range(n): | ||
| if grid[i][j] == "1" and visited[i][j] == 0: | ||
| dfs(i,j) | ||
| count += 1 | ||
| return count | ||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
|
|
||
| if head is None: | ||
| return None | ||
|
|
||
| st = [] | ||
| cur = head | ||
|
|
||
| while cur: | ||
| st.append(cur.val) | ||
| cur = cur.next | ||
|
|
||
| dummy = ListNode(0) | ||
| cur = dummy | ||
|
|
||
| while st: | ||
| cur_value = st.pop() | ||
| cur.next = ListNode(cur_value) | ||
| cur = cur.next | ||
|
|
||
| return dummy.next | ||
|
|
||
|
|
||
|
|
||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| class Solution: | ||
| def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
| ''' | ||
| 1.문제: 나선형으로 numbers return | ||
| 2.조건 | ||
| - m, n 길이 최소 = 1. 최대 = 10 | ||
| - 원소 값 최소 = -100, 최대 = 100 | ||
| 3.풀이 | ||
| - 마지막 컬럼에 오면 index j change | ||
| ''' | ||
|
|
||
| if len(matrix) == 1 and len(matrix[0]) == 1: | ||
| return [matrix[0][0]] | ||
|
|
||
| left = 0 | ||
| right = len(matrix[0]) - 1 | ||
| top = 0 | ||
| bottom = len(matrix) - 1 | ||
| result = [] | ||
|
|
||
| while top <= bottom and left <= right: | ||
| #left -> right | ||
| for i in range(left, right + 1): | ||
| result.append(matrix[top][i]) | ||
| top += 1 | ||
|
|
||
| #top -> bottom | ||
| for i in range(top, bottom + 1): | ||
| result.append(matrix[i][right]) | ||
| right -= 1 | ||
|
|
||
| #right -> left | ||
| if top <= bottom: | ||
| for i in range(right, left - 1, -1): | ||
| result.append(matrix[bottom][i]) | ||
| bottom -= 1 | ||
|
|
||
| #bottom -> top | ||
| if left <= right: | ||
| for i in range(bottom, top - 1, -1): | ||
| result.append(matrix[i][left]) | ||
| left += 1 | ||
| return result |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| ''' | ||
| 문제: string s 에 대해서 valid parentheses 이면 true 아니면 false | ||
| conditions | ||
| - open brackets must be closed by the same type | ||
| - // must be closed in the correct order | ||
| - s 최소 길이 = 1, 최대 10^4 | ||
| solution | ||
| - open brackets -> st array 에 저장, close brackets -> st.pop -> check valid | ||
| - 마지막에 st array length 가 1 이상이면 return False | ||
|
|
||
| - time complexity: O(n) | ||
| - space complexity: O(n) | ||
| ''' | ||
|
|
||
| st = [] | ||
|
|
||
| for i in range(len(s)): | ||
| if s[i] == '(' or s[i] == '{' or s[i] == '[': | ||
| st.append(s[i]) | ||
| else: | ||
| # check if st is empty | ||
| if len(st) <= 0: | ||
| return False | ||
| cur = st.pop() | ||
| if s[i] == ')' and cur != '(': | ||
| return False | ||
| if s[i] == '}' and cur != '{': | ||
| return False | ||
| if s[i] == ']' and cur != '[': | ||
| return False | ||
| if len(st) > 0: | ||
| return False | ||
| return True | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석