Skip to content

Commit af82c08

Browse files
authored
Merge pull request #2103 from se6816/main
[se6816] WEEK 03 Solutions
2 parents bc4bc43 + 8561368 commit af82c08

File tree

5 files changed

+216
-0
lines changed

5 files changed

+216
-0
lines changed

combination-sum/se6816.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
중복 조합을 통해 조건에 맞는 조합을 찾는 방식
3+
candidates의 길이 -> N
4+
시간 복잡도 : O(2^N)
5+
공간 복잡도 : O(N)
6+
*/
7+
class Solution {
8+
List<List<Integer>> list;
9+
List<Integer> dataList;
10+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
11+
Arrays.sort(candidates);
12+
list = new ArrayList<>();
13+
dataList = new ArrayList<>();
14+
execute(candidates, 0, 0, target);
15+
return list;
16+
}
17+
18+
public void execute(int[] candidates, int idx, int sum, int target) {
19+
if(idx >= candidates.length) {
20+
return;
21+
}
22+
23+
if(sum == target) {
24+
list.add(new ArrayList<>(dataList));
25+
return;
26+
}
27+
28+
if(sum+candidates[idx] > target) {
29+
return;
30+
}
31+
32+
33+
34+
35+
dataList.add(candidates[idx]);
36+
execute(candidates, idx, sum + candidates[idx], target);
37+
dataList.remove(dataList.size()-1);
38+
execute(candidates, idx+1, sum, target);
39+
}
40+
}

decode-ways/se6816.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
HashSet과 substring을 이용하여 문자열을 비교하면서 가능한 조합의 수를 누적하는 방식
3+
문자열 S 의 길이 -> N
4+
시간 복잡도 : O(N^2)
5+
공간 복잡도 : O(N)
6+
*/
7+
class Solution2 {
8+
Set<String> numberSet = new HashSet<>();
9+
{
10+
numberSet.addAll(Arrays.asList("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26"));
11+
}
12+
public int numDecodings(String s) {
13+
if(s.length() == 1) return s.charAt(0) == '0' ? 0 : 1;
14+
15+
int[] list = new int[s.length()];
16+
list[0] = isImpossibleDecode(s.substring(0,1)) ? 0 : 1;
17+
list[1] = isImpossibleDecode(s.substring(0,2)) ? 0 : 1;
18+
list[1] += list[0]==1 && !isImpossibleDecode(s.substring(1,2)) ? 1 : 0;
19+
20+
21+
for(int i = 2; i < s.length() ; i++) {
22+
list[i] += isImpossibleDecode(s.substring(i-1,i+1)) ? 0 : list[i-2];
23+
list[i] += isImpossibleDecode(s.substring(i,i+1)) ? 0 : list[i-1];
24+
}
25+
26+
return list[s.length()-1];
27+
28+
}
29+
30+
public boolean isImpossibleDecode(String target) {
31+
int len = target.length();
32+
if(!numberSet.contains(target)) {
33+
return true;
34+
}
35+
36+
return false;
37+
38+
}
39+
}
40+
41+
/**
42+
이전 substring() 연산 부분 최적화
43+
이전 substring()으로 문자열을 자르지 않고, charAt()을 이용하여 직접 연산하여 조합의 수를 누적시키는 방식
44+
문자열 S 의 길이 -> N
45+
시간 복잡도 : O(N)
46+
공간 복잡도 : O(N)
47+
*/
48+
class Solution {
49+
50+
public int numDecodings(String s) {
51+
if(s.length() == 1) return s.charAt(0) == '0' ? 0 : 1;
52+
53+
int[] list = new int[s.length()];
54+
list[0] = s.charAt(0) == '0' ? 0 : 1;
55+
int target = (s.charAt(0) - '0') * 10 + (s.charAt(1) - '0');
56+
list[1] = (target >= 10) && (target <= 26) ? 1 : 0;
57+
list[1] += list[0]==1 && s.charAt(1) != '0' ? 1 : 0;
58+
59+
60+
for(int i = 2; i < s.length() ; i++) {
61+
target = (s.charAt(i-1) - '0') * 10 + (s.charAt(i) - '0');
62+
list[i] += (target >= 10) && (target <= 26) ? list[i-2] : 0;
63+
list[i] += s.charAt(i) != '0' ? list[i-1] : 0;
64+
}
65+
66+
return list[s.length()-1];
67+
68+
}
69+
}

maximum-subarray/se6816.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
dp를 이용하여 지속적으로 최대의 값을 기록하면서 연속 부분 배열의 최대 합을 구하는 방식
3+
nums 의 길이 -> N
4+
시간 복잡도 : O(N)
5+
공간 복잡도 : O(N)
6+
*/
7+
class Solution2 {
8+
public int maxSubArray(int[] nums) {
9+
int[] dp = new int[nums.length];
10+
dp[0] = nums[0];
11+
for(int i = 1; i < nums.length; i++) {
12+
dp[i] = Math.max(nums[i], dp[i-1] + nums[i]);
13+
}
14+
15+
for(int i = 0; i < dp.length; i++) {
16+
}
17+
return Arrays.stream(dp)
18+
.max()
19+
.getAsInt();
20+
}
21+
}
22+
23+
/**
24+
이전 방식과 동일하나 변수 하나로 이전 값만 유지하는 방식
25+
nums 의 길이 -> N
26+
시간 복잡도 : O(N)
27+
공간 복잡도 : O(1)
28+
*/
29+
class Solution {
30+
public int maxSubArray(int[] nums) {
31+
int prevSum = nums[0];
32+
int result = prevSum;
33+
for(int i = 1; i < nums.length; i++) {
34+
prevSum = Math.max(nums[i], prevSum + nums[i]);
35+
result = Math.max(result, prevSum);
36+
}
37+
38+
return result;
39+
}
40+
}

number-of-1-bits/se6816.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
비트 연산을 통해, 1의 개수를 구하는 방식
3+
숫자 n -> N
4+
시간 복잡도 : O(logN)
5+
공간 복잡도 : O(1)
6+
*/
7+
class Solution2 {
8+
public int hammingWeight(int n) {
9+
int count = 0;
10+
long bit = 1;
11+
while(bit <= n) {
12+
if((n & bit) > 0) {
13+
count++;
14+
}
15+
bit <<= 1;
16+
}
17+
return count;
18+
}
19+
}
20+
21+
/**
22+
Integer.bitCount() 메소드를 통해, 1의 개수를 구하는 방식
23+
시간 복잡도 : O(1)
24+
공간 복잡도 : O(1)
25+
*/
26+
class Solution {
27+
public int hammingWeight(int n) {
28+
return Integer.bitCount(n);
29+
}
30+
}

valid-palindrome/se6816.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
투포인터 방식을 통해 맨 앞과 맨 끝 포인터에서 맨 끝 포인터가 맨 앞 포인터를 앞지를 때까지 비교하는 방식
3+
문자열 s의 길이 -> N
4+
시간 복잡도 : O(N)
5+
공간 복잡도 : O(1)
6+
*/
7+
class Solution {
8+
public boolean isPalindrome(String s) {
9+
s=s.toUpperCase();
10+
int start=0;
11+
int end=s.length()-1;
12+
while(start < end){
13+
char ch=s.charAt(start);
14+
char ch2=s.charAt(end);
15+
16+
// 문자 체크
17+
if(!((ch>=65 && ch<=90) || (ch>=48 && ch<=57))){
18+
start++;
19+
continue;
20+
}
21+
if(!((ch2>=65 && ch2<=90) || (ch2>=48 && ch2<=57))){
22+
end--;
23+
continue;
24+
}
25+
26+
if(ch==ch2){
27+
start++;
28+
end--;
29+
continue;
30+
}
31+
32+
return false;
33+
34+
}
35+
return true;
36+
}
37+
}

0 commit comments

Comments
 (0)