-
-
Notifications
You must be signed in to change notification settings - Fork 195
[crispy] week 4 solution #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// time complexity: O(n) | ||
// spatail complexity: O(n) | ||
|
||
class Solution { | ||
public: | ||
int longestConsecutive(vector<int>& nums) { | ||
unordered_set<int> exisingNum(nums.begin(), nums.end()); | ||
|
||
int maxLength = 0, length; | ||
for(int num : nums) { | ||
if(exisingNum.find(num - 1) != exisingNum.end()) { | ||
continue; | ||
} | ||
|
||
length = 1; | ||
while(exisingNum.find(num + length) != exisingNum.end()) { | ||
++length; | ||
} | ||
|
||
maxLength = max(maxLength, length); | ||
} | ||
|
||
return maxLength; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// time complexity: O(n) | ||
// spatial complexity: O(n) | ||
|
||
class Solution { | ||
public: | ||
int missingNumber(vector<int>& nums) { | ||
set<int> exisingNums(nums.begin(), nums.end()); | ||
|
||
int answer = -1; | ||
for(int i = 0; i <= nums.size(); ++i) { | ||
if(exisingNums.find(i) == exisingNums.end()) { | ||
answer = i; | ||
break; | ||
} | ||
} | ||
|
||
return answer; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Time Complexity: O(n) | ||
// Spatial Complexity: O(n) | ||
|
||
class Solution { | ||
public: | ||
bool isPalindrome(string s) { | ||
string temp = ""; | ||
for(char c : s) { | ||
if(isalnum(c)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오, isalnum 내장 함수에 대해서도 처음 알았네요! |
||
temp += tolower(c); | ||
} | ||
} | ||
|
||
int length = temp.length(); | ||
for(int i = 0; i < length / 2; ++i) { | ||
if(temp[i] != temp[length - 1 - i]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// time complexity: O(n * m * 3 ^ L), L은 최대 깊이(문자열 길이) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 시간복잡도에서 상하좌우의 4^L이 아닌 3^L로 계산하신 부분에 대해 설명이 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 기존에 있던 방향은 확인하지 않아서 3^L로 계산했습니다! |
||
// spatial complexity: O((n * m ) ^ 2) | ||
|
||
class Solution { | ||
public: | ||
bool exist(vector<vector<char>>& board, string word) { | ||
vector<vector<bool>> visit; | ||
for(int i = 0; i < board.size(); ++i) { | ||
for(int j = 0; j < board[0].size(); ++j) { | ||
if(board[i][j] != word[0]) continue; | ||
visit = vector(board.size(), vector(board[0].size(), false)); | ||
visit[i][j] = true; | ||
if (find(board, word, 1, {i,j}, visit)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool find( | ||
vector<vector<char>>& board, | ||
string word, | ||
int fi, | ||
pair<int,int> curPos, | ||
vector<vector<bool>>& visit | ||
) { | ||
if(fi == word.length()) { | ||
return true; | ||
} | ||
|
||
char target = word[fi]; | ||
int nr,ny; | ||
for(int i = 0; i < 4; ++i) { | ||
nr = curPos.first + DIRECTIONS[i][0]; | ||
ny = curPos.second+ DIRECTIONS[i][1]; | ||
|
||
if (isOutSideOfBoard({nr,ny}, {board.size(), board[0].size()}) || visit[nr][ny] || board[nr][ny] != target) { | ||
continue; | ||
} | ||
|
||
visit[nr][ny] = true; | ||
if(find(board, word, fi + 1, {nr,ny}, visit)) { | ||
return true; | ||
} | ||
visit[nr][ny] = false; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
int DIRECTIONS[4][2] = { | ||
{-1, 0}, | ||
{0, 1}, | ||
{1, 0}, | ||
{0, -1}, | ||
}; | ||
|
||
bool isOutSideOfBoard(pair<int,int> cur, pair<int,int> boardSize) { | ||
return cur.first < 0 || cur.second < 0 || cur.first >= boardSize.first || cur.second >= boardSize.second; | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c++에서 find의 반환값(찾고자하는 값이 없는 경우)가 .end였군요!👍