From c059cddc34c949b8d254e4093d9874f8b8885691 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Sat, 31 Aug 2024 16:42:54 -0400 Subject: [PATCH 01/11] =?UTF-8?q?=EC=9D=B8=ED=84=B0=EB=84=B7=20=EC=86=94?= =?UTF-8?q?=EB=A3=A8=EC=85=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- product-of-array-except-self/yeonguchoe.java | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 product-of-array-except-self/yeonguchoe.java diff --git a/product-of-array-except-self/yeonguchoe.java b/product-of-array-except-self/yeonguchoe.java new file mode 100644 index 000000000..5ebb3283d --- /dev/null +++ b/product-of-array-except-self/yeonguchoe.java @@ -0,0 +1,24 @@ +class Solution { + // Time complexity: O(n) + // Space complexity: O(n) + public int[] productExceptSelf(int[] nums) { + int[] leftMultiplier = new int[nums.length]; + int[] rightMultiplier = new int[nums.length]; + + leftMultiplier[0] = 1; + for (int i = 0; i < nums.length - 1; i++) { + leftMultiplier[i + 1] = nums[i] * leftMultiplier[i]; + } + + rightMultiplier[nums.length - 1] = 1; + for (int i = nums.length - 1; i > 0; i--) { + rightMultiplier[i - 1] = nums[i] * rightMultiplier[i]; + } + + int[] result = new int[nums.length]; + for (int i = 0; i < result.length; i++) { + result[i] = leftMultiplier[i] * rightMultiplier[i]; + } + return result; + } +} From 4bf41fd05574b36a0b30801dd327da5373a76717 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Sat, 31 Aug 2024 17:57:56 -0400 Subject: [PATCH 02/11] Create yeonguchoe.java --- combination-sum/yeonguchoe.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 combination-sum/yeonguchoe.java diff --git a/combination-sum/yeonguchoe.java b/combination-sum/yeonguchoe.java new file mode 100644 index 000000000..6d3fb6808 --- /dev/null +++ b/combination-sum/yeonguchoe.java @@ -0,0 +1,30 @@ +class Solution { + // Time complexity: O(candidate^(target/smallest candidate)) + // Space complexity: O(target/smallest candidate) + + List> result = new ArrayList<>(); + + public void backtracking(int remainingAmount, List combination, int startPoint, int[] candidateList, + List> resultList) { + if (remainingAmount == 0) { + resultList.add(new ArrayList<>(combination)); + return; + } + + if (remainingAmount < 0) { + return; + } + + for (int i = startPoint; i < candidateList.length; i++) { + combination.add(candidateList[i]); + backtracking(remainingAmount - candidateList[i], combination, i, candidateList, resultList); + combination.remove(combination.size() - 1); + } + } + + public List> combinationSum(int[] candidates, int target) { + ArrayList initialCombination = new ArrayList<>(); + backtracking(target, initialCombination, 0, candidates, result); + return result; + } +} From d35c00d9f721163949dcb4a2086a4dafd3673770 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Sat, 31 Aug 2024 21:31:39 -0400 Subject: [PATCH 03/11] Create yeonguchoe.java --- coin-change/yeonguchoe.java | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 coin-change/yeonguchoe.java diff --git a/coin-change/yeonguchoe.java b/coin-change/yeonguchoe.java new file mode 100644 index 000000000..8cc3a835f --- /dev/null +++ b/coin-change/yeonguchoe.java @@ -0,0 +1,37 @@ +class Solution { + private Integer[] memo; + + public int helper(int[] coins, int remain) { + if (remain < 0) { // 0 이하로 가는 경우 -1로 지정 + return -1; + } + if (remain == 0) { // 처음 값은 0으로 설정 + return 0; + } + + if (memo[remain] != null) { + return memo[remain]; + } + + int minNumCoin = Integer.MAX_VALUE; // 세개 중에 최소값을 고르는 로직 + + for (int coin : coins) { + int numCoin = helper(coins, remain - coin); + if (numCoin != -1) { // 도달 가능한 경우 + minNumCoin = Math.min(minNumCoin, numCoin + 1); + } + } + if (minNumCoin < Integer.MAX_VALUE) { // 도달 가능한 경우 + memo[remain] = minNumCoin; + } else { + memo[remain] = -1; + } + + return memo[remain]; + } + + public int coinChange(int[] coins, int amount) { + memo = new Integer[amount + 1]; + return helper(coins, amount); + } +} From c41e3be29b906c0d061c0139ab54e71dce37379d Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Fri, 6 Sep 2024 20:28:56 -0400 Subject: [PATCH 04/11] =?UTF-8?q?=EB=B0=B1=ED=8A=B8=EB=9E=98=ED=82=B9=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- word-search/yeonguchoe.cpp | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 word-search/yeonguchoe.cpp diff --git a/word-search/yeonguchoe.cpp b/word-search/yeonguchoe.cpp new file mode 100644 index 000000000..8cdf2694a --- /dev/null +++ b/word-search/yeonguchoe.cpp @@ -0,0 +1,59 @@ +class Solution { +public: + bool backtrack(vector>& board, const string& word, + int current_row, int current_column, int index) { + // 이전 iteration까지 backtrack을 통과한 경우 + // index가 단어의 길이와 같아지는 경우 + if (index == word.size()) { + return true; + } + + // 탐색중인 cell이 board를 넘어선 경우 + if (current_row < 0 || current_row >= board.size() || + current_column < 0 || current_column >= board[0].size()) { + return false; + } + + // 탐색중인 cell에 있는 문자가 단어의 문자와 다른 경우 + if (board[current_row][current_column] != word[index]) { + return false; + } + + // 탐색중인 cell의 문자를 글자가 아닌것으로 지정하여, 방문 표시 + char temp = board[current_row][current_column]; + board[current_row][current_column] = '#'; + + // 탐색중인 cell의 주변을 backtrack으로 탐색 + bool found = + backtrack(board, word, current_row - 1, current_column, + index + 1) or + backtrack(board, word, current_row, current_column - 1, + index + 1) or + backtrack(board, word, current_row + 1, current_column, + index + 1) or + backtrack(board, word, current_row, current_column + 1, index + 1); + + // 탐색중인 cell의 값을 원래대로 돌려 놓음 + board[current_row][current_column] = temp; + + // 만약에 찾아진 경우, true를 위로 올림 + // 만약에 탐색 도중에 2가지 false를 반환하는 케이스에 걸렸으면 false를 + // 위로 올림 + return found; + } + + bool exist(vector>& board, string word) { + + // 시작점을 설정 + for (int row = 0; row < board.size(); ++row) { + for (int col = 0; col < board[0].size(); ++col) { + // 만약에 backtrack 함수가 true인 경우를 찾았으면, true 반환 + if (backtrack(board, word, row, col, 0)) { + return true; + } + } + } + // true인 경우를 한번도 찾지 못했다면, false 반환 + return false; + } +}; From 2f687ad24631182af350ec9233ab42bd3654dfde Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Fri, 6 Sep 2024 21:48:46 -0400 Subject: [PATCH 05/11] Create yeonguchoe.cpp --- maximum-product-subarray/yeonguchoe.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 maximum-product-subarray/yeonguchoe.cpp diff --git a/maximum-product-subarray/yeonguchoe.cpp b/maximum-product-subarray/yeonguchoe.cpp new file mode 100644 index 000000000..abde97847 --- /dev/null +++ b/maximum-product-subarray/yeonguchoe.cpp @@ -0,0 +1,20 @@ +class Solution { + // Time complexity: O(n) + // Space complexity: O(1) +public: + int maxProduct(vector& nums) { + + int result = nums[0]; + int previous_max = 1; + int previous_min = 1; + + for (int num : nums) { + int temp = previous_max; + previous_max = max({num, previous_max * num, previous_min * num}); + previous_min = min({num, temp * num, previous_min * num}); + result = max(result, previous_max); + } + + return result; + } +}; From e1c4fe0fedca39005bae41434093974cc7049c36 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Thu, 19 Sep 2024 17:08:00 -0400 Subject: [PATCH 06/11] Create yeonguchoe.cs --- valid-parentheses/yeonguchoe.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 valid-parentheses/yeonguchoe.cs diff --git a/valid-parentheses/yeonguchoe.cs b/valid-parentheses/yeonguchoe.cs new file mode 100644 index 000000000..99b321826 --- /dev/null +++ b/valid-parentheses/yeonguchoe.cs @@ -0,0 +1,24 @@ +public class Solution { + public bool IsValid(string s) { + Stack parentheses = new Stack(); + + Dictionary pair = new Dictionary { + { ')', '(' }, + { '}', '{' }, + { ']', '[' } + }; + + foreach (char c in s) { + if (c == '(' || c == '{' || c == '[') { + parentheses.Push(c); + } + else if (c == ')' || c == '}' || c == ']') { + if (parentheses.Count == 0 || parentheses.Peek() != pair[c]) { + return false; + } + parentheses.Pop(); + } + } + + return parentheses.Count == 0; + } From cf44d0ffd43b9a58e6e1553bd6808f85990209d1 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Thu, 19 Sep 2024 19:06:46 -0400 Subject: [PATCH 07/11] =?UTF-8?q?=ED=95=B4=EC=84=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spiral-matrix/yeonguchoe.cs | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 spiral-matrix/yeonguchoe.cs diff --git a/spiral-matrix/yeonguchoe.cs b/spiral-matrix/yeonguchoe.cs new file mode 100644 index 000000000..2fb154331 --- /dev/null +++ b/spiral-matrix/yeonguchoe.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; + +public class Solution { + public IList SpiralOrder(int[][] matrix) { + List result = new List(); + int row = matrix.Length; + if (row == 0) return result; + int column = matrix[0].Length; + + int upRail = 0; + int downRail = row - 1; + int leftRail = 0; + int rightRail = column - 1; + + while (result.Count < row * column) { + // L->R + for (int c = leftRail; c <= rightRail; c++) { + result.Add(matrix[upRail][c]); + } + // T->B + for (int r = upRail + 1; r <= downRail; r++) { + result.Add(matrix[r][rightRail]); + } + + // R->L + if (upRail != downRail) { + for (int c = rightRail - 1; c >= leftRail; c--) { + result.Add(matrix[downRail][c]); + } + } + + // B->T + if (leftRail != rightRail) { + for (int r = downRail - 1; r > upRail; r--) { + result.Add(matrix[r][leftRail]); + } + } + + leftRail += 1; + rightRail -= 1; + upRail += 1; + downRail -= 1; + } + + return result; + } +} From afecf62f1def06636512ff738714bdf496deb31f Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Thu, 19 Sep 2024 19:59:16 -0400 Subject: [PATCH 08/11] Create yeonguchoe.cs --- longest-increasing-subsequence/yeonguchoe.cs | 25 ++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 longest-increasing-subsequence/yeonguchoe.cs diff --git a/longest-increasing-subsequence/yeonguchoe.cs b/longest-increasing-subsequence/yeonguchoe.cs new file mode 100644 index 000000000..b702c1d7a --- /dev/null +++ b/longest-increasing-subsequence/yeonguchoe.cs @@ -0,0 +1,25 @@ +public class Solution { + public int LengthOfLIS(int[] nums) { + if (nums.Length == 0) return 0; + + int[] dp = new int[nums.Length]; + for (int i = 0; i < nums.Length; i++) { + dp[i] = 1; + } + + for (int i = 1; i < nums.Length; i++) { + for (int j = 0; j < i; j++) { + if (nums[j] < nums[i]) { + dp[i] = Math.Max(dp[i], dp[j] + 1); + } + } + } + + int maxLength = 1; + foreach (int length in dp) { + maxLength = Math.Max(maxLength, length); + } + + return maxLength; + } +} From 41581898b3bb22276acc58e317f172575b300acb Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Thu, 19 Sep 2024 20:11:10 -0400 Subject: [PATCH 09/11] Create yeonguchoe.cs --- container-with-most-water/yeonguchoe.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 container-with-most-water/yeonguchoe.cs diff --git a/container-with-most-water/yeonguchoe.cs b/container-with-most-water/yeonguchoe.cs new file mode 100644 index 000000000..80abe578d --- /dev/null +++ b/container-with-most-water/yeonguchoe.cs @@ -0,0 +1,22 @@ +using System; + +public class Solution { + public int MaxArea(int[] height) { + int currentMax = int.MinValue; + int leftPtr = 0; + int rightPtr = height.Length - 1; + + while (leftPtr < rightPtr) { + int currentAmount = (rightPtr - leftPtr) * Math.Min(height[leftPtr], height[rightPtr]); + currentMax = Math.Max(currentMax, currentAmount); + + if (height[leftPtr] < height[rightPtr]) { + leftPtr++; + } else { + rightPtr--; + } + } + + return currentMax; + } +} From 4517980ca89574b27e7a97147cfe2c8b803a9287 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Thu, 19 Sep 2024 22:16:25 -0400 Subject: [PATCH 10/11] =?UTF-8?q?DFS=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yeonguchoe.cs | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 design-add-and-search-words-data-structure/yeonguchoe.cs diff --git a/design-add-and-search-words-data-structure/yeonguchoe.cs b/design-add-and-search-words-data-structure/yeonguchoe.cs new file mode 100644 index 000000000..8ae88216f --- /dev/null +++ b/design-add-and-search-words-data-structure/yeonguchoe.cs @@ -0,0 +1,67 @@ +public class LetterNode +{ + public LetterNode[] Alphabet { get; } = new LetterNode[26]; // Initialize to null + public bool IsEnd { get; set; } +} + +public class WordDictionary +{ + private LetterNode root; + + public WordDictionary() + { + root = new LetterNode(); + } + + public void AddWord(string word) + { + LetterNode currentNode = root; + foreach (char c in word) + { + int index = c - 'a'; + if (currentNode.Alphabet[index] == null) + { + currentNode.Alphabet[index] = new LetterNode(); + } + currentNode = currentNode.Alphabet[index]; + } + currentNode.IsEnd = true; + } + + public bool Search(string word) + { + return Dfs(word, root); + } + + private bool Dfs(string target, LetterNode currentRoot) + { + if (target.Length == 0) + { + return currentRoot.IsEnd; + } + + char firstLetter = target[0]; + string remainingTarget = target.Substring(1); + + if (firstLetter == '.') + { + for (int i = 0; i < 26; i++) + { + if (currentRoot.Alphabet[i] != null && Dfs(remainingTarget, currentRoot.Alphabet[i])) + { + return true; + } + } + } + else + { + int index = firstLetter - 'a'; + if (currentRoot.Alphabet[index] != null) + { + return Dfs(remainingTarget, currentRoot.Alphabet[index]); + } + } + + return false; + } +} From e051f73c074b3d079c2a0dc977b3ddab9d792852 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Fri, 20 Sep 2024 20:54:49 -0400 Subject: [PATCH 11/11] Update longest-increasing-subsequence/yeonguchoe.cs Co-authored-by: Dongyeong Chon --- longest-increasing-subsequence/yeonguchoe.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/longest-increasing-subsequence/yeonguchoe.cs b/longest-increasing-subsequence/yeonguchoe.cs index b702c1d7a..f75f1160c 100644 --- a/longest-increasing-subsequence/yeonguchoe.cs +++ b/longest-increasing-subsequence/yeonguchoe.cs @@ -7,17 +7,14 @@ public int LengthOfLIS(int[] nums) { dp[i] = 1; } + int maxLength = 1; for (int i = 1; i < nums.Length; i++) { for (int j = 0; j < i; j++) { if (nums[j] < nums[i]) { dp[i] = Math.Max(dp[i], dp[j] + 1); } } - } - - int maxLength = 1; - foreach (int length in dp) { - maxLength = Math.Max(maxLength, length); + maxLength = Math.max(maxLength, dp[i]); } return maxLength;