Skip to content

Commit db66b5d

Browse files
authored
Added tasks 3248-3261
1 parent e7a9312 commit db66b5d

File tree

14 files changed

+1205
-8
lines changed
  • src/main/java/g3201_3300
    • s3217_delete_nodes_from_linked_list_present_in_array
    • s3248_snake_in_matrix
    • s3249_count_the_number_of_good_nodes
    • s3250_find_the_count_of_monotonic_pairs_i
    • s3251_find_the_count_of_monotonic_pairs_ii
    • s3254_find_the_power_of_k_size_subarrays_i
    • s3255_find_the_power_of_k_size_subarrays_ii
    • s3256_maximum_value_sum_by_placing_three_rooks_i
    • s3257_maximum_value_sum_by_placing_three_rooks_ii
    • s3258_count_substrings_that_satisfy_k_constraint_i
    • s3259_maximum_energy_boost_from_two_drinks
    • s3260_find_the_largest_palindrome_divisible_by_k
    • s3261_count_substrings_that_satisfy_k_constraint_ii

14 files changed

+1205
-8
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,15 +1816,27 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3261 |[Count Substrings That Satisfy K-Constraint II](src/main/java/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii)| Hard | Array, String, Binary_Search, Prefix_Sum, Sliding_Window | 8 | 100.00
1820+
| 3260 |[Find the Largest Palindrome Divisible by K](src/main/java/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k)| Hard | String, Dynamic_Programming, Math, Greedy, Number_Theory | 4 | 97.21
1821+
| 3259 |[Maximum Energy Boost From Two Drinks](src/main/java/g3201_3300/s3259_maximum_energy_boost_from_two_drinks)| Medium | Array, Dynamic_Programming | 3 | 100.00
1822+
| 3258 |[Count Substrings That Satisfy K-Constraint I](src/main/java/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i)| Easy | String, Sliding_Window | 1 | 100.00
1823+
| 3257 |[Maximum Value Sum by Placing Three Rooks II](src/main/java/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii)| Hard | Array, Dynamic_Programming, Matrix, Enumeration | 18 | 99.59
1824+
| 3256 |[Maximum Value Sum by Placing Three Rooks I](src/main/java/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i)| Hard | Array, Dynamic_Programming, Matrix, Enumeration | 7 | 100.00
1825+
| 3255 |[Find the Power of K-Size Subarrays II](src/main/java/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii)| Medium | Array, Sliding_Window | 3 | 99.24
1826+
| 3254 |[Find the Power of K-Size Subarrays I](src/main/java/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i)| Medium | Array, Sliding_Window | 1 | 100.00
1827+
| 3251 |[Find the Count of Monotonic Pairs II](src/main/java/g3201_3300/s3251_find_the_count_of_monotonic_pairs_ii)| Hard | Array, Dynamic_Programming, Math, Prefix_Sum, Combinatorics | 24 | 100.00
1828+
| 3250 |[Find the Count of Monotonic Pairs I](src/main/java/g3201_3300/s3250_find_the_count_of_monotonic_pairs_i)| Hard | Array, Dynamic_Programming, Math, Prefix_Sum, Combinatorics | 3 | 100.00
1829+
| 3249 |[Count the Number of Good Nodes](src/main/java/g3201_3300/s3249_count_the_number_of_good_nodes)| Medium | Depth_First_Search, Tree | 34 | 100.00
1830+
| 3248 |[Snake in Matrix](src/main/java/g3201_3300/s3248_snake_in_matrix)| Easy | Array, String, Simulation | 2 | 98.05
18191831
| 3245 |[Alternating Groups III](src/main/java/g3201_3300/s3245_alternating_groups_iii)| Hard | Array, Binary_Indexed_Tree | 36 | 82.22
18201832
| 3244 |[Shortest Distance After Road Addition Queries II](src/main/java/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii)| Hard | Array, Greedy, Graph, Ordered_Set | 5 | 97.43
1821-
| 3243 |[Shortest Distance After Road Addition Queries I](src/main/java/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i)| Medium | Array, Graph, Breadth_First_Search | 6 | 100.00
1833+
| 3243 |[Shortest Distance After Road Addition Queries I](src/main/java/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i)| Medium | Array, Breadth_First_Search, Graph | 6 | 100.00
18221834
| 3242 |[Design Neighbor Sum Service](src/main/java/g3201_3300/s3242_design_neighbor_sum_service)| Easy | Array, Hash_Table, Matrix, Design, Simulation | 14 | 100.00
1823-
| 3241 |[Time Taken to Mark All Nodes](src/main/java/g3201_3300/s3241_time_taken_to_mark_all_nodes)| Hard | Dynamic_Programming, Tree, Graph, Depth_First_Search | 39 | 100.00
1835+
| 3241 |[Time Taken to Mark All Nodes](src/main/java/g3201_3300/s3241_time_taken_to_mark_all_nodes)| Hard | Dynamic_Programming, Depth_First_Search, Tree, Graph | 39 | 100.00
18241836
| 3240 |[Minimum Number of Flips to Make Binary Grid Palindromic II](src/main/java/g3201_3300/s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii)| Medium | Array, Matrix, Two_Pointers | 3 | 96.90
18251837
| 3239 |[Minimum Number of Flips to Make Binary Grid Palindromic I](src/main/java/g3201_3300/s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i)| Medium | Array, Matrix, Two_Pointers | 3 | 100.00
18261838
| 3238 |[Find the Number of Winning Players](src/main/java/g3201_3300/s3238_find_the_number_of_winning_players)| Easy | Array, Hash_Table, Counting | 1 | 100.00
1827-
| 3235 |[Check if the Rectangle Corner Is Reachable](src/main/java/g3201_3300/s3235_check_if_the_rectangle_corner_is_reachable)| Hard | Array, Math, Union_Find, Geometry, Depth_First_Search, Breadth_First_Search | 95 | 59.46
1839+
| 3235 |[Check if the Rectangle Corner Is Reachable](src/main/java/g3201_3300/s3235_check_if_the_rectangle_corner_is_reachable)| Hard | Array, Math, Depth_First_Search, Breadth_First_Search, Union_Find, Geometry | 95 | 59.46
18281840
| 3234 |[Count the Number of Substrings With Dominant Ones](src/main/java/g3201_3300/s3234_count_the_number_of_substrings_with_dominant_ones)| Medium | String, Sliding_Window, Enumeration | 163 | 93.90
18291841
| 3233 |[Find the Count of Numbers Which Are Not Special](src/main/java/g3201_3300/s3233_find_the_count_of_numbers_which_are_not_special)| Medium | Array, Math, Number_Theory | 20 | 70.12
18301842
| 3232 |[Find if Digit Game Can Be Won](src/main/java/g3201_3300/s3232_find_if_digit_game_can_be_won)| Easy | Array, Math | 0 | 100.00

src/main/java/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/readme.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@ No node has value 5.
5757
```java
5858
import com_github_leetcode.ListNode;
5959

60-
61-
/**
62-
* Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode() {}
63-
* ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val;
64-
* this.next = next; } }
60+
/*
61+
* Definition for singly-linked list.
62+
* public class ListNode {
63+
* int val;
64+
* ListNode next;
65+
* ListNode() {}
66+
* ListNode(int val) { this.val = val; }
67+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
68+
* }
6569
*/
6670
public class Solution {
6771
public ListNode modifiedList(int[] nums, ListNode head) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
## 3248\. Snake in Matrix
5+
6+
Easy
7+
8+
There is a snake in an `n x n` matrix `grid` and can move in **four possible directions**. Each cell in the `grid` is identified by the position: `grid[i][j] = (i * n) + j`.
9+
10+
The snake starts at cell 0 and follows a sequence of commands.
11+
12+
You are given an integer `n` representing the size of the `grid` and an array of strings `commands` where each `command[i]` is either `"UP"`, `"RIGHT"`, `"DOWN"`, and `"LEFT"`. It's guaranteed that the snake will remain within the `grid` boundaries throughout its movement.
13+
14+
Return the position of the final cell where the snake ends up after executing `commands`.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 2, commands = ["RIGHT","DOWN"]
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
![image](image01.png)
25+
26+
**Example 2:**
27+
28+
**Input:** n = 3, commands = ["DOWN","RIGHT","UP"]
29+
30+
**Output:** 1
31+
32+
**Explanation:**
33+
34+
![image](image02.png)
35+
36+
**Constraints:**
37+
38+
* `2 <= n <= 10`
39+
* `1 <= commands.length <= 100`
40+
* `commands` consists only of `"UP"`, `"RIGHT"`, `"DOWN"`, and `"LEFT"`.
41+
* The input is generated such the snake will not move outside of the boundaries.
42+
43+
## Solution
44+
45+
```java
46+
import java.util.List;
47+
48+
public class Solution {
49+
public int finalPositionOfSnake(int n, List<String> commands) {
50+
int x = 0;
51+
int y = 0;
52+
for (String command : commands) {
53+
switch (command) {
54+
case "UP":
55+
if (x > 0) {
56+
x--;
57+
}
58+
break;
59+
case "DOWN":
60+
if (x < n - 1) {
61+
x++;
62+
}
63+
break;
64+
case "LEFT":
65+
if (y > 0) {
66+
y--;
67+
}
68+
break;
69+
case "RIGHT":
70+
if (y < n - 1) {
71+
y++;
72+
}
73+
break;
74+
default:
75+
break;
76+
}
77+
}
78+
return (x * n) + y;
79+
}
80+
}
81+
```
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
## 3249\. Count the Number of Good Nodes
5+
6+
Medium
7+
8+
There is an **undirected** tree with `n` nodes labeled from `0` to `n - 1`, and rooted at node `0`. You are given a 2D integer array `edges` of length `n - 1`, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.
9+
10+
A node is **good** if all the subtrees rooted at its children have the same size.
11+
12+
Return the number of **good** nodes in the given tree.
13+
14+
A **subtree** of `treeName` is a tree consisting of a node in `treeName` and all of its descendants.
15+
16+
**Example 1:**
17+
18+
**Input:** edges = \[\[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
19+
20+
**Output:** 7
21+
22+
**Explanation:**
23+
24+
![](https://assets.leetcode.com/uploads/2024/05/26/tree1.png)
25+
26+
All of the nodes of the given tree are good.
27+
28+
**Example 2:**
29+
30+
**Input:** edges = \[\[0,1],[1,2],[2,3],[3,4],[0,5],[1,6],[2,7],[3,8]]
31+
32+
**Output:** 6
33+
34+
**Explanation:**
35+
36+
![](https://assets.leetcode.com/uploads/2024/06/03/screenshot-2024-06-03-193552.png)
37+
38+
There are 6 good nodes in the given tree. They are colored in the image above.
39+
40+
**Example 3:**
41+
42+
**Input:** edges = \[\[0,1],[1,2],[1,3],[1,4],[0,5],[5,6],[6,7],[7,8],[0,9],[9,10],[9,12],[10,11]]
43+
44+
**Output:** 12
45+
46+
**Explanation:**
47+
48+
![](https://assets.leetcode.com/uploads/2024/08/08/rob.jpg)
49+
50+
All nodes except node 9 are good.
51+
52+
**Constraints:**
53+
54+
* <code>2 <= n <= 10<sup>5</sup></code>
55+
* `edges.length == n - 1`
56+
* `edges[i].length == 2`
57+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
58+
* The input is generated such that `edges` represents a valid tree.
59+
60+
## Solution
61+
62+
```java
63+
import java.util.ArrayList;
64+
import java.util.List;
65+
66+
public class Solution {
67+
private int count = 0;
68+
69+
public int countGoodNodes(int[][] edges) {
70+
int n = edges.length + 1;
71+
TNode[] nodes = new TNode[n];
72+
nodes[0] = new TNode(0);
73+
for (int[] edge : edges) {
74+
int a = edge[0];
75+
int b = edge[1];
76+
if (nodes[b] != null && nodes[a] == null) {
77+
nodes[a] = new TNode(a);
78+
nodes[b].children.add(nodes[a]);
79+
} else {
80+
if (nodes[a] == null) {
81+
nodes[a] = new TNode(a);
82+
}
83+
if (nodes[b] == null) {
84+
nodes[b] = new TNode(b);
85+
}
86+
nodes[a].children.add(nodes[b]);
87+
}
88+
}
89+
sizeOfTree(nodes[0]);
90+
return count;
91+
}
92+
93+
private int sizeOfTree(TNode node) {
94+
if (node.size > 0) {
95+
return node.size;
96+
}
97+
List<TNode> children = node.children;
98+
if (children.isEmpty()) {
99+
count++;
100+
node.size = 1;
101+
return 1;
102+
}
103+
int size = sizeOfTree(children.get(0));
104+
int sum = size;
105+
boolean goodNode = true;
106+
for (int i = 1; i < children.size(); ++i) {
107+
TNode child = children.get(i);
108+
if (size != sizeOfTree(child)) {
109+
goodNode = false;
110+
}
111+
sum += sizeOfTree(child);
112+
}
113+
if (goodNode) {
114+
count++;
115+
}
116+
sum++;
117+
node.size = sum;
118+
return sum;
119+
}
120+
121+
private static class TNode {
122+
int val;
123+
int size;
124+
List<TNode> children;
125+
126+
TNode(int val) {
127+
this.val = val;
128+
this.size = -1;
129+
this.children = new ArrayList<>();
130+
}
131+
}
132+
}
133+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
## 3250\. Find the Count of Monotonic Pairs I
5+
6+
Hard
7+
8+
You are given an array of **positive** integers `nums` of length `n`.
9+
10+
We call a pair of **non-negative** integer arrays `(arr1, arr2)` **monotonic** if:
11+
12+
* The lengths of both arrays are `n`.
13+
* `arr1` is monotonically **non-decreasing**, in other words, `arr1[0] <= arr1[1] <= ... <= arr1[n - 1]`.
14+
* `arr2` is monotonically **non-increasing**, in other words, `arr2[0] >= arr2[1] >= ... >= arr2[n - 1]`.
15+
* `arr1[i] + arr2[i] == nums[i]` for all `0 <= i <= n - 1`.
16+
17+
Return the count of **monotonic** pairs.
18+
19+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
20+
21+
**Example 1:**
22+
23+
**Input:** nums = [2,3,2]
24+
25+
**Output:** 4
26+
27+
**Explanation:**
28+
29+
The good pairs are:
30+
31+
1. `([0, 1, 1], [2, 2, 1])`
32+
2. `([0, 1, 2], [2, 2, 0])`
33+
3. `([0, 2, 2], [2, 1, 0])`
34+
4. `([1, 2, 2], [1, 1, 0])`
35+
36+
**Example 2:**
37+
38+
**Input:** nums = [5,5,5,5]
39+
40+
**Output:** 126
41+
42+
**Constraints:**
43+
44+
* `1 <= n == nums.length <= 2000`
45+
* `1 <= nums[i] <= 50`
46+
47+
## Solution
48+
49+
```java
50+
public class Solution {
51+
public int countOfPairs(int[] nums) {
52+
int[] maxShift = new int[nums.length];
53+
maxShift[0] = nums[0];
54+
int currShift = 0;
55+
for (int i = 1; i < nums.length; i++) {
56+
currShift = Math.max(currShift, nums[i] - maxShift[i - 1]);
57+
maxShift[i] = Math.min(maxShift[i - 1], nums[i] - currShift);
58+
if (maxShift[i] < 0) {
59+
return 0;
60+
}
61+
}
62+
int[][] cases = getAllCases(nums, maxShift);
63+
return cases[nums.length - 1][maxShift[nums.length - 1]];
64+
}
65+
66+
private int[][] getAllCases(int[] nums, int[] maxShift) {
67+
int[] currCases;
68+
int[][] cases = new int[nums.length][];
69+
cases[0] = new int[maxShift[0] + 1];
70+
for (int i = 0; i < cases[0].length; i++) {
71+
cases[0][i] = i + 1;
72+
}
73+
for (int i = 1; i < nums.length; i++) {
74+
currCases = new int[maxShift[i] + 1];
75+
currCases[0] = 1;
76+
for (int j = 1; j < currCases.length; j++) {
77+
int prevCases =
78+
j < cases[i - 1].length
79+
? cases[i - 1][j]
80+
: cases[i - 1][cases[i - 1].length - 1];
81+
currCases[j] = (currCases[j - 1] + prevCases) % (1_000_000_000 + 7);
82+
}
83+
cases[i] = currCases;
84+
}
85+
return cases;
86+
}
87+
}
88+
```

0 commit comments

Comments
 (0)