diff --git a/longest-consecutive-sequence/heozeop.cpp b/longest-consecutive-sequence/heozeop.cpp new file mode 100644 index 000000000..d8d74530f --- /dev/null +++ b/longest-consecutive-sequence/heozeop.cpp @@ -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; + } +}; diff --git a/missing-number/heozeop.cpp b/missing-number/heozeop.cpp new file mode 100644 index 000000000..db7839d24 --- /dev/null +++ b/missing-number/heozeop.cpp @@ -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; + } +}; diff --git a/valid-palindrome/heozeop.cpp b/valid-palindrome/heozeop.cpp new file mode 100644 index 000000000..30b6b42f4 --- /dev/null +++ b/valid-palindrome/heozeop.cpp @@ -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)) { + 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; + } +}; diff --git a/word-search/heozeop.cpp b/word-search/heozeop.cpp new file mode 100644 index 000000000..14bec79ca --- /dev/null +++ b/word-search/heozeop.cpp @@ -0,0 +1,63 @@ +// time complexity: O(n * m * 3 ^ L), 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; + } +};