Skip to content

Commit 51f1e1e

Browse files
authoredMay 15, 2025
Merge pull request #1447 from sungjinwi/main
[sungjinwi] Week 06 solution
2 parents 5b25a99 + b4ad510 commit 51f1e1e

File tree

5 files changed

+248
-0
lines changed

5 files changed

+248
-0
lines changed
 
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
풀이 :
3+
최대 넓이는 양 끝 기둥 길이 중 짧은 쪽을 기준으로 정해진다
4+
양 끝에서 기둥을 시작하고 둘 중 짧은 쪽을 안쪽으로 이동하면서 최대 넓이를 찾는다
5+
6+
height의 개수 : N
7+
8+
TC : O(N)
9+
10+
SC : O(1)
11+
*/
12+
13+
class Solution {
14+
public:
15+
int maxArea(vector<int>& height) {
16+
int left = 0;
17+
int right = height.size() - 1;
18+
int max_area = 0;
19+
20+
while (left < right)
21+
{
22+
int cur_area = (right - left) * min(height[left], height[right]);
23+
if (cur_area > max_area)
24+
max_area = cur_area;
25+
if (height[left] <= height[right])
26+
left++;
27+
else
28+
right--;
29+
}
30+
return max_area;
31+
}
32+
};
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
풀이 :
3+
Trie를 활용해서 단어를 저장한다
4+
글자마다 이어지는 다음 글자에 대한 children[26](알파벳 소문자 개수 26개)를 가지고 현재 글자에서 끝나는지 여부(isEnd)를 가진다
5+
6+
addWord는 한글자씩 node에 없으면 추가하고 다음 글자의 노드로 이동하며
7+
word에 대한 반복을 마친 뒤 마지막 node의 isEnd를 1로 바꿈
8+
9+
search는 한글자씩 다음 노드로 이동하며 word의 끝에 다다랐을 떄 isEnd가 1인지 return
10+
dfs를 활용하여 '.'이 나올 경우 알파벳 26개 모두에 대해 다음 글자부터 시작하는 dfs를 호출
11+
12+
word의 길이 : W
13+
14+
TC :
15+
addWord : O(W)
16+
word 길이에 비례하여 반복문
17+
18+
search : O(26^W)
19+
최악의 경우 word길이 만큼 '.'이 있으면 하나하나 마다 26번의 재귀호출이 있다
20+
21+
SC :
22+
addWord : O(W)
23+
TrieNode 개수는 word길이에 비례
24+
25+
search : O(W)
26+
재귀 호출스택은 word 길이에 비례
27+
*/
28+
29+
class WordDictionary {
30+
private:
31+
struct TrieNode {
32+
int isEnd = 0;
33+
TrieNode* children[26] = {};
34+
};
35+
TrieNode* root;
36+
37+
public:
38+
WordDictionary() {
39+
this->root = new TrieNode();
40+
}
41+
42+
void addWord(string word) {
43+
TrieNode* node = root;
44+
for (auto& c : word)
45+
{
46+
int idx = c - 'a';
47+
if (!node->children[idx])
48+
node->children[idx] = new TrieNode();
49+
node = node->children[idx];
50+
}
51+
node->isEnd = 1;
52+
}
53+
54+
bool search(string word) {
55+
return dfs(word, this->root);
56+
}
57+
58+
bool dfs(string word, TrieNode* node) {
59+
if (word.empty())
60+
return node->isEnd;
61+
62+
for (int i = 0; i < word.size(); i++)
63+
{
64+
if (word[i] == '.')
65+
{
66+
for (int j=0; j < 26; j++)
67+
{
68+
if (node->children[j] && this->dfs(word.substr(i + 1), node->children[j]))
69+
return true;
70+
}
71+
return false;
72+
}
73+
int idx = word[i] - 'a';
74+
if (!node->children[idx])
75+
return false;
76+
node = node->children[idx];
77+
}
78+
return node->isEnd;
79+
}
80+
};
81+
82+
/**
83+
* Your WordDictionary object will be instantiated and called as such:
84+
* WordDictionary* obj = new WordDictionary();
85+
* obj->addWord(word);
86+
* bool param_2 = obj->search(word);
87+
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
nums와 길이가 같은 dp배열을 만들고 1로 초기화한다
3+
dp[i]의 값은 nums[i]으로 끝나는 LIS의 길이
4+
i 이후의 j에 대해 nums[j] > nums[i]일 경우 nums[j]를 nums[i]로 끝나는 LIS에 붙일 수 있으므로
5+
dp[j]와 dp[i + 1]을 비교해 큰 값으로 업데이트
6+
7+
nums의 길이 : N
8+
9+
TC : O(N^2)
10+
반복문 내부의 반복문
11+
12+
SC : O(N)
13+
dp의 길이는 nums길이에 비례
14+
*/
15+
16+
class Solution {
17+
public:
18+
int lengthOfLIS(vector<int>& nums) {
19+
vector<int> dp(nums.size(), 1);
20+
int max_len = 1;
21+
22+
for (int i = 0; i < nums.size(); i++)
23+
for (int j = i + 1; j < nums.size(); j++)
24+
{
25+
if (nums[j] > nums[i])
26+
{
27+
dp[j] = max(dp[i] + 1, dp[j]);
28+
max_len = max(dp[j], max_len);
29+
}
30+
}
31+
return max_len;
32+
}
33+
};

‎spiral-matrix/sungjinwi.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
풀이 :
3+
상하좌우 범위지정해서 이동하는 것이 아니라 n_rows, n_cols 길이만큼 이동으로 풀이
4+
col 방향으로 움직이면 이동할 row 감소, row 방향 움직이면 n_cols 1 감소
5+
row, col 한번씩 이동하면 direction *= -1로 방향을 바꿔준다
6+
7+
matrix 크기 : M * N
8+
9+
TC : O(M * N)
10+
matrix 전체 순환
11+
12+
SC : O(1)
13+
리턴할 ans 제외하면 추가 메모리 사용은 상수 개수의 변수
14+
*/
15+
16+
class Solution {
17+
public:
18+
vector<int> spiralOrder(vector<vector<int>>& matrix) {
19+
int n_rows = matrix.size();
20+
int n_cols = matrix[0].size();
21+
vector<int> ans;
22+
23+
int row = 0, col = -1;
24+
int direction = 1;
25+
26+
while (n_rows > 0 && n_cols > 0)
27+
{
28+
for (int i = 0; i < n_cols; i++)
29+
{
30+
col += direction;
31+
ans.push_back(matrix[row][col]);
32+
}
33+
n_rows--;
34+
35+
for (int i = 0; i < n_rows; i++)
36+
{
37+
row += direction;
38+
ans.push_back(matrix[row][col]);
39+
}
40+
n_cols--;
41+
42+
direction *= -1;
43+
}
44+
return ans;
45+
}
46+
};

‎valid-parentheses/sungjinwi.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
풀이 :
3+
stack을 이용해서 괄호를 넣고 뺸다.
4+
닫는 괄호 ),],}를 만났을 때 가장 위의 괄호가 대응하는 괄호면 제거하고 비어있거나 맞지 않는 괄호면 return false
5+
string을 모두 순회했을 떄 stack이 비어있지 않으면 return false
6+
7+
s의 길이 N
8+
9+
TC : O(N)
10+
s에 대해 반복 1회
11+
12+
SC : O(N)
13+
최악의 경우 stack 크기는 s의 길이에 비례
14+
*/
15+
16+
class Solution {
17+
public:
18+
bool isValid(string s) {
19+
stack<char> st;
20+
21+
for (auto& c : s)
22+
{
23+
if (c == ')')
24+
{
25+
if (st.empty() || st.top() != '(')
26+
return false;
27+
st.pop();
28+
}
29+
if (c == '}')
30+
{
31+
if (st.empty() || st.top() != '{')
32+
return false;
33+
st.pop();
34+
}
35+
if (c == ']')
36+
{
37+
if (st.empty() || st.top() != '[')
38+
return false;
39+
st.pop();
40+
}
41+
else
42+
st.push();
43+
}
44+
45+
if (!st.empty())
46+
return false;
47+
else
48+
return true;
49+
}
50+
};

0 commit comments

Comments
 (0)
Please sign in to comment.