-
Notifications
You must be signed in to change notification settings - Fork 126
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
[호돌이] Week 6 #892
Merged
Merged
[호돌이] Week 6 #892
Changes from all commits
Commits
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 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,24 @@ | ||
/** | ||
* @param {number[]} height | ||
* @return {number} | ||
*/ | ||
|
||
var maxArea = function (height) { | ||
let max = 0; // 최대값 | ||
let left = 0; // 왼쪽 값 | ||
let right = height.length - 1; // 오른쪽 값 | ||
|
||
// 왼쪽과 오른쪽을 하나씩 줄여가며 가운데서 만날 때 까지 반복 | ||
while (left < right) { | ||
// 최대값 = 가로길이(오른쪽 값 - 왼쪽 값) * 세로길이(왼쪽 값, 오른쪽 값 중 더 작은 값) | ||
max = Math.max(max, (right - left) * Math.min(height[left], height[right])); | ||
// 오른쪽 세로가 더 높다면 왼쪽 값 증가 | ||
if (height[left] < height[right]) { | ||
left++; | ||
} | ||
else { | ||
right--; | ||
} | ||
} | ||
return max; | ||
}; |
This file contains 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,73 @@ | ||
// node 함수선언 | ||
function Node() { | ||
this.child = {}; | ||
this.end = false; | ||
} | ||
|
||
// 최상단의 루트를 노드로 초기화 | ||
var WordDictionary = function () { | ||
this.root = new Node(); | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {void} | ||
*/ | ||
WordDictionary.prototype.addWord = function (word) { | ||
// 현재위치를 최상단으로 초기화 | ||
let current = this.root; | ||
|
||
// 문자를 받고 단어하나씩 노드에 저장 | ||
for (const char of word) { | ||
if (!current.child[char]) { | ||
current.child[char] = new Node(); | ||
} | ||
current = current.child[char]; | ||
} | ||
|
||
// 반복이 끝났으면 true; | ||
current.end = true; | ||
|
||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
WordDictionary.prototype.search = function (word) { | ||
|
||
// i 를 받아 단어 만큼 재귀하는 함수 | ||
const searchHelper = (current, i) => { | ||
// i와 단어의 길이가 같으면 종료 | ||
if (i === word.length) return current.end; | ||
|
||
// 단어 = 찾을 문자의 i번째 단어 | ||
const char = word[i]; | ||
|
||
// 만약 문자가 . 라면 | ||
if (char === '.') { | ||
// 해당 현재 것들의 키를 가지고 반복 | ||
for (const char of Object.keys(current.child)) { | ||
const children = current.child[char]; | ||
// end를 true로 하고 i+1로 재귀 | ||
if (searchHelper(children, i + 1)) return true; | ||
} | ||
return false; | ||
} | ||
else { | ||
// 현재 자식에 해당 문자가 없으면 false | ||
if (!(char in current.child)) return false; | ||
// 아니면 한번 더 재귀 | ||
return searchHelper(current.child[char], i + 1); | ||
} | ||
} | ||
// 결과 리턴 | ||
return searchHelper(this.root, 0); | ||
}; | ||
|
||
/** | ||
* Your WordDictionary object will be instantiated and called as such: | ||
* var obj = new WordDictionary() | ||
* obj.addWord(word) | ||
* var param_2 = obj.search(word) | ||
*/ |
Oops, something went wrong.
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.
Node 라는 새로운 객체를 만드신 것과 활용이 잘 적용된 풀이 같아요!