Skip to content

Commit b21b003

Browse files
authored
Merge pull request #464 from obzva/main
[Flynn] Week6
2 parents 189509b + 44ac5da commit b21b003

File tree

5 files changed

+329
-0
lines changed

5 files changed

+329
-0
lines changed
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* ํ’€์ด
3+
* - container์˜ ๋ฐ‘๋ณ€์ด ๋„“๊ณ  ๋†’์ด๊ฐ€ ๋†’์„ ์ˆ˜๋ก ์ €์žฅํ•˜๋Š” ๋ฌผ์˜ ์–‘์ด ๋งŽ์Šต๋‹ˆ๋‹ค
4+
* - ๋”ฐ๋ผ์„œ ๋ฐ‘๋ณ€์ด ๊ฐ€์žฅ ๋„“์€ container๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉด ํƒ์ƒ‰ ํšจ์œจ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ƒ๊ฐ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค
5+
* - ์–‘ ๋์—์„œ๋ถ€ two pointer๋ฅผ ์ด์šฉํ•˜์—ฌ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค
6+
* - lo, hi ์ค‘์—์„œ ๋†’์ด๊ฐ€ ๋” ๋‚ฎ์€ pointer๋ฅผ ์•ˆ์ชฝ์œผ๋กœ ์ด๋™์‹œํ‚ต๋‹ˆ๋‹ค
7+
* - ์™œ๋ƒํ•˜๋ฉด ๋†’์ด๊ฐ€ ๋” ๋‚ฎ์€ pointer๋ฅผ ์ด๋™์‹œ์ผฐ์„ ๋•Œ ๊ธฐ์กด container๋ณด๋‹ค ๋” ๋†’์ด๊ฐ€ ๋†’์€ container๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋Šฅ์„ฑ์ด ์ƒ๊ธฐ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค
8+
*
9+
* Big O
10+
* - N: ์ฃผ์–ด์ง„ ๋ฐฐ์—ด height์˜ ํฌ๊ธฐ
11+
*
12+
* - Time complexity: O(N)
13+
* - ๋ฐฐ์—ด height๋ฅผ ์กฐํšŒํ•˜๋ฏ€๋กœ ์ „์ฒด ์‹คํ–‰์‹œ๊ฐ„ ๋˜ํ•œ N์— ๋น„๋ก€ํ•˜์—ฌ ์„ ํ˜•์ ์œผ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
14+
*
15+
* - Space complexity: O(1)
16+
*/
17+
18+
class Solution {
19+
public:
20+
int maxArea(vector<int>& height) {
21+
int n = height.size();
22+
int lo = 0;
23+
int hi = n - 1;
24+
25+
int res = 0;
26+
while (lo < hi) {
27+
int w = hi - lo;
28+
int h = min(height[lo], height[hi]);
29+
30+
res = max(res, w * h);
31+
32+
if (height[lo] > height[hi]) {
33+
--hi;
34+
} else {
35+
++lo;
36+
}
37+
}
38+
39+
return res;
40+
}
41+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* ํ’€์ด
3+
* - Trie ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ’€์ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
4+
* - wildcard์ธ '.'์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค
5+
*
6+
* Big O
7+
* - N: ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด word์˜ ๊ธธ์ด
8+
* - M: ํ˜„์žฌ WordDictionary์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” TrieNode์˜ ์ˆ˜
9+
*
10+
* - void addWord(string word)
11+
* - Time complexity: O(N)
12+
* - Space complexity: O(N)
13+
* - ์ตœ์•…์˜ ๊ฒฝ์šฐ word์˜ ๋ชจ๋“  ๋ฌธ์ž์— ๋Œ€ํ•ด ์ƒˆ๋กœ์šด TrieNode๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
14+
*
15+
* - bool search(string word)
16+
* - bool _search(string word, int idx, TrieNode* node)
17+
* - Time complexity: best O(N), worst O(M) < O(26^N)
18+
* - wildcard ์‚ฌ์šฉ ๋ฐ ๊ธฐ์กด์— ์ €์žฅ๋œ word์˜ ์ƒํƒœ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค
19+
* - Space complexity: O(N)
20+
* - _search๊ฐ€ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ์˜ ๊นŠ์ด๋งŒํผ ์ถ”๊ฐ€์ ์ธ ๊ณต๊ฐ„์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค
21+
* - ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ์˜ ๊นŠ์ด๋Š” ํ˜„์žฌ ์ฐพ๋Š” word์˜ ๊ธธ์ด์— ์„ ํ˜•์ ์œผ๋กœ ๋น„๋ก€ํ•ฉ๋‹ˆ๋‹ค
22+
*/
23+
24+
class TrieNode {
25+
public:
26+
array<TrieNode*, 27> links;
27+
bool word;
28+
29+
TrieNode(): word(false) {
30+
links.fill(nullptr);
31+
}
32+
};
33+
34+
class WordDictionary {
35+
public:
36+
WordDictionary(): root(new TrieNode()) {}
37+
38+
void addWord(string word) {
39+
TrieNode* current = root;
40+
41+
for (char c : word) {
42+
if (current->links[c - 'a'] == nullptr) {
43+
current->links[c - 'a'] = new TrieNode();
44+
}
45+
current = current->links[c - 'a'];
46+
}
47+
48+
current->word = true;
49+
}
50+
51+
bool search(string word) {
52+
return _search(word, 0, root);
53+
}
54+
55+
private:
56+
TrieNode* root;
57+
58+
bool _search(string word, int idx, TrieNode* node) {
59+
if (node == nullptr) return false;
60+
61+
if (word.size() == idx) return node->word;
62+
63+
char c = word[idx];
64+
65+
if (c != '.') {
66+
TrieNode* next_node = node->links[c - 'a'];
67+
int next_idx = idx + 1;
68+
69+
return _search(word, next_idx, next_node);
70+
} else {
71+
for (TrieNode* next_node : node->links) {
72+
int next_idx = idx + 1;
73+
if (_search(word, next_idx, next_node)) return true;
74+
}
75+
return false;
76+
}
77+
}
78+
};
79+
80+
/**
81+
* Your WordDictionary object will be instantiated and called as such:
82+
* WordDictionary* obj = new WordDictionary();
83+
* obj->addWord(word);
84+
* bool param_2 = obj->search(word);
85+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* ํ’€์ด
3+
* - ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฐ์—ด memo๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•˜๋Š” ํ’€์ด์ž…๋‹ˆ๋‹ค
4+
* - memo[i]: nums[i]๋กœ ๋๋‚˜๋Š” subsequence ์ค‘์—์„œ ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ๊ธด subsequence์˜ ๊ธธ์ด
5+
*
6+
* Big O
7+
* - N: ๋ฐฐ์—ด nums์˜ ๊ธธ์ด
8+
* - Time complexity: O(N^2)
9+
* - Space complexity: O(N)
10+
*/
11+
12+
class Solution {
13+
public:
14+
int lengthOfLIS(vector<int>& nums) {
15+
vector<int> memo;
16+
memo.push_back(1);
17+
18+
int res = 1;
19+
for (int i = 1; i < nums.size(); ++i) {
20+
int tmp = 1;
21+
for (int j = 0; j < i; ++j) {
22+
if (nums[j] < nums[i]) tmp = max(tmp, memo[j] + 1);
23+
}
24+
memo.push_back(tmp);
25+
res = max(res, tmp);
26+
}
27+
28+
return res;
29+
}
30+
};
31+
32+
/**
33+
* ํ’€์ด
34+
* - wikipedia์˜ pseudo code๋ฅผ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค
35+
* ๋‹ฌ๋ ˆ๋‹˜ ๋ธ”๋กœ๊ทธ์— ์‹ค๋ฆฐ ํ’€์ด๋ฅผ ํ†ตํ•ด์„œ ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๋ฐ”๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋ฌธ์ œ์˜ ํ’€์ด๋งŒ์„ ์›ํ•˜์‹ ๋‹ค๋ฉด ๋‹ฌ๋ ˆ๋‹˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ถ”์ฒœ๋“œ๋ฆฌ๊ณ 
36+
* ์ข€ ๋” generalํ•˜๊ณ  ํ™•์žฅ์„ฑ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ๊ถ๊ธˆํ•˜์‹  ๋ถ„๋“ค๊ป˜์„œ๋Š” wiki๋„ ์ฝ์–ด๋ณด์‹œ๋Š” ๊ฑธ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค (์ดํ•ดํ•˜๋Š” ๋ฐ์—๋Š” ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค)
37+
*
38+
* ์ œ๊ฐ€ ์ฝ๊ณ  ์ดํ•ดํ•œ ๊ฒฐ๊ณผ wiki ํ’€์ด์™€ ๋‹ฌ๋ ˆ๋‹˜ ํ’€์ด์˜ ๋น„๊ต๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค
39+
*
40+
* ๊ณตํ†ต์ : ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๋ฐ”๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Œ (LIS์˜ ๊ธธ์ด)
41+
* ์ฐจ์ด์ : wiki ํ’€์ด๋Š” ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๋ฐ”๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์— ๋น„ํ•ด overkill์ž…๋‹ˆ๋‹ค (์ด ๋ฌธ์ œ์—์„œ๋Š” ๊ตณ์ด ํ•„์š” ์—†๋Š” ๋ถ€๋ถ„์ด ๊ฝค ์žˆ์Œ)
42+
* ๋Œ€์‹ , wiki ํ’€์ด๋Š” ํ™•์žฅ์„ฑ์ด ์ข€ ๋” ๋„“์€ ํ’€์ด์ž…๋‹ˆ๋‹ค (๊ฐ ๊ธธ์ด์— ํ•ด๋‹นํ•˜๋Š” increasing subsequence๋ฅผ ์žฌ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ์Œ)
43+
*
44+
* ๊ด€์‹ฌ ์žˆ์œผ์‹  ๋ถ„๋“ค๊ป˜์„œ๋Š” ํ•œ ๋ฒˆ ์ฝ์–ด๋ณด์‹œ๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค :)
45+
* - ์ฐธ๊ณ : https://en.wikipedia.org/wiki/Longest_increasing_subsequence#Efficient_algorithms
46+
*
47+
* - memo[l]: ํ˜„์žฌ nums[i]๋ฅผ ํƒ์ƒ‰์ค‘์ด๋ผ๊ณ  ํ•  ๋•Œ, l <= i์ธ l์— ๋Œ€ํ•˜์—ฌ
48+
* ๊ธธ์ด๊ฐ€ l์ธ increasing subsequence๋“ค์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ ์ค‘์—์„œ
49+
* ๊ฐ€์žฅ ์ตœ์†Œ๊ฐ’์ธ nums[k]์˜ ์ธ๋ฑ์Šค k
50+
* nums๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ํƒ์ƒ‰ํ•ด ๋‚˜๊ฐ์— ๋”ฐ๋ผ ๊ณ„์† ๊ฐฑ์‹ ๋˜๋ฉฐ ์ •๋ ฌ๋œ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค (if x < y then nums[memo[x]] < nums[memo[y]])
51+
*
52+
* - predec[i]: nums[i]๋ฅผ ๋งˆ์ง€๋ง‰ ์›์†Œ๋กœ ํ•˜๋Š” ๊ฐ€์žฅ ๊ธด increasing subsequence์—์„œ nums[i] ๋ฐ”๋กœ ์•ž์— ์˜ค๋Š” ์›์†Œ์˜ index
53+
*
54+
* - nums๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋ฉฐ, ํ˜„์žฌ ํƒ์ƒ‰ ์ค‘์ธ nums[i]๋ฅผ ๋งˆ์ง€๋ง‰ ์›์†Œ๋กœ ์‚ผ๋Š” ๊ฐ€์žฅ ๊ธด Increasing subsequence๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค
55+
* ๊ฐ€์žฅ ๊ธด Increasing subsequence๋Š” memo ๋ฐฐ์—ด์— ๋Œ€ํ•ด ์ด๋ถ„ํƒ์ƒ‰์„ ์‹คํ–‰ํ•˜์—ฌ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
56+
*
57+
* Big O
58+
* - N: ๋ฐฐ์—ด nums์˜ ๊ธธ์ด
59+
*
60+
* - Time complexity: O(NlogN)
61+
* - nums์˜ ๊ฐ ์›์†Œ๋งˆ๋‹ค memo์— ๋Œ€ํ•ด ์ด๋ถ„ํƒ์ƒ‰์„ ์‹คํ–‰ํ•˜๋ฏ€๋กœ N์ด ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ์‹คํ–‰ ์‹œ๊ฐ„์€ N * logN ํ˜•ํƒœ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
62+
* - Space complexity: O(N)
63+
* - memo ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” N์ด ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ์„ ํ˜•์ ์œผ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
64+
* (- predec ๋ฐฐ์—ด์˜ ํฌ๊ธฐ ๋˜ํ•œ N์ด ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ์„ ํ˜•์ ์œผ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค)
65+
*/
66+
67+
class Solution {
68+
public:
69+
int lengthOfLIS(vector<int>& nums) {
70+
int n = nums.size();
71+
72+
vector<int> memo(n + 1, -1);
73+
74+
// vector<int> predec(n, -1);
75+
// ๊ฐ ๊ธธ์ด์— ๋งž๋Š” increasing subsequence๋ฅผ ์žฌ๊ตฌ์ถ•ํ•  ๋•Œ ์“ฐ์ž…๋‹ˆ๋‹ค
76+
77+
int max_len = 0;
78+
for (int i = 0; i < nums.size(); ++i) {
79+
int lo = 1;
80+
int hi = max_len + 1;
81+
while (lo < hi) {
82+
int mid = lo + (hi - lo) / 2;
83+
if (nums[memo[mid]] < nums[i]) lo = mid + 1;
84+
else hi = mid;
85+
}
86+
// ์œ„ ์ด๋ถ„ํƒ์ƒ‰์„ ๋งˆ์น˜๋ฉด lo == hi์ธ๋ฐ
87+
// lo (ํ˜น์€ hi)๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” `nums[i]๊ฐ€ ๋งˆ์ง€๋ง‰ ์›์†Œ์ธ increasing subsequence ์ค‘์— ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ๊ธด ๋…€์„์˜ ๊ธธ์ด` ์ž…๋‹ˆ๋‹ค
88+
int curr_len = lo;
89+
// ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ๋” curr_len์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์ค๋‹ˆ๋‹ค
90+
91+
// predec[i] = memo[curr_len - 1];
92+
// nums[i]๊ฐ€ ๋งˆ์ง€๋ง‰์œผ๋กœ ์˜ค๋Š” ๊ฐ€์žฅ ๊ธด increasing subsequence์—์„œ nums[i]์˜ ๋ฐ”๋กœ ์ „ ์›์†Œ์˜ index๋ฅผ ๊ธฐ๋กํ•ด์ค๋‹ˆ๋‹ค
93+
//
94+
memo[curr_len] = i;
95+
96+
if (curr_len > max_len) {
97+
// ๋งŒ์•ฝ ์ด์ „๊นŒ์ง€ ์ฐพ์•˜๋˜ max_len๋ณด๋‹ค ๋” ๊ธธ์ด๊ฐ€ ๊ธด increasing subsequence๋ฅผ max_len
98+
max_len = curr_len;
99+
}
100+
}
101+
102+
return max_len;
103+
104+
// ๊ธธ์ด L์งœ๋ฆฌ increasing subsequence ์ค‘ ํ•˜๋‚˜๋ฅผ ์žฌ๊ตฌ์ถ•ํ•˜๋ ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค
105+
// [P...P[memo[L]], ..., P[P[memo[L]]], P[memo[L]] ,memo[L]]
106+
107+
// vector<int> s(L, -1);
108+
// int k = memo[L];
109+
// for (int i = L - 1; i >= 0; --i) {
110+
// s[i] = nums[k];
111+
// k = predec[k];
112+
// }
113+
}
114+
};

โ€Žspiral-matrix/flynn.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* ํ’€์ด
3+
* - ํƒ์ƒ‰ ๋ฐฉํ–ฅ์„ 90๋„์”ฉ ํšŒ์ „ํ•ด๋‚˜๊ฐ€๋ฉด์„œ ์ฃผ์–ด์ง„ 2์ฐจ์› ๋ฐฐ์—ด matrix๋ฅผ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค
4+
* - ํ•œ๊ณ„: ์ฃผ์–ด์ง„ matrix๋ฅผ ๋ณ€ํ˜•ํ•˜๊ฒŒ ๋˜๋ฉฐ, ํ•ด๋‹น ๋ณ€ํ˜•์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ถ”๊ฐ€์ ์ธ ๊ณต๊ฐ„ ์‚ฌ์šฉ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค
5+
*
6+
* Big O
7+
* - M: ์ฃผ์–ด์ง„ matrix์˜ ํ–‰์˜ ๊ฐœ์ˆ˜
8+
* - N: ์—ด์˜ ๊ฐœ์ˆ˜
9+
*
10+
* - Time complexity: O(MN)
11+
* - Space complexity: O(1)
12+
*/
13+
14+
class Solution {
15+
public:
16+
pair<int, int> rotate(pair<int, int> dir) {
17+
// ์‹œ๊ณ„๋ฐฉํ–ฅ 90๋„ ํšŒ์ „
18+
// ํ–‰๋ ฌ๊ณฑ์œผ๋กœ ๊ตฌํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
19+
// | 0 -1 | | dir.first | = | -dir.second |
20+
// | 1 0 | | dir.second | | dir.first |
21+
return {dir.second, -dir.first};
22+
}
23+
24+
pair<int, int> get_next(pair<int, int> curr, pair<int, int> dir) {
25+
return {curr.first + dir.first, curr.second + dir.second};
26+
}
27+
28+
vector<int> spiralOrder(vector<vector<int>>& matrix) {
29+
int m = matrix.size();
30+
int n = matrix[0].size();
31+
int cnt = m * n;
32+
33+
pair<int, int> curr = {0, 0};
34+
pair<int, int> curr_dir = {0, 1};
35+
36+
vector<int> res;
37+
38+
while (cnt) {
39+
res.push_back(matrix[curr.first][curr.second]);
40+
41+
matrix[curr.first][curr.second] = 101; // constraint ๋ฐ–์˜ ๊ฐ’ 101๋กœ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค
42+
--cnt;
43+
44+
pair<int, int> next = get_next(curr, curr_dir);
45+
46+
if (0 > next.first || next.first >= m
47+
|| 0 > next.second || next.second >= n
48+
|| matrix[next.first][next.second] == 101) {
49+
curr_dir = rotate(curr_dir);
50+
curr = get_next(curr, curr_dir);
51+
} else {
52+
curr = next;
53+
}
54+
}
55+
56+
return res;
57+
}
58+
};

โ€Žvalid-parentheses/flynn.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* ํ’€์ด
3+
* - stack ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค
4+
*
5+
* Big O
6+
* - N: ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด
7+
*
8+
* - Time complexity: O(N)
9+
* - ๋ฌธ์ž์—ด s ์ „์ฒด๋ฅผ ์ˆœํšŒํ•  ๊ฒฝ์šฐ ์‹คํ–‰์‹œ๊ฐ„์€ N์— ์„ ํ˜•์ ์œผ๋กœ ๋น„๋ก€ํ•˜์—ฌ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
10+
* - Space complexity: O(N)
11+
* - "((((((...((((((" ์™€ ๊ฐ™์€ ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด stack์˜ ํฌ๊ธฐ๊ฐ€ ์ตœ๋Œ€ N๊นŒ์ง€ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
12+
*/
13+
14+
class Solution {
15+
public:
16+
bool isValid(string s) {
17+
stack<char> st;
18+
for (char ch : s) {
19+
if (ch == '(' || ch == '{' || ch == '[') {
20+
st.push(ch);
21+
} else {
22+
if (st.empty()) return false;
23+
else if (st.top() == '(' && ch == ')') st.pop();
24+
else if (st.top() == '{' && ch == '}') st.pop();
25+
else if (st.top() == '[' && ch == ']') st.pop();
26+
else return false;
27+
}
28+
}
29+
return st.empty();
30+
}
31+
};

0 commit comments

Comments
ย (0)