-
-
Notifications
You must be signed in to change notification settings - Fork 305
[leehyeyun] WEEK 06 solutions #2203
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
leehyeyun
wants to merge
2
commits into
DaleStudy:main
Choose a base branch
from
leehyeyun: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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,89 @@ | ||
| /** | ||
| * @param {number[]} height | ||
| * @return {number} | ||
| */ | ||
| /* | ||
| 주어진 정수 배열 height는 여러 개의 세로 막대기 높이를 나타낸다. | ||
| 각 막대기는 x축 위의 서로 다른 위치에 수직으로 세워져 있다. | ||
|
|
||
| i번째 막대기의 좌표는 (i, 0)에서 (i, height[i])이다. | ||
|
|
||
| 이 막대기들 중에서 서로 다른 두 개의 막대기를 선택하면, | ||
| 두 막대기와 x축이 만나 하나의 물을 담을 수 있는 용기(container)를 만들 수 있다. | ||
|
|
||
| 문제 목표: | ||
| - 두 막대기를 선택했을 때 | ||
| - 그 사이에 담을 수 있는 물의 양(면적)이 최대가 되도록 한다. | ||
| - 그 최대 물의 양을 반환한다. | ||
|
|
||
| 중요한 조건: | ||
| 1) 용기는 기울일 수 없다. (막대기는 항상 수직) | ||
| 2) 물의 높이는 선택한 두 막대기 중 더 낮은 높이로 결정된다. | ||
| 3) 물의 너비는 두 막대기 사이의 거리(인덱스 차이)이다. | ||
|
|
||
| 즉, | ||
| 물의 양 = (두 막대기 사이 거리) × (두 막대기 중 더 낮은 높이) | ||
|
|
||
| 입력 형식: | ||
| - height: 정수 배열 | ||
| - n == height.length | ||
| - 2 <= n <= 100,000 | ||
| - 0 <= height[i] <= 10,000 | ||
|
|
||
| 출력 형식: | ||
| - 담을 수 있는 최대 물의 양 (정수) | ||
|
|
||
| 예시: | ||
|
|
||
| Example 1 | ||
| 입력: height = [1,8,6,2,5,4,8,3,7] | ||
| 출력: 49 | ||
| 설명: | ||
| - 여러 막대기 쌍 중 | ||
| - 특정 두 막대기를 선택했을 때 | ||
| - 거리와 높이의 곱이 가장 커짐 | ||
| - 그 최대값이 49 | ||
|
|
||
| Example 2 | ||
| 입력: height = [1,1] | ||
| 출력: 1 | ||
| 설명: | ||
| - 두 막대기 사이 거리 = 1 | ||
| - 더 낮은 높이 = 1 | ||
| - 물의 양 = 1 × 1 = 1 | ||
| */ | ||
|
|
||
| var maxArea = function(height) { | ||
|
|
||
| let leftIndex = 0; | ||
| let rightIndex = height.length - 1; | ||
| let maxValue = 0; | ||
|
|
||
| while (leftIndex < rightIndex) { | ||
| const width = rightIndex - leftIndex; | ||
| let heightValue = 0; | ||
| if (height[leftIndex] < height[rightIndex]){ | ||
| heightValue = height[leftIndex]; | ||
| }else { | ||
| heightValue = height[rightIndex]; | ||
| } | ||
| const area = width * heightValue; | ||
|
|
||
| if (area > maxValue) { | ||
| maxValue = area; | ||
| } | ||
|
|
||
| if (height[leftIndex] < height[rightIndex]) { | ||
| leftIndex++; | ||
| } else { | ||
| rightIndex--; | ||
| } | ||
| } | ||
|
|
||
| return maxValue; | ||
|
|
||
| }; | ||
|
|
||
| console.log(maxArea([1,8,6,2,5,4,8,3,7])) | ||
| console.log(maxArea([1,1])) | ||
|
|
||
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,92 @@ | ||
| /** | ||
| * @param {string} s | ||
| * @return {boolean} | ||
| */ | ||
| /* | ||
| 주어진 문자열 s는 괄호 문자로만 이루어져 있다. | ||
| 사용 가능한 문자는 '(', ')', '{', '}', '[', ']' 총 6가지이다. | ||
|
|
||
| 문자열이 올바른 괄호 문자열인지 판단하라. | ||
|
|
||
| 올바른 괄호 문자열의 조건: | ||
| 1) 여는 괄호는 반드시 같은 종류의 닫는 괄호로 닫혀야 한다. | ||
| 2) 괄호는 올바른 순서로 닫혀야 한다. | ||
| (가장 마지막에 열린 괄호가 가장 먼저 닫혀야 함) | ||
| 3) 모든 닫는 괄호는 대응되는 여는 괄호가 있어야 한다. | ||
|
|
||
| 입력 형식 : | ||
| - s: 문자열 | ||
| - 1 <= s.length <= 10,000 | ||
| - s는 '()[]{}' 문자로만 구성됨 | ||
|
|
||
| 출력 형식 : | ||
| - 올바른 괄호 문자열이면 true | ||
| - 그렇지 않으면 false | ||
|
|
||
| 예시 : | ||
|
|
||
| Example 1 | ||
| 입력 : s = "()" | ||
| 출력 : true | ||
| 설명 : | ||
| - '('가 ')'로 정상적으로 닫힘 | ||
|
|
||
| Example 2 | ||
| 입력 : s = "()[]{}" | ||
| 출력 : true | ||
| 설명 : | ||
| - 모든 괄호가 종류와 순서에 맞게 닫힘 | ||
|
|
||
| Example 3 | ||
| 입력 : s = "(]" | ||
| 출력 : false | ||
| 설명 : | ||
| - 여는 괄호 '('와 닫는 괄호 ']'의 종류가 다름 | ||
|
|
||
| Example 4 | ||
| 입력 : s = "([])" | ||
| 출력 : true | ||
| 설명 : | ||
| - 괄호가 중첩되어 있으나 순서와 종류 모두 올바름 | ||
|
|
||
| Example 5 | ||
| 입력 : s = "([)]" | ||
| 출력 : false | ||
| 설명 : | ||
| - 괄호의 닫히는 순서가 올바르지 않음 | ||
| */ | ||
| var isValid = function (s) { | ||
| const stack = []; | ||
|
|
||
| for (var char of s) { | ||
| if(char == "(" || char == "{" || char == "[") | ||
| { | ||
| stack.push(char); | ||
| }else { | ||
| const last = stack[stack.length - 1]; | ||
| if(char == ")" && last == "("){ | ||
| stack.pop(); | ||
| }else if (char == "}" && last == "{"){ | ||
| stack.pop(); | ||
| }else if (char == "]" && last == "["){ | ||
| stack.pop(); | ||
| }else { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if(stack.length == 0) | ||
| { | ||
| return true; | ||
| }else { | ||
| return false | ||
| } | ||
| }; | ||
|
|
||
| console.log(isValid("()")) | ||
| console.log(isValid("()[]{}")) | ||
| console.log(isValid("(]")) | ||
| console.log(isValid("([])")) | ||
| console.log(isValid("([)]")) | ||
|
|
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.
위에 동일한 조건문으로 검사를 하는데 아래에서 분리해서 작성하신 이유가 있으실까요?
없다면 위의 로직에서 합쳐도 좋을 것 같아요 🤔