Skip to content

[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

Merged
merged 5 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions longest-consecutive-sequence/heozeop.cpp
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()) {
Comment on lines +11 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c++에서 find의 반환값(찾고자하는 값이 없는 경우)가 .end였군요!👍

++length;
}

maxLength = max(maxLength, length);
}

return maxLength;
}
};
19 changes: 19 additions & 0 deletions missing-number/heozeop.cpp
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;
}
};
23 changes: 23 additions & 0 deletions valid-palindrome/heozeop.cpp
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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
};
63 changes: 63 additions & 0 deletions word-search/heozeop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// time complexity: O(n * m * 3 ^ L), L은 최대 깊이(문자열 길이)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

시간복잡도에서 상하좌우의 4^L이 아닌 3^L로 계산하신 부분에 대해 설명이 궁금합니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
};