-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #429 from obzva/main
- Loading branch information
Showing
5 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* ํ์ด | ||
* - ์ฃผ์ด์ง ๋ฐฐ์ด `nums`๋ก set `s`๋ฅผ ๋ง๋ญ๋๋ค | ||
* - `nums`๋ฅผ ์กฐํํ๋๋ฐ, ํ์ฌ ์กฐํ ์ค์ธ `num`์ ๋ํ์ฌ `num - 1`์ด `s`์ ํฌํจ๋์ง ์๋ ๊ฒฝ์ฐ๋ง while๋ฌธ์ ์คํํ์ฌ subsequence์ ๊ธธ์ด๋ฅผ ์ธก์ ํฉ๋๋ค | ||
* - `num - 1`์ด `s`์ ํฌํจ๋์ง ์๋๋ค๋ ๊ฒ์ `num`์ด ํด๋น subsequence์ ์ฒซ ์์์ ๋ปํฉ๋๋ค | ||
* | ||
* Big-O | ||
* - N: ์ฃผ์ด์ง ๋ฐฐ์ด `nums`์ ๊ธธ์ด | ||
* | ||
* - Time complexity: O(N) | ||
* - ์ด์ค ๋ฐ๋ณต๋ฌธ์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ก์ง๋ง, ์กฐ๊ฑด๋ฌธ๋๋ฌธ์ ๊ฐ ์์๋ฅผ ํ ๋ฒ์ฉ๋ง ์กฐํํฉ๋๋ค | ||
* | ||
* - Space complexity: O(N) | ||
* - `nums`๋ฅผ ๊ตฌ์ฑํ๋ ์์๊ฐ ๋ชจ๋ ๊ณ ์ ํ ์ ์์ผ ๊ฒฝ์ฐ, `s`์ ํฌ๊ธฐ๊ฐ `nums`๋งํผ ์ปค์ง ์ ์์ต๋๋ค | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int longestConsecutive(vector<int>& nums) { | ||
unordered_set<int> s(nums.begin(), nums.end()); | ||
|
||
int res = 0; | ||
for (int num : nums) { | ||
if (s.find(num - 1) != s.end()) continue; | ||
|
||
int curr = num; | ||
int streak = 1; | ||
while (s.find(curr + 1) != s.end()) { | ||
++curr; | ||
++streak; | ||
} | ||
res = max(res, streak); | ||
} | ||
|
||
return res; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* ํ์ด | ||
* - ์ฃผ์ด์ง ๋ฐฐ์ด `nums`๋ฅผ ์์๋๋ก ์กฐํํฉ๋๋ค | ||
* - 0๊ณผ ์์๋ฅผ ๊ณฑํ๋ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํ๊ธฐ ์ํด ํ์ฌ subarray์ ๊ณฑ์ ์ต๋๊ฐ๋ฟ๋ง ์๋๋ผ ์ต์๊ฐ ๋ํ ๊ธฐ๋กํฉ๋๋ค | ||
* | ||
* Big-O | ||
* - N: ์ฃผ์ด์ง ๋ฐฐ์ด `nums`์ size | ||
* | ||
* - Time complexity: O(N) | ||
* - Space complexity: O(1) | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int maxProduct(vector<int>& nums) { | ||
int max_prod = nums[0]; | ||
int min_prod = nums[0]; | ||
int res = nums[0]; | ||
|
||
for (int i = 1; i < nums.size(); i++) { | ||
int curr = nums[i]; | ||
|
||
int tmp_max = max(curr, max(curr * max_prod, curr * min_prod)); | ||
min_prod = min(curr, min(curr * max_prod, curr * min_prod)); | ||
max_prod = tmp_max; | ||
|
||
res = max(res, max_prod); | ||
} | ||
|
||
return res; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* ํ์ด | ||
* - ํน์ ์ ์๊ฐ `nums` ๋ฐฐ์ด์ ํตํด ์ฃผ์ด์ก๋์ง ์ฌ๋ถ๋ฅผ ์ฒดํฌํ๋ ๋ฐฐ์ด `check`๋ฅผ ๋ง๋ญ๋๋ค | ||
* - `nums`๋ฅผ ์กฐํํ๋ฉฐ `check`๋ฐฐ์ด์ ๊ฐ๋ค์ ๋ณ๊ฒฝํฉ๋๋ค | ||
* - `check`๋ฐฐ์ด์ ์กฐํํ์ฌ ๋๋ฝ๋์๋ ์ ์๋ฅผ ํ์ธํฉ๋๋ค | ||
* | ||
* Big-O | ||
* - N: ์ฃผ์ด์ง ๋ฐฐ์ด `nums`์ ํฌ๊ธฐ | ||
* | ||
* - Time Complexity: O(N) | ||
* - ๋ฐฐ์ด `nums`๋ฅผ ์กฐํํ๋ ๋ฐ๋ณต๋ฌธ์ O(N)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋๋ค | ||
* - ๋ฐฐ์ด `check`๋ฅผ ์กฐํํ๋ ๋ฐ๋ณต๋ฌธ ๋ํ O(N)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋๋ค | ||
* - ๋ฐ๋ผ์ ์ ์ฒด ์๊ฐ ๋ณต์ก๋๋ O(N)์ ๋๋ค | ||
* | ||
* - Space Complexity: O(N) | ||
* - `check`๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ ์ ๋ ฅ๊ฐ์ ๋น๋กํ์ฌ ์ ํ์ ์ผ๋ก ์ฆ๊ฐํฉ๋๋ค | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int missingNumber(vector<int>& nums) { | ||
int n = nums.size(); | ||
|
||
vector<bool> check(n + 1, false); | ||
|
||
for (auto num : nums) check[num] = true; | ||
|
||
int res; | ||
for (int i = 0; i < n + 1; i++) { | ||
if (!check[i]) { | ||
res = i; | ||
break; | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* ํ์ด | ||
* - ์ฃผ์ด์ง string `s`์ ์ ๋์์๋ถํฐ ์ฐจ๋ก๋๋ก ๋น๊ตํด๊ฐ๋ฉฐ palindrome ์ฌ๋ถ๋ฅผ ํ๋จํฉ๋๋ค | ||
* | ||
* Big-O | ||
* - N: ์ฃผ์ด์ง string `s`์ ๊ธธ์ด | ||
* | ||
* - Time Complexity: O(N) | ||
* - `s`๊ฐ palindrome์ธ ๊ฒฝ์ฐ, `s` ์ ์ฒด๋ฅผ ํ์ํ๋ฏ๋ก O(N)์ ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋๋ค | ||
* | ||
* - Space Complexity: O(1) | ||
* - ์ ๋ ฅ๊ฐ๊ณผ ๋ฌด๊ดํ๊ฒ ์ผ์ ํ ์ ์ฅ๊ณต๊ฐ์ ์ฌ์ฉํฉ๋๋ค | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
bool isPalindrome(string s) { | ||
string::iterator lo = s.begin(); | ||
string::iterator hi = s.end(); | ||
|
||
while (lo <= hi) { | ||
if (isalnum(*lo) && isalnum(*hi)) { | ||
if (tolower(*lo) != tolower(*hi)) return false; | ||
else { | ||
++lo; | ||
--hi; | ||
continue; | ||
} | ||
} | ||
if (!isalnum(*lo)) ++lo; | ||
if (!isalnum(*hi)) --hi; | ||
} | ||
|
||
return true; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* ํ์ด | ||
* - dfs์ backtracking์ ์ด์ฉํ์ฌ ํ์์ต๋๋ค | ||
* | ||
* Big-O | ||
* - M: ์ฃผ์ด์ง grid `board`์ ํ ์ | ||
* - N: ์ฃผ์ด์ง grid `board`์ ์ด ์ | ||
* - W: ์ฃผ์ด์ง string `word`์ size | ||
* | ||
* - Time complexity: O(M * N * 3 ^ W) | ||
* - `exist`ํจ์๊ฐ grid ์์ ๋ชจ๋๋ฅผ ์กฐํํฉ๋๋ค -> O(M * N) | ||
* - ๋ง์ฝ `dfs`ํจ์๊ฐ ์คํ๋ ๊ฒฝ์ฐ, ํด๋น ํจ์๋ ์ต๋ 3๋ฐฉํฅ์ ๋ํด ์ฌ๊ทํธ์ถ์ ์คํํฉ๋๋ค (์ด์ ์ขํ๋ก๋ `dfs`๋ฅผ ํธ์ถํ์ง ์๊ธฐ ๋๋ฌธ) | ||
* - ์ฌ๊ท ํธ์ถ ์คํ์ ํฌ๊ธฐ๋ ์ฃผ์ด์ง string `word`์ ๊ธธ์ด์ ๋น๋กํฉ๋๋ค -> O(3^W) | ||
* | ||
* - Space complexity: O(M * N + W) | ||
* - ์ฌ๊ท ํธ์ถ ์คํ์ ํฌ๊ธฐ๋ ์ฃผ์ด์ง string `word`์ ๊ธธ์ด์ ๋น๋กํฉ๋๋ค -> O(W) | ||
* - ํ์ ์ฌ๋ถ๋ฅผ ๊ธฐ๋กํ๋ `visit` ๋ฐฐ์ด์ ํฌ๊ธฐ๋ `board`์ ๊ฐ์ต๋๋ค -> O(M * N) | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
bool dfs(vector<vector<char>>& board, string word, vector<vector<bool>>& visit, int idx, int r, int c) { | ||
if (word.size() - 1 == idx) return true; | ||
|
||
pair<int, int> dirs[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; | ||
int R = board.size(); | ||
int C = board[0].size(); | ||
|
||
visit[r][c] = true; | ||
|
||
int next_idx = idx + 1; | ||
|
||
bool res = false; | ||
|
||
for (auto dir : dirs) { | ||
int next_r = r + dir.first; | ||
int next_c = c + dir.second; | ||
|
||
if (0 <= next_r && next_r < R && 0 <= next_c && next_c < C && !visit[next_r][next_c]) { | ||
if (board[next_r][next_c] == word[next_idx] && dfs(board, word, visit, next_idx, next_r, next_c)) { | ||
res = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
visit[r][c] = false; | ||
|
||
return res; | ||
} | ||
|
||
bool exist(vector<vector<char>>& board, string word) { | ||
int R = board.size(); | ||
int C = board[0].size(); | ||
vector<vector<bool>> visit; | ||
for (int i = 0; i < R; i++) { | ||
vector<bool> tmp; | ||
for (int j = 0; j < C; j++) { | ||
tmp.push_back(false); | ||
} | ||
visit.push_back(tmp); | ||
} | ||
|
||
for (int i = 0; i < R; i++) { | ||
for (int j = 0; j < C; j++) { | ||
if (board[i][j] == word[0] && dfs(board, word, visit, 0, i, j)) return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
}; |