Skip to content

Commit 356ae6d

Browse files
authored
Merge pull request #2186 from hozzijeong/main
[hozzijeong] WEEK 06 solutions
2 parents ba523f1 + fa0d021 commit 356ae6d

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var maxArea = function(height) {
6+
// x축이 가장 길면서 y축 역시 가장 높은 즉 높이가 가장 높은 값을 찾으라는 의미.
7+
// startIndex값과 그 값에 해당하는 height를 받고
8+
// endIndex의 값과 그 값에 해당하는 height를 받는다.
9+
// 여기서 가장 큰 점은 startIndex와 endIndex가 멀면 멀수록 좋다
10+
// 그리고 그 값을 하나씩 줄여가면서 더 큰 값을 찾아 적용한다
11+
12+
let max = 0;
13+
14+
let startIndex = 0;
15+
let endIndex = height.length -1;
16+
17+
while(startIndex < endIndex){
18+
const startHeight = height[startIndex];
19+
const endHeight = height[endIndex];
20+
21+
const xLength = endIndex - startIndex;
22+
const minHeight = Math.min(startHeight, endHeight);
23+
24+
const size = xLength * minHeight;
25+
26+
max = Math.max(size,max);
27+
28+
if(startHeight >= endHeight){
29+
endIndex -=1;
30+
}else{
31+
startIndex +=1;
32+
}
33+
};
34+
35+
return max
36+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
var WordDictionary = function() {
3+
const dictionary = new Set('');
4+
5+
this.dictionary = dictionary
6+
};
7+
8+
/**
9+
* @param {string} word
10+
* @return {void}
11+
*/
12+
WordDictionary.prototype.addWord = function(word) {
13+
this.dictionary.add(word);
14+
};
15+
16+
/**
17+
* @param {string} word
18+
* @return {boolean}
19+
*/
20+
WordDictionary.prototype.search = function(word) {
21+
if(word.includes('.')){
22+
const dictionaryList = [...this.dictionary];
23+
24+
return dictionaryList.some((dic) => {
25+
if(dic.length !== word.length) return false;
26+
27+
return [...dic].every((char, index) => word[index] === '.' ? true : word[index] === char)
28+
})
29+
};
30+
31+
return this.dictionary.has(word)
32+
};
33+
34+
/**
35+
* Your WordDictionary object will be instantiated and called as such:
36+
* var obj = new WordDictionary()
37+
* obj.addWord(word)
38+
* var param_2 = obj.search(word)
39+
*/

reverse-linked-list/hozzijeong.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var reverseList = function(head) {
13+
let prev = null; // 1 |
14+
let current = head; // [null, 2] | []
15+
let next = null // 2 | 3
16+
17+
// prev = null
18+
// current = 1
19+
// next =2
20+
21+
// prev = 1
22+
// current = 2
23+
// next = 3
24+
25+
// prev = 2
26+
// current = 3
27+
// next = 4
28+
29+
// prev = 3
30+
// current = 4
31+
// next = 5
32+
33+
// prev = 4
34+
// current = 5
35+
// next = null
36+
37+
while(current != null){
38+
next = current.next;
39+
current.next = prev
40+
prev = current;
41+
current = next;
42+
}
43+
44+
head = prev
45+
return head
46+
};
47+

valid-parentheses/hozzijeong.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isValid = function(s) {
6+
const smallBracket = ['(',')'];
7+
const middleBracket = ['{','}'];
8+
const largeBracket = ['[',']'];
9+
10+
11+
const leftStack = [];
12+
13+
for(const char of s) {
14+
if(char === smallBracket[0]){
15+
leftStack.push(smallBracket[1])
16+
}else if (char === middleBracket[0]){
17+
leftStack.push(middleBracket[1])
18+
}else if(char === largeBracket[0]){
19+
leftStack.push(largeBracket[1])
20+
}else{
21+
const last = leftStack.pop();
22+
if(char === smallBracket[1] && last !== char) return false;
23+
if(char === middleBracket[1] && last !== char) return false;
24+
if(char === largeBracket[1] && last !== char) return false;
25+
}
26+
}
27+
28+
29+
return leftStack.length === 0
30+
};

0 commit comments

Comments
 (0)