Skip to content

Commit d14f76a

Browse files
authored
Added tasks 2435, 2437, 2438.
1 parent 562b0e4 commit d14f76a

File tree

9 files changed

+318
-0
lines changed

9 files changed

+318
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g2401_2500.s2435_paths_in_matrix_whose_sum_is_divisible_by_k;
2+
3+
// #Hard #Array #Dynamic_Programming #Matrix #2022_12_07_Time_70_ms_(99.20%)_Space_84.5_MB_(87.93%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
private int[][] p = new int[50005][50];
9+
private int[][] r = new int[50005][50];
10+
11+
public int numberOfPaths(int[][] grid, int k) {
12+
int m = grid.length;
13+
int n = grid[0].length;
14+
int x = 0;
15+
for (int i = 0; i < n; ++i) {
16+
Arrays.fill(r[i], 0, k, 0);
17+
x += grid[0][i];
18+
r[i][x % k] = 1;
19+
}
20+
for (int i = 1; i < m; ++i) {
21+
int[][] t = p;
22+
p = r;
23+
r = t;
24+
x = grid[i][0] % k;
25+
for (int j = 0; j < k; ++j) {
26+
r[0][(x + j) % k] = p[0][j];
27+
}
28+
for (int j = 1, pj = 0; j < n; ++j, ++pj) {
29+
x = grid[i][j] % k;
30+
int y = (x > 0) ? (k - x) : 0;
31+
for (int l = 0; l < k; ++l, ++y) {
32+
if (y == k) {
33+
y = 0;
34+
}
35+
int modulo = 1000000007;
36+
r[j][l] = (p[j][y] + r[pj][y]) % modulo;
37+
}
38+
}
39+
}
40+
return r[n - 1][0];
41+
}
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2435\. Paths in Matrix Whose Sum Is Divisible by K
2+
3+
Hard
4+
5+
You are given a **0-indexed** `m x n` integer matrix `grid` and an integer `k`. You are currently at position `(0, 0)` and you want to reach position `(m - 1, n - 1)` moving only **down** or **right**.
6+
7+
Return _the number of paths where the sum of the elements on the path is divisible by_ `k`. Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2022/08/13/image-20220813183124-1.png)
12+
13+
**Input:** grid = [[5,2,4],[3,0,5],[0,7,2]], k = 3
14+
15+
**Output:** 2
16+
17+
**Explanation:** There are two paths where the sum of the elements on the path is divisible by k. The first path highlighted in red has a sum of 5 + 2 + 4 + 5 + 2 = 18 which is divisible by 3. The second path highlighted in blue has a sum of 5 + 3 + 0 + 5 + 2 = 15 which is divisible by 3.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2022/08/17/image-20220817112930-3.png)
22+
23+
**Input:** grid = [[0,0]], k = 5
24+
25+
**Output:** 1
26+
27+
**Explanation:** The path highlighted in red has a sum of 0 + 0 = 0 which is divisible by 5.
28+
29+
**Example 3:**
30+
31+
![](https://assets.leetcode.com/uploads/2022/08/12/image-20220812224605-3.png)
32+
33+
**Input:** grid = [[7,3,4,9],[2,3,6,2],[2,3,7,0]], k = 1
34+
35+
**Output:** 10
36+
37+
**Explanation:** Every integer is divisible by 1 so the sum of the elements on every possible path is divisible by k.
38+
39+
**Constraints:**
40+
41+
* `m == grid.length`
42+
* `n == grid[i].length`
43+
* <code>1 <= m, n <= 5 * 10<sup>4</sup></code>
44+
* <code>1 <= m * n <= 5 * 10<sup>4</sup></code>
45+
* `0 <= grid[i][j] <= 100`
46+
* `1 <= k <= 50`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2401_2500.s2437_number_of_valid_clock_times;
2+
3+
// #Easy #String #Enumeration #2022_12_07_Time_0_ms_(100.00%)_Space_42.2_MB_(29.58%)
4+
5+
public class Solution {
6+
public int countTime(String time) {
7+
int[] counts = new int[] {3, 10, 0, 6, 10};
8+
char[] ch = time.toCharArray();
9+
int result = 1;
10+
if (ch[0] == '2') {
11+
counts[1] = 4;
12+
}
13+
if ((ch[1] - '0') > 3) {
14+
counts[0] = 2;
15+
}
16+
if (ch[0] == '?' && ch[1] == '?') {
17+
counts[0] = 1;
18+
counts[1] = 24;
19+
}
20+
for (int i = 0; i < 5; i++) {
21+
char ch1 = ch[i];
22+
if (ch1 == '?') {
23+
result *= counts[i];
24+
}
25+
}
26+
return result;
27+
}
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2437\. Number of Valid Clock Times
2+
3+
Easy
4+
5+
You are given a string of length `5` called `time`, representing the current time on a digital clock in the format `"hh:mm"`. The **earliest** possible time is `"00:00"` and the **latest** possible time is `"23:59"`.
6+
7+
In the string `time`, the digits represented by the `?` symbol are **unknown**, and must be **replaced** with a digit from `0` to `9`.
8+
9+
Return _an integer_ `answer`_, the number of valid clock times that can be created by replacing every_ `?`_with a digit from_ `0` _to_ `9`.
10+
11+
**Example 1:**
12+
13+
**Input:** time = "?5:00"
14+
15+
**Output:** 2
16+
17+
**Explanation:** We can replace the ? with either a 0 or 1, producing "05:00" or "15:00". Note that we cannot replace it with a 2, since the time "25:00" is invalid. In total, we have two choices.
18+
19+
**Example 2:**
20+
21+
**Input:** time = "0?:0?"
22+
23+
**Output:** 100
24+
25+
**Explanation:** Each ? can be replaced by any digit from 0 to 9, so we have 100 total choices.
26+
27+
**Example 3:**
28+
29+
**Input:** time = "??:??"
30+
31+
**Output:** 1440
32+
33+
**Explanation:** There are 24 possible choices for the hours, and 60 possible choices for the minutes. In total, we have 24 \* 60 = 1440 choices.
34+
35+
**Constraints:**
36+
37+
* `time` is a valid string of length `5` in the format `"hh:mm"`.
38+
* `"00" <= hh <= "23"`
39+
* `"00" <= mm <= "59"`
40+
* Some of the digits might be replaced with `'?'` and need to be replaced with digits from `0` to `9`.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g2401_2500.s2438_range_product_queries_of_powers;
2+
3+
// #Medium #Array #Bit_Manipulation #Prefix_Sum
4+
// #2022_12_07_Time_73_ms_(65.07%)_Space_134.1_MB_(41.58%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Solution {
10+
public int[] productQueries(int n, int[][] queries) {
11+
int length = queries.length;
12+
final long mod = (long) (1e9 + 7);
13+
// convert n to binary form
14+
// take the set bit and find the corresponding 2^i
15+
// now answer for the query
16+
int[] powerTracker = new int[32];
17+
List<Long> productTakingPowers = new ArrayList<>();
18+
int[] result = new int[length];
19+
fillPowerTracker(powerTracker, n);
20+
fillProductTakingPowers(productTakingPowers, powerTracker);
21+
int index = 0;
22+
for (int[] query : queries) {
23+
int left = query[0];
24+
int right = query[1];
25+
long product = 1;
26+
for (int i = left; i <= right; i++) {
27+
product = (product * productTakingPowers.get(i)) % mod;
28+
}
29+
result[index++] = (int) (product % mod);
30+
}
31+
return result;
32+
}
33+
34+
private void fillPowerTracker(int[] powerTracker, int n) {
35+
int index = 0;
36+
while (n > 0) {
37+
powerTracker[index++] = n & 1;
38+
n >>= 1;
39+
}
40+
}
41+
42+
private void fillProductTakingPowers(List<Long> productTakingPowers, int[] powerTracker) {
43+
for (int i = 0; i < 32; i++) {
44+
if (powerTracker[i] == 1) {
45+
long power = (long) (Math.pow(2, i));
46+
productTakingPowers.add(power);
47+
}
48+
}
49+
}
50+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2438\. Range Product Queries of Powers
2+
3+
Medium
4+
5+
Given a positive integer `n`, there exists a **0-indexed** array called `powers`, composed of the **minimum** number of powers of `2` that sum to `n`. The array is sorted in **non-decreasing** order, and there is **only one** way to form the array.
6+
7+
You are also given a **0-indexed** 2D integer array `queries`, where <code>queries[i] = [left<sub>i</sub>, right<sub>i</sub>]</code>. Each `queries[i]` represents a query where you have to find the product of all `powers[j]` with <code>left<sub>i</sub> <= j <= right<sub>i</sub></code>.
8+
9+
Return _an array_ `answers`_, equal in length to_ `queries`_, where_ `answers[i]` _is the answer to the_ <code>i<sup>th</sup></code> _query_. Since the answer to the <code>i<sup>th</sup></code> query may be too large, each `answers[i]` should be returned **modulo** <code>10<sup>9</sup> + 7</code>.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 15, queries = [[0,1],[2,2],[0,3]]
14+
15+
**Output:** [2,4,64]
16+
17+
**Explanation:**
18+
19+
For n = 15, powers = [1,2,4,8]. It can be shown that powers cannot be a smaller size.
20+
21+
Answer to 1st query: powers[0] * powers[1] = 1 * 2 = 2.
22+
23+
Answer to 2nd query: powers[2] = 4.
24+
25+
Answer to 3rd query: powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64.
26+
27+
Each answer modulo 10<sup>9</sup> + 7 yields the same answer, so [2,4,64] is returned.
28+
29+
**Example 2:**
30+
31+
**Input:** n = 2, queries = [[0,0]]
32+
33+
**Output:** [2]
34+
35+
**Explanation:** For n = 2, powers = [2]. The answer to the only query is powers[0] = 2. The answer modulo 10<sup>9</sup> + 7 is the same, so [2] is returned.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= n <= 10<sup>9</sup></code>
40+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
41+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> < powers.length</code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2401_2500.s2435_paths_in_matrix_whose_sum_is_divisible_by_k;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void numberOfPaths() {
11+
assertThat(
12+
new Solution().numberOfPaths(new int[][] {{5, 2, 4}, {3, 0, 5}, {0, 7, 2}}, 3),
13+
equalTo(2));
14+
}
15+
16+
@Test
17+
void numberOfPairs2() {
18+
assertThat(new Solution().numberOfPaths(new int[][] {{0, 0}}, 5), equalTo(1));
19+
}
20+
21+
@Test
22+
void numberOfPairs3() {
23+
assertThat(
24+
new Solution()
25+
.numberOfPaths(new int[][] {{7, 3, 4, 9}, {2, 3, 6, 2}, {2, 3, 7, 0}}, 1),
26+
equalTo(10));
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2401_2500.s2437_number_of_valid_clock_times;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void countTime() {
11+
assertThat(new Solution().countTime("?5:00"), equalTo(2));
12+
}
13+
14+
@Test
15+
void countTime2() {
16+
assertThat(new Solution().countTime("0?:0?"), equalTo(100));
17+
}
18+
19+
@Test
20+
void countTime3() {
21+
assertThat(new Solution().countTime("??:??"), equalTo(1440));
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2401_2500.s2438_range_product_queries_of_powers;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void productQueries() {
11+
assertThat(
12+
new Solution().productQueries(15, new int[][] {{0, 1}, {2, 2}, {0, 3}}),
13+
equalTo(new int[] {2, 4, 64}));
14+
}
15+
16+
@Test
17+
void productQueries2() {
18+
assertThat(new Solution().productQueries(2, new int[][] {{0, 0}}), equalTo(new int[] {2}));
19+
}
20+
}

0 commit comments

Comments
 (0)