Skip to content

Commit d2910bf

Browse files
authored
Merge pull request #2528 from hwi-middle/main
[hwi-middle] WEEK 06 solutions
2 parents 4b77d54 + dd2cee8 commit d2910bf

File tree

5 files changed

+253
-0
lines changed

5 files changed

+253
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int maxArea(vector<int>& height) {
4+
int l = 0;
5+
int r = height.size() - 1;
6+
7+
int area = 0;
8+
9+
// ํˆฌ ํฌ์ธํ„ฐ๋กœ ์ ‘๊ทผ
10+
while (l < r)
11+
{
12+
// ๋„ˆ๋น„(๋ฌผ ์ €์žฅ๋Ÿ‰)๋ฅผ ๊ตฌํ•˜๊ณ  ์ตœ๋Œ“๊ฐ’ ์—…๋ฐ์ดํŠธ
13+
area = max(area, (r - l) * min(height[r], height[l]));
14+
15+
// ๋†’์ด๊ฐ€ ๋‚ฎ์€ ์ชฝ์˜ ์ปค์„œ๋ฅผ ์ด๋™
16+
// -> ๋†’์€ ์ชฝ์˜ ์ปค์„œ๋ฅผ ์›€์ง์ด๋ฉด ๋ฌด์กฐ๊ฑด ์†ํ•ด
17+
// -> ๋„ˆ๋น„๋Š” ์ค„์–ด๋“ค๊ณ  ๋†’์ด๊นŒ์ง€ ์ค„์–ด๋“œ๋‹ˆ๊นŒ
18+
if (height[l] < height[r])
19+
{
20+
l++;
21+
}
22+
else
23+
{
24+
r--;
25+
}
26+
}
27+
28+
return area;
29+
}
30+
};
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// ํ’€์ด ์ ‘๊ทผ: Trie๋กœ ํ•ด๊ฒฐ
2+
3+
class WordDictionary {
4+
public:
5+
WordDictionary() {
6+
mRoot = make_unique<Node>(); // RAII๋ฅผ ์œ„ํ•ด ์Šค๋งˆํŠธ ํฌ์ธํ„ฐ ์‚ฌ์šฉ
7+
}
8+
9+
void addWord(string word) {
10+
Node* curNode = mRoot.get();
11+
for (char c : word)
12+
{
13+
auto& child = curNode->children[c - 'a'];
14+
if (child == nullptr)
15+
{
16+
child = make_unique<Node>();
17+
}
18+
19+
curNode = child.get();
20+
}
21+
22+
curNode->isWord = true;
23+
}
24+
25+
bool search(string word) {
26+
return search_impl(word, mRoot.get());
27+
}
28+
29+
private:
30+
struct Node
31+
{
32+
bool isWord = false;
33+
unique_ptr<Node> children[26];
34+
};
35+
36+
unique_ptr<Node> mRoot;
37+
38+
// ๋ณต์‚ฌ ์—†๋Š” substr์„ ์œ„ํ•ด std::string_view ํ™œ์šฉ
39+
bool search_impl(string_view word, Node* root)
40+
{
41+
Node* curNode = root;
42+
for (int i = 0; i < word.size(); ++i)
43+
{
44+
char c = word[i];
45+
if (c == '.') // '.'์€ ์™€์ผ๋“œ ์นด๋“œ์ด๋ฏ€๋กœ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ์ˆœํšŒํ•˜๋ฉฐ ์‹œ๋„
46+
{
47+
for (auto& p : curNode->children)
48+
{
49+
if (p != nullptr && search_impl(word.substr(i + 1), p.get()))
50+
{
51+
return true;
52+
}
53+
}
54+
55+
return false;
56+
}
57+
else
58+
{
59+
auto& child = curNode->children[c - 'a'];
60+
if (child == nullptr)
61+
{
62+
return false;
63+
}
64+
65+
curNode = child.get();
66+
}
67+
}
68+
69+
return curNode->isWord;
70+
}
71+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// O(n^2) ํ’€์ด
2+
class Solution {
3+
public:
4+
int lengthOfLIS(vector<int>& nums) {
5+
int n = nums.size();
6+
vector<int> d(n); // ์ ํ™”์‹: d[i] = a[i]๋ฅผ ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•˜๋Š” LIS ๊ธธ์ด
7+
8+
for (int i = 0; i < n; ++i)
9+
{
10+
d[i] = 1; // ์ตœ์†Ÿ๊ฐ’์€ 1
11+
for (int j = 0; j < i; ++j)
12+
{
13+
// ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„์ˆ˜์—ด์ด๊ณ , ๊ธฐ์กด์— ๊ตฌํ•œ ๊ธธ์ด๋ณด๋‹ค ๊ธธ๋ฉด ์—…๋ฐ์ดํŠธ
14+
if (nums[j] < nums[i] && d[j] + 1 > d[i])
15+
{
16+
d[i] = d[j] + 1;
17+
}
18+
}
19+
}
20+
21+
// d์˜ ์ตœ๋Œ“๊ฐ’ ๋ฐ˜ํ™˜
22+
return *max_element(d.begin(), d.end());
23+
}
24+
};
25+
26+
// O(n log n) ํ’€์ด
27+
class Solution {
28+
public:
29+
int lengthOfLIS(vector<int>& nums) {
30+
int n = nums.size();
31+
vector<int> d; // d[i] = ๊ธธ์ด๊ฐ€ i + 1์ธ LIS์˜ ๊ฐ€์žฅ ์ž‘์€ ๋งˆ์ง€๋ง‰ ๊ฐ’
32+
33+
for (int i = 0; i < n; ++i)
34+
{
35+
auto it = lower_bound(d.begin(), d.end(), nums[i]);
36+
if (it == d.end()) // nums[i]๊ฐ€ d์˜ ๋ชจ๋“  ์›์†Œ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ
37+
{
38+
d.push_back(nums[i]);
39+
}
40+
else // nums[i]์˜ ์ž๋ฆฌ๋ฅผ ์ฐพ์€ ๊ฒฝ์šฐ
41+
{
42+
*it = nums[i]; // ๊ทธ๋ƒฅ ๋Œ€์ฒดํ•˜๋Š”๊ฒŒ ์ด๋“์ž„ -> ๊ธธ์ด๋Š” ์œ ์ง€๋๊ณ , ๋” ์ด์–ด๋ถ™์ผ ์ˆ˜ ์žˆ๋Š” ์ˆ˜์˜ ๋ฒ”์œ„๋Š” ๋Š˜์–ด๋‚˜๋ฏ€๋กœ
43+
}
44+
}
45+
46+
return d.size();
47+
}
48+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Solution {
2+
public:
3+
vector<int> spiralOrder(vector<vector<int>>& matrix) {
4+
int m = matrix.size();
5+
int n = matrix[0].size();
6+
7+
int top = 0;
8+
int bottom = m - 1;
9+
int left = 0;
10+
int right = n - 1;
11+
12+
vector<int> v;
13+
v.reserve(m * n);
14+
15+
while(top <= bottom && left <= right)
16+
{
17+
// ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™
18+
for (int i = left; i <= right; ++i)
19+
{
20+
v.push_back(matrix[top][i]);
21+
}
22+
top++;
23+
24+
// ์•„๋ž˜์ชฝ์œผ๋กœ ์ด๋™
25+
for (int i = top; i <= bottom; ++i)
26+
{
27+
v.push_back(matrix[i][right]);
28+
}
29+
right--;
30+
31+
// ์™ผ์ชฝ์œผ๋กœ ์ด๋™
32+
if (top <= bottom)
33+
{
34+
for (int i = right; i >= left; --i)
35+
{
36+
v.push_back(matrix[bottom][i]);
37+
}
38+
bottom--;
39+
}
40+
41+
// ์œ„์ชฝ์œผ๋กœ ์ด๋™
42+
if (left <= right)
43+
{
44+
for (int i = bottom; i >= top; --i)
45+
{
46+
v.push_back(matrix[i][left]);
47+
}
48+
left++;
49+
}
50+
}
51+
52+
return v;
53+
}
54+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution {
2+
public:
3+
bool isValid(string s) {
4+
stack<char> st; // ์—ฌ๋Š” ๊ด„ํ˜ธ๋ฅผ ์ €์žฅํ•  ์Šคํƒ
5+
6+
for (char ch : s)
7+
{
8+
if (isOpening(ch)) // ์—ฌ๋Š” ๊ด„ํ˜ธ๋ผ๋ฉด ์Šคํƒ์— push
9+
{
10+
st.push(ch);
11+
continue;
12+
}
13+
14+
if (st.empty()) // ์Šคํƒ์ด ๋น„์—ˆ๋Š”๋ฐ ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ฉด invalid
15+
{
16+
return false;
17+
}
18+
19+
char t = st.top();
20+
st.pop();
21+
22+
// ๊ด„ํ˜ธ ์Œ์ด ๋งž์œผ๋ฉด ๊ณ„์† ์ง„ํ–‰
23+
if ((ch == ')' && t == '(')
24+
|| (ch == ']' && t == '[')
25+
|| (ch == '}' && t == '{'))
26+
{
27+
continue;
28+
}
29+
30+
// ๊ด„ํ˜ธ ์Œ์ด ๋งž์ง€ ์•Š์œผ๋ฉด invalid
31+
return false;
32+
}
33+
34+
return st.empty();
35+
}
36+
37+
private:
38+
bool isOpening(char c)
39+
{
40+
switch(c)
41+
{
42+
case '(':
43+
case '[':
44+
case '{':
45+
return true;
46+
}
47+
48+
return false;
49+
}
50+
};

0 commit comments

Comments
ย (0)