Skip to content

Commit 078bdfc

Browse files
committed
070523
1 parent 38f1109 commit 078bdfc

File tree

4 files changed

+186
-103
lines changed

4 files changed

+186
-103
lines changed

src/Contest.java

Lines changed: 41 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,108 +3,64 @@
33

44
import java.util.*;
55

6-
class TreeNode {
7-
int val;
8-
TreeNode left;
9-
TreeNode right;
10-
11-
TreeNode() {
12-
}
13-
14-
TreeNode(int val) {
15-
this.val = val;
16-
}
17-
18-
TreeNode(int val, TreeNode left, TreeNode right) {
19-
this.val = val;
20-
this.left = left;
21-
this.right = right;
22-
}
23-
}
246

257
class Solution {
268

27-
HashSet<Integer> used = new HashSet<>();
28-
HashMap<String, TreeNode> trees = new HashMap<>();
29-
30-
public List<TreeNode> generateTrees(int n) {
31-
List<TreeNode> ans = new ArrayList<>();
32-
for (int i = 1; i <= n; i++) {
33-
generateTrees(i, n, ans);
34-
}
35-
36-
return trees.values().stream().toList();
37-
}
38-
39-
public TreeNode generateTrees(int i, int n, List<TreeNode> ans) {
40-
41-
used.add(i);
42-
TreeNode root = new TreeNode(i);
43-
for (int j = i - 1; j >= 1; j--) {
44-
if (!used.contains(j)) {
45-
root.left = generateTrees(j, n, ans);
46-
if (used.size() == n) {
47-
appendResult(root);
48-
49-
}
9+
TreeSet<Integer> indexes = new TreeSet<>();
10+
11+
public int[] colorTheArray(int n, int[][] queries) {
12+
// TreeMap<Integer, TreeSet<Integer>> timeline = new TreeMap<>();
13+
14+
15+
int[] values = new int[n];
16+
int[] jumps = new int[n];
17+
Arrays.fill(jumps, -1);
18+
int[] ans = new int[queries.length];
19+
for (int i = 0; i < queries.length; i++) {
20+
int index = queries[i][0];
21+
int value = queries[i][1];
22+
// if (values[index] != 0) {
23+
// timeline.get(values[index]).remove(index);
24+
// }
25+
values[index] = value;
26+
jumps[index] = index;
27+
if (index + 1 < n && values[index + 1] == value) {
28+
jumps[index + 1] = Math.min(index, jumps[index + 1]);
29+
jumps[index] = Math.min(index, jumps[index + 1]);
5030
}
51-
52-
}
53-
root.left = null;
54-
for (int j = i + 1; j >= 1; j++) {
55-
if (!used.contains(j)) {
56-
root.right = generateTrees(j, n, ans);
57-
if (used.size() == n) {
58-
appendResult(root);
59-
60-
}
31+
if (index > 0 && values[index - 1] == value) {
32+
jumps[index] = Math.min(jumps[index - 1], index);
6133
}
6234

63-
}
6435

65-
66-
for (int j = i - 1; j >= 1; j++) {
67-
if (!used.contains(j)) {
68-
root.left = generateTrees(j, n, ans);
69-
for (int k = i + 1; k <= n; k++) {
70-
if (!used.contains(k)) {
71-
root.right = generateTrees(j, n, ans);
72-
if (used.size() == n) {
73-
appendResult(root);
74-
}
75-
}
76-
}
77-
78-
}
36+
indexes.add(index);
37+
ans[i] = count(jumps, values);
7938
}
8039

8140

82-
used.remove(i);
83-
return root;
41+
return ans;
8442

8543
}
8644

87-
private void appendResult(TreeNode root) {
88-
trees.put(treeToString(root).toString(), root);
89-
}
45+
private int count(int[] jumps, int[] values) {
9046

91-
private StringBuilder treeToString(TreeNode root) {
92-
if (root == null) {
93-
return null;
94-
}
95-
StringBuilder sb = new StringBuilder();
96-
sb.append(root.val);
97-
if (root.left != null) {
98-
sb.append("L:");
99-
sb.append(treeToString(root.left));
100-
}
101-
if (root.right != null) {
102-
sb.append("R:");
103-
sb.append(treeToString(root.right));
47+
int ans = 0;
48+
int i = 0;
49+
while (i >= 0) {
50+
int value = values[i];
51+
int start = jumps[i];
52+
while (jumps[start] != start && values[start] == value) {
53+
start = jumps[start];
54+
}
55+
if (start != i) {
56+
ans += i - start;
57+
}
58+
i = start - 1;
10459
}
105-
return sb;
60+
return ans;
10661
}
10762

63+
10864
}
10965

11066
public class Contest extends Solution {

src/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import leetcode.editor.en.Q1752.CheckIfArrayIsSortedAndRotated;
55
import leetcode.editor.en.Q188.BestTimeToBuyAndSellStockIv;
66
import leetcode.editor.en.Q1964.FindTheLongestValidObstacleCourseAtEachPosition;
7+
import leetcode.editor.en.Q2466.CountWaysToBuildGoodStrings;
78
import leetcode.editor.en.Q354.RussianDollEnvelopes;
89
import leetcode.editor.en.Q377.CombinationSumIv;
910
import leetcode.editor.en.Q474.OnesAndZeroes;
1011
import leetcode.editor.en.Q518.CoinChangeII;
1112
import leetcode.editor.en.Q649.Dota2Senate;
13+
import leetcode.editor.en.Q91.DecodeWays;
1214
import org.json.JSONArray;
1315
import org.json.JSONTokener;
1416

@@ -22,7 +24,7 @@
2224

2325
public class Main {
2426
public static void main(String[] args) throws IOException {
25-
System.out.println(new NumberOfSubsequencesThatSatisfyTheGivenSumCondition().numSubseq(toIntArray("[3,5,6,7]"), 9));
27+
System.out.println(new DecodeWays().numDecodings("230"));
2628

2729
}
2830

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package leetcode.editor.en.Q2466;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//Given the integers zero, one, low, and high, we can construct a string by
8+
//starting with an empty string, and then at each step perform either of the
9+
//following:
10+
//
11+
//
12+
// Append the character '0' zero times.
13+
// Append the character '1' one times.
14+
//
15+
//
16+
// This can be performed any number of times.
17+
//
18+
// A good string is a string constructed by the above process having a length
19+
//between low and high (inclusive).
20+
//
21+
// Return the number of different good strings that can be constructed
22+
//satisfying these properties. Since the answer can be large, return it modulo 10⁹ + 7.
23+
//
24+
//
25+
// Example 1:
26+
//
27+
//
28+
//Input: low = 3, high = 3, zero = 1, one = 1
29+
//Output: 8
30+
//Explanation:
31+
//One possible valid good string is "011".
32+
//It can be constructed as follows: "" -> "0" -> "01" -> "011".
33+
//All binary strings from "000" to "111" are good strings in this example.
34+
//
35+
//
36+
// Example 2:
37+
//
38+
//
39+
//Input: low = 2, high = 3, zero = 1, one = 2
40+
//Output: 5
41+
//Explanation: The good strings are "00", "11", "000", "110", and "011".
42+
//
43+
//
44+
//
45+
// Constraints:
46+
//
47+
//
48+
// 1 <= low <= high <= 10⁵
49+
// 1 <= zero, one <= low
50+
//
51+
//
52+
// 👍 352 👎 22
53+
54+
55+
//leetcode submit region begin(Prohibit modification and deletion)
56+
class Solution {
57+
Long[] cache;
58+
long MOD = 1000000007L;
59+
60+
public int countGoodStrings(int low, int high, int zero, int one) {
61+
StringBuilder zeros = new StringBuilder();
62+
for (int i = 0; i < zero; i++) {
63+
zeros.append('0');
64+
}
65+
StringBuilder ones = new StringBuilder();
66+
for (int i = 0; i < one; i++) {
67+
ones.append('1');
68+
}
69+
cache = new Long[ high + 1];
70+
return countGoodStrings(new StringBuilder(), low, high, zeros.toString(), ones.toString()).intValue();
71+
}
72+
73+
74+
public Long countGoodStrings(StringBuilder sb, int low, int high, String zero, String one) {
75+
if (sb.length() == high) {
76+
return 1L;
77+
}
78+
if (cache[sb.length()] != null) {
79+
return cache[sb.length()];
80+
}
81+
long ans = 0;
82+
if (sb.length() >= low && sb.length() <= high) {
83+
ans = (ans + 1) % MOD;
84+
}
85+
if (sb.length() + zero.length() <= high) {
86+
sb.append(zero);
87+
ans = (ans + countGoodStrings(sb, low, high, zero, one)) % MOD;
88+
sb.delete(sb.length() - zero.length(), sb.length());
89+
}
90+
if (sb.length() + one.length() <= high) {
91+
sb.append(one);
92+
ans = (ans + countGoodStrings(sb, low, high, zero, one)) % MOD;
93+
sb.delete(sb.length() - one.length(), sb.length());
94+
}
95+
ans %= MOD;
96+
cache[sb.length()] = ans;
97+
return ans;
98+
}
99+
100+
101+
}
102+
//leetcode submit region end(Prohibit modification and deletion)
103+
104+
105+
public class CountWaysToBuildGoodStrings extends Solution {
106+
}

src/leetcode/editor/en/Q91/DecodeWays.java

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,47 @@
6969

7070
//leetcode submit region begin(Prohibit modification and deletion)
7171
class Solution {
72+
Integer cache[];
73+
7274
public int numDecodings(String s) {
73-
if (s.charAt(0) == '0') {
74-
return 0;
75+
if (s.charAt(0) == '0'
76+
|| s.contains("00")
77+
|| s.contains("30")
78+
|| s.contains("40")
79+
|| s.contains("50")
80+
|| s.contains("60")
81+
|| s.contains("70")
82+
|| s.contains("80")
83+
|| s.contains("90")
84+
) return 0;
85+
86+
cache = new Integer[s.length()];
87+
return Math.max(decodeWays(s.length() - 1, s), 0);
88+
}
89+
90+
private int decodeWays(int i, String s) {
91+
if (i < 0) {
92+
return 1;
7593
}
94+
if (cache[i] != null) return cache[i];
7695

77-
int n = s.length();
78-
int twoBack = 1;
79-
int oneBack = 1;
80-
for (int i = 1; i < n; i++) {
81-
int current = 0;
82-
if (s.charAt(i) != '0') {
83-
current = oneBack;
84-
}
85-
int twoDigit = Integer.parseInt(s.substring(i - 1, i + 1));
86-
if (twoDigit >= 10 && twoDigit <= 26) {
87-
current += twoBack;
96+
int ans = 0;
97+
int cur = Character.getNumericValue(s.charAt(i));
98+
if (cur == 0) {
99+
cache[i] = decodeWays(i - 2, s);
100+
return cache[i];
101+
}
102+
ans += decodeWays(i - 1, s);
103+
if (i > 0) {
104+
int prev = Character.getNumericValue(s.charAt(i - 1));
105+
int comp = (prev * 10) + cur;
106+
if (comp >= 10 && comp <= 26) {
107+
ans += decodeWays(i - 2, s);
88108
}
89-
90-
twoBack = oneBack;
91-
oneBack = current;
92109
}
93-
return oneBack;
110+
cache[i] = ans;
111+
return ans;
112+
94113
}
95114
}
96115
//leetcode submit region end(Prohibit modification and deletion)

0 commit comments

Comments
 (0)