Skip to content

Commit 5647fc5

Browse files
authored
Merge pull request #1411 from JANGSEYEONG/main
[JANGSEYEONG] WEEK 06 solutions
2 parents 849650a + 35a77b4 commit 5647fc5

File tree

5 files changed

+196
-0
lines changed

5 files changed

+196
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/** ์ฒซ๋ฒˆ์งธ ํ’€์ด: ์žฌ๊ท€
2+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(2^n)
3+
*/
4+
/**
5+
*
6+
var maxArea = function(height) {
7+
let maxWater = (height.length - 1) * Math.min(height[0], height[height.length-1]);
8+
function recursion(start, end){
9+
if(start >= end){
10+
// start์™€ end๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ start๊ฐ€ end๋ณด๋‹ค ์ปค์ง„ ๊ฒฝ์šฐ
11+
return;
12+
}
13+
maxWater = Math.max(maxWater, (end-start)*(Math.min(height[start], height[end])));
14+
recursion(start+1, end); // ์™ผ์ชฝ ๋Š˜๋ ค๋ณด๊ณ 
15+
recursion(start, end-1); // ์šฐ์ธก ๋Š˜๋ ค๋ณด๊ธฐ
16+
}
17+
recursion(0, height.length-1);
18+
return maxWater;
19+
};
20+
21+
/**
22+
* ๋‘๋ฒˆ์งธ ํ’€์ด: ํˆฌํฌ์ธํ„ฐ
23+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
24+
*/
25+
/**
26+
* @param {number[]} height
27+
* @return {number}
28+
*/
29+
var maxArea = function (height) {
30+
let left = 0;
31+
let right = height.length - 1;
32+
let maxWater = 0;
33+
34+
while (left < right) {
35+
// ํ˜„์žฌ ํฌ์ธํ„ฐ ์œ„์น˜์—์„œ ๋ฌผ์˜ ์–‘ ๊ณ„์‚ฐ
36+
const water = (right - left) * Math.min(height[left], height[right]);
37+
maxWater = Math.max(maxWater, water);
38+
39+
// ๋” ์ž‘์€ ๋†’์ด๋ฅผ ๊ฐ€์ง„ ์ชฝ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™
40+
// ๋ฌผ์˜ ์–‘์€ ๋” ์ž‘์€ ๋†’์ด์— ์˜ํ•ด ์ œํ•œ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์€ ๋†’์ด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋˜ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™
41+
if (height[left] < height[right]) {
42+
left++;
43+
} else {
44+
right--;
45+
}
46+
}
47+
48+
return maxWater;
49+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var WordDictionary = function () {
2+
this.root = {};
3+
};
4+
5+
/**
6+
* @param {string} word
7+
* @return {void}
8+
*/
9+
WordDictionary.prototype.addWord = function (word) {
10+
let node = this.root;
11+
for (const char of word) {
12+
if (!node[char]) {
13+
// ํ˜„์žฌ ๋ฌธ์ž์— ํ•ด๋‹นํ•˜๋Š” ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑ
14+
node[char] = { isEnd: false };
15+
}
16+
node = node[char]; // ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™
17+
}
18+
node.isEnd = true; // ๋‹จ์–ด ๋ ํ‘œ์‹œ
19+
};
20+
21+
/**
22+
* @param {string} word
23+
* @return {boolean}
24+
*/
25+
WordDictionary.prototype.search = function (word) {
26+
function dfs(node, index) {
27+
// ๋‹จ์–ด์˜ ๋์— ๋„๋‹ฌํ–ˆ์œผ๋ฉด isEnd ๊ฐ’ ๋ฐ˜ํ™˜
28+
if (index === word.length) return node.isEnd;
29+
30+
const char = word[index];
31+
32+
if (node[char]) {
33+
// ํ˜„์žฌ ๋ฌธ์ž๊ฐ€ ๋…ธ๋“œ์— ์กด์žฌํ•˜๋ฉด ํ•ด๋‹น ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜์—ฌ ๊ณ„์† ๊ฒ€์ƒ‰
34+
return dfs(node[char], index + 1);
35+
}
36+
37+
if (char === ".") {
38+
// "."์ธ ๊ฒฝ์šฐ: ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž์— ๋Œ€ํ•ด ๊ฒ€์ƒ‰ ์‹œ๋„
39+
return Object.keys(node)
40+
.filter((key) => key !== "isEnd")
41+
.some((key) => dfs(node[key], index + 1)); // ํ•˜๋‚˜๋ผ๋„ true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด true
42+
}
43+
44+
return false;
45+
}
46+
return dfs(this.root, 0);
47+
};
48+
49+
/**
50+
* Your WordDictionary object will be instantiated and called as such:
51+
* var obj = new WordDictionary()
52+
* obj.addWord(word)
53+
* var param_2 = obj.search(word)
54+
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var lengthOfLIS = function (nums) {
6+
let sub = [nums[0]]; // ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ์›์†Œ ์ผ๋‹จ ๋„ฃ๊ณ  ๋ณด๊ธฐ
7+
for (const num of nums.slice(1)) {
8+
if (num > sub[sub.length - 1]) {
9+
// ์ˆ˜์—ด์˜ ๋งˆ์ง€๋ง‰๊ฐ’๋ณด๋‹ค ํฌ๋ฉด ๋ฌด์กฐ๊ฑด ์ถ”๊ฐ€
10+
sub.push(num);
11+
} else {
12+
// ์ˆ˜์—ด์˜ ๋งˆ์ง€๋ง‰๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์ด ์ˆซ์ž๋ณด๋‹ค ํฐ ์ˆซ์ž๋“ค ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆซ์ž๋ฅผ ์ฐพ์•„์„œ ๊ต์ฒด
13+
let i = 0;
14+
while (sub[i] < num) {
15+
i++;
16+
}
17+
sub[i] = num;
18+
}
19+
}
20+
return sub.length;
21+
};

โ€Žspiral-matrix/JANGSEYEONG.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {number[]}
4+
*/
5+
var spiralOrder = function (matrix) {
6+
let top = 0; // ์ƒ๋‹จ ํ–‰ ์ธ๋ฑ์Šค
7+
let bottom = matrix.length - 1; // ํ•˜๋‹จ ํ–‰ ์ธ๋ฑ์Šค
8+
let left = 0; // ์ขŒ์ธก ์—ด ์ธ๋ฑ์Šค
9+
let right = matrix[0].length - 1; // ์šฐ์ธก ์—ด ์ธ๋ฑ์Šค
10+
11+
const answer = [];
12+
13+
// ์ƒ๋‹จ, ์šฐ์ธก, ํ•˜๋‹จ, ์ขŒ์ธก ๊ฒฝ๊ณ„๋ฅผ ์ฐจ๋ก€๋กœ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ’์„ ์ˆ˜์ง‘
14+
// ๊ฐ ๋ฐ˜๋ณต๋งˆ๋‹ค ๊ฒฝ๊ณ„๊ฐ€ ์•ˆ์ชฝ์œผ๋กœ ์ค„์–ด๋“ฆ
15+
while (top <= bottom && left <= right) {
16+
// 1. ์ƒ๋‹จ ํ–‰ ์ˆœํšŒ
17+
for (let col = left; col <= right; col++) {
18+
answer.push(matrix[top][col]);
19+
}
20+
top++; // ์ƒ๋‹จ ๊ฒฝ๊ณ„๋ฅผ ์•„๋ž˜๋กœ ํ•œ์นธ ์ด๋™
21+
// ์ƒ๋‹จ ๊ฒฝ๊ณ„๊ฐ€ ํ•˜๋‹จ ๊ฒฝ๊ณ„๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด ์ค‘๋‹จ
22+
if (top > bottom) {
23+
break;
24+
}
25+
// 2. ์šฐ์ธก ์—ด ์ˆœํšŒ
26+
for (let row = top; row <= bottom; row++) {
27+
answer.push(matrix[row][right]);
28+
}
29+
right--; // ์šฐ์ธก ๊ฒฝ๊ณ„๋ฅผ ์™ผ์ชฝ์œผ๋กœ ํ•œ์นธ ์ด๋™
30+
// ์šฐ์ธก ๊ฒฝ๊ณ„๊ฐ€ ์ขŒ์ธก ๊ฒฝ๊ณ„๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด ์ค‘๋‹จ
31+
if (left > right) {
32+
break;
33+
}
34+
// 3. ํ•˜๋‹จ ํ–‰ ์ˆœํšŒ
35+
for (let col = right; col >= left; col--) {
36+
answer.push(matrix[bottom][col]);
37+
}
38+
bottom--; // ํ•˜๋‹จ ๊ฒฝ๊ณ„๋ฅผ ์œ„์ชฝ์œผ๋กœ ํ•œ์นธ ์ด๋™
39+
40+
// 4. ์ขŒ์ธก ์—ด ์ˆœํšŒ
41+
for (let row = bottom; row >= top; row--) {
42+
answer.push(matrix[row][left]);
43+
}
44+
left++; // ์ขŒ์ธก ๊ฒฝ๊ณ„๋ฅผ ์šฐ์ธก์œผ๋กœ ํ•œ์นธ ์ด๋™
45+
}
46+
47+
return answer;
48+
};

โ€Žvalid-parentheses/JANGSEYEONG.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(n) - ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋ฅผ ํ•œ ๋ฒˆ์”ฉ๋งŒ ์ˆœํšŒ
3+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(n) - ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ๋ฌธ์ž๊ฐ€ ์—ฌ๋Š” ๊ด„ํ˜ธ์ผ ๋•Œ ์Šคํƒ์— n๊ฐœ ์ €์žฅ
4+
* @param {string} s
5+
* @return {boolean}
6+
*/
7+
var isValid = function (s) {
8+
const length = s.length;
9+
if (length % 2 === 1) return false; // ํ™€์ˆ˜์ผ ๊ฒฝ์šฐ ๋ฐ”๋กœ return false
10+
11+
const MATCHES = { "(": ")", "{": "}", "[": "]" };
12+
13+
// ์—ฌ๋Š” ๊ด„ํ˜ธ๊ฐ€ ๋‚˜์˜ค๋ฉด ์Šคํƒ์— ์ €์žฅ, ๋‹ซ๋Š” ๊ด„ํ˜ธ๊ฐ€ ๋‚˜์˜ค๋ฉด ์Šคํƒ์˜ ๋งˆ์ง€๋ง‰ ์—ฌ๋Š” ๊ด„ํ˜ธ์™€ ๋น„๊ต
14+
const stack = [];
15+
for (let char of s) {
16+
if (char in MATCHES) {
17+
stack.push(char);
18+
} else if (MATCHES[stack.pop()] !== char) {
19+
return false;
20+
}
21+
}
22+
// ์ง์ด ๋งž์œผ๋ฉด size 0
23+
return stack.length === 0;
24+
};

0 commit comments

Comments
ย (0)