Skip to content

Commit 68d5193

Browse files
authored
Added tasks 38 - 40.
1 parent cbb65a0 commit 68d5193

File tree

6 files changed

+174
-0
lines changed

6 files changed

+174
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package s0038_count_and_say;
2+
3+
public class Solution {
4+
public String countAndSay(int n) {
5+
if (n == 1) return "1";
6+
StringBuilder res = new StringBuilder();
7+
String prev = countAndSay(n - 1);
8+
int count = 1;
9+
for (int i = 1; i < prev.length(); i++) {
10+
if (prev.charAt(i) == prev.charAt(i - 1)) count++;
11+
else {
12+
res.append(count).append(prev.charAt(i - 1));
13+
count = 1;
14+
}
15+
}
16+
res.append(count).append(prev.charAt(prev.length() - 1));
17+
return res.toString();
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package s0039_combination_sum;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Solution {
7+
public List<List<Integer>> combinationSum(int[] coins, int amount) {
8+
List<List<Integer>> ans = new ArrayList<>();
9+
List<Integer> subList = new ArrayList<>();
10+
combinationSumRec(coins.length, coins, amount, subList, ans);
11+
return ans;
12+
}
13+
14+
private void combinationSumRec(
15+
int n, int[] coins, int amount, List<Integer> subList, List<List<Integer>> ans) {
16+
if (amount == 0 || n == 0) {
17+
if (amount == 0) {
18+
List<Integer> base = new ArrayList<>(subList);
19+
ans.add(base);
20+
}
21+
return;
22+
}
23+
24+
if (amount - coins[n - 1] >= 0) {
25+
subList.add(coins[n - 1]);
26+
combinationSumRec(n, coins, amount - coins[n - 1], subList, ans);
27+
subList.remove(subList.size() - 1);
28+
}
29+
30+
combinationSumRec(n - 1, coins, amount, subList, ans);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package s0040_combination_sum_ii;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
10+
List<List<Integer>> sums = new ArrayList<>();
11+
Arrays.sort(candidates); // optimize
12+
combinationSum(candidates, target, 0, sums, new LinkedList<>());
13+
return sums;
14+
}
15+
16+
private void combinationSum(
17+
int[] candidates,
18+
int target,
19+
int start,
20+
List<List<Integer>> sums,
21+
LinkedList<Integer> sum) {
22+
if (target == 0) {
23+
// make a deep copy of the current combination
24+
sums.add(new ArrayList<>(sum));
25+
return;
26+
}
27+
for (int i = start; i < candidates.length && target >= candidates[i]; i++) {
28+
// If candidate[i] equals candidate[i-1], then solutions for i is subset of
29+
// solution of i-1
30+
// Refer:
31+
// https://leetcode.com/problems/combination-sum-ii/discuss/16861/Java-solution-using-dfs-easy-understand/977097
32+
if (i == start || (i > start && candidates[i] != candidates[i - 1])) {
33+
sum.addLast(candidates[i]);
34+
// call on 'i+1' (not i) to avoid duplicate usage of same element
35+
combinationSum(candidates, target - candidates[i], i + 1, sums, sum);
36+
sum.removeLast();
37+
}
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package s0038_count_and_say;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void countAndSay() {
11+
assertThat(new Solution().countAndSay(1), equalTo("1"));
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package s0039_combination_sum;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import org.junit.Test;
10+
11+
public class SolutionTest {
12+
@Test
13+
public void combinationSum() {
14+
List<List<Integer>> expected = new ArrayList<List<Integer>>();
15+
16+
List<Integer> sublist1 = new ArrayList<Integer>();
17+
sublist1.add(7);
18+
List<Integer> sublist2 = new ArrayList<Integer>();
19+
sublist2.addAll(Arrays.asList(3, 2, 2));
20+
21+
expected.add(sublist1);
22+
expected.add(sublist2);
23+
24+
List<List<Integer>> actual = new Solution().combinationSum(new int[] {2, 3, 6, 7}, 7);
25+
26+
for (int i = 0; i < expected.size(); i++) {
27+
assertThat(actual.get(i).toArray(), equalTo(expected.get(i).toArray()));
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package s0040_combination_sum_ii;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import org.junit.Test;
10+
11+
public class SolutionTest {
12+
@Test
13+
public void combinationSumII() {
14+
List<List<Integer>> expected = new ArrayList<>();
15+
16+
List<Integer> sub1 = new ArrayList<Integer>();
17+
sub1.addAll(Arrays.asList(1, 1, 6));
18+
19+
List<Integer> sub2 = new ArrayList<Integer>();
20+
sub2.addAll(Arrays.asList(1, 2, 5));
21+
22+
List<Integer> sub3 = new ArrayList<Integer>();
23+
sub3.addAll(Arrays.asList(1, 7));
24+
25+
List<Integer> sub4 = new ArrayList<Integer>();
26+
sub4.addAll(Arrays.asList(2, 6));
27+
28+
expected.add(sub1);
29+
expected.add(sub2);
30+
expected.add(sub3);
31+
expected.add(sub4);
32+
33+
List<List<Integer>> actual =
34+
new Solution().combinationSum2(new int[] {10, 1, 2, 7, 6, 1, 5}, 8);
35+
36+
for (int i = 0; i < expected.size(); i++) {
37+
assertThat(actual.get(i).toArray(), equalTo(expected.get(i).toArray()));
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)