-
-
Notifications
You must be signed in to change notification settings - Fork 305
[Seoya0512] WEEK 06 solutions #2204
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
Open
Seoya0512
wants to merge
6
commits into
DaleStudy:main
Choose a base branch
from
Seoya0512:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+122
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
95d9c8a
feat: week6 - valid parentheses
Seoya0512 870556d
feat: week6 - container with most water
Seoya0512 712a408
feat: week6 - design and search words data structure
Seoya0512 11fba4b
feat: week6 - longest increasing subsequence
Seoya0512 43a359e
feat: week6 - spiral matrix
Seoya0512 e2b90ee
fix: 공백추가
Seoya0512 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| ''' | ||
| 이중 For-loop을 사용하면 시간복잡도가 O(n^2)이 되므로 시간초과가 발생 | ||
| 리트코드의 Hint를 참고해 Two Pointer를 사용하는 방식으로 시도했습니다. | ||
|
|
||
| Time Complexity: O(n) | ||
| - While 루프를 사용해서 인덱스 i와 j가 만날 때까지 반복으로 진행하기에 O(n) | ||
|
|
||
| Space Complexity: O(1) | ||
| - height 값과, max_area 값 모두 상수 공간을 사용 | ||
| ''' | ||
| class Solution: | ||
| def maxArea(self, height: List[int]) -> int: | ||
| i, j = 0, len(height) - 1 | ||
| max_area = 0 | ||
|
|
||
| while i < j: | ||
| area = min(height[i], height[j]) * (j-i) | ||
| max_area = max(area, max_area) | ||
|
|
||
| if height[i] < height[j]: | ||
| i += 1 | ||
| else: | ||
| j -= 1 | ||
|
|
||
| return max_area | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| ''' | ||
| 이번 문제는 Trie 자료구조를 활용한 문제였습니다. | ||
| 이를 파악하지 못해서 전체 탐색하는 구조를 만들었다 결국 또 도움을 받아 이해하는 것에 목표를 뒀습니다. | ||
| ''' | ||
| class WordDictionary: | ||
| def __init__(self): | ||
| self.root = {"$": True} | ||
|
|
||
| def addWord(self, word: str) -> None: | ||
| node = self.root | ||
| for ch in word: | ||
| if ch not in node: | ||
| node[ch] = {"$": False} | ||
| node = node[ch] | ||
| node["$"] = True | ||
|
|
||
| def search(self, word: str) -> bool: | ||
| def dfs(node, idx): | ||
| if idx == len(word): | ||
| return node["$"] | ||
| ch = word[idx] | ||
| if ch in node: | ||
| return dfs(node[ch], idx + 1) | ||
| if ch == ".": | ||
| return any(dfs(node[k], idx + 1) for k in node if k != "$") | ||
| return False | ||
| return dfs(self.root, 0) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| ''' | ||
| Dynamic Programming 문제로 누적합을 이용해 풀어보려 했으나 끝내 해결을 못해 참고했습니다. | ||
| ''' | ||
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| dp = [1] * len(nums) | ||
| for cur in range(1, len(nums)): | ||
| for prev in range(len(nums)): | ||
| if nums[prev] < nums[cur]: | ||
| dp[cur] = max(1+dp[prev], dp[cur]) | ||
| return max(dp) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| ''' | ||
| 해당 문제를 확인하고 | ||
| 상단 -> 우측 -> 하단 -> 좌측 방향으로 나선형으로 접근하는 것을 우선순위로 생각했습니다. | ||
| pop을 통해서 matrix의 행과 열을 제거하면서 접근하는 방식을 사용해 해결했습니다. | ||
|
|
||
| 이렇게 문제를 풀었을때 pop을 사용하여 행과 열을 제거하기 때문에 | ||
| 비싼 계산 비용이 발생하는 부분을 고려하지 못했다는 점이 아쉽게 느껴지네요. | ||
|
|
||
| Time Complexity: O(mxn) | ||
| - m: 행의 수만큼 접근 | ||
| - n: 열의 수만큼 접근 | ||
|
|
||
| Space Complexity: O(mxn) | ||
| - result 리스트가 전체 요소를 저장 | ||
| ''' | ||
| class Solution: | ||
| def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
| result = [] | ||
| idx = 0 # 처음 값 | ||
| while matrix: | ||
| result += matrix.pop(idx) | ||
| for i in range(len(matrix)): | ||
| if matrix[i]: | ||
| result.append(matrix[i].pop()) | ||
|
|
||
| # 빈 행 제거 (중요!) | ||
| matrix = [row for row in matrix if row] | ||
| if not matrix: | ||
| break | ||
|
|
||
| result += matrix.pop()[::-1] | ||
|
|
||
| for j in range(len(matrix)-1, -1, -1): | ||
| if matrix[j]: | ||
| result.append(matrix[j].pop(0)) | ||
|
|
||
| return result | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| ''' | ||
| 20. Valid Parentheses | ||
| Stack을 사용하는 문제임을 파악하지 못해서 결국 알고달레님의 도움을 받았습니다. | ||
|
|
||
| Time Complexity: O(n) | ||
| Space Complexity: O(n) | ||
| ''' | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| parens = {"(": ")", "{": "}", "[": "]"} | ||
| stack = [] | ||
| for val in s: | ||
| if val in parens: | ||
| stack.append(val) | ||
| else: | ||
| if not stack or val != parens[stack.pop()]: | ||
| return False | ||
| return not stack | ||
|
Member
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. 저는 마지막에 return true 라고만 했더니 실패케이스들이 발생하여 (s len == 1 or "((" 따로 조건문을 추가해주었었습니다. |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
여기서 왜 빈행이 왜 생기고 왜 제거를 제거해줘야 하는지 여쭤보고싶습니다
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.
테스트하는 과정에서 maxtirx가 [[],[]] 이런식으로 이중 빈배열인 경우가 발생하는데요,
시스템에서 내부의 빈배열들이 모두 비어 있으면 반복문을 탈출 하도록 의도하며 작성했는데, 정작 내부 배열은 처리하지 못하더라고요.
그래서 에러가 발생해 중간에 해당 로직을 추가해뒀습니다.