Skip to content

Commit

Permalink
Merge pull request #429 from obzva/main
Browse files Browse the repository at this point in the history
  • Loading branch information
obzva authored Sep 7, 2024
2 parents d8b4c28 + f47870c commit 7b991fd
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 0 deletions.
37 changes: 37 additions & 0 deletions longest-consecutive-sequence/flynn.cpp
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;
}
};
32 changes: 32 additions & 0 deletions maximum-product-subarray/flynn.cpp
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;
}
};
38 changes: 38 additions & 0 deletions missing-number/flynn.cpp
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;
}
};
36 changes: 36 additions & 0 deletions valid-palindrome/flynn.cpp
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;
}
};
72 changes: 72 additions & 0 deletions word-search/flynn.cpp
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;
}
};

0 comments on commit 7b991fd

Please sign in to comment.