From ea58a3b917ab7456e5a04cd8849377b7e0d1ad06 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Sat, 12 Apr 2025 12:42:54 +0900 Subject: [PATCH 1/6] add: valid-palindrome --- valid-palindrome/YoungSeok-Choi.java | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 valid-palindrome/YoungSeok-Choi.java 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; + } +} From b06a76b46bb3914466d5f7a46561fcfbc2f1e61b Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Sat, 12 Apr 2025 14:17:29 +0900 Subject: [PATCH 2/6] add: num of 1 bit --- number-of-1-bits/YoungSeok-Choi.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 number-of-1-bits/YoungSeok-Choi.java diff --git a/number-of-1-bits/YoungSeok-Choi.java b/number-of-1-bits/YoungSeok-Choi.java new file mode 100644 index 000000000..247998a65 --- /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; + } +} \ No newline at end of file From 4abe4805c998e145dfbd8b6365fcdb54f257462b Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Sun, 13 Apr 2025 11:57:52 +0900 Subject: [PATCH 3/6] fix:lint --- number-of-1-bits/YoungSeok-Choi.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/number-of-1-bits/YoungSeok-Choi.java b/number-of-1-bits/YoungSeok-Choi.java index 247998a65..a0d92892b 100644 --- a/number-of-1-bits/YoungSeok-Choi.java +++ b/number-of-1-bits/YoungSeok-Choi.java @@ -13,4 +13,4 @@ public int hammingWeight(int n) { return oneCnt; } -} \ No newline at end of file +} From 0a28c5f387f6dc68069a245f5148289f7889c806 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Mon, 14 Apr 2025 10:37:40 +0900 Subject: [PATCH 4/6] add: maximum sub list --- maximum-subarray/YoungSeok-Choi.java | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 maximum-subarray/YoungSeok-Choi.java diff --git a/maximum-subarray/YoungSeok-Choi.java b/maximum-subarray/YoungSeok-Choi.java new file mode 100644 index 000000000..18cd9a387 --- /dev/null +++ b/maximum-subarray/YoungSeok-Choi.java @@ -0,0 +1,43 @@ + +// 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; + } + +} From 68d4ab4566763375990310a15cf2f42f461e606d Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Tue, 15 Apr 2025 21:07:55 +0900 Subject: [PATCH 5/6] =?UTF-8?q?add.=20decode=20way,=20=EC=B9=B4=EB=8D=B0?= =?UTF-8?q?=EC=9D=B8=20=EC=95=8C=EA=B3=A0=EB=A6=AC=EC=A6=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- decode-ways/YoungSeok-Choi.java | 80 ++++++++++++++++++++++++++++ maximum-subarray/YoungSeok-Choi.java | 3 +- 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 decode-ways/YoungSeok-Choi.java 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 index 18cd9a387..e76988a71 100644 --- a/maximum-subarray/YoungSeok-Choi.java +++ b/maximum-subarray/YoungSeok-Choi.java @@ -1,5 +1,4 @@ - -// NOTE: 카데인 알고리즘. +// NOTE: 카데인 알고리즘 // TODO: O(n^2) 복잡도의 브루트포스 방식으로도 풀어보기. class Solution { public int maxSubArray(int[] nums) { From b5b77c23300f0cb3b628f6c9ec0a65e1259843b6 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Sun, 20 Apr 2025 08:49:30 +0900 Subject: [PATCH 6/6] add: combination sum --- combination-sum/YoungSeok-Choi.java | 119 ++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 combination-sum/YoungSeok-Choi.java 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; + } +}