Skip to content

Commit e0c4f4c

Browse files
authored
Merge pull request #1804 from hoyeongkwak/main
[hoyeongkwak] Week3 Solutions
2 parents a9a2e59 + a3ecb00 commit e0c4f4c

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

combination-sum/hoyeongkwak.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
Time complexity : O(n ^ (target / min_val))
3+
Space complexity : O(target / min_val)
4+
backtracking
5+
c = [2], total = 2
6+
c = [2, 2], total = 4
7+
c = [2, 2, 2] total = 6
8+
c = [2, 2, 2, 2] total = 8, 초과 backtrack
9+
c = [2, 2, 2, 3] total = 9, 초과 backtrack
10+
c = [2, 2, 2, 6] total = 12, 초과 backtrack
11+
c = [2, 2, 2, 7] total = 13, 초과 backtrack
12+
c = [2, 2, 3] total = 7 정답 추가
13+
c = [2, 2, 6] total = 10, 초과 backtrack
14+
c = [2, 2, 7] total = 11, 초과 backtrack
15+
....
16+
[2,2,3], [7]
17+
18+
'''
19+
20+
class Solution:
21+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
22+
result = []
23+
24+
def backtrack(start, current, total):
25+
if total == target:
26+
result.append(list(current))
27+
return
28+
if total > target:
29+
return
30+
31+
for i in range(start, len(candidates)):
32+
current.append(candidates[i])
33+
backtrack(i, current, total + candidates[i])
34+
current.pop()
35+
backtrack(0, [], 0)
36+
return result

decode-ways/hoyeongkwak.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Time complexity : O(n)
3+
Space complexity : O(n)
4+
dp를 사용
5+
현재 숫자만 사용(1~9)
6+
이전 숫자와 합쳐서 사용(10~26)
7+
ex) 226
8+
2 2 6
9+
22 6
10+
2 26
11+
*/
12+
class Solution {
13+
public int numDecodings(String s) {
14+
if (s == null || s.length() == 0 || s.charAt(0) == '0') {
15+
return 0;
16+
}
17+
18+
int n = s.length();
19+
int[] dp = new int[n + 1];
20+
21+
dp[0] = 1;
22+
dp[1] = 1;
23+
24+
for (int i = 2; i <= n; i++) {
25+
int oneDigit = Integer.parseInt(s.substring(i - 1, i));
26+
if (oneDigit >= 1 && oneDigit <= 9) {
27+
dp[i] += dp[i - 1];
28+
}
29+
int twoDigits = Integer.parseInt(s.substring(i - 2, i));
30+
if (twoDigits >= 10 && twoDigits <= 26) {
31+
dp[i] += dp[i - 2];
32+
}
33+
}
34+
return dp[n];
35+
}
36+
}

maximum-subarray/hoyeongkwak.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Time complexity : O(n)
3+
Space complexity : O(1)
4+
*/
5+
class Solution {
6+
public int maxSubArray(int[] nums) {
7+
int maxSum = Integer.MIN_VALUE;
8+
int currentSum = 0;
9+
10+
for (int i = 0; i < nums.length; i++) {
11+
currentSum += nums[i];
12+
13+
if (currentSum > maxSum) {
14+
maxSum = currentSum;
15+
}
16+
17+
if (currentSum < 0) {
18+
currentSum = 0;
19+
}
20+
}
21+
return maxSum;
22+
}
23+
}

number-of-1-bits/hoyeongkwak.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Time complexity : O(n)
3+
Space complexity : O(1)
4+
*/
5+
class Solution {
6+
public int hammingWeight(int n) {
7+
int sum = 0;
8+
while (n != 0) {
9+
n &= (n - 1);
10+
sum++;
11+
}
12+
return sum;
13+
}
14+
}

valid-palindrome/hoyeongkwak.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
Time Complexity : O(n)
3+
Space Complexity : O(n)
4+
*/
5+
class Solution {
6+
public boolean isPalindrome(String s) {
7+
if (s.length() == 1) return true;
8+
String sLower = s.toLowerCase().replaceAll("[^a-zA-Z0-9]", "");
9+
StringBuffer strBuilder = new StringBuffer(sLower);
10+
String revStr = strBuilder.reverse().toString();
11+
return sLower.equals(revStr);
12+
}
13+
}

0 commit comments

Comments
 (0)