Skip to content

Commit f11c7bd

Browse files
committed
230423
1 parent 22296e6 commit f11c7bd

File tree

4 files changed

+237
-1
lines changed

4 files changed

+237
-1
lines changed

src/Contest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
import java.util.*;
3+
4+
class Solution {
5+
public int[] getSubarrayBeauty(int[] nums, int k, int x) {
6+
TreeMap<Integer, Integer> numbers = new TreeMap<>();
7+
LinkedList<Integer> sliding = new LinkedList<>();
8+
int n = nums.length;
9+
int[] ans = new int[n - k + 1];
10+
int ansIdx = 0;
11+
for (int num : nums) {
12+
sliding.add(num);
13+
numbers.put(num, numbers.getOrDefault(num, 0) + 1);
14+
if (sliding.size() == k) {
15+
int toRemove = sliding.pollFirst();
16+
int index = 0;
17+
for (Map.Entry<Integer, Integer> kv : numbers.entrySet()) {
18+
index += kv.getValue();
19+
if (index >= x) {
20+
int value = kv.getKey();
21+
if (value < 0) ans[ansIdx++] = value;
22+
else ansIdx++;
23+
break;
24+
}
25+
}
26+
int total = numbers.get(toRemove);
27+
total--;
28+
if (total == 0) numbers.remove(toRemove);
29+
else numbers.put(toRemove, total);
30+
}
31+
}
32+
return ans;
33+
}
34+
35+
36+
}
37+
38+
public class Contest extends Solution {
39+
}

src/Main.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import leetcode.editor.en.Q1014.BestSightseeingPair;
22
import leetcode.editor.en.Q1051.HeightChecker;
33
import leetcode.editor.en.Q122.BestTimeToBuyAndSellStockIi;
4+
import leetcode.editor.en.Q1416.RestoreTheArray;
45
import leetcode.editor.en.Q152.MaximumProductSubarray;
56
import leetcode.editor.en.Q1548.TheMostSimilarPathInAGraph;
67
import leetcode.editor.en.Q1567.MaximumLengthOfSubarrayWithPositiveProduct;
@@ -22,13 +23,15 @@
2223
import java.io.IOException;
2324
import java.io.InputStream;
2425
import java.util.ArrayList;
26+
import java.util.Arrays;
2527
import java.util.LinkedList;
2628
import java.util.List;
2729

2830

2931
public class Main {
3032
public static void main(String[] args) throws IOException {
31-
System.out.println(new HeightChecker().heightChecker(toIntArray("[1,1,4,2,1,3]")));
33+
34+
System.out.println(new RestoreTheArray().numberOfArrays("1317", 2000));
3235

3336
}
3437

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package leetcode.editor.en.Q1312;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//Given a string s. In one step you can insert any character at any index of
8+
//the string.
9+
//
10+
// Return the minimum number of steps to make s palindrome.
11+
//
12+
// A Palindrome String is one that reads the same backward as well as forward.
13+
//
14+
//
15+
// Example 1:
16+
//
17+
//
18+
//Input: s = "zzazz"
19+
//Output: 0
20+
//Explanation: The string "zzazz" is already palindrome we do not need any
21+
//insertions.
22+
//
23+
//
24+
// Example 2:
25+
//
26+
//
27+
//Input: s = "mbadm"
28+
//Output: 2
29+
//Explanation: String can be "mbdadbm" or "mdbabdm".
30+
//
31+
//
32+
// Example 3:
33+
//
34+
//
35+
//Input: s = "leetcode"
36+
//Output: 5
37+
//Explanation: Inserting 5 characters the string becomes "leetcodocteel".
38+
//
39+
//
40+
//
41+
// Constraints:
42+
//
43+
//
44+
// 1 <= s.length <= 500
45+
// s consists of lowercase English letters.
46+
//
47+
//
48+
// 👍 3613 👎 48
49+
50+
51+
//leetcode submit region begin(Prohibit modification and deletion)
52+
class Solution {
53+
Integer cache[][];
54+
55+
public int minInsertions(String s) {
56+
cache = new Integer[s.length()][s.length()];
57+
int left = 0;
58+
int right = s.length() - 1;
59+
60+
return minInsertions(left, right, s);
61+
}
62+
63+
private int minInsertions(int left, int right, String s) {
64+
if (left >= right) {
65+
return 0;
66+
}
67+
if (cache[left][right] != null) {
68+
return cache[left][right];
69+
}
70+
int lChar = s.charAt(left);
71+
int rChar = s.charAt(right);
72+
if (lChar == rChar) {
73+
cache[left][right] = minInsertions(left + 1, right - 1, s);
74+
return cache[left][right];
75+
}
76+
int moveLeft = minInsertions(left + 1, right, s);
77+
int moveRight = minInsertions(left, right - 1, s);
78+
cache[left][right] = 1 + Math.min(moveLeft, moveRight);
79+
return cache[left][right];
80+
}
81+
}
82+
//leetcode submit region end(Prohibit modification and deletion)
83+
84+
85+
public class MinimumInsertionStepsToMakeAStringPalindrome extends Solution {
86+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package leetcode.editor.en.Q1416;
2+
3+
import java.math.BigInteger;
4+
import java.util.*;
5+
6+
import javafx.util.Pair;
7+
import leetcode.editor.en.Q868.BinaryGap;
8+
9+
//A program was supposed to print an array of integers. The program forgot to
10+
//print whitespaces and the array is printed as a string of digits s and all we
11+
//know is that all integers in the array were in the range [1, k] and there are no
12+
//leading zeros in the array.
13+
//
14+
// Given the string s and the integer k, return the number of the possible
15+
//arrays that can be printed as s using the mentioned program. Since the answer may be
16+
//very large, return it modulo 10⁹ + 7.
17+
//
18+
//
19+
// Example 1:
20+
//
21+
//
22+
//Input: s = "1000", k = 10000
23+
//Output: 1
24+
//Explanation: The only possible array is [1000]
25+
//
26+
//
27+
// Example 2:
28+
//
29+
//
30+
//Input: s = "1000", k = 10
31+
//Output: 0
32+
//Explanation: There cannot be an array that was printed this way and has all
33+
//integer >= 1 and <= 10.
34+
//
35+
//
36+
// Example 3:
37+
//
38+
//
39+
//Input: s = "1317", k = 2000
40+
//Output: 8
41+
//Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31
42+
//,7],[1,3,17],[1,3,1,7]
43+
//
44+
//
45+
//
46+
// Constraints:
47+
//
48+
//
49+
// 1 <= s.length <= 10⁵
50+
// s consists of only digits and does not contain leading zeros.
51+
// 1 <= k <= 10⁹
52+
//
53+
//
54+
// 👍 1050 👎 37
55+
56+
57+
//leetcode submit region begin(Prohibit modification and deletion)
58+
import java.math.BigInteger;
59+
60+
class Solution {
61+
62+
private static final long MOD = 1000000007L;
63+
BigInteger[] cache;
64+
65+
public int numberOfArrays(String s, int k) {
66+
cache = new BigInteger[s.length()];
67+
BigInteger ans = numberOfArrays(0, s, new BigInteger(String.valueOf(k)));
68+
return ans.mod(new BigInteger("1000000007")).intValue();
69+
70+
}
71+
72+
private BigInteger numberOfArrays(int i, String s, BigInteger k) {
73+
if (i == s.length()) {
74+
return new BigInteger("1");
75+
}
76+
int c = Character.getNumericValue(s.charAt(i));
77+
if (c == 0) {
78+
return new BigInteger("0");
79+
}
80+
if (cache[i] != null) {
81+
return cache[i];
82+
}
83+
84+
BigInteger num = new BigInteger("0");
85+
BigInteger ans = new BigInteger("0");
86+
BigInteger ten = new BigInteger("10");
87+
for (int j = i; j < s.length(); j++) {
88+
num = num.multiply(ten);
89+
num = num.add(new BigInteger(String.valueOf(s.charAt(j))));
90+
if (num.compareTo(k) <= 0) {
91+
ans = ans.add(numberOfArrays(j + 1, s, k));
92+
} else {
93+
break;
94+
}
95+
}
96+
ans = ans.mod(new BigInteger("1000000007"));
97+
cache[i] = ans;
98+
99+
return ans;
100+
}
101+
102+
103+
}
104+
//leetcode submit region end(Prohibit modification and deletion)
105+
106+
107+
public class RestoreTheArray extends Solution {
108+
}

0 commit comments

Comments
 (0)