Skip to content

Commit 5708432

Browse files
authored
Added tasks 2477, 2478, 2481, 2482, 2483
1 parent df6cca6 commit 5708432

File tree

16 files changed

+578
-0
lines changed

16 files changed

+578
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,11 @@ implementation 'com.github.javadev:leetcode-in-java:1.17'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2483 |[Minimum Penalty for a Shop](src/main/java/g2401_2500/s2483_minimum_penalty_for_a_shop/Solution.java)| Medium | String, Prefix_Sum | 17 | 69.62
1852+
| 2482 |[Difference Between Ones and Zeros in Row and Column](src/main/java/g2401_2500/s2482_difference_between_ones_and_zeros_in_row_and_column/Solution.java)| Medium | Array, Matrix, Simulation | 9 | 94.05
1853+
| 2481 |[Minimum Cuts to Divide a Circle](src/main/java/g2401_2500/s2481_minimum_cuts_to_divide_a_circle/Solution.java)| Easy | Math, Geometry | 0 | 100.00
1854+
| 2478 |[Number of Beautiful Partitions](src/main/java/g2401_2500/s2478_number_of_beautiful_partitions/Solution.java)| Hard | String, Dynamic_Programming | 44 | 90.08
1855+
| 2477 |[Minimum Fuel Cost to Report to the Capital](src/main/java/g2401_2500/s2477_minimum_fuel_cost_to_report_to_the_capital/Solution.java)| Medium | Tree, Graph, Breadth_First_Search, Depth_First_Search | 70 | 72.49
18511856
| 2476 |[Closest Nodes Queries in a Binary Search Tree](src/main/java/g2401_2500/s2476_closest_nodes_queries_in_a_binary_search_tree/Solution.java)| Medium | Array, Tree, Binary_Search, Binary_Tree, Depth_First_Search | 185 | 51.92
18521857
| 2475 |[Number of Unequal Triplets in Array](src/main/java/g2401_2500/s2475_number_of_unequal_triplets_in_array/Solution.java)| Easy | Array, Hash_Table | 1 | 97.49
18531858
| 2472 |[Maximum Number of Non-overlapping Palindrome Substrings](src/main/java/g2401_2500/s2472_maximum_number_of_non_overlapping_palindrome_substrings/Solution.java)| Hard | String, Dynamic_Programming | 2 | 91.04
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g2401_2500.s2477_minimum_fuel_cost_to_report_to_the_capital;
2+
3+
// #Medium #Tree #Graph #Breadth_First_Search #Depth_First_Search
4+
// #2023_01_25_Time_70_ms_(72.49%)_Space_102.5_MB_(74.76%)
5+
6+
import java.util.ArrayList;
7+
8+
public class Solution {
9+
private long ans = 0L;
10+
11+
public long minimumFuelCost(int[][] roads, int seats) {
12+
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
13+
int n = roads.length + 1;
14+
ans = 0L;
15+
for (int i = 0; i < n; i++) {
16+
adj.add(new ArrayList<>());
17+
}
18+
for (int[] a : roads) {
19+
adj.get(a[0]).add(a[1]);
20+
adj.get(a[1]).add(a[0]);
21+
}
22+
solve(adj, seats, 0, -1);
23+
return ans;
24+
}
25+
26+
private long solve(ArrayList<ArrayList<Integer>> adj, int seats, int src, int parent) {
27+
int people = 1;
28+
for (int i : adj.get(src)) {
29+
if (i != parent) {
30+
people += solve(adj, seats, i, src);
31+
}
32+
}
33+
if (src != 0) {
34+
ans += (long) Math.ceil((double) people / seats);
35+
}
36+
return people;
37+
}
38+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2477\. Minimum Fuel Cost to Report to the Capital
2+
3+
Medium
4+
5+
There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of `n` cities numbered from `0` to `n - 1` and exactly `n - 1` roads. The capital city is city `0`. You are given a 2D integer array `roads` where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a **bidirectional road** connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.
6+
7+
There is a meeting for the representatives of each city. The meeting is in the capital city.
8+
9+
There is a car in each city. You are given an integer `seats` that indicates the number of seats in each car.
10+
11+
A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.
12+
13+
Return _the minimum number of liters of fuel to reach the capital city_.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png)
18+
19+
**Input:** roads = [[0,1],[0,2],[0,3]], seats = 5
20+
21+
**Output:** 3
22+
23+
**Explanation:**
24+
- Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel.
25+
- Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel.
26+
- Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel.
27+
28+
It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed.
29+
30+
**Example 2:**
31+
32+
![](https://assets.leetcode.com/uploads/2022/11/16/2.png)
33+
34+
**Input:** roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2
35+
36+
**Output:** 7
37+
38+
**Explanation:**
39+
- Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel.
40+
- Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel.
41+
- Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel.
42+
- Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel.
43+
- Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel.
44+
- Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel.
45+
- Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel.
46+
47+
It costs 7 liters of fuel at minimum.
48+
It can be proven that 7 is the minimum number of liters of fuel needed.
49+
50+
**Example 3:**
51+
52+
![](https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png)
53+
54+
**Input:** roads = [], seats = 1
55+
56+
**Output:** 0
57+
58+
**Explanation:** No representatives need to travel to the capital city.
59+
60+
**Constraints:**
61+
62+
* <code>1 <= n <= 10<sup>5</sup></code>
63+
* `roads.length == n - 1`
64+
* `roads[i].length == 2`
65+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
66+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
67+
* `roads` represents a valid tree.
68+
* <code>1 <= seats <= 10<sup>5</sup></code>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g2401_2500.s2478_number_of_beautiful_partitions;
2+
3+
// #Hard #String #Dynamic_Programming #2023_01_25_Time_44_ms_(90.08%)_Space_55.1_MB_(32.23%)
4+
5+
@SuppressWarnings("java:S135")
6+
public class Solution {
7+
public int beautifulPartitions(String s, int k, int l) {
8+
char[] cs = s.toCharArray();
9+
int n = cs.length;
10+
if (!prime(cs[0]) || prime(cs[n - 1])) {
11+
return 0;
12+
}
13+
int[][] dp = new int[k][n];
14+
for (int i = n - l; 0 <= i; --i) {
15+
dp[0][i] = prime(cs[i]) ? 1 : 0;
16+
}
17+
for (int i = 1; i < k; ++i) {
18+
int sum = 0;
19+
for (int j = n - i * l; 0 <= j; --j) {
20+
int mod = (int) 1e9 + 7;
21+
if (0 == dp[i - 1][j]) {
22+
dp[i - 1][j] = sum;
23+
} else if (0 != j && 0 == dp[i - 1][j - 1]) {
24+
sum = (sum + dp[i - 1][j]) % mod;
25+
}
26+
}
27+
int p = l - 1;
28+
for (int j = 0; j + l * i < n; ++j) {
29+
if (!prime(cs[j])) {
30+
continue;
31+
}
32+
p = Math.max(p, j + l - 1);
33+
while (prime(cs[p])) {
34+
p++;
35+
}
36+
if (0 == dp[i - 1][p]) {
37+
break;
38+
}
39+
dp[i][j] = dp[i - 1][p];
40+
}
41+
}
42+
return dp[k - 1][0];
43+
}
44+
45+
private boolean prime(char c) {
46+
return '2' == c || '3' == c || '5' == c || '7' == c;
47+
}
48+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2478\. Number of Beautiful Partitions
2+
3+
Hard
4+
5+
You are given a string `s` that consists of the digits `'1'` to `'9'` and two integers `k` and `minLength`.
6+
7+
A partition of `s` is called **beautiful** if:
8+
9+
* `s` is partitioned into `k` non-intersecting substrings.
10+
* Each substring has a length of **at least** `minLength`.
11+
* Each substring starts with a **prime** digit and ends with a **non-prime** digit. Prime digits are `'2'`, `'3'`, `'5'`, and `'7'`, and the rest of the digits are non-prime.
12+
13+
Return _the number of **beautiful** partitions of_ `s`. Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
14+
15+
A **substring** is a contiguous sequence of characters within a string.
16+
17+
**Example 1:**
18+
19+
**Input:** s = "23542185131", k = 3, minLength = 2
20+
21+
**Output:** 3
22+
23+
**Explanation:** There exists three ways to create a beautiful partition:
24+
25+
"2354 | 218 | 5131"
26+
27+
"2354 | 21851 | 31"
28+
29+
"2354218 | 51 | 31"
30+
31+
**Example 2:**
32+
33+
**Input:** s = "23542185131", k = 3, minLength = 3
34+
35+
**Output:** 1
36+
37+
**Explanation:** There exists one way to create a beautiful partition: "2354 | 218 | 5131".
38+
39+
**Example 3:**
40+
41+
**Input:** s = "3312958", k = 3, minLength = 1
42+
43+
**Output:** 1
44+
45+
**Explanation:** There exists one way to create a beautiful partition: "331 | 29 | 58".
46+
47+
**Constraints:**
48+
49+
* `1 <= k, minLength <= s.length <= 1000`
50+
* `s` consists of the digits `'1'` to `'9'`.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package g2401_2500.s2481_minimum_cuts_to_divide_a_circle;
2+
3+
// #Easy #Math #Geometry #2023_01_25_Time_0_ms_(100.00%)_Space_39.2_MB_(87.50%)
4+
5+
public class Solution {
6+
public int numberOfCuts(int n) {
7+
if (n == 1) {
8+
return 0;
9+
}
10+
return n % 2 > 0 ? n : n / 2;
11+
}
12+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2481\. Minimum Cuts to Divide a Circle
2+
3+
Easy
4+
5+
A **valid cut** in a circle can be:
6+
7+
* A cut that is represented by a straight line that touches two points on the edge of the circle and passes through its center, or
8+
* A cut that is represented by a straight line that touches one point on the edge of the circle and its center.
9+
10+
Some valid and invalid cuts are shown in the figures below.
11+
12+
![](https://assets.leetcode.com/uploads/2022/10/29/alldrawio.png)
13+
14+
Given the integer `n`, return _the **minimum** number of cuts needed to divide a circle into_ `n` _equal slices_.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2022/10/24/11drawio.png)
19+
20+
**Input:** n = 4
21+
22+
**Output:** 2
23+
24+
**Explanation:** The above figure shows how cutting the circle twice through the middle divides it into 4 equal slices.
25+
26+
**Example 2:**
27+
28+
![](https://assets.leetcode.com/uploads/2022/10/24/22drawio.png)
29+
30+
**Input:** n = 3
31+
32+
**Output:** 3
33+
34+
**Explanation:**
35+
36+
At least 3 cuts are needed to divide the circle into 3 equal slices.
37+
38+
It can be shown that less than 3 cuts cannot result in 3 slices of equal size and shape.
39+
40+
Also note that the first cut will not divide the circle into distinct parts.
41+
42+
**Constraints:**
43+
44+
* `1 <= n <= 100`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g2401_2500.s2482_difference_between_ones_and_zeros_in_row_and_column;
2+
3+
// #Medium #Array #Matrix #Simulation #2023_01_25_Time_9_ms_(94.05%)_Space_76.3_MB_(95.36%)
4+
5+
public class Solution {
6+
public int[][] onesMinusZeros(int[][] grid) {
7+
int[] rowOne = new int[grid.length];
8+
int[] colOne = new int[grid[0].length];
9+
int m = grid.length;
10+
int n = grid[0].length;
11+
for (int i = 0; i < m; i++) {
12+
int c = 0;
13+
for (int j = 0; j < n; j++) {
14+
if (grid[i][j] == 1) {
15+
c++;
16+
}
17+
}
18+
rowOne[i] = c;
19+
}
20+
for (int i = 0; i < n; i++) {
21+
int c = 0;
22+
for (int[] ints : grid) {
23+
if (ints[i] == 1) {
24+
c++;
25+
}
26+
}
27+
colOne[i] = c;
28+
}
29+
for (int i = 0; i < m; i++) {
30+
for (int j = 0; j < n; j++) {
31+
grid[i][j] = rowOne[i] + colOne[j] - (m - rowOne[i]) - (n - colOne[j]);
32+
}
33+
}
34+
return grid;
35+
}
36+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2482\. Difference Between Ones and Zeros in Row and Column
2+
3+
Medium
4+
5+
You are given a **0-indexed** `m x n` binary matrix `grid`.
6+
7+
A **0-indexed** `m x n` difference matrix `diff` is created with the following procedure:
8+
9+
* Let the number of ones in the <code>i<sup>th</sup></code> row be <code>onesRow<sub>i</sub></code>.
10+
* Let the number of ones in the <code>j<sup>th</sup></code> column be <code>onesCol<sub>j</sub></code>.
11+
* Let the number of zeros in the <code>i<sup>th</sup></code> row be <code>zerosRow<sub>i</sub></code>.
12+
* Let the number of zeros in the <code>j<sup>th</sup></code> column be <code>zerosCol<sub>j</sub></code>.
13+
* <code>diff[i][j] = onesRow<sub>i</sub> + onesCol<sub>j</sub> - zerosRow<sub>i</sub> - zerosCol<sub>j</sub></code>
14+
15+
Return _the difference matrix_ `diff`.
16+
17+
**Example 1:**
18+
19+
![](https://assets.leetcode.com/uploads/2022/11/06/image-20221106171729-5.png)
20+
21+
**Input:** grid = [[0,1,1],[1,0,1],[0,0,1]]
22+
23+
**Output:** [[0,0,4],[0,0,4],[-2,-2,2]]
24+
25+
**Explanation:**
26+
- diff[0][0] = <code>onesRow<sub>0</sub> + onesCol<sub>0</sub> - zerosRow<sub>0</sub> - zerosCol<sub>0</sub></code> = 2 + 1 - 1 - 2 = 0
27+
- diff[0][1] = <code>onesRow<sub>0</sub> + onesCol<sub>1</sub> - zerosRow<sub>0</sub> - zerosCol<sub>1</sub></code> = 2 + 1 - 1 - 2 = 0
28+
- diff[0][2] = <code>onesRow<sub>0</sub> + onesCol<sub>2</sub> - zerosRow<sub>0</sub> - zerosCol<sub>2</sub></code> = 2 + 3 - 1 - 0 = 4
29+
- diff[1][0] = <code>onesRow<sub>1</sub> + onesCol<sub>0</sub> - zerosRow<sub>1</sub> - zerosCol<sub>0</sub></code> = 2 + 1 - 1 - 2 = 0
30+
- diff[1][1] = <code>onesRow<sub>1</sub> + onesCol<sub>1</sub> - zerosRow<sub>1</sub> - zerosCol<sub>1</sub></code> = 2 + 1 - 1 - 2 = 0
31+
- diff[1][2] = <code>onesRow<sub>1</sub> + onesCol<sub>2</sub> - zerosRow<sub>1</sub> - zerosCol<sub>2</sub></code> = 2 + 3 - 1 - 0 = 4
32+
- diff[2][0] = <code>onesRow<sub>2</sub> + onesCol<sub>0</sub> - zerosRow<sub>2</sub> - zerosCol<sub>0</sub></code> = 1 + 1 - 2 - 2 = -2
33+
- diff[2][1] = <code>onesRow<sub>2</sub> + onesCol<sub>1</sub> - zerosRow<sub>2</sub> - zerosCol<sub>1</sub></code> = 1 + 1 - 2 - 2 = -2
34+
- diff[2][2] = <code>onesRow<sub>2</sub> + onesCol<sub>2</sub> - zerosRow<sub>2</sub> - zerosCol<sub>2</sub></code> = 1 + 3 - 2 - 0 = 2
35+
36+
**Example 2:**
37+
38+
![](https://assets.leetcode.com/uploads/2022/11/06/image-20221106171747-6.png)
39+
40+
**Input:** grid = [[1,1,1],[1,1,1]]
41+
42+
**Output:** [[5,5,5],[5,5,5]]
43+
44+
**Explanation:**
45+
- diff[0][0] = onesRow<sub>0</sub> + onesCol<sub>0</sub> - zerosRow<sub>0</sub> - zerosCol<sub>0</sub> = 3 + 2 - 0 - 0 = 5
46+
- diff[0][1] = onesRow<sub>0</sub> + onesCol<sub>1</sub> - zerosRow<sub>0</sub> - zerosCol<sub>1</sub> = 3 + 2 - 0 - 0 = 5
47+
- diff[0][2] = onesRow<sub>0</sub> + onesCol<sub>2</sub> - zerosRow<sub>0</sub> - zerosCol<sub>2</sub> = 3 + 2 - 0 - 0 = 5
48+
- diff[1][0] = onesRow<sub>1</sub> + onesCol<sub>0</sub> - zerosRow<sub>1</sub> - zerosCol<sub>0</sub> = 3 + 2 - 0 - 0 = 5
49+
- diff[1][1] = onesRow<sub>1</sub> + onesCol<sub>1</sub> - zerosRow<sub>1</sub> - zerosCol<sub>1</sub> = 3 + 2 - 0 - 0 = 5
50+
- diff[1][2] = onesRow<sub>1</sub> + onesCol<sub>2</sub> - zerosRow<sub>1</sub> - zerosCol<sub>2</sub> = 3 + 2 - 0 - 0 = 5
51+
52+
**Constraints:**
53+
54+
* `m == grid.length`
55+
* `n == grid[i].length`
56+
* <code>1 <= m, n <= 10<sup>5</sup></code>
57+
* <code>1 <= m * n <= 10<sup>5</sup></code>
58+
* `grid[i][j]` is either `0` or `1`.

0 commit comments

Comments
 (0)