Skip to content

Commit e47d685

Browse files
authored
Merge pull request #1420 from clara-shin/main
[clara-shin] WEEK 06 solutions
2 parents f005a8b + a23b16b commit e47d685

File tree

5 files changed

+347
-0
lines changed

5 files changed

+347
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* ๋‘ ์„ ์„ ์„ ํƒํ•ด ๋ฌผ์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฉด์ ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
3+
*
4+
* ๊ฐ€๋กœ ๊ธธ์ด: ๋‘ ์„  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ(์ธ๋ฑ์Šค ์ฐจ์ด)
5+
* ์„ธ๋กœ ๊ธธ์ด: ๋‘ ์„  ์ค‘ ๋” ์งง์€ ๋†’์ด(๋ฌผ์€ ๋‚ฎ์€ ์ชฝ์œผ๋กœ ๋„˜์น˜๊ธฐ ๋•Œ๋ฌธ)
6+
* ๋ฉด์  = ๊ฐ€๋กœ ๊ธธ์ด ร— ์„ธ๋กœ ๊ธธ์ด
7+
*
8+
* ์–‘์ชฝ ๋์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๋‘ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉ
9+
* ํˆฌ ํฌ์ธํ„ฐ ๋ฐฉ์‹์ด ํšจ์œจ์ ์ธ ์ด์œ : ํ•ญ์ƒ ๋” ์ž‘์€ ๋†’์ด๋ฅผ ๊ฐ€์ง„ ์ชฝ์„ ์ด๋™์‹œํ‚ค๋ฉด ์ตœ๋Œ€ ๋ฉด์ ์„ ๋†“์น˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ
10+
* ๋” ํฐ ๋†’์ด ์ชฝ์„ ์ด๋™์‹œํ‚ค๋ฉด ๊ฐ€๋กœ ๊ธธ์ด๋Š” ์ค„์–ด๋“ค๊ณ , ์„ธ๋กœ ๊ธธ์ด๋Š” ๊ฐ™๊ฑฐ๋‚˜ ๋” ์ž‘์•„์ ธ์„œ ๋ฉด์ ์ด ์ค„์–ด๋“ค ์ˆ˜๋ฐ–์— ์—†์Œ
11+
*
12+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n) - ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒ
13+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1) - ์ถ”๊ฐ€ ๊ณต๊ฐ„ ์‚ฌ์šฉ ์—†์Œ
14+
*/
15+
/**
16+
* @param {number[]} height
17+
* @return {number}
18+
*/
19+
var maxArea = function (height) {
20+
let left = 0;
21+
let right = height.length - 1;
22+
let maxWater = 0;
23+
24+
while (left < right) {
25+
// ํ˜„์žฌ ๋‘ ์„ ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฌผ์˜ ์–‘ ๊ณ„์‚ฐ
26+
const width = right - left;
27+
const minHeight = Math.min(height[left], height[right]);
28+
const water = width * minHeight;
29+
30+
// ์ตœ๋Œ€๊ฐ’ ์—…๋ฐ์ดํŠธ
31+
maxWater = Math.max(maxWater, water);
32+
33+
// ๋” ์ž‘์€ ๋†’์ด๋ฅผ ๊ฐ€์ง„ ์ชฝ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™์‹œํ‚ด
34+
if (height[left] < height[right]) {
35+
left++;
36+
} else {
37+
right--;
38+
}
39+
}
40+
41+
return maxWater;
42+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* ๋‹จ์–ด๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ๋งŒ๋“ค๊ธฐ
3+
*
4+
* ํŠธ๋ผ์ด(Trie) ์ž๋ฃŒ๊ตฌ์กฐ: ๋ฌธ์ž์—ด ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•˜๋Š” ํŠธ๋ฆฌ ๊ธฐ๋ฐ˜ ์ž๋ฃŒ๊ตฌ์กฐ, (๋ฌธ์ž์—ด ๊ฒ€์ƒ‰์— ํšจ์œจ์ )
5+
- ๊ฐ ๋…ธ๋“œ๋Š” ๋ฌธ์ž ํ•˜๋‚˜๋ฅผ ๋‚˜ํƒ€๋ƒ„
6+
- ๋ฃจํŠธ์—์„œ ํŠน์ • ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฒฝ๋กœ๋Š” ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด์„ ๋‚˜ํƒ€๋ƒ„
7+
- ๊ฐ ๋…ธ๋“œ๋Š” ์ž์‹ ๋…ธ๋“œ๋“ค์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋งํฌ(๋ณดํ†ต ํ•ด์‹œ๋งต)๋ฅผ ๊ฐ€์ง
8+
- ๋‹จ์–ด์˜ ๋์„ ํ‘œ์‹œํ•˜๋Š” ํ”Œ๋ž˜๊ทธ๊ฐ€ ํ•„์š”ํ•จ
9+
*
10+
* ๋Œ€๋ถ€๋ถ„์˜ ๊ฒ€์ƒ‰์€ ์™€์ผ๋“œ์นด๋“œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์ผ ํ…Œ๋‹ˆ๊นŒ, ์ด๊ฒƒ๋งŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ณ„๋„ ๋ฉ”์„œ๋“œ๋กœ ๋ฝ‘์Œ(์™€์ผ๋“œ์นด๋“œ ๋ถ„๊ธฐ์ฒ˜๋ฆฌ)
11+
*
12+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(m) (m: ๋‹จ์–ด์˜ ๊ธธ์ด)
13+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(n) (n: ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜)
14+
*/
15+
16+
var WordDictionary = function () {
17+
// ํŠธ๋ผ์ด ๋…ธ๋“œ ํด๋ž˜์Šค ์ •์˜
18+
this.TrieNode = function () {
19+
this.children = {}; // ์ž์‹ ๋…ธ๋“œ๋“ค์„ ์ €์žฅํ•˜๋Š” ํ•ด์‹œ๋งต
20+
this.isEnd = false; // ๋‹จ์–ด์˜ ๋์„ ํ‘œ์‹œํ•˜๋Š” ํ”Œ๋ž˜๊ทธ
21+
};
22+
23+
this.root = new this.TrieNode(); // ๋ฃจํŠธ ๋…ธ๋“œ ์ƒ์„ฑ
24+
};
25+
26+
/**
27+
* ๋‹จ์–ด๋ฅผ ํŠธ๋ผ์ด์— ์ถ”๊ฐ€
28+
* @param {string} word
29+
* @return {void}
30+
*/
31+
WordDictionary.prototype.addWord = function (word) {
32+
let node = this.root;
33+
34+
// ๋‹จ์–ด์˜ ๊ฐ ๋ฌธ์ž๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ํŠธ๋ผ์ด์— ์ถ”๊ฐ€
35+
for (let i = 0; i < word.length; i++) {
36+
const char = word[i];
37+
38+
// ํ˜„์žฌ ๋ฌธ์ž์— ํ•ด๋‹นํ•˜๋Š” ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด ์ƒ์„ฑ
39+
if (!node.children[char]) {
40+
node.children[char] = new this.TrieNode();
41+
}
42+
43+
// ๋‹ค์Œ ๋ ˆ๋ฒจ๋กœ ์ด๋™
44+
node = node.children[char];
45+
}
46+
47+
// ๋‹จ์–ด์˜ ๋ ํ‘œ์‹œ
48+
node.isEnd = true;
49+
};
50+
51+
/**
52+
* ํŠธ๋ผ์ด์—์„œ ๋‹จ์–ด ๊ฒ€์ƒ‰ (์™€์ผ๋“œ์นด๋“œ '.' ์ง€์›)
53+
* @param {string} word
54+
* @return {boolean}
55+
*/
56+
WordDictionary.prototype.search = function (word) {
57+
// ์™€์ผ๋“œ์นด๋“œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
58+
if (!word.includes('.')) {
59+
return this.searchExact(word);
60+
}
61+
62+
return this.searchWithWildcard(word, 0, this.root);
63+
};
64+
65+
/**
66+
* ์™€์ผ๋“œ์นด๋“œ ์—†์ด ์ •ํ™•ํ•œ ๋‹จ์–ด ๊ฒ€์ƒ‰
67+
* @param {string} word
68+
* @return {boolean}
69+
*/
70+
WordDictionary.prototype.searchExact = function (word) {
71+
let node = this.root;
72+
73+
for (let i = 0; i < word.length; i++) {
74+
const char = word[i];
75+
76+
// ํ•ด๋‹น ๋ฌธ์ž์˜ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด false
77+
if (!node.children[char]) {
78+
return false;
79+
}
80+
81+
node = node.children[char];
82+
}
83+
84+
// ๋‹จ์–ด์˜ ๋์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ isEnd ํ”Œ๋ž˜๊ทธ ํ™•์ธ
85+
return node.isEnd;
86+
};
87+
88+
/**
89+
* ์™€์ผ๋“œ์นด๋“œ๋ฅผ ํฌํ•จํ•œ ๋‹จ์–ด ๊ฒ€์ƒ‰ (์žฌ๊ท€์ )
90+
* @param {string} word - ๊ฒ€์ƒ‰ํ•  ๋‹จ์–ด
91+
* @param {number} index - ํ˜„์žฌ ๊ฒ€์ƒ‰์ค‘์ธ ๋ฌธ์ž ์ธ๋ฑ์Šค
92+
* @param {object} node - ํ˜„์žฌ ๊ฒ€์ƒ‰์ค‘์ธ ๋…ธ๋“œ
93+
* @return {boolean}
94+
*/
95+
WordDictionary.prototype.searchWithWildcard = function (word, index, node) {
96+
// ๋‹จ์–ด์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ๊ฒ€์‚ฌํ–ˆ์œผ๋ฉด
97+
if (index === word.length) {
98+
return node.isEnd;
99+
}
100+
101+
const char = word[index];
102+
103+
// ์™€์ผ๋“œ์นด๋“œ('.')์ธ ๊ฒฝ์šฐ
104+
if (char === '.') {
105+
// ํ˜„์žฌ ๋…ธ๋“œ์˜ ๋ชจ๋“  ์ž์‹์— ๋Œ€ํ•ด ์žฌ๊ท€์ ์œผ๋กœ ๊ฒ€์ƒ‰
106+
for (const key in node.children) {
107+
if (this.searchWithWildcard(word, index + 1, node.children[key])) {
108+
return true;
109+
}
110+
}
111+
return false;
112+
}
113+
// ์ผ๋ฐ˜ ๋ฌธ์ž์ธ ๊ฒฝ์šฐ
114+
else {
115+
// ํ•ด๋‹น ๋ฌธ์ž์— ๋Œ€ํ•œ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด false
116+
if (!node.children[char]) {
117+
return false;
118+
}
119+
120+
// ๋‹ค์Œ ๋ฌธ์ž ๊ฒ€์ƒ‰
121+
return this.searchWithWildcard(word, index + 1, node.children[char]);
122+
}
123+
};
124+
125+
/**
126+
* Your WordDictionary object will be instantiated and called as such:
127+
* var obj = new WordDictionary()
128+
* obj.addWord(word)
129+
* var param_2 = obj.search(word)
130+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* ์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด(Longest Increasing Subsequence, LIS)'์„ ๊ตฌํ•˜๊ธฐ
3+
* ๋ถ€๋ถ„ ์ˆ˜์—ด(Subsequence): ์›๋ž˜ ์ˆ˜์—ด์—์„œ ๋ช‡ ๊ฐœ์˜ ์›์†Œ๋ฅผ ๊ณจ๋ผ์„œ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ์ง€ ์•Š๊ณ  ๋‚˜์—ดํ•œ ๊ฒƒ
4+
* ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด(Increasing Subsequence): ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ์›์†Œ๋“ค์ด ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๊ฒƒ
5+
*
6+
* ์ ‘๊ทผ๋ฐฉ๋ฒ•: ๋™์ ๊ณ„ํš๋ฒ•(DP) ๋˜๋Š” ์ด์ง„ํƒ์ƒ‰(Binary Search)
7+
* 1. DP๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•: ์‹œ๊ฐ„๋ณต์žก๋„ O(n^2)
8+
* 2. ์ด์ง„ํƒ์ƒ‰์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•: ์‹œ๊ฐ„๋ณต์žก๋„ O(n log n) โœ… follow-up ๊ณ ๋ ค
9+
*/
10+
11+
/** ๋™์ ๊ณ„ํš๋ฒ•(DP)์œผ๋กœ ์ ‘๊ทผ
12+
* @param {number[]} nums
13+
* @return {number}
14+
*/
15+
var lengthOfLIS = function (nums) {
16+
if (nums.length === 0) return 0;
17+
18+
// dp[i]๋Š” ์ธ๋ฑ์Šค i๊นŒ์ง€์˜ ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๊ธธ์ด
19+
const dp = Array(nums.length).fill(1);
20+
21+
// ๋ชจ๋“  ์œ„์น˜์— ๋Œ€ํ•ด ๊ฒ€์‚ฌ
22+
for (let i = 1; i < nums.length; i++) {
23+
// ํ˜„์žฌ ์œ„์น˜๋ณด๋‹ค ์ด์ „์˜ ๋ชจ๋“  ์œ„์น˜๋ฅผ ๊ฒ€์‚ฌ
24+
for (let j = 0; j < i; j++) {
25+
// ํ˜„์žฌ ๊ฐ’์ด ์ด์ „ ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด, ์ด์ „ ์œ„์น˜์˜ LIS์— ํ˜„์žฌ ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ
26+
if (nums[i] > nums[j]) {
27+
// ๊ธฐ์กด ๊ฐ’๊ณผ (์ด์ „ ์œ„์น˜์˜ LIS ๊ธธ์ด + 1) ์ค‘ ๋” ํฐ ๊ฐ’์„ ์„ ํƒ
28+
dp[i] = Math.max(dp[i], dp[j] + 1);
29+
}
30+
}
31+
}
32+
33+
// dp ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์ด LIS์˜ ๊ธธ์ด
34+
return Math.max(...dp);
35+
};
36+
37+
/** ์ด์ง„ํƒ์ƒ‰(Binary Search)์œผ๋กœ ์ ‘๊ทผ
38+
* @param {number[]} nums
39+
* @return {number}
40+
*/
41+
var lengthOfLIS = function (nums) {
42+
if (nums.length === 0) return 0;
43+
44+
// tails[i]๋Š” ๊ธธ์ด๊ฐ€ i+1์ธ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’
45+
const tails = [];
46+
47+
for (let num of nums) {
48+
// ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ num์ด ๋“ค์–ด๊ฐˆ ์œ„์น˜ ์ฐพ๊ธฐ
49+
let left = 0;
50+
let right = tails.length;
51+
52+
while (left < right) {
53+
const mid = Math.floor((left + right) / 2);
54+
if (tails[mid] < num) {
55+
left = mid + 1;
56+
} else {
57+
right = mid;
58+
}
59+
}
60+
61+
// ์ฐพ์€ ์œ„์น˜์— num ์‚ฝ์ž… ๋˜๋Š” ๋Œ€์ฒด
62+
if (left === tails.length) {
63+
tails.push(num); // ์ƒˆ๋กœ์šด ์ตœ์žฅ ๊ธธ์ด ๋ฐœ๊ฒฌ
64+
} else {
65+
tails[left] = num; // ๊ธฐ์กด ๊ฐ’ ๊ฐฑ์‹ 
66+
}
67+
}
68+
69+
// tails ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ LIS์˜ ๊ธธ์ด
70+
return tails.length;
71+
};

โ€Žspiral-matrix/clara-shin.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* ์˜ค๋ฅธ์ชฝ -> ์•„๋ž˜์ชฝ -> ์™ผ์ชฝ -> ์œ„์ชฝ
3+
* --- ํ–‰๋ ฌ์˜ ๊ฒฝ๊ณ„ ---
4+
* top: ์œ„์ชฝ ๊ฒฝ๊ณ„
5+
* bottom: ์•„๋ž˜์ชฝ ๊ฒฝ๊ณ„
6+
* left: ์™ผ์ชฝ ๊ฒฝ๊ณ„
7+
* right: ์˜ค๋ฅธ์ชฝ ๊ฒฝ๊ณ„
8+
* ----------------
9+
* ๊ฐ ๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ๋ฐ”ํ€ด๋ฅผ ๋Œ ๋•Œ๋งˆ๋‹ค ๊ฒฝ๊ณ„๋ฅผ ํ•˜๋‚˜์”ฉ ์ค„์—ฌ๊ฐ€๋ฉฐ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ฐฉ๋ฌธ
10+
*/
11+
12+
/**
13+
* @param {number[][]} matrix
14+
* @return {number[]}
15+
*/
16+
var spiralOrder = function (matrix) {
17+
if (!matrix.length || !matrix[0].length) return []; // ๋นˆ ํ–‰๋ ฌ ์ฒดํฌ
18+
19+
const result = []; // ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
20+
21+
let top = 0;
22+
let bottom = matrix.length - 1;
23+
let left = 0;
24+
let right = matrix[0].length - 1;
25+
26+
// ์•„์ง ์ฒ˜๋ฆฌํ•  ์š”์†Œ๊ฐ€ ๋‚จ์•„์žˆ๋Š” ๋™์•ˆ ๊ณ„์† ์ˆœํšŒ
27+
// top > bottom ๋˜๋Š” left > right๊ฐ€ ๋˜๋ฉด ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ฐฉ๋ฌธํ•œ ๊ฒƒ
28+
while (top <= bottom && left <= right) {
29+
// 1. ์œ„์ชฝ ํ–‰: ์™ผ์ชฝ โ†’ ์˜ค๋ฅธ์ชฝ ์ด๋™
30+
for (let i = left; i <= right; i++) {
31+
result.push(matrix[top][i]);
32+
}
33+
// ์œ„์ชฝ ํ–‰์„ ์ฒ˜๋ฆฌํ–ˆ์œผ๋ฏ€๋กœ top ์ธ๋ฑ์Šค๋ฅผ 1 ์ฆ๊ฐ€
34+
top++;
35+
36+
// 2. ์˜ค๋ฅธ์ชฝ ์—ด: ์œ„ โ†’ ์•„๋ž˜ ์ด๋™
37+
for (let i = top; i <= bottom; i++) {
38+
result.push(matrix[i][right]);
39+
}
40+
// ์˜ค๋ฅธ์ชฝ ์—ด์„ ์ฒ˜๋ฆฌํ–ˆ์œผ๋ฏ€๋กœ right ์ธ๋ฑ์Šค๋ฅผ 1 ๊ฐ์†Œ
41+
right--;
42+
43+
// 3. ์•„๋ž˜์ชฝ ํ–‰: ์˜ค๋ฅธ์ชฝ โ†’ ์™ผ์ชฝ ์ด๋™
44+
// ์ด๋ฏธ top์ด bottom์„ ์ดˆ๊ณผํ•œ ๊ฒฝ์šฐ, ์•„๋ž˜์ชฝ ํ–‰์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์Œ
45+
if (top <= bottom) {
46+
// ํ˜„์žฌ bottom ํ–‰์—์„œ right๋ถ€ํ„ฐ left๊นŒ์ง€์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ญ์ˆœ์œผ๋กœ ์ˆœํšŒ
47+
for (let i = right; i >= left; i--) {
48+
result.push(matrix[bottom][i]);
49+
}
50+
// ์•„๋ž˜์ชฝ ํ–‰์„ ์ฒ˜๋ฆฌํ–ˆ์œผ๋ฏ€๋กœ bottom ์ธ๋ฑ์Šค๋ฅผ 1 ๊ฐ์†Œ
51+
bottom--;
52+
}
53+
54+
// 4. ์™ผ์ชฝ ์—ด: ์•„๋ž˜ โ†’ ์œ„ ์ด๋™
55+
// ์ด๋ฏธ left๊ฐ€ right๋ฅผ ์ดˆ๊ณผํ•œ ๊ฒฝ์šฐ, ์™ผ์ชฝ ์—ด์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์Œ
56+
if (left <= right) {
57+
// ํ˜„์žฌ left ์—ด์—์„œ bottom๋ถ€ํ„ฐ top๊นŒ์ง€์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ญ์ˆœ์œผ๋กœ ์ˆœํšŒ
58+
for (let i = bottom; i >= top; i--) {
59+
result.push(matrix[i][left]);
60+
}
61+
// ์™ผ์ชฝ ์—ด์„ ์ฒ˜๋ฆฌํ–ˆ์œผ๋ฏ€๋กœ left ์ธ๋ฑ์Šค๋ฅผ 1 ์ฆ๊ฐ€
62+
left++;
63+
}
64+
}
65+
66+
return result;
67+
};

โ€Žvalid-parentheses/clara-shin.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* ๊ด„ํ˜ธ ๋ฌธ์ž์—ด ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ
3+
*
4+
* ์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ
5+
* 1. ๊ด„ํ˜ธ ์Œ์„ ๋งคํ•‘ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์กฐ๊ฑด์„ ํ™•์ธ
6+
* 2. ์—ด๋ฆฐ ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚˜๋ฉด ํ•ด๋‹นํ•˜๋Š” ๋‹ซํžŒ ๊ด„ํ˜ธ๋ฅผ ์Šคํƒ์— ์ง์ ‘ push
7+
* 3. ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚ฌ์„ ๋•Œ, ์Šคํƒ์ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜ ์ง์ด ๋งž์ง€ ์•Š์œผ๋ฉด false
8+
* 4. ๋ฌธ์ž์—ด์„ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•œ ํ›„, ์Šคํƒ์ด ๋น„์–ด์žˆ์–ด์•ผ(๋ฌธ์ž์—ด ๊ธธ์ด๊ฐ€ 0์ด์–ด์•ผ) ๋ชจ๋“  ๊ด„ํ˜ธ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ง์ง€์–ด์ง„ ๊ฒƒ(true)
9+
*/
10+
11+
/**
12+
* @param {string} s
13+
* @return {boolean}
14+
*/
15+
var isValid = function (s) {
16+
// ๋นˆ ๋ฌธ์ž์—ด์ด๋‚˜ ํ™€์ˆ˜ ๊ธธ์ด๋Š” ์œ ํšจํ•˜์ง€ ์•Š์Œ
17+
if (s.length === 0 || s.length % 2 !== 0) return false;
18+
19+
const stack = [];
20+
21+
for (let i = 0; i < s.length; i++) {
22+
const char = s[i];
23+
24+
if (char === '(') {
25+
stack.push(')');
26+
} else if (char === '{') {
27+
stack.push('}');
28+
} else if (char === '[') {
29+
stack.push(']');
30+
} else if (stack.length === 0 || stack.pop() !== char) {
31+
// ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚ฌ์„ ๋•Œ, ์Šคํƒ์ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜ ์ง์ด ๋งž์ง€ ์•Š์Œ
32+
return false;
33+
}
34+
}
35+
36+
return stack.length === 0;
37+
};

0 commit comments

Comments
ย (0)