Skip to content

Commit 1cb001f

Browse files
authored
Merge pull request #409 from heozeop/main
[crispy] week 4 solution
2 parents 94bfcdb + d3098b5 commit 1cb001f

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// time complexity: O(n)
2+
// spatail complexity: O(n)
3+
4+
class Solution {
5+
public:
6+
int longestConsecutive(vector<int>& nums) {
7+
unordered_set<int> exisingNum(nums.begin(), nums.end());
8+
9+
int maxLength = 0, length;
10+
for(int num : nums) {
11+
if(exisingNum.find(num - 1) != exisingNum.end()) {
12+
continue;
13+
}
14+
15+
length = 1;
16+
while(exisingNum.find(num + length) != exisingNum.end()) {
17+
++length;
18+
}
19+
20+
maxLength = max(maxLength, length);
21+
}
22+
23+
return maxLength;
24+
}
25+
};

missing-number/heozeop.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// time complexity: O(n)
2+
// spatial complexity: O(n)
3+
4+
class Solution {
5+
public:
6+
int missingNumber(vector<int>& nums) {
7+
set<int> exisingNums(nums.begin(), nums.end());
8+
9+
int answer = -1;
10+
for(int i = 0; i <= nums.size(); ++i) {
11+
if(exisingNums.find(i) == exisingNums.end()) {
12+
answer = i;
13+
break;
14+
}
15+
}
16+
17+
return answer;
18+
}
19+
};

valid-palindrome/heozeop.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time Complexity: O(n)
2+
// Spatial Complexity: O(n)
3+
4+
class Solution {
5+
public:
6+
bool isPalindrome(string s) {
7+
string temp = "";
8+
for(char c : s) {
9+
if(isalnum(c)) {
10+
temp += tolower(c);
11+
}
12+
}
13+
14+
int length = temp.length();
15+
for(int i = 0; i < length / 2; ++i) {
16+
if(temp[i] != temp[length - 1 - i]) {
17+
return false;
18+
}
19+
}
20+
21+
return true;
22+
}
23+
};

word-search/heozeop.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// time complexity: O(n * m * 3 ^ L), L은 최대 깊이(문자열 길이)
2+
// spatial complexity: O((n * m ) ^ 2)
3+
4+
class Solution {
5+
public:
6+
bool exist(vector<vector<char>>& board, string word) {
7+
vector<vector<bool>> visit;
8+
for(int i = 0; i < board.size(); ++i) {
9+
for(int j = 0; j < board[0].size(); ++j) {
10+
if(board[i][j] != word[0]) continue;
11+
visit = vector(board.size(), vector(board[0].size(), false));
12+
visit[i][j] = true;
13+
if (find(board, word, 1, {i,j}, visit)) {
14+
return true;
15+
}
16+
}
17+
}
18+
19+
return false;
20+
}
21+
22+
bool find(
23+
vector<vector<char>>& board,
24+
string word,
25+
int fi,
26+
pair<int,int> curPos,
27+
vector<vector<bool>>& visit
28+
) {
29+
if(fi == word.length()) {
30+
return true;
31+
}
32+
33+
char target = word[fi];
34+
int nr,ny;
35+
for(int i = 0; i < 4; ++i) {
36+
nr = curPos.first + DIRECTIONS[i][0];
37+
ny = curPos.second+ DIRECTIONS[i][1];
38+
39+
if (isOutSideOfBoard({nr,ny}, {board.size(), board[0].size()}) || visit[nr][ny] || board[nr][ny] != target) {
40+
continue;
41+
}
42+
43+
visit[nr][ny] = true;
44+
if(find(board, word, fi + 1, {nr,ny}, visit)) {
45+
return true;
46+
}
47+
visit[nr][ny] = false;
48+
}
49+
50+
return false;
51+
}
52+
53+
int DIRECTIONS[4][2] = {
54+
{-1, 0},
55+
{0, 1},
56+
{1, 0},
57+
{0, -1},
58+
};
59+
60+
bool isOutSideOfBoard(pair<int,int> cur, pair<int,int> boardSize) {
61+
return cur.first < 0 || cur.second < 0 || cur.first >= boardSize.first || cur.second >= boardSize.second;
62+
}
63+
};

0 commit comments

Comments
 (0)