From 2508b4824805e151a1e0ced575bacddc9c111e6c Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Sun, 1 Sep 2024 15:34:02 +0900 Subject: [PATCH 1/5] heozeop: valid palindrome --- valid-palindrome/heozeop.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 valid-palindrome/heozeop.cpp 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; + } +}; From 7fb858fb01df494f79fbef97dacb83564585427a Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Fri, 6 Sep 2024 10:40:50 +0900 Subject: [PATCH 2/5] heozeop: missing number --- missing-number/heozeop.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 missing-number/heozeop.cpp 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& nums) { + set 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; + } +}; From 8020e427ab3d03abac02ed7398e3841d4ce9c445 Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Fri, 6 Sep 2024 19:09:14 +0900 Subject: [PATCH 3/5] heozeop: longest consecutive sequence --- longest-consecutive-sequence/heozeop.cpp | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 longest-consecutive-sequence/heozeop.cpp 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& nums) { + unordered_set 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; + } +}; From 746fb8da87bb15bad0f5c0a595cffed2ac44e1cd Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Fri, 6 Sep 2024 19:16:12 +0900 Subject: [PATCH 4/5] heozeop: word search --- word-search/heozeop.cpp | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 word-search/heozeop.cpp diff --git a/word-search/heozeop.cpp b/word-search/heozeop.cpp new file mode 100644 index 000000000..5d763b5c6 --- /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>& board, string word) { + vector> 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>& board, + string word, + int fi, + pair curPos, + vector>& 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 cur, pair boardSize) { + return cur.first < 0 || cur.second < 0 || cur.first >= boardSize.first || cur.second >= boardSize.second; + } +}; From d3098b5d2cc6418401c52975337dd8bb5068b779 Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Fri, 6 Sep 2024 19:17:45 +0900 Subject: [PATCH 5/5] heozeop: word search fix time complexity --- word-search/heozeop.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/word-search/heozeop.cpp b/word-search/heozeop.cpp index 5d763b5c6..14bec79ca 100644 --- a/word-search/heozeop.cpp +++ b/word-search/heozeop.cpp @@ -1,4 +1,4 @@ -// time complexity: O(n * m * 3 ^ L), L은 최대 깊이 +// time complexity: O(n * m * 3 ^ L), L은 최대 깊이(문자열 길이) // spatial complexity: O((n * m ) ^ 2) class Solution {