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/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/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; + } +};