From 138a29442ea1d48bf6a24215da9dd143deed6fb5 Mon Sep 17 00:00:00 2001 From: obzva Date: Thu, 5 Sep 2024 21:51:13 +0900 Subject: [PATCH 1/5] Solution: Valid Palindrome --- valid-palindrome/flynn.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 valid-palindrome/flynn.cpp diff --git a/valid-palindrome/flynn.cpp b/valid-palindrome/flynn.cpp new file mode 100644 index 000000000..a61e543c7 --- /dev/null +++ b/valid-palindrome/flynn.cpp @@ -0,0 +1,36 @@ +/** + * 풀이 + * - 주어진 string `s`의 양 끝에서부터 차례대로 비교해가며 palindrome 여부를 판단합니다 + * + * Big-O + * - N: 주어진 string `s`의 길이 + * + * - Time Complexity: O(N) + * - `s`가 palindrome인 경우, `s` 전체를 탐색하므로 O(N)의 시간복잡도를 가집니다 + * + * - Space Complexity: O(1) + * - 입력값과 무관하게 일정한 저장공간을 사용합니다 + */ + +class Solution { +public: + bool isPalindrome(string s) { + string::iterator lo = s.begin(); + string::iterator hi = s.end(); + + while (lo <= hi) { + if (isalnum(*lo) && isalnum(*hi)) { + if (tolower(*lo) != tolower(*hi)) return false; + else { + ++lo; + --hi; + continue; + } + } + if (!isalnum(*lo)) ++lo; + if (!isalnum(*hi)) --hi; + } + + return true; + } +}; From c40ce208a11074ad924e19a8857c615ef92d6d72 Mon Sep 17 00:00:00 2001 From: obzva Date: Thu, 5 Sep 2024 22:17:04 +0900 Subject: [PATCH 2/5] Solution: Missing Number --- missing-number/flynn.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 missing-number/flynn.cpp diff --git a/missing-number/flynn.cpp b/missing-number/flynn.cpp new file mode 100644 index 000000000..acdfc69d7 --- /dev/null +++ b/missing-number/flynn.cpp @@ -0,0 +1,38 @@ +/** + * 풀이 + * - 특정 정수가 `nums` 배열을 통해 주어졌는지 여부를 체크하는 배열 `check`를 만듭니다 + * - `nums`를 조회하며 `check`배열의 값들을 변경합니다 + * - `check`배열을 조회하여 누락되었던 정수를 확인합니다 + * + * Big-O + * - N: 주어진 배열 `nums`의 크기 + * + * - Time Complexity: O(N) + * - 배열 `nums`를 조회하는 반복문은 O(N)의 시간 복잡도를 가집니다 + * - 배열 `check`를 조회하는 반복문 또한 O(N)의 시간 복잡도를 가집니다 + * - 따라서 전체 시간 복잡도는 O(N)입니다 + * + * - Space Complexity: O(N) + * - `check`배열의 크기가 입력값에 비례하여 선형적으로 증가합니다 + */ + +class Solution { +public: + int missingNumber(vector& nums) { + int n = nums.size(); + + vector check(n + 1, false); + + for (auto num : nums) check[num] = true; + + int res; + for (int i = 0; i < n + 1; i++) { + if (!check[i]) { + res = i; + break; + } + } + + return res; + } +}; From 02645c84ad2bd3c591bffc0aec09fd69d29ddb48 Mon Sep 17 00:00:00 2001 From: obzva Date: Sat, 7 Sep 2024 03:09:44 +0900 Subject: [PATCH 3/5] Solution: Longest Consecutive Sequence --- longest-consecutive-sequence/flynn.cpp | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 longest-consecutive-sequence/flynn.cpp diff --git a/longest-consecutive-sequence/flynn.cpp b/longest-consecutive-sequence/flynn.cpp new file mode 100644 index 000000000..e744a3f27 --- /dev/null +++ b/longest-consecutive-sequence/flynn.cpp @@ -0,0 +1,37 @@ +/** + * 풀이 + * - 주어진 배열 `nums`로 set `s`를 만듭니다 + * - `nums`를 조회하는데, 현재 조회 중인 `num`에 대하여 `num - 1`이 `s`에 포함되지 않는 경우만 while문을 실행하여 subsequence의 길이를 측정합니다 + * - `num - 1`이 `s`에 포함되지 않는다는 것은 `num`이 해당 subsequence의 첫 수임을 뜻합니다 + * + * Big-O + * - N: 주어진 배열 `nums`의 길이 + * + * - Time complexity: O(N) + * - 이중 반복문의 구조를 가졌지만, 조건문때문에 각 원소를 한 번씩만 조회합니다 + * + * - Space complexity: O(N) + * - `nums`를 구성하는 원소가 모두 고유한 정수일 경우, `s`의 크기가 `nums`만큼 커질 수 있습니다 + */ + +class Solution { +public: + int longestConsecutive(vector& nums) { + unordered_set s(nums.begin(), nums.end()); + + int res = 0; + for (int num : nums) { + if (s.find(num - 1) != s.end()) continue; + + int curr = num; + int streak = 1; + while (s.find(curr + 1) != s.end()) { + ++curr; + ++streak; + } + res = max(res, streak); + } + + return res; + } +}; From 9aee3328a76ae64fca594dcc656c0b3639145890 Mon Sep 17 00:00:00 2001 From: obzva Date: Sat, 7 Sep 2024 04:22:56 +0900 Subject: [PATCH 4/5] Solution: Word Search --- word-search/flynn.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 word-search/flynn.cpp diff --git a/word-search/flynn.cpp b/word-search/flynn.cpp new file mode 100644 index 000000000..06825e21e --- /dev/null +++ b/word-search/flynn.cpp @@ -0,0 +1,72 @@ +/** + * 풀이 + * - dfs와 backtracking을 이용하여 풀었습니다 + * + * Big-O + * - M: 주어진 grid `board`의 행 수 + * - N: 주어진 grid `board`의 열 수 + * - W: 주어진 string `word`의 size + * + * - Time complexity: O(M * N * 3 ^ W) + * - `exist`함수가 grid 원소 모두를 조회합니다 -> O(M * N) + * - 만약 `dfs`함수가 실행될 경우, 해당 함수는 최대 3방향에 대해 재귀호출을 실행합니다 (이전 좌표로는 `dfs`를 호출하지 않기 때문) + * - 재귀 호출 스택의 크기는 주어진 string `word`의 길이에 비례합니다 -> O(3^W) + * + * - Space complexity: O(M * N + W) + * - 재귀 호출 스택의 크기는 주어진 string `word`의 길이에 비례합니다 -> O(W) + * - 탐색 여부를 기록하는 `visit` 배열의 크기는 `board`와 같습니다 -> O(M * N) + */ + +class Solution { +public: + bool dfs(vector>& board, string word, vector>& visit, int idx, int r, int c) { + if (word.size() - 1 == idx) return true; + + pair dirs[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + int R = board.size(); + int C = board[0].size(); + + visit[r][c] = true; + + int next_idx = idx + 1; + + bool res = false; + + for (auto dir : dirs) { + int next_r = r + dir.first; + int next_c = c + dir.second; + + if (0 <= next_r && next_r < R && 0 <= next_c && next_c < C && !visit[next_r][next_c]) { + if (board[next_r][next_c] == word[next_idx] && dfs(board, word, visit, next_idx, next_r, next_c)) { + res = true; + break; + } + } + } + + visit[r][c] = false; + + return res; + } + + bool exist(vector>& board, string word) { + int R = board.size(); + int C = board[0].size(); + vector> visit; + for (int i = 0; i < R; i++) { + vector tmp; + for (int j = 0; j < C; j++) { + tmp.push_back(false); + } + visit.push_back(tmp); + } + + for (int i = 0; i < R; i++) { + for (int j = 0; j < C; j++) { + if (board[i][j] == word[0] && dfs(board, word, visit, 0, i, j)) return true; + } + } + + return false; + } +}; From f47870c6c28b4ed179874949a8ae3326072dad44 Mon Sep 17 00:00:00 2001 From: obzva Date: Sat, 7 Sep 2024 04:46:37 +0900 Subject: [PATCH 5/5] Solution: Maximum Product Subarray --- maximum-product-subarray/flynn.cpp | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 maximum-product-subarray/flynn.cpp diff --git a/maximum-product-subarray/flynn.cpp b/maximum-product-subarray/flynn.cpp new file mode 100644 index 000000000..d7411c4f8 --- /dev/null +++ b/maximum-product-subarray/flynn.cpp @@ -0,0 +1,32 @@ +/** + * 풀이 + * - 주어진 배열 `nums`를 순서대로 조회합니다 + * - 0과 음수를 곱하는 경우를 고려하기 위해 현재 subarray의 곱의 최대값뿐만 아니라 최소값 또한 기록합니다 + * + * Big-O + * - N: 주어진 배열 `nums`의 size + * + * - Time complexity: O(N) + * - Space complexity: O(1) + */ + +class Solution { +public: + int maxProduct(vector& nums) { + int max_prod = nums[0]; + int min_prod = nums[0]; + int res = nums[0]; + + for (int i = 1; i < nums.size(); i++) { + int curr = nums[i]; + + int tmp_max = max(curr, max(curr * max_prod, curr * min_prod)); + min_prod = min(curr, min(curr * max_prod, curr * min_prod)); + max_prod = tmp_max; + + res = max(res, max_prod); + } + + return res; + } +};