From 459aef8eea65131efc610e15c260e6118da507dc Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sun, 13 Apr 2025 21:33:17 +0900 Subject: [PATCH 1/6] valid palindrome --- valid-palindrome/eunhwa99.java | 68 ++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/valid-palindrome/eunhwa99.java b/valid-palindrome/eunhwa99.java index 8af372df8..071e5c43e 100644 --- a/valid-palindrome/eunhwa99.java +++ b/valid-palindrome/eunhwa99.java @@ -1,23 +1,51 @@ class Solution { - public boolean isPalindrome(String s) { - StringBuilder str = new StringBuilder(); - - for (int i = 0; i < s.length(); i++) { - char c = s.charAt(i); - if (Character.isLetterOrDigit(c)) { - str.append(Character.toLowerCase(c)); - } - } - - int left = 0, right = str.length() - 1; - while (left < right) { - if (str.charAt(left) != str.charAt(right)) { - return false; - } - left++; - right--; - } - - return true; + + public boolean isPalindrome(String s) { + StringBuilder str = new StringBuilder(); + + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (Character.isLetterOrDigit(c)) { + str.append(Character.toLowerCase(c)); + } } + + int left = 0, right = str.length() - 1; + while (left < right) { + if (str.charAt(left) != str.charAt(right)) { + return false; + } + left++; + right--; + } + + return true; + } +} + +// 시간 복잡도 O(n) - 문자열 길이 n +// 공간 복잡도 O(1) +class newSolution { + public boolean isPalindrome(String s) { + int left = 0, right = s.length() - 1; + + while (left < right) { + // 알파벳/숫자가 아닌 경우 skip + while (left < right && !Character.isLetterOrDigit(s.charAt(left))) { + left++; + } + while (left < right && !Character.isLetterOrDigit(s.charAt(right))) { + right--; + } + + // 소문자로 비교 + if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { + return false; + } + + left++; + right--; + } + return true; + } } From a5fc4b70cd995375b709ccb86f314908c1f33183 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sun, 13 Apr 2025 21:39:40 +0900 Subject: [PATCH 2/6] number of 1 bits --- number-of-1-bits/eunhwa99.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 number-of-1-bits/eunhwa99.java diff --git a/number-of-1-bits/eunhwa99.java b/number-of-1-bits/eunhwa99.java new file mode 100644 index 000000000..b4cb798da --- /dev/null +++ b/number-of-1-bits/eunhwa99.java @@ -0,0 +1,13 @@ +// 시간 복잡도 O(log n) - n은 주어진 정수 +// 공간 복잡도 O(1) - 상수 공간 사용 +class Solution{ + public int hammingWeight(int n) { + int count = 0; + while (n != 0) { + count += (n & 1); // 마지막 비트가 1인지 확인 + n >>= 1; // 오른쪽으로 비트 이동 + } + return count; + } +} + From 99460e3e2bfe57f8f9c61491ac3bf64ce472bb06 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sun, 13 Apr 2025 21:56:21 +0900 Subject: [PATCH 3/6] maximum subarray --- maximum-subarray/eunhwa99.java | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/maximum-subarray/eunhwa99.java b/maximum-subarray/eunhwa99.java index 0383bfbdc..a43ea09b6 100644 --- a/maximum-subarray/eunhwa99.java +++ b/maximum-subarray/eunhwa99.java @@ -1,15 +1,18 @@ -// 시간 복잡도: DP -> O(N) -// 공간 복잡도: nums 배열 크기 - O(N) +// 이전 솔루션과 동일 +// 시간 복잡도: O(n) - n은 주어진 배열의 길이 +// 공간 복잡도: O(1) - 상수 공간 사용 class Solution { - public int maxSubArray(int[] nums) { - int currentSum = nums[0]; - int maxSum = currentSum; - for (int i = 1; i < nums.length; ++i) { - currentSum = Math.max(currentSum + nums[i], nums[i]); - maxSum = Math.max(maxSum, currentSum); - } - - return maxSum; + + public int maxSubArray(int[] nums) { + int currentSum = nums[0]; + int maxSum = currentSum; + for (int i = 1; i < nums.length; ++i) { + currentSum = Math.max(currentSum + nums[i], nums[i]); + maxSum = Math.max(maxSum, currentSum); } + + return maxSum; + } } + From 614c2371ab9370f20ef0a466248f1893a40ced98 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sun, 13 Apr 2025 22:00:47 +0900 Subject: [PATCH 4/6] decode ways --- decode-ways/eunhwa99.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/decode-ways/eunhwa99.java b/decode-ways/eunhwa99.java index 3354af071..3f538fe84 100644 --- a/decode-ways/eunhwa99.java +++ b/decode-ways/eunhwa99.java @@ -47,3 +47,35 @@ private boolean checkRange(char left, char right) { return (num >= 10 && num <= 26); } } + +// 시간 복잡도: O(n) - 문자열 길이 n에 비례 +// 공간 복잡도: O(n) - dp 배열 사용 +class newSolution { + public int numDecodings(String s) { + int n = s.length(); + if (n == 0 || s.charAt(0) == '0') return 0; + + int[] dp = new int[n + 1]; + dp[0] = 1; // 빈 문자열은 한 가지 방법 + dp[1] = 1; // 첫 문자가 0이 아니면 한 가지 방법 + + for (int i = 2; i <= n; i++) { + char one = s.charAt(i - 1); // 한 자리 숫자 + char twoPrev = s.charAt(i - 2); // 두 자리 숫자의 앞자리 + + // 1칸 이동 가능 (0은 이동 불가) + if (one != '0') { + dp[i] += dp[i - 1]; + } + + // 2칸 이동 가능한지 확인 (10~26) + int num = (twoPrev - '0') * 10 + (one - '0'); + if (num >= 10 && num <= 26) { + dp[i] += dp[i - 2]; + } + } + + return dp[n]; + } +} + From e8f647ce63ac83eaf15276b8d81c688f768902cd Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sun, 13 Apr 2025 22:07:41 +0900 Subject: [PATCH 5/6] combination sum --- combination-sum/eunhwa99.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/combination-sum/eunhwa99.java b/combination-sum/eunhwa99.java index 5d6561a6b..004459582 100644 --- a/combination-sum/eunhwa99.java +++ b/combination-sum/eunhwa99.java @@ -30,3 +30,25 @@ private void backtrack(int[] candidates, int target, int start, List cu } } } + +class newSolution{ + public List> combinationSum(int[] candidates, int target) { + List> result = new ArrayList<>(); + backtrack(result, new ArrayList<>(), candidates, target, 0); + return result; + } + + private void backtrack(List> result, List tempList, int[] candidates, int remain, int start) { + if (remain < 0) return; // 넘치면 종료 + if (remain == 0) { + result.add(new ArrayList<>(tempList)); // 정답 조합 발견 + return; + } + + for (int i = start; i < candidates.length; i++) { + tempList.add(candidates[i]); // 후보 추가 + backtrack(result, tempList, candidates, remain - candidates[i], i); // **같은 수를 다시 사용할 수 있으므로 i** + tempList.removeLast(); // 백트래킹 (되돌리기) + } + } +} From 14c1f058295cc31671c1cbaf50660c9d551eaf6e Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sun, 13 Apr 2025 22:09:00 +0900 Subject: [PATCH 6/6] =?UTF-8?q?combination=20sum=20=EC=84=A4=EB=AA=85?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- combination-sum/eunhwa99.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/combination-sum/eunhwa99.java b/combination-sum/eunhwa99.java index 004459582..c855a2020 100644 --- a/combination-sum/eunhwa99.java +++ b/combination-sum/eunhwa99.java @@ -31,6 +31,8 @@ private void backtrack(int[] candidates, int target, int start, List cu } } +// 시간 복잡도: O(2^(target)) - 각 후보 숫자가 target을 만드는 데에 기여할 수도 있고 안 할 수도 있음 +// 공간 복잡도: O(k * t) - k는 가능한 조합의 수, t는 각 조합의 크기 class newSolution{ public List> combinationSum(int[] candidates, int target) { List> result = new ArrayList<>(); @@ -52,3 +54,4 @@ private void backtrack(List> result, List tempList, int[] } } } +