Skip to content

Commit 7b991fd

Browse files
authored
Merge pull request #429 from obzva/main
2 parents d8b4c28 + f47870c commit 7b991fd

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 풀이
3+
* - 주어진 배열 `nums`로 set `s`를 만듭니다
4+
* - `nums`를 조회하는데, 현재 조회 중인 `num`에 대하여 `num - 1`이 `s`에 포함되지 않는 경우만 while문을 실행하여 subsequence의 길이를 측정합니다
5+
* - `num - 1`이 `s`에 포함되지 않는다는 것은 `num`이 해당 subsequence의 첫 수임을 뜻합니다
6+
*
7+
* Big-O
8+
* - N: 주어진 배열 `nums`의 길이
9+
*
10+
* - Time complexity: O(N)
11+
* - 이중 반복문의 구조를 가졌지만, 조건문때문에 각 원소를 한 번씩만 조회합니다
12+
*
13+
* - Space complexity: O(N)
14+
* - `nums`를 구성하는 원소가 모두 고유한 정수일 경우, `s`의 크기가 `nums`만큼 커질 수 있습니다
15+
*/
16+
17+
class Solution {
18+
public:
19+
int longestConsecutive(vector<int>& nums) {
20+
unordered_set<int> s(nums.begin(), nums.end());
21+
22+
int res = 0;
23+
for (int num : nums) {
24+
if (s.find(num - 1) != s.end()) continue;
25+
26+
int curr = num;
27+
int streak = 1;
28+
while (s.find(curr + 1) != s.end()) {
29+
++curr;
30+
++streak;
31+
}
32+
res = max(res, streak);
33+
}
34+
35+
return res;
36+
}
37+
};

maximum-product-subarray/flynn.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 풀이
3+
* - 주어진 배열 `nums`를 순서대로 조회합니다
4+
* - 0과 음수를 곱하는 경우를 고려하기 위해 현재 subarray의 곱의 최대값뿐만 아니라 최소값 또한 기록합니다
5+
*
6+
* Big-O
7+
* - N: 주어진 배열 `nums`의 size
8+
*
9+
* - Time complexity: O(N)
10+
* - Space complexity: O(1)
11+
*/
12+
13+
class Solution {
14+
public:
15+
int maxProduct(vector<int>& nums) {
16+
int max_prod = nums[0];
17+
int min_prod = nums[0];
18+
int res = nums[0];
19+
20+
for (int i = 1; i < nums.size(); i++) {
21+
int curr = nums[i];
22+
23+
int tmp_max = max(curr, max(curr * max_prod, curr * min_prod));
24+
min_prod = min(curr, min(curr * max_prod, curr * min_prod));
25+
max_prod = tmp_max;
26+
27+
res = max(res, max_prod);
28+
}
29+
30+
return res;
31+
}
32+
};

missing-number/flynn.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 풀이
3+
* - 특정 정수가 `nums` 배열을 통해 주어졌는지 여부를 체크하는 배열 `check`를 만듭니다
4+
* - `nums`를 조회하며 `check`배열의 값들을 변경합니다
5+
* - `check`배열을 조회하여 누락되었던 정수를 확인합니다
6+
*
7+
* Big-O
8+
* - N: 주어진 배열 `nums`의 크기
9+
*
10+
* - Time Complexity: O(N)
11+
* - 배열 `nums`를 조회하는 반복문은 O(N)의 시간 복잡도를 가집니다
12+
* - 배열 `check`를 조회하는 반복문 또한 O(N)의 시간 복잡도를 가집니다
13+
* - 따라서 전체 시간 복잡도는 O(N)입니다
14+
*
15+
* - Space Complexity: O(N)
16+
* - `check`배열의 크기가 입력값에 비례하여 선형적으로 증가합니다
17+
*/
18+
19+
class Solution {
20+
public:
21+
int missingNumber(vector<int>& nums) {
22+
int n = nums.size();
23+
24+
vector<bool> check(n + 1, false);
25+
26+
for (auto num : nums) check[num] = true;
27+
28+
int res;
29+
for (int i = 0; i < n + 1; i++) {
30+
if (!check[i]) {
31+
res = i;
32+
break;
33+
}
34+
}
35+
36+
return res;
37+
}
38+
};

valid-palindrome/flynn.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 풀이
3+
* - 주어진 string `s`의 양 끝에서부터 차례대로 비교해가며 palindrome 여부를 판단합니다
4+
*
5+
* Big-O
6+
* - N: 주어진 string `s`의 길이
7+
*
8+
* - Time Complexity: O(N)
9+
* - `s`가 palindrome인 경우, `s` 전체를 탐색하므로 O(N)의 시간복잡도를 가집니다
10+
*
11+
* - Space Complexity: O(1)
12+
* - 입력값과 무관하게 일정한 저장공간을 사용합니다
13+
*/
14+
15+
class Solution {
16+
public:
17+
bool isPalindrome(string s) {
18+
string::iterator lo = s.begin();
19+
string::iterator hi = s.end();
20+
21+
while (lo <= hi) {
22+
if (isalnum(*lo) && isalnum(*hi)) {
23+
if (tolower(*lo) != tolower(*hi)) return false;
24+
else {
25+
++lo;
26+
--hi;
27+
continue;
28+
}
29+
}
30+
if (!isalnum(*lo)) ++lo;
31+
if (!isalnum(*hi)) --hi;
32+
}
33+
34+
return true;
35+
}
36+
};

word-search/flynn.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* 풀이
3+
* - dfs와 backtracking을 이용하여 풀었습니다
4+
*
5+
* Big-O
6+
* - M: 주어진 grid `board`의 행 수
7+
* - N: 주어진 grid `board`의 열 수
8+
* - W: 주어진 string `word`의 size
9+
*
10+
* - Time complexity: O(M * N * 3 ^ W)
11+
* - `exist`함수가 grid 원소 모두를 조회합니다 -> O(M * N)
12+
* - 만약 `dfs`함수가 실행될 경우, 해당 함수는 최대 3방향에 대해 재귀호출을 실행합니다 (이전 좌표로는 `dfs`를 호출하지 않기 때문)
13+
* - 재귀 호출 스택의 크기는 주어진 string `word`의 길이에 비례합니다 -> O(3^W)
14+
*
15+
* - Space complexity: O(M * N + W)
16+
* - 재귀 호출 스택의 크기는 주어진 string `word`의 길이에 비례합니다 -> O(W)
17+
* - 탐색 여부를 기록하는 `visit` 배열의 크기는 `board`와 같습니다 -> O(M * N)
18+
*/
19+
20+
class Solution {
21+
public:
22+
bool dfs(vector<vector<char>>& board, string word, vector<vector<bool>>& visit, int idx, int r, int c) {
23+
if (word.size() - 1 == idx) return true;
24+
25+
pair<int, int> dirs[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
26+
int R = board.size();
27+
int C = board[0].size();
28+
29+
visit[r][c] = true;
30+
31+
int next_idx = idx + 1;
32+
33+
bool res = false;
34+
35+
for (auto dir : dirs) {
36+
int next_r = r + dir.first;
37+
int next_c = c + dir.second;
38+
39+
if (0 <= next_r && next_r < R && 0 <= next_c && next_c < C && !visit[next_r][next_c]) {
40+
if (board[next_r][next_c] == word[next_idx] && dfs(board, word, visit, next_idx, next_r, next_c)) {
41+
res = true;
42+
break;
43+
}
44+
}
45+
}
46+
47+
visit[r][c] = false;
48+
49+
return res;
50+
}
51+
52+
bool exist(vector<vector<char>>& board, string word) {
53+
int R = board.size();
54+
int C = board[0].size();
55+
vector<vector<bool>> visit;
56+
for (int i = 0; i < R; i++) {
57+
vector<bool> tmp;
58+
for (int j = 0; j < C; j++) {
59+
tmp.push_back(false);
60+
}
61+
visit.push_back(tmp);
62+
}
63+
64+
for (int i = 0; i < R; i++) {
65+
for (int j = 0; j < C; j++) {
66+
if (board[i][j] == word[0] && dfs(board, word, visit, 0, i, j)) return true;
67+
}
68+
}
69+
70+
return false;
71+
}
72+
};

0 commit comments

Comments
 (0)