Skip to content

Commit 2b50e9f

Browse files
committed
090823
1 parent a4647dc commit 2b50e9f

File tree

4 files changed

+165
-1
lines changed

4 files changed

+165
-1
lines changed

src/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import leetcode.editor.en.Q1035.UncrossedLines;
2+
import leetcode.editor.en.Q1220.CountVowelsPermutation;
23
import leetcode.editor.en.Q1498.NumberOfSubsequencesThatSatisfyTheGivenSumCondition;
34
import leetcode.editor.en.Q1697.CheckingExistenceOfEdgeLengthLimitedPaths;
45
import leetcode.editor.en.Q1752.CheckIfArrayIsSortedAndRotated;
@@ -10,6 +11,7 @@
1011
import leetcode.editor.en.Q377.CombinationSumIv;
1112
import leetcode.editor.en.Q474.OnesAndZeroes;
1213
import leetcode.editor.en.Q518.CoinChangeII;
14+
import leetcode.editor.en.Q54.SpiralMatrix;
1315
import leetcode.editor.en.Q649.Dota2Senate;
1416
import leetcode.editor.en.Q91.DecodeWays;
1517
import org.json.JSONArray;
@@ -25,7 +27,7 @@
2527

2628
public class Main {
2729
public static void main(String[] args) throws IOException {
28-
System.out.println(Arrays.deepToString(new SparseMatrixMultiplication().multiply(toIntMatrix("[[1,-5]]"), toIntMatrix("[[12],[-1]]"))));
30+
System.out.println(new CountVowelsPermutation().countVowelPermutation(2));
2931

3032
}
3133

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package leetcode.editor.en.Q1220;
2+
3+
import java.math.BigInteger;
4+
import java.util.*;
5+
6+
import javafx.util.Pair;
7+
8+
//leetcode submit region begin(Prohibit modification and deletion)
9+
import java.math.BigInteger;
10+
11+
class Solution {
12+
BigInteger[][] cache;
13+
14+
public int countVowelPermutation(int n) {
15+
BigInteger ans = new BigInteger("0");
16+
cache = new BigInteger[26][n + 1];
17+
ans = ans.add(countVowelPermutation('a', n - 1));
18+
ans = ans.add(countVowelPermutation('e', n - 1));
19+
ans = ans.add(countVowelPermutation('i', n - 1));
20+
ans = ans.add(countVowelPermutation('o', n - 1));
21+
ans = ans.add(countVowelPermutation('u', n - 1));
22+
return ans.mod(new BigInteger("1000000007")).intValue();
23+
}
24+
25+
public BigInteger countVowelPermutation(char c, int n) {
26+
if (n == 0) {
27+
return new BigInteger("1");
28+
}
29+
BigInteger ans = new BigInteger("0");
30+
if (cache[c - 'a'][n] != null) {
31+
return cache[c - 'a'][n];
32+
}
33+
switch (c) {
34+
case 'a' -> {
35+
ans = ans.add(countVowelPermutation('e', n - 1));
36+
}
37+
case 'e' -> {
38+
ans = ans.add(countVowelPermutation('a', n - 1));
39+
ans = ans.add(countVowelPermutation('i', n - 1));
40+
}
41+
case 'i' -> {
42+
ans = ans.add(countVowelPermutation('a', n - 1));
43+
ans = ans.add(countVowelPermutation('e', n - 1));
44+
ans = ans.add(countVowelPermutation('o', n - 1));
45+
ans = ans.add(countVowelPermutation('u', n - 1));
46+
}
47+
case 'o' -> {
48+
ans = ans.add(countVowelPermutation('i', n - 1));
49+
ans = ans.add(countVowelPermutation('u', n - 1));
50+
}
51+
case 'u' -> {
52+
ans = ans.add(countVowelPermutation('a', n - 1));
53+
}
54+
55+
}
56+
cache[c - 'a'][n] = ans;
57+
return ans;
58+
59+
}
60+
}
61+
//leetcode submit region end(Prohibit modification and deletion)
62+
63+
64+
public class CountVowelsPermutation extends Solution {
65+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package leetcode.editor.en.Q265;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
10+
Integer[][] cache;
11+
12+
public int minCostII(int[][] costs) {
13+
cache = new Integer[costs.length][costs[0].length + 1];
14+
return minCostII(0, costs[0].length, costs);
15+
}
16+
17+
private int minCostII(int i, int cantBe, int[][] costs) {
18+
if (i == costs.length) {
19+
return 0;
20+
}
21+
if (cache[i][cantBe] != null) {
22+
return cache[i][cantBe];
23+
}
24+
25+
int[] colors = costs[i];
26+
int ans = Integer.MAX_VALUE;
27+
for (int j = 0; j < colors.length; j++) {
28+
if (j == cantBe) continue;
29+
int cost = colors[j] + minCostII(i + 1, j, costs);
30+
ans = Math.min(ans, cost);
31+
}
32+
cache[i][cantBe] = ans;
33+
return ans;
34+
}
35+
}
36+
//leetcode submit region end(Prohibit modification and deletion)
37+
38+
39+
public class PaintHouseIi extends Solution {
40+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package leetcode.editor.en.Q54;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
List<Integer> ans;
10+
private int[][] DIRECTIONS = new int[][]{
11+
new int[]{0, 1}, //right
12+
new int[]{1, 0}, //down
13+
new int[]{0, -1}, //left
14+
new int[]{-1, 0}, //up
15+
};
16+
17+
public List<Integer> spiralOrder(int[][] matrix) {
18+
boolean[][] visited = new boolean[matrix.length][matrix[0].length];
19+
ans = new ArrayList<>();
20+
dfs(0, 0, 0, visited, matrix);
21+
return ans;
22+
}
23+
24+
private void dfs(int row, int col, int dir, boolean[][] visited, int[][] matrix) {
25+
visited[row][col] = true;
26+
ans.add(matrix[row][col]);
27+
int[] next = newDir(row, col, dir, visited);
28+
if (next[0] == -1) return;
29+
dfs(next[0], next[1], next[2], visited, matrix);
30+
}
31+
32+
private int[] newDir(int row, int col, int dir, boolean[][] visited) {
33+
int newRow = row + DIRECTIONS[dir][0];
34+
int newCol = col + DIRECTIONS[dir][1];
35+
if (isValidIdx(newRow, newCol, visited)) {
36+
return new int[]{newRow, newCol, dir};
37+
} else {
38+
dir = ((dir + 1) % DIRECTIONS.length);
39+
newRow = row + DIRECTIONS[dir][0];
40+
newCol = col + DIRECTIONS[dir][1];
41+
if (isValidIdx(newRow, newCol, visited)) {
42+
return new int[]{newRow, newCol, dir};
43+
}
44+
}
45+
return new int[]{-1, -1, -1};
46+
}
47+
48+
private boolean isValidIdx(int row, int col, boolean[][] visited) {
49+
return row >= 0 && row < visited.length && col >= 0 && col < visited[row].length && !visited[row][col];
50+
}
51+
52+
}
53+
//leetcode submit region end(Prohibit modification and deletion)
54+
55+
56+
public class SpiralMatrix extends Solution {
57+
}

0 commit comments

Comments
 (0)