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); + } +} 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; + } +} 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; + } +} 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; + } +} diff --git a/longest-increasing-subsequence/yeonguchoe.cs b/longest-increasing-subsequence/yeonguchoe.cs new file mode 100644 index 000000000..f75f1160c --- /dev/null +++ b/longest-increasing-subsequence/yeonguchoe.cs @@ -0,0 +1,22 @@ +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; + } + + 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); + } + } + maxLength = Math.max(maxLength, dp[i]); + } + + return maxLength; + } +} 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; + } +}; 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; + } +} 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; + } +} 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; + } 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; + } +};