-
-
Notifications
You must be signed in to change notification settings - Fork 245
[byol-han] WEEK 06 solutions #1430
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c5ef509
valid parentheses solution
byol-han 7b47bb7
container with most water solution
byol-han db563ea
longest increasing subsequence solution
byol-han ebe936c
spiral matrix solution
byol-han e08cf3a
design-add-and-search-words-data-structure solution
byol-han 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,27 @@ | ||
/** | ||
* https://leetcode.com/problems/container-with-most-water/ | ||
* @param {number[]} height | ||
* @return {number} | ||
*/ | ||
var maxArea = function (height) { | ||
let left = 0; | ||
let right = height.length - 1; | ||
let maxWater = 0; | ||
|
||
while (left < right) { | ||
let width = right - left; // 현재 너비는 두 포인터 사이 거리 | ||
let minHeight = Math.min(height[left], height[right]); // 현재 높이는 두 선 중 더 낮은 값 (물이 넘치지 않기 위해 낮은 선 기준) | ||
let area = width * minHeight; // 현재 구간이 담을 수 있는 물의 양 계산 | ||
|
||
maxWater = Math.max(maxWater, area); // 최대 물 저장량 갱신 | ||
|
||
// 낮은 쪽 포인터를 이동시켜서 더 큰 높이를 찾음 | ||
if (height[left] < height[right]) { | ||
left++; | ||
} else { | ||
right--; | ||
} | ||
} | ||
|
||
return maxWater; | ||
}; |
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,23 @@ | ||
/** | ||
* https://leetcode.com/problems/longest-increasing-subsequence/ | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var lengthOfLIS = function (nums) { | ||
if (nums.length === 0) return 0; | ||
|
||
const dp = new Array(nums.length).fill(1); // 최소 길이는 1 (자기 자신만 포함) | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
// 현재 숫자 이전의 숫자들과 비교 | ||
for (let j = 0; j < i; j++) { | ||
// 증가하는 순서인지 확인 | ||
if (nums[i] > nums[j]) { | ||
// 증가하는 subsequence가 발견되면 길이 갱신 | ||
dp[i] = Math.max(dp[i], dp[j] + 1); | ||
} | ||
} | ||
} | ||
|
||
return Math.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,22 @@ | ||
function isValid(s) { | ||
const stack = []; | ||
const bracketMap = { | ||
")": "(", | ||
"}": "{", | ||
"]": "[", | ||
}; | ||
Comment on lines
+3
to
+7
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. 딕셔너리를 활용한 방법이 굉장히 깔끔하네요! |
||
|
||
for (let char of s) { | ||
if (char === "(" || char === "{" || char === "[") { | ||
stack.push(char); | ||
} else { | ||
// 닫는 괄호가 나왔을 때 스택이 비었거나 짝이 안 맞는 경우 | ||
if (stack.pop() !== bracketMap[char]) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
// 모든 괄호가 짝지어졌는지 확인 | ||
return stack.length === 0; | ||
} |
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.
area변수가 있어 가독성이 좋지만, 생략하고 Math.max(maxWater, width * minHeight) 이렇게 하는 방법도 괜찮을 것 같아요. 주석이 상세히 적혀있어 이해하기 쉬웠습니다.