diff --git a/combination-sum/YoungSeok-Choi.java b/combination-sum/YoungSeok-Choi.java new file mode 100644 index 000000000..49948adc7 --- /dev/null +++ b/combination-sum/YoungSeok-Choi.java @@ -0,0 +1,119 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +// 시간복잡도: O(n ^ target) +class Solution { + + public List tArr = new ArrayList<>(); + public Set> s = new HashSet<>(); + + public List> combinationSum(int[] candidates, int target) { + if(target == 1) { + return new ArrayList<>(); + } + + Arrays.sort(candidates); + dfs(candidates, target, 0); + + return new ArrayList<>(s); + } + + public void dfs(int[] candi, int target, int now) { + + if(target < now) { + return; + } + + // 정답 배열에 추가. + if(target == now) { + List temp = new ArrayList<>(tArr); + Collections.sort(temp); + s.add(temp); + return; + } + + for(int i = 0; i < candi.length; i++) { + tArr.add(candi[i]); + dfs(candi, target, (now + candi[i])); + tArr.remove(tArr.size() - 1); // 가장 마지막 배열의 원소를 제거. + } + } +} + + +// NOTE: 세 수 이상의 조합의 합을 구하지 못하는 알고리즘. +class WrongSolution { + public List> combinationSum(int[] candidates, int target) { + List> res = new ArrayList<>(); + List> result = new ArrayList<>(); + + if(target == 1) { + return new ArrayList<>(); + } + + Arrays.sort(candidates); + + // i번째 값의 메모 구하기 + for(int i = 0; i < candidates.length; i++) { + + // 0 ~ i - 1까지만 의 값으로 i번째 값끼리 답이 나는지 확인. + for(int j = 0; j < i; j++) { + int current = candidates[i]; + int before = candidates[j]; + + int start = 1; + while(true) { + + int startValue = current * start; + if(startValue >= target) { + break; + } + + List ans = new ArrayList<>(); + + int diff = target - startValue; + + if(diff % before == 0) { + + for(int k = 0; k < start; k++) { + ans.add(current); + } + + int temp = diff / before; + for(int k = temp; k > 0; k--) { + ans.add(before); + } + + res.add(ans); + } + + start++; + } + } + + // i로만 답을 구하는 경우. + int copy = target; + List ans = new ArrayList<>(); + if(target % candidates[i] == 0) { + int mok = copy /= candidates[i]; + while(mok > 0) { + ans.add(candidates[i]); + mok--; + } + res.add(ans); + } + } + + + for (int i = res.size() - 1; i >= 0; i--) { + List inner = new ArrayList<>(res.get(i)); + result.add(inner); + } + + return result; + } +} diff --git a/decode-ways/YoungSeok-Choi.java b/decode-ways/YoungSeok-Choi.java new file mode 100644 index 000000000..811ca1598 --- /dev/null +++ b/decode-ways/YoungSeok-Choi.java @@ -0,0 +1,80 @@ + +// NOTE: O(n) +class Solution { + public int numDecodings(String s) { + + char[] cArr = s.toCharArray(); + int[] decode = new int[cArr.length]; + + if((cArr[0] - 48) == 0) { + return 0; + } + + decode[0] = 1; + + for(int i = 1; i < cArr.length; i++) { + int cur = (int) cArr[i] - 48; + int prev = (int) cArr[i - 1] - 48; + + if(cur == prev && cur == 0) return 0; + + String temp = "" + cArr[i - 1] + cArr[i]; + int tempI = Integer.parseInt(temp); + + // NOTE: 아래 if 문 두개가 core logic + if(cur != 0) { + decode[i] += decode[i - 1]; + } + + if(tempI >= 10 && tempI <= 26) { + decode[i] += (i >= 2 ? decode[i - 2] : 1); + } + } + + return decode[decode.length - 1]; + } +} + + +class WrongSolution { + public int numDecodings(String s) { + + char[] cArr = s.toCharArray(); + int[] decode = new int[cArr.length]; + + if((cArr[0] - 48) == 0) { + return 0; + } + + decode[0] = 1; + + // 유효한 경우만 판단해서 처리해주면 됐었던 문제.. + // 어떻게 처리해야할지 모르겠는 분기는 처리하지 않하느니만 못하다...ㅜ + for(int i = 1; i < cArr.length; i++) { + int cur = (int) cArr[i] - 48; + int prev = (int) cArr[i - 1] - 48; + + if(cur == prev && cur == 0) return 0; + + String temp = "" + cArr[i - 1] + cArr[i]; + int tempI = Integer.parseInt(temp); + + if(cur >= 1 && cur <= 9) { + decode[i] = decode[i - 1]; + + if(prev >= 1 && prev <= 9) { + if(tempI >= 1 && tempI <= 26) { + decode[i] = i == 1 ? decode[i - 1] + 1 : decode[i - 1] + decode[i - 2]; + } + } else { // 이전값이 0이라 정상적인 디코딩이 안되는 경우.. + decode[i]--; + } + + } else { // 뒷 숫자와 합쳐져서 유효하게 될 수도 있는경우 + decode[i] = (tempI >= 1 && tempI <= 26) ? decode[i - 1] : 0; + } + } + + return decode[decode.length - 1]; + } +} diff --git a/maximum-subarray/YoungSeok-Choi.java b/maximum-subarray/YoungSeok-Choi.java new file mode 100644 index 000000000..e76988a71 --- /dev/null +++ b/maximum-subarray/YoungSeok-Choi.java @@ -0,0 +1,42 @@ +// NOTE: 카데인 알고리즘 +// TODO: O(n^2) 복잡도의 브루트포스 방식으로도 풀어보기. +class Solution { + public int maxSubArray(int[] nums) { + + int maxSum = nums[0]; + int curSum = nums[0]; + + for (int i = 1; i < nums.length; i++) { + curSum = Math.max(nums[i], curSum + nums[i]); + maxSum = Math.max(maxSum, curSum); + } + + return maxSum; + } +} + +// NOTE: 시작점 변경의 조건(?)을 제대로 정의하지 못해 틀린문제.. +// 답지 보고 해결.... +class WrongSolution { + public int maxSubArray(int[] nums) { + int gMax = -123456789; + int curMax = -123456789; + int curSum = 0; + + for(int i = 0; i < nums.length; i++) { + if(curMax < nums[i]) { + // 시작점 변경. + curSum = nums[i]; + curMax = nums[i]; + } else { + curSum += nums[i]; + curMax = Math.max(curMax, curSum); + } + + gMax = Math.max(gMax, curMax); + } + + return gMax; + } + +} diff --git a/number-of-1-bits/YoungSeok-Choi.java b/number-of-1-bits/YoungSeok-Choi.java new file mode 100644 index 000000000..a0d92892b --- /dev/null +++ b/number-of-1-bits/YoungSeok-Choi.java @@ -0,0 +1,16 @@ +// NOTE: 10진수를 2 진수로 변환 +// O(logN) +class Solution { + public int hammingWeight(int n) { + int oneCnt = 0; + + while(n >= 1) { + if((n % 2) == 1) { + oneCnt++; + } + n = n / 2; + } + + return oneCnt; + } +} diff --git a/valid-palindrome/YoungSeok-Choi.java b/valid-palindrome/YoungSeok-Choi.java new file mode 100644 index 000000000..1c7a8bf0c --- /dev/null +++ b/valid-palindrome/YoungSeok-Choi.java @@ -0,0 +1,31 @@ + +// NOTE: 팰린드롬은 알파벳 소문자와 "숫자" 까지 포함하는듯 하다. +// O(n) 알고리즘. +import java.util.stream.Collectors; + +class Solution { + public boolean isPalindrome(String s) { + + String filtered = s.chars() + .filter(Character::isLetterOrDigit) + .mapToObj(c -> String.valueOf((char) c)) + .map(String::toLowerCase) + .collect(Collectors.joining()); + + char[] cArr = filtered.toCharArray(); + + int startIdx = 0; + int endIdx = cArr.length - 1; + + while(startIdx < endIdx) { + if(cArr[startIdx] != cArr[endIdx]) { + return false; + } + + startIdx++; + endIdx--; + } + + return true; + } +}