Skip to content

[Tessa1217] Week 03 Solutions #1307

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 5 commits into from
Apr 17, 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
36 changes: 36 additions & 0 deletions combination-sum/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.List;
import java.util.ArrayList;
/**
중복 되지 않은 요소들이 들어있는 candidates 배열이 주어지고 target 값이 주어진다.
합이 target과 같은 중복되지 않은 조합을 모두 반환하시오.
*/
class Solution {

List<List<Integer>> answer = new ArrayList<>();

public List<List<Integer>> combinationSum(int[] candidates, int target) {
combination(0, candidates, target, 0, new ArrayList<>());
return answer;
}

// 시간복잡도 O(2^target)
public void combination(int idx, int[] candidates, int target, int currentSum, List<Integer> comb) {
// 누적 합이 넘으면
if (currentSum > target) {
return;
}

// 누적 합이 타겟 값과 같으면
if (currentSum == target) {
answer.add(new ArrayList<>(comb));
return;
}

for (int i = idx; i < candidates.length; i++) {
comb.add(candidates[i]);
combination(i, candidates, target, currentSum + candidates[i], comb);
comb.remove(comb.size() - 1);
}
}
}

43 changes: 43 additions & 0 deletions decode-ways/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
주어진 문자열을 복호화 할 수 있는 경우의 수를 반환하시오.
문자열은 A-Z까지 숫자 1-26으로 치환
예시: "AAJF" => (1, 1, 10, 6), (11, 10, 6)...
*/
class Solution {

// 시간복잡도: O(n), 공간복잡도: O(n)
public int numDecodings(String s) {

int[] dp = new int[s.length() + 1];

// contain leading zero(s)
if (s.charAt(0) == '0') {
return 0;
}

dp[0] = 1;
dp[1] = 1;

for (int i = 2; i <= s.length(); i++) {

// 1자리수 검사
int one = Integer.parseInt(Character.toString(s.charAt(i - 1)));

if (one != 0) {
dp[i] += dp[i - 1];
}

// 2자리수 검사
int two = Integer.parseInt(Character.toString(s.charAt(i - 2))) * 10 + one;

if (two >= 10 && two <= 26) {
dp[i] += dp[i - 2];
}

}

return dp[s.length()];
}

}

21 changes: 21 additions & 0 deletions maximum-subarray/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
정수 배열이 주어질 때 부분 수열의 가장 큰 합을 구하시오.
*/
class Solution {

// 시간복잡도: O(n), 공간복잡도: O(1)
public int maxSubArray(int[] nums) {

int sum = nums[0];

for (int i = 1; i < nums.length; i++) {
// 수 이어서 더할지 아니면 현재 값으로 초기화할지 여부 판단
nums[i] = Math.max(nums[i], nums[i] + nums[i - 1]);
sum = Math.max(nums[i], sum);
}

return sum;
}

}

32 changes: 32 additions & 0 deletions number-of-1-bits/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/** 주어진 숫자의 Hamming weight 구하기 */
class Solution {

// 시간복잡도: O(1), 공간복잡도: O(1), 비트 연산자 사용
public int hammingWeight(int n) {
int count = 0;
while (n != 0) {
count += (n & 1);
n >>>= 1;
}
return count;
}

// 시간복잡도: O(1), 공간복잡도: O(1)
// public int hammingWeight(int n) {

// int count = 0;
// while (n != 0) {
// if (n % 2 == 1) {
// count++;
// }
// n /= 2;
// }
// return count;
// }

// 시간복잡도: O(1), 공간복잡도: O(n)
// public int hammingWeight(int n) {
// return Integer.toBinaryString(n).replaceAll("0", "").length();
// }
}

54 changes: 54 additions & 0 deletions valid-palindrome/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/** 대문자 문자들을 소문자로 변환하고, 알파벳이 아닌 문자들을 제거했을 때 앞뒤가 똑같이 읽히는 구문을 palindrome이라고 한다.
주어진 구문이 palindrome인지 여부를 확인하여 boolean 값을 반환하세요.
*/
class Solution {

// 투 포인터 활용 시간 복잡도: O(n), 공간복잡도: O(n)
public boolean isPalindrome(String s) {


int left = 0;
int right = s.length() - 1;

while (left < right) {
// 문자 또는 숫자 아닐 시
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;
}

// char 배열 활용 풀이: 시간 복잡도: O(n), 공간복잡도: O(n)
// public boolean isPalindrome(String s) {
// // non-alphanumeric (숫자 포함 - test case "0P")
// char[] convertArr = s.toLowerCase().replaceAll("[^a-z0-9]", "").toCharArray();
// int maxIdx = convertArr.length - 1;
// for (int i = 0; i <= maxIdx; i++) {
// if (convertArr[i] != convertArr[maxIdx - i]) {
// return false;
// }
// }
// return true;
// }

// StringBuffer reverse() 활용
// public boolean isPalindrome(String s) {
// // non-alphanumeric (숫자 포함 - test case "0P")
// String convertedString = s.toLowerCase().replaceAll("[^a-z0-9]", "");
// StringBuffer sb = new StringBuffer(convertedString);
// return convertedString.equals(sb.reverse().toString());
// }
}