Skip to content

Commit 9932b54

Browse files
authored
Merge pull request #1293 from Yn3-3xh/main
[Yn3-3xh] WEEK 03 solutions
2 parents a81d487 + a650368 commit 9932b54

File tree

5 files changed

+230
-0
lines changed

5 files changed

+230
-0
lines changed

combination-sum/Yn3-3xh.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
[문제풀이]
3+
- target 숫자를 주어진 배열로 나눠보면서 0이 되는 것들을 고르면 되지 않을까?
4+
- dfs로 풀어보자.
5+
time: O(2^N), space: O(N);
6+
7+
[회고]
8+
!!! dfs에서 기존 리스트가 이후에 변경될 수 있으므로 새 객체를 생성해서 넣어줘야 한다. !!!
9+
10+
DP로도 풀어보려고 했지만, 솔루션을 봐도 내가 풀 수 없을 것 같았다..
11+
*/
12+
class Solution {
13+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
14+
List<List<Integer>> results = new ArrayList<>();
15+
dfs(candidates, target, 0, results, new ArrayList<>(), 0);
16+
return results;
17+
}
18+
19+
private void dfs(int[] candidates, int target, int index, List<List<Integer>> results, List<Integer> result, int sum) {
20+
if (sum == target) {
21+
results.add(new ArrayList<>(result));
22+
return;
23+
}
24+
25+
if (sum > target || index >= candidates.length) {
26+
return;
27+
}
28+
29+
int num = candidates[index];
30+
result.add(num);
31+
dfs(candidates, target, index, results, result, sum + num);
32+
33+
result.remove(result.size() - 1);
34+
dfs(candidates, target, index + 1, results, result, sum);
35+
}
36+
}
37+

decode-ways/Yn3-3xh.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
[문제풀이]
3+
- 만들 수 있는 가지수를 모두 구하기
4+
- A ~ Z -> 1 ~ 26
5+
- DP로 풀어보자.
6+
time: O(N), space: O(N)
7+
1212를 예시로,
8+
첫번째 수를 가져올 때 dp[1] = 1
9+
만들어지는 수: 1
10+
11+
한자리 수를 가져올 때 dp[2] = dp[1] = 1
12+
만들어지는 수: 1, 2
13+
두자리 수를 가져올 때 dp[2] = d[2] + dp[0] = 1 + 1 = 2
14+
만들어지는 수: 12
15+
>> 1, 2 | 12
16+
17+
한자리 수를 가져올 때 dp[3] = dp[2] = 2
18+
만들어지는 수: 1, 2, 1 | 12, 1
19+
두자리 수를 가져올 때 dp[3] = dp[3] + dp[1] = 2 + 1 = 3
20+
만들어지는 수: 1, 21
21+
>> 1, 2, 1 | 12, 1 | 1, 21
22+
23+
한자리 수를 가져올 때 dp[4] = dp[3] = 3
24+
만들어지는 수: 1, 2, 1, 2 | 12, 1, 2 | 1, 21, 2
25+
두자리 수를 가져올 때 dp[4] = dp[4] + dp[2] = 3 + 2 = 5
26+
만들어지는 수: 1, 2, 12 | 12, 2
27+
>> 1, 2, 1, 2 | 12, 1, 2 | 1, 21, 2 | 1, 2, 12 | 12, 2
28+
29+
[회고]
30+
dfs로 풀려다가 잘 안풀어져서 DP로 노선을 틀었는데, 아직 DP 구현이 모자른 것 같다..
31+
32+
*/
33+
class Solution {
34+
public int numDecodings(String s) {
35+
if (s.charAt(0) == '0') {
36+
return 0;
37+
}
38+
39+
int len = s.length();
40+
int[] dp = new int[len + 1];
41+
dp[0] = 1;
42+
dp[1] = 1;
43+
44+
for (int i = 2; i <= len; i++) {
45+
int one = Character.getNumericValue(s.charAt(i - 1));
46+
int two = Integer.parseInt(s.substring(i - 2, i));
47+
48+
if (1 <= one && one <= 9) {
49+
dp[i] = dp[i - 1];
50+
}
51+
if (10 <= two && two <= 26) {
52+
dp[i] += dp[i - 2];
53+
}
54+
}
55+
return dp[len];
56+
}
57+
}
58+

maximum-subarray/Yn3-3xh.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
[문제풀이]
3+
- 주어진 배열에서 연속된 수 배열의 합이 큰 수 구하기
4+
- (1) 현재수와 현재까지의 합 중 큰 수 구하기 : 현재 인덱스부터 시작되거나, 현재 인덱스까지 더해질 것
5+
- (2) 최댓값과 (1)번 수 중 큰 수 구하기
6+
time: O(N), space: O(1)
7+
8+
[회고]
9+
솔루션까지는 근접하는데, 결국 해결은 자꾸 안된다..
10+
어떻게 접근해야 솔루션까지 도달 할 수 있을까..
11+
*/
12+
class Solution {
13+
public int maxSubArray(int[] nums) {
14+
int max = nums[0];
15+
int sum = nums[0];
16+
for (int i = 1; i < nums.length; i++) {
17+
sum = Math.max(nums[i], sum + nums[i]);
18+
max = Math.max(max, sum);
19+
}
20+
return max;
21+
}
22+
}
23+

number-of-1-bits/Yn3-3xh.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
[문제풀이]
3+
- 주어진 양의 정수를 2진수로 변환하여, 1의 개수 구하기
4+
5+
- 풀이 1
6+
time: O(log N), space: O(1)
7+
class Solution {
8+
public int hammingWeight(int n) {
9+
int count = 0;
10+
while (n > 0) {
11+
if (n % 2 != 0) {
12+
count++;
13+
}
14+
n /= 2;
15+
}
16+
return count;
17+
}
18+
}
19+
- 풀이 2
20+
time: O(log N), space: O(1)
21+
22+
[회고]
23+
`n >> 1`비트 연산자는 `n / 2` 와 같다.
24+
`n >> 1`로 풀면 비교도 비트로!
25+
26+
이진 표현에서 1의 개수를 세어주는 Integer.bitCount() 메서드도 있었다.
27+
*/
28+
class Solution {
29+
public int hammingWeight(int n) {
30+
int count = 0;
31+
while (n > 0) {
32+
if ((n & 1) == 1) {
33+
count++;
34+
}
35+
n >>= 1;
36+
}
37+
return count;
38+
}
39+
}

valid-palindrome/Yn3-3xh.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
[문제풀이]
3+
- 대문자 -> 소문자 변환
4+
- 소문자가 아닌 문자면 넘어가고, 소문자만 체크
5+
- 숫자는 넘어갈 수 없다.
6+
- 소문자 input, reverse input 비교
7+
8+
- 풀이1
9+
time: O(N), space: O(1)
10+
class Solution {
11+
public boolean isPalindrome(String s) {
12+
if (s.trim().length() == 0) {
13+
return true;
14+
}
15+
16+
s = s.toLowerCase();
17+
int left = 0;
18+
int right = s.length() - 1;
19+
while (left < right) {
20+
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
21+
left++;
22+
}
23+
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
24+
right--;
25+
}
26+
27+
if (s.charAt(left) != s.charAt(right)) {
28+
return false;
29+
}
30+
left++;
31+
right--;
32+
}
33+
return true;
34+
}
35+
}
36+
37+
- 풀이2
38+
time: O(N), space: O(1)
39+
40+
[회고]
41+
중간에 공백을 replace하면 안좋을 것 같은데..
42+
-> 알파벳인지를 비교해서 인덱스를 바꾸자!
43+
44+
for문만 고집하지 말고, while문도 사용해보자!
45+
(처음에 for문으로 풀다가 스파게티가 되어버렸ㄷ..)
46+
47+
!!! String.toLowerCase() 로 소문자로 변환하는 것이 좋은 방법일까?? !!!
48+
*/
49+
50+
class Solution {
51+
public boolean isPalindrome(String s) {
52+
s = s.toLowerCase();
53+
int left = 0;
54+
int right = s.length() - 1;
55+
while (left < right) {
56+
if (!Character.isLetterOrDigit(s.charAt(left))) {
57+
left++;
58+
continue;
59+
}
60+
if (!Character.isLetterOrDigit(s.charAt(right))) {
61+
right--;
62+
continue;
63+
}
64+
65+
if (s.charAt(left) != s.charAt(right)) {
66+
return false;
67+
}
68+
left++;
69+
right--;
70+
}
71+
return true;
72+
}
73+
}

0 commit comments

Comments
 (0)