From 20d457305456254b83e28ddca45a92df0299f3ba Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Thu, 10 Apr 2025 23:10:12 +0900 Subject: [PATCH 1/5] #218 valid-anagram solution --- valid-anagram/sungjinwi.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 valid-anagram/sungjinwi.cpp diff --git a/valid-anagram/sungjinwi.cpp b/valid-anagram/sungjinwi.cpp new file mode 100644 index 000000000..19573ca95 --- /dev/null +++ b/valid-anagram/sungjinwi.cpp @@ -0,0 +1,26 @@ +/* + 풀이 : + 해시테이블에 string을 순회하며 문자 : 빈도로 값 저장 후 두 데이터가 같은 지 비교 + + TC : O(S + T) + SC : O(S + T) +*/ + +#include +#include +using namespace std; + +class Solution { +public: + bool isAnagram(string s, string t) { + unordered_map s_map; + unordered_map t_map; + + for (char c : s) + s_map[c]++; + for (char c : t) + t_map[c]++; + + return (s_map == t_map); + } +}; From 3549a8c16bbae5eaad304c7cef85ea3595fe3f27 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Thu, 10 Apr 2025 23:30:42 +0900 Subject: [PATCH 2/5] #230 climbing-stairs solution --- climbing-stairs/sungjinwi.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 climbing-stairs/sungjinwi.cpp diff --git a/climbing-stairs/sungjinwi.cpp b/climbing-stairs/sungjinwi.cpp new file mode 100644 index 000000000..16b4a3ff4 --- /dev/null +++ b/climbing-stairs/sungjinwi.cpp @@ -0,0 +1,31 @@ +/* + 풀이 : + n이 1과 2일 떄는 따로 처리, 그 외에 n번째는 prv(n - 2번째) + cur(n -1번째)로 값을 업데이트 하며 n까지 더해나감 + + TC : O(N) + n의 크기에 반복문이 비례한다 + + SC : O(1) + n의 크기와 상관없이 3개의 변수 사용 +*/ + +class Solution { +public: + int climbStairs(int n) { + if (n == 1) + return 1; + if (n == 2) + return 2; + + int prv = 1; + int cur = 2; + int tmp; + for (int i = 3; i <= n; i++) + { + tmp = cur; + cur = cur + prv; + prv = tmp; + } + return cur; + } +}; From c4240c81afb5f7363fca88298ceca8e11f43c4ad Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Fri, 11 Apr 2025 00:30:58 +0900 Subject: [PATCH 3/5] #239 product-of-array-except-self --- product-of-array-except-self/sungjinwi.cpp | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 product-of-array-except-self/sungjinwi.cpp diff --git a/product-of-array-except-self/sungjinwi.cpp b/product-of-array-except-self/sungjinwi.cpp new file mode 100644 index 000000000..b3f569b3d --- /dev/null +++ b/product-of-array-except-self/sungjinwi.cpp @@ -0,0 +1,39 @@ +/* + 풀이 : + 해당 인덱스 이전 값들의 곱을 before에 저장(인덱스 0 이전에는 값이 없으므로 1로 초기화) + 해당 인덱스 이후 값들의 곱을 after에 저장(인덱스 n - 1이후에는 값이 없으므로 1로 초기화) + 인덱스 0부터 반복문을 돌며 ans에 인덱스 이전값들의 곱 before를 곱함 & before값에 현재 num을 곱함 + 인덱스 끝부터 반복문을 돌며 after를 이용해 동일 작업 + + nums의 길이 : N + + TC : O(N) + 반복문 2회 O(N + N) + + SC : O(1) (ans배열 제외) + N에 관계없이 before와 after 변수만 사용 +*/ + +#include +using namespace std; + +class Solution { +public: + vector productExceptSelf(vector& nums) { + int before = 1; + int after = 1; + vector ans(nums.size(), 1); + + for (int i = 0; i < nums.size(); i++) + { + ans[i] *= before; + before *= nums[i]; + } + for (int i = nums.size() - 1; i >= 0; i--) + { + ans[i] *= after; + after *= nums[i]; + } + return ans; + } +}; From 9a1aa258ca14edb59050a5cc75c956c6030c2b10 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Fri, 11 Apr 2025 20:14:32 +0900 Subject: [PATCH 4/5] #241 3sum solution --- 3sum/sungjinwi.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 3sum/sungjinwi.cpp diff --git a/3sum/sungjinwi.cpp b/3sum/sungjinwi.cpp new file mode 100644 index 000000000..b24b3b15d --- /dev/null +++ b/3sum/sungjinwi.cpp @@ -0,0 +1,60 @@ +/* + 풀이 : + nums 배열을 정렬시킨 후 반복되는 값을 건너뛰며 두 포인터 기법을 사용한다 + i포인터와 left, right 포인터의 값의 합이 0보다 작으면 left++, 크면 right-- + 0이면 ans에 저장하고 left++, right--하는 로직을 left < right인 동안 반복한다 + + nums의 길이 N + + TC : O(N^2) + 외부 반복문 N * 내부 반복문 N + + SC : O(1) (ans 제외) + left, right, threeSum 3개의 변수만 사용한다 +*/ + +#include +#include +using namespace std; + +class Solution { +public: + vector> threeSum(vector& nums) { + vector> ans; + int left; + int right; + int threeSum; + + sort(nums.begin(), nums.end()); + for (int i = 0; i < nums.size() - 2; i++) + { + // i포인터 중복 제거 + if (i > 0 && nums[i] == nums[i - 1]) + continue ; + + left = i + 1; + right = nums.size() - 1; + while (left < right) + { + threeSum = nums[i] + nums[left] + nums[right]; + if (threeSum < 0) + left++; + else if(threeSum > 0) + right--; + else + { + ans.push_back({nums[i], nums[left], nums[right]}); + // left포인터 중복 제거 + while (left < right && nums[left] == nums[left + 1]) + left++; + // right 포인터 중복 제거 + while (left < right && nums[right] == nums[right - 1]) + right--; + left++; + right--; + } + } + } + return ans; + } +}; From 3dc6c7dd46848009f0de642dc5d6c88428cf708d Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Fri, 11 Apr 2025 20:25:32 +0900 Subject: [PATCH 5/5] #251 validate-binary-search-tree solution --- validate-binary-search-tree/sungjinwi.cpp | 53 +++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 validate-binary-search-tree/sungjinwi.cpp diff --git a/validate-binary-search-tree/sungjinwi.cpp b/validate-binary-search-tree/sungjinwi.cpp new file mode 100644 index 000000000..42a9524ac --- /dev/null +++ b/validate-binary-search-tree/sungjinwi.cpp @@ -0,0 +1,53 @@ +/* + 풀이 : + 중위순회로 BST를 탐색하면 오름차순으로 탐색 + ->중위순회 했을 떄 오름차순이 아니면 BST가 아니다 + 현재 node의 val이 last_val보다 더 큰지 확인하면서 탐색한다 + + 초기값은 INT_MIN이 있을경우를 생각해서 그보다 작은 값과 비교하기 위해 LONG_MIN사용 + + node 갯수 N + + TC : O(N) + 노드 전체 순회 + + SC : O(N) + 재귀 호출스택이 N에 비례 +*/ + +#include + +// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + bool isValidBST(TreeNode* root) { + long min = LONG_MIN; + return dfs(root, &min); + } + + bool dfs(TreeNode* node, long* last_val){ + if (!node) + return true; + + if (!dfs(node->left, last_val)) + return false; + + if (*last_val >= node->val) + return false; + *last_val = node->val; + + if (!dfs(node->right, last_val)) + return false; + + return true; + } +};