Skip to content

Commit bfa4662

Browse files
authored
Added tasks 3345-3352
1 parent c67d4ff commit bfa4662

File tree

12 files changed

+859
-242
lines changed
  • src/main/java
    • g0101_0200
    • g3101_3200/s3165_maximum_sum_of_subsequence_with_non_adjacent_elements
    • g3301_3400
      • s3345_smallest_divisible_digit_product_i
      • s3346_maximum_frequency_of_an_element_after_performing_operations_i
      • s3347_maximum_frequency_of_an_element_after_performing_operations_ii
      • s3348_smallest_divisible_digit_product_ii
      • s3349_adjacent_increasing_subarrays_detection_i
      • s3350_adjacent_increasing_subarrays_detection_ii
      • s3351_sum_of_good_subsequences
      • s3352_count_k_reducible_numbers_less_than_n

12 files changed

+859
-242
lines changed

README.md

Lines changed: 111 additions & 103 deletions
Large diffs are not rendered by default.

src/main/java/g0101_0200/s0139_word_break/readme.md

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,41 +42,34 @@ Given a string `s` and a dictionary of strings `wordDict`, return `true` if `s`
4242
## Solution
4343

4444
```java
45-
import java.util.HashSet;
4645
import java.util.List;
47-
import java.util.Set;
4846

4947
public class Solution {
48+
private Boolean[] memo;
49+
5050
public boolean wordBreak(String s, List<String> wordDict) {
51-
Set<String> set = new HashSet<>();
52-
int max = 0;
53-
boolean[] flag = new boolean[s.length() + 1];
54-
for (String st : wordDict) {
55-
set.add(st);
56-
if (max < st.length()) {
57-
max = st.length();
58-
}
59-
}
60-
for (int i = 1; i <= max; i++) {
61-
if (dfs(s, 0, i, max, set, flag)) {
62-
return true;
63-
}
64-
}
65-
return false;
51+
memo = new Boolean[s.length() + 1];
52+
return dp(s, 0, wordDict);
6653
}
6754

68-
private boolean dfs(String s, int start, int end, int max, Set<String> set, boolean[] flag) {
69-
if (!flag[end] && set.contains(s.substring(start, end))) {
70-
flag[end] = true;
71-
if (end == s.length()) {
72-
return true;
55+
public boolean dp(String s, int i, List<String> wordDict) {
56+
if (i == s.length()) {
57+
return true;
58+
}
59+
if (memo[i] != null) {
60+
return memo[i];
61+
}
62+
for (String word : wordDict) {
63+
int len = word.length();
64+
if (i + len > s.length() || !s.substring(i, i + len).equals(word)) {
65+
continue;
7366
}
74-
for (int i = 1; i <= max; i++) {
75-
if (end + i <= s.length() && dfs(s, end, end + i, max, set, flag)) {
76-
return true;
77-
}
67+
if (dp(s, i + len, wordDict)) {
68+
memo[i] = true;
69+
return true;
7870
}
7971
}
72+
memo[i] = false;
8073
return false;
8174
}
8275
}

src/main/java/g0101_0200/s0152_maximum_product_subarray/readme.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,20 @@ A **subarray** is a contiguous subsequence of the array.
3838
```java
3939
public class Solution {
4040
public int maxProduct(int[] nums) {
41-
int currentMaxProd = nums[0];
42-
int currentMinProd = nums[0];
43-
int overAllMaxProd = nums[0];
44-
for (int i = 1; i < nums.length; i++) {
45-
if (nums[i] < 0) {
46-
int temp = currentMaxProd;
47-
currentMaxProd = currentMinProd;
48-
currentMinProd = temp;
41+
int overAllMaxProd = Integer.MIN_VALUE;
42+
int n = nums.length;
43+
int start = 1;
44+
int end = 1;
45+
for (int i = 0; i < n; i++) {
46+
if (start == 0) {
47+
start = 1;
4948
}
50-
currentMaxProd = Math.max(nums[i], nums[i] * currentMaxProd);
51-
currentMinProd = Math.min(nums[i], nums[i] * currentMinProd);
52-
overAllMaxProd = Math.max(overAllMaxProd, currentMaxProd);
49+
if (end == 0) {
50+
end = 1;
51+
}
52+
start = start * nums[i];
53+
end = end * nums[n - i - 1];
54+
overAllMaxProd = Math.max(overAllMaxProd, Math.max(start, end));
5355
}
5456
return overAllMaxProd;
5557
}

src/main/java/g3101_3200/s3165_maximum_sum_of_subsequence_with_non_adjacent_elements/readme.md

Lines changed: 54 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -46,124 +46,76 @@ A **subsequence** is an array that can be derived from another array by deleting
4646
## Solution
4747

4848
```java
49-
import java.util.stream.Stream;
50-
5149
public class Solution {
50+
private static final int YY = 0;
51+
private static final int YN = 1;
52+
private static final int NY = 2;
53+
private static final int NN = 3;
5254
private static final int MOD = 1_000_000_007;
5355

5456
public int maximumSumSubsequence(int[] nums, int[][] queries) {
55-
int ans = 0;
56-
SegTree segTree = new SegTree(nums);
57-
for (int[] q : queries) {
58-
int idx = q[0];
59-
int val = q[1];
60-
segTree.update(idx, val);
61-
ans = (ans + segTree.getMax()) % MOD;
57+
long[][] tree = build(nums);
58+
long result = 0;
59+
for (int i = 0; i < queries.length; ++i) {
60+
result += set(tree, queries[i][0], queries[i][1]);
61+
result %= MOD;
6262
}
63-
return ans;
63+
return (int) result;
6464
}
6565

66-
static class SegTree {
67-
private static class Record {
68-
int takeFirstTakeLast;
69-
int takeFirstSkipLast;
70-
int skipFirstSkipLast;
71-
int skipFirstTakeLast;
72-
73-
public Integer getMax() {
74-
return Stream.of(
75-
this.takeFirstSkipLast,
76-
this.takeFirstTakeLast,
77-
this.skipFirstSkipLast,
78-
this.skipFirstTakeLast)
79-
.max(Integer::compare)
80-
.orElse(null);
81-
}
82-
83-
public Integer skipLast() {
84-
return Stream.of(this.takeFirstSkipLast, this.skipFirstSkipLast)
85-
.max(Integer::compare)
86-
.orElse(null);
87-
}
88-
89-
public Integer takeLast() {
90-
return Stream.of(this.skipFirstTakeLast, this.takeFirstTakeLast)
91-
.max(Integer::compare)
92-
.orElse(null);
93-
}
66+
private static long[][] build(int[] nums) {
67+
final int len = nums.length;
68+
int size = 1;
69+
while (size < len) {
70+
size <<= 1;
9471
}
95-
96-
private final Record[] seg;
97-
private final int[] nums;
98-
99-
public SegTree(int[] nums) {
100-
this.nums = nums;
101-
seg = new Record[4 * nums.length];
102-
for (int i = 0; i < 4 * nums.length; ++i) {
103-
seg[i] = new Record();
104-
}
105-
build(0, nums.length - 1, 0);
106-
}
107-
108-
private void build(int i, int j, int k) {
109-
if (i == j) {
110-
seg[k].takeFirstTakeLast = nums[i];
111-
return;
112-
}
113-
int mid = (i + j) >> 1;
114-
build(i, mid, 2 * k + 1);
115-
build(mid + 1, j, 2 * k + 2);
116-
merge(k);
72+
long[][] tree = new long[size * 2][4];
73+
for (int i = 0; i < len; ++i) {
74+
tree[size + i][YY] = nums[i];
11775
}
118-
119-
// merge [2*k+1, 2*k+2] into k
120-
private void merge(int k) {
121-
seg[k].takeFirstSkipLast =
76+
for (int i = size - 1; i > 0; --i) {
77+
tree[i][YY] =
12278
Math.max(
123-
seg[2 * k + 1].takeFirstSkipLast + seg[2 * k + 2].skipLast(),
124-
seg[2 * k + 1].takeFirstTakeLast + seg[2 * k + 2].skipFirstSkipLast);
125-
126-
seg[k].takeFirstTakeLast =
79+
tree[2 * i][YY] + tree[2 * i + 1][NY],
80+
tree[2 * i][YN] + Math.max(tree[2 * i + 1][YY], tree[2 * i + 1][NY]));
81+
tree[i][YN] =
12782
Math.max(
128-
seg[2 * k + 1].takeFirstSkipLast + seg[2 * k + 2].takeLast(),
129-
seg[2 * k + 1].takeFirstTakeLast + seg[2 * k + 2].skipFirstTakeLast);
130-
131-
seg[k].skipFirstTakeLast =
83+
tree[2 * i][YY] + tree[2 * i + 1][NN],
84+
tree[2 * i][YN] + Math.max(tree[2 * i + 1][YN], tree[2 * i + 1][NN]));
85+
tree[i][NY] =
13286
Math.max(
133-
seg[2 * k + 1].skipFirstSkipLast + seg[2 * k + 2].takeLast(),
134-
seg[2 * k + 1].skipFirstTakeLast + seg[2 * k + 2].skipFirstTakeLast);
135-
136-
seg[k].skipFirstSkipLast =
87+
tree[2 * i][NY] + tree[2 * i + 1][NY],
88+
tree[2 * i][NN] + Math.max(tree[2 * i + 1][YY], tree[2 * i + 1][NY]));
89+
tree[i][NN] =
13790
Math.max(
138-
seg[2 * k + 1].skipFirstSkipLast + seg[2 * k + 2].skipLast(),
139-
seg[2 * k + 1].skipFirstTakeLast + seg[2 * k + 2].skipFirstSkipLast);
140-
}
141-
142-
// child -> parent
143-
public void update(int idx, int val) {
144-
int i = 0;
145-
int j = nums.length - 1;
146-
int k = 0;
147-
update(idx, val, k, i, j);
148-
}
149-
150-
private void update(int idx, int val, int k, int i, int j) {
151-
if (i == j) {
152-
seg[k].takeFirstTakeLast = val;
153-
return;
154-
}
155-
int mid = (i + j) >> 1;
156-
if (idx <= mid) {
157-
update(idx, val, 2 * k + 1, i, mid);
158-
} else {
159-
update(idx, val, 2 * k + 2, mid + 1, j);
160-
}
161-
merge(k);
91+
tree[2 * i][NY] + tree[2 * i + 1][NN],
92+
tree[2 * i][NN] + Math.max(tree[2 * i + 1][YN], tree[2 * i + 1][NN]));
16293
}
94+
return tree;
95+
}
16396

164-
public int getMax() {
165-
return seg[0].getMax();
97+
private static long set(long[][] tree, int idx, int val) {
98+
int size = tree.length / 2;
99+
tree[size + idx][YY] = val;
100+
for (int i = (size + idx) / 2; i > 0; i /= 2) {
101+
tree[i][YY] =
102+
Math.max(
103+
tree[2 * i][YY] + tree[2 * i + 1][NY],
104+
tree[2 * i][YN] + Math.max(tree[2 * i + 1][YY], tree[2 * i + 1][NY]));
105+
tree[i][YN] =
106+
Math.max(
107+
tree[2 * i][YY] + tree[2 * i + 1][NN],
108+
tree[2 * i][YN] + Math.max(tree[2 * i + 1][YN], tree[2 * i + 1][NN]));
109+
tree[i][NY] =
110+
Math.max(
111+
tree[2 * i][NY] + tree[2 * i + 1][NY],
112+
tree[2 * i][NN] + Math.max(tree[2 * i + 1][YY], tree[2 * i + 1][NY]));
113+
tree[i][NN] =
114+
Math.max(
115+
tree[2 * i][NY] + tree[2 * i + 1][NN],
116+
tree[2 * i][NN] + Math.max(tree[2 * i + 1][YN], tree[2 * i + 1][NN]));
166117
}
118+
return Math.max(tree[1][YY], Math.max(tree[1][YN], Math.max(tree[1][NY], tree[1][NN])));
167119
}
168120
}
169121
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3345\. Smallest Divisible Digit Product I
5+
6+
Easy
7+
8+
You are given two integers `n` and `t`. Return the **smallest** number greater than or equal to `n` such that the **product of its digits** is divisible by `t`.
9+
10+
**Example 1:**
11+
12+
**Input:** n = 10, t = 2
13+
14+
**Output:** 10
15+
16+
**Explanation:**
17+
18+
The digit product of 10 is 0, which is divisible by 2, making it the smallest number greater than or equal to 10 that satisfies the condition.
19+
20+
**Example 2:**
21+
22+
**Input:** n = 15, t = 3
23+
24+
**Output:** 16
25+
26+
**Explanation:**
27+
28+
The digit product of 16 is 6, which is divisible by 3, making it the smallest number greater than or equal to 15 that satisfies the condition.
29+
30+
**Constraints:**
31+
32+
* `1 <= n <= 100`
33+
* `1 <= t <= 10`
34+
35+
## Solution
36+
37+
```java
38+
public class Solution {
39+
public int smallestNumber(int n, int t) {
40+
int num = -1;
41+
int check = n;
42+
while (num == -1) {
43+
int product = findProduct(check);
44+
if (product % t == 0) {
45+
num = check;
46+
}
47+
check += 1;
48+
}
49+
return num;
50+
}
51+
52+
private int findProduct(int check) {
53+
int res = 1;
54+
while (check > 0) {
55+
res *= check % 10;
56+
check = check / 10;
57+
}
58+
return res;
59+
}
60+
}
61+
```

0 commit comments

Comments
 (0)