Skip to content

[eunhwa99] Week 03 Solutions #1280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions combination-sum/eunhwa99.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,28 @@ private void backtrack(int[] candidates, int target, int start, List<Integer> cu
}
}
}

// 시간 복잡도: O(2^(target)) - 각 후보 숫자가 target을 만드는 데에 기여할 수도 있고 안 할 수도 있음
// 공간 복잡도: O(k * t) - k는 가능한 조합의 수, t는 각 조합의 크기
class newSolution{
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), candidates, target, 0);
return result;
}

private void backtrack(List<List<Integer>> result, List<Integer> 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(); // 백트래킹 (되돌리기)
}
}
}

32 changes: 32 additions & 0 deletions decode-ways/eunhwa99.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

25 changes: 14 additions & 11 deletions maximum-subarray/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

13 changes: 13 additions & 0 deletions number-of-1-bits/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

68 changes: 48 additions & 20 deletions valid-palindrome/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -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;
}
}