Skip to content

Commit a310b9e

Browse files
author
ALVIN-DESKTOP\Alvin Toh
committed
feat(patch): update java 1220 and kotlin 1143 problem set to latest repo
1 parent d395ba2 commit a310b9e

File tree

2 files changed

+76
-138
lines changed

2 files changed

+76
-138
lines changed
Lines changed: 75 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,85 @@
1+
/* Bottom-Up Approach
2+
-----------------------------------------
3+
Time Complexity : O(n)
4+
Space Complexity : O(n)
5+
----------------------------------------*/
6+
17
class Solution {
2-
// 0: 'a', 1: 'e', 2: 'i', 3: 'o', 4: 'u'
8+
int MOD = (int) 1e9+7;
39

4-
private static int MOD = 1_000_000_000 + 7;
5-
6-
private int getSum(long[] arr) {
7-
long sum = 0;
8-
for(long x: arr) {
9-
sum = sum + x;
10-
sum = sum % MOD;
10+
public int countVowelPermutation(int n) {
11+
if (n == 1) {
12+
return 5;
1113
}
12-
return (int) sum;
13-
}
14-
15-
private long[] getBaseCounts() {
16-
return new long[]{1, 1, 1, 1, 1};
17-
}
18-
19-
private Map<Integer, List<Integer>> getNextCountMapping() {
20-
Map<Integer, List<Integer>> map = new HashMap<>();
21-
22-
/* 0 1 2 3 4
23-
a e i o u
24-
25-
Reverse mapping i.e. "depends on"
26-
{a: [e, i, u]}, {e: [a, i]}, {i: [e, o]},
27-
{o: [i]}, {u: [i, o]}
28-
*/
29-
30-
map.put(0, new ArrayList<>(List.of(1, 2, 4)));
31-
map.put(1, new ArrayList<>(List.of(0, 2)));
32-
map.put(2, new ArrayList<>(List.of(1, 3)));
33-
map.put(3, new ArrayList<>(List.of(2)));
34-
map.put(4, new ArrayList<>(List.of(2, 3)));
35-
36-
return map;
37-
}
38-
39-
private long[] getNextCounts(
40-
long[] currentCounts,
41-
Map<Integer, List<Integer>> mapNextCounting
42-
) {
43-
long[] nextCounts = new long[5];
44-
Arrays.fill(nextCounts, 0);
45-
46-
// Mapping conversion
47-
for(int key: mapNextCounting.keySet()) {
48-
for(int val: mapNextCounting.get(key)) {
49-
nextCounts[val] += (long) currentCounts[key];
50-
nextCounts[val] %= MOD;
51-
}
14+
15+
long[][] dp = new long[n + 1][5];
16+
for (int j = 0; j < 5; j++) {
17+
dp[1][j] = 1;
5218
}
53-
54-
return nextCounts;
19+
20+
for (int i = 2; i <= n; i++) {
21+
dp[i][0] = dp[i - 1][1];
22+
dp[i][1] = (dp[i - 1][0] + dp[i - 1][2]) % MOD;
23+
dp[i][2] = (dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][3] + dp[i - 1][4]) % MOD;
24+
dp[i][3] = (dp[i - 1][2] + dp[i - 1][4]) % MOD;
25+
dp[i][4] = dp[i - 1][0];
26+
}
27+
28+
long result = 0;
29+
for (int j = 0; j < 5; j++) {
30+
result = (result + dp[n][j]) % MOD;
31+
}
32+
33+
return (int)result;
5534
}
56-
35+
}
36+
37+
/* Top Down Approach
38+
-----------------------------------------
39+
Time Complexity : O(n)
40+
Space Complexity : O(n)
41+
----------------------------------------*/
42+
43+
class Solution {
44+
HashMap<String, Integer> memo = new HashMap<>();
45+
int MOD = (int) 1e9+7;
46+
5747
public int countVowelPermutation(int n) {
58-
long[] counts = getBaseCounts();
59-
if(n == 1) {
60-
return getSum(counts);
61-
}
48+
long ans = 0;
49+
ans = (ans + dfs('a', n, 1)) % MOD;
50+
ans = (ans + dfs('e', n, 1)) % MOD;
51+
ans = (ans + dfs('i', n, 1)) % MOD;
52+
ans = (ans + dfs('o', n, 1)) % MOD;
53+
ans = (ans + dfs('u', n, 1)) % MOD;
54+
return (int)ans;
55+
}
56+
57+
private int dfs(char c, int n, int l){
58+
if(l == n)
59+
return 1;
6260

63-
Map<Integer, List<Integer>> mapNextCounting;
64-
mapNextCounting = getNextCountMapping();
65-
66-
for(int i=1; i<n; i++) {
67-
counts = getNextCounts(counts, mapNextCounting);
61+
String key = c + "_" + l;
62+
if (memo.containsKey(key)) return memo.get(key);
63+
64+
long res = 0;
65+
if(c == 'a') {
66+
res = dfs('e', n, l+1);
67+
} else if(c == 'e') {
68+
res = (res + dfs('a', n, l+1)) % MOD;
69+
res = (res + dfs('i', n, l+1)) % MOD;
70+
} else if(c == 'i') {
71+
res = (res + dfs('a', n, l+1)) % MOD;
72+
res = (res + dfs('e', n, l+1)) % MOD;
73+
res = (res + dfs('o', n, l+1)) % MOD;
74+
res = (res + dfs('u', n, l+1)) % MOD;
75+
} else if(c == 'o') {
76+
res = (res + dfs('i', n, l+1)) % MOD;
77+
res = (res + dfs('u', n, l+1)) % MOD;
78+
} else {
79+
res = dfs('a', n, l+1);
6880
}
69-
70-
return getSum(counts);
81+
82+
memo.put(key, (int)(res % MOD));
83+
return (int)(res % MOD);
7184
}
7285
}

kotlin/1143-longest-common-subsequence.kt

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -20,79 +20,4 @@ class Solution {
2020
}
2121
return dp[M][N]
2222
}
23-
}
24-
25-
/*
26-
* Different solutions
27-
*/
28-
29-
// Recursion + Memoization, Time Complexity of O(n * m) and space complexity of O(n * m)
30-
class Solution {
31-
fun longestCommonSubsequence(t1: String, t2: String): Int {
32-
val n = t1.length
33-
val m = t2.length
34-
val dp = Array (n) { IntArray (m) { -1 } }
35-
36-
fun dfs(i: Int, j: Int): Int {
37-
if (i == n || j == m) return 0
38-
if (dp[i][j] != -1) return dp[i][j]
39-
40-
if (t1[i] == t2[j])
41-
dp[i][j] = 1 + dfs(i + 1, j + 1)
42-
else
43-
dp[i][j] = maxOf(dfs(i + 1, j), dfs(i, j + 1))
44-
45-
return dp[i][j]
46-
}
47-
48-
return dfs(0, 0)
49-
}
50-
}
51-
52-
// Top down DP, Time Complexity of O(n * m) and space complexity of O(n * m)
53-
class Solution {
54-
fun longestCommonSubsequence(t1: String, t2: String): Int {
55-
val n = t1.length
56-
val m = t2.length
57-
val dp = Array (n + 1) { IntArray (m + 1) }
58-
59-
for (i in n - 1 downTo 0) {
60-
for (j in m - 1 downTo 0) {
61-
if (t1[i] == t2[j])
62-
dp[i][j] = 1 + dp[i + 1][j + 1]
63-
else
64-
dp[i][j] = maxOf(dp[i + 1][j], dp[i][j + 1])
65-
}
66-
}
67-
68-
return dp[0][0]
69-
}
70-
}
71-
72-
// Optimized DP (Works both for both Top-down and Bottom-up, but here we use bottom-up approach)
73-
// Time Complexity of O(n * m) and space complexity of O(maxOf(n, m))
74-
class Solution {
75-
fun longestCommonSubsequence(t1: String, t2: String): Int {
76-
val m = t1.length
77-
val n = t2.length
78-
if (m < n) return longestCommonSubsequence(t2, t1)
79-
80-
var dp = IntArray (n + 1)
81-
82-
for (i in m downTo 0) {
83-
var newDp = IntArray (n + 1)
84-
for (j in n downTo 0) {
85-
if (i == m || j == n) {
86-
newDp[j] = 0
87-
} else if (t1[i] == t2[j]) {
88-
newDp[j] = 1 + dp[j + 1]
89-
} else {
90-
newDp[j] = maxOf(dp[j], newDp[j + 1])
91-
}
92-
}
93-
dp = newDp
94-
}
95-
96-
return dp[0]
97-
}
98-
}
23+
}

0 commit comments

Comments
 (0)