Skip to content

Commit 622f2e2

Browse files
authored
Added tasks 2176-2197.
1 parent e0a666f commit 622f2e2

File tree

12 files changed

+884
-38
lines changed
  • src/main/java
    • g0001_0100/s0056_merge_intervals
    • g2101_2200
      • s2176_count_equal_and_divisible_pairs_in_an_array
      • s2177_find_three_consecutive_integers_that_sum_to_a_given_number
      • s2188_minimum_time_to_finish_the_race
      • s2190_most_frequent_number_following_key_in_an_array
      • s2191_sort_the_jumbled_numbers
      • s2192_all_ancestors_of_a_node_in_a_directed_acyclic_graph
      • s2194_cells_in_a_range_on_an_excel_sheet
      • s2195_append_k_integers_with_minimal_sum
      • s2196_create_binary_tree_from_descriptions
      • s2197_replace_non_coprime_numbers_in_array

12 files changed

+884
-38
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
121121
|-|-|-|-|-|-
122122
| 0075 |[Sort Colors](src/main/java/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 1 | 33.23
123-
| 0056 |[Merge Intervals](src/main/java/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting | 22 | 12.57
123+
| 0056 |[Merge Intervals](src/main/java/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting | 7 | 97.44
124124
| 0706 |[Design HashMap](src/main/java/g0701_0800/s0706_design_hashmap)| Easy | Array, Hash_Table, Design, Linked_List, Hash_Function | 13 | 95.71
125125

126126
#### Day 3 Array
@@ -1333,9 +1333,19 @@
13331333
|------|----------------|-------------|-------------|----------|--------
13341334
| 2246 |[Longest Path With Different Adjacent Characters](src/main/java/g2201_2300/s2246_longest_path_with_different_adjacent_characters)| Hard | Array, String, Depth_First_Search, Tree, Graph, Topological_Sort | 75 | 97.79
13351335
| 2200 |[Find All K-Distant Indices in an Array](src/main/java/g2101_2200/s2200_find_all_k_distant_indices_in_an_array)| Easy | Array | 2 | 95.30
1336+
| 2197 |[Replace Non-Coprime Numbers in Array](src/main/java/g2101_2200/s2197_replace_non_coprime_numbers_in_array)| Hard | Array, Math, Stack, Number_Theory | 60 | 85.52
1337+
| 2196 |[Create Binary Tree From Descriptions](src/main/java/g2101_2200/s2196_create_binary_tree_from_descriptions)| Medium | Array, Hash_Table, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 85 | 76.70
1338+
| 2195 |[Append K Integers With Minimal Sum](src/main/java/g2101_2200/s2195_append_k_integers_with_minimal_sum)| Medium | Array, Math, Sorting, Greedy | 19 | 96.88
1339+
| 2194 |[Cells in a Range on an Excel Sheet](src/main/java/g2101_2200/s2194_cells_in_a_range_on_an_excel_sheet)| Easy | String | 1 | 99.92
13361340
| 2193 |[Minimum Number of Moves to Make Palindrome](src/main/java/g2101_2200/s2193_minimum_number_of_moves_to_make_palindrome)| Hard | String, Greedy, Two_Pointers, Binary_Indexed_Tree | 8 | 98.76
1341+
| 2192 |[All Ancestors of a Node in a Directed Acyclic Graph](src/main/java/g2101_2200/s2192_all_ancestors_of_a_node_in_a_directed_acyclic_graph)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 82 | 90.80
1342+
| 2191 |[Sort the Jumbled Numbers](src/main/java/g2101_2200/s2191_sort_the_jumbled_numbers)| Medium | Array, Sorting | 117 | 96.53
1343+
| 2190 |[Most Frequent Number Following Key In an Array](src/main/java/g2101_2200/s2190_most_frequent_number_following_key_in_an_array)| Easy | Array, Hash_Table, Counting | 1 | 100.00
1344+
| 2188 |[Minimum Time to Finish the Race](src/main/java/g2101_2200/s2188_minimum_time_to_finish_the_race)| Hard | Array, Dynamic_Programming | 15 | 93.69
13371345
| 2187 |[Minimum Time to Complete Trips](src/main/java/g2101_2200/s2187_minimum_time_to_complete_trips)| Medium | Array, Binary_Search | 187 | 95.03
13381346
| 2181 |[Merge Nodes in Between Zeros](src/main/java/g2101_2200/s2181_merge_nodes_in_between_zeros)| Medium | Simulation, Linked_List | 6 | 96.26
1347+
| 2177 |[Find Three Consecutive Integers That Sum to a Given Number](src/main/java/g2101_2200/s2177_find_three_consecutive_integers_that_sum_to_a_given_number)| Medium | Math, Simulation | 1 | 78.46
1348+
| 2176 |[Count Equal and Divisible Pairs in an Array](src/main/java/g2101_2200/s2176_count_equal_and_divisible_pairs_in_an_array)| Easy | Array | 4 | 78.29
13391349
| 2172 |[Maximum AND Sum of Array](src/main/java/g2101_2200/s2172_maximum_and_sum_of_array)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Bitmask | 21 | 87.50
13401350
| 2166 |[Design Bitset](src/main/java/g2101_2200/s2166_design_bitset)| Medium | Array, Hash_Table, Design | 81 | 73.38
13411351
| 2165 |[Smallest Value of the Rearranged Number](src/main/java/g2101_2200/s2165_smallest_value_of_the_rearranged_number)| Medium | Math, Sorting | 1 | 100.00
@@ -2986,7 +2996,7 @@
29862996
| 0059 |[Spiral Matrix II](src/main/java/g0001_0100/s0059_spiral_matrix_ii)| Medium | Array, Matrix, Simulation, Data_Structure_II_Day_3_Array | 0 | 100.00
29872997
| 0058 |[Length of Last Word](src/main/java/g0001_0100/s0058_length_of_last_word)| Easy | String, Programming_Skills_II_Day_6 | 0 | 100.00
29882998
| 0057 |[Insert Interval](src/main/java/g0001_0100/s0057_insert_interval)| Medium | Array | 1 | 99.15
2989-
| 0056 |[Merge Intervals](src/main/java/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Data_Structure_II_Day_2_Array | 22 | 12.57
2999+
| 0056 |[Merge Intervals](src/main/java/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Data_Structure_II_Day_2_Array | 7 | 97.44
29903000
| 0055 |[Jump Game](src/main/java/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Algorithm_II_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_4 | 2 | 87.28
29913001
| 0054 |[Spiral Matrix](src/main/java/g0001_0100/s0054_spiral_matrix)| Medium | Top_Interview_Questions, Array, Matrix, Simulation, Programming_Skills_II_Day_8 | 0 | 100.00
29923002
| 0053 |[Maximum Subarray](src/main/java/g0001_0100/s0053_maximum_subarray)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Data_Structure_I_Day_1_Array, Dynamic_Programming_I_Day_5 | 1 | 100.00

src/main/java/g0001_0100/s0056_merge_intervals/readme.md

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,44 +35,19 @@ import java.util.List;
3535

3636
public class Solution {
3737
public int[][] merge(int[][] intervals) {
38-
// sorting
39-
// so that we can perform the task linearly
40-
Arrays.sort(
41-
intervals,
42-
(int[] a, int[] b) -> {
43-
if (a[0] == b[0]) {
44-
return Integer.compare(a[1], b[1]);
45-
}
46-
return Integer.compare(a[0], b[0]);
47-
});
48-
49-
List<List<Integer>> list = new ArrayList<>();
50-
int i = 0;
51-
while (i < intervals.length) {
52-
// storing start
53-
int start = intervals[i][0];
54-
int end = intervals[i][1];
55-
i++;
56-
while (i < intervals.length && intervals[i][0] <= end) {
57-
// making sure range is not shrinking
58-
if (intervals[i][1] > end) {
59-
end = intervals[i][1];
60-
}
61-
i++;
38+
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
39+
List<int[]> list = new ArrayList<>();
40+
int[] current = intervals[0];
41+
list.add(current);
42+
for (int[] next : intervals) {
43+
if (current[1] >= next[0]) {
44+
current[1] = Math.max(current[1], next[1]);
45+
} else {
46+
current = next;
47+
list.add(current);
6248
}
63-
List<Integer> temp = new ArrayList<>();
64-
temp.add(start);
65-
temp.add(end);
66-
list.add(temp);
67-
}
68-
int[][] arr = new int[list.size()][2];
69-
i = 0;
70-
for (List<Integer> l : list) {
71-
arr[i][0] = l.get(0);
72-
arr[i][1] = l.get(1);
73-
i++;
7449
}
75-
return arr;
50+
return list.toArray(new int[list.size()][]);
7651
}
7752
}
7853
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## 2176\. Count Equal and Divisible Pairs in an Array
2+
3+
Easy
4+
5+
Given a **0-indexed** integer array `nums` of length `n` and an integer `k`, return _the **number of pairs**_ `(i, j)` _where_ `0 <= i < j < n`, _such that_ `nums[i] == nums[j]` _and_ `(i * j)` _is divisible by_ `k`.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [3,1,2,2,2,1,3], k = 2
10+
11+
**Output:** 4
12+
13+
**Explanation:** There are 4 pairs that meet all the requirements:
14+
15+
- nums[0] == nums[6], and 0 \* 6 == 0, which is divisible by 2.
16+
17+
- nums[2] == nums[3], and 2 \* 3 == 6, which is divisible by 2.
18+
19+
- nums[2] == nums[4], and 2 \* 4 == 8, which is divisible by 2.
20+
21+
- nums[3] == nums[4], and 3 \* 4 == 12, which is divisible by 2.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [1,2,3,4], k = 1
26+
27+
**Output:** 0
28+
29+
**Explanation:** Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.
30+
31+
**Constraints:**
32+
33+
* `1 <= nums.length <= 100`
34+
* `1 <= nums[i], k <= 100`
35+
36+
## Solution
37+
38+
```java
39+
public class Solution {
40+
public int countPairs(int[] nums, int k) {
41+
int ans = 0;
42+
for (int i = 0; i < nums.length; i++) {
43+
for (int j = i + 1; j < nums.length; j++) {
44+
if (nums[i] == nums[j] && (i * j) % k == 0) {
45+
++ans;
46+
}
47+
}
48+
}
49+
return ans;
50+
}
51+
}
52+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## 2177\. Find Three Consecutive Integers That Sum to a Given Number
2+
3+
Medium
4+
5+
Given an integer `num`, return _three consecutive integers (as a sorted array)_ _that **sum** to_ `num`. If `num` cannot be expressed as the sum of three consecutive integers, return _an **empty** array._
6+
7+
**Example 1:**
8+
9+
**Input:** num = 33
10+
11+
**Output:** [10,11,12]
12+
13+
**Explanation:** 33 can be expressed as 10 + 11 + 12 = 33.
14+
15+
10, 11, 12 are 3 consecutive integers, so we return [10, 11, 12].
16+
17+
**Example 2:**
18+
19+
**Input:** num = 4
20+
21+
**Output:** []
22+
23+
**Explanation:** There is no way to express 4 as the sum of 3 consecutive integers.
24+
25+
**Constraints:**
26+
27+
* <code>0 <= num <= 10<sup>15</sup></code>
28+
29+
## Solution
30+
31+
```java
32+
public class Solution {
33+
public long[] sumOfThree(long num) {
34+
if (num % 3 == 0) {
35+
return new long[] {num / 3 - 1, num / 3, num / 3 + 1};
36+
}
37+
return new long[0];
38+
}
39+
}
40+
```
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
## 2188\. Minimum Time to Finish the Race
2+
3+
Hard
4+
5+
You are given a **0-indexed** 2D integer array `tires` where <code>tires[i] = [f<sub>i</sub>, r<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> tire can finish its <code>x<sup>th</sup></code> successive lap in <code>f<sub>i</sub> * r<sub>i</sub><sup>(x-1)</sup></code> seconds.
6+
7+
* For example, if <code>f<sub>i</sub> = 3</code> and <code>r<sub>i</sub> = 2</code>, then the tire would finish its <code>1<sup>st</sup></code> lap in `3` seconds, its <code>2<sup>nd</sup></code> lap in `3 * 2 = 6` seconds, its <code>3<sup>rd</sup></code> lap in <code>3 * 2<sup>2</sup> = 12</code> seconds, etc.
8+
9+
You are also given an integer `changeTime` and an integer `numLaps`.
10+
11+
The race consists of `numLaps` laps and you may start the race with **any** tire. You have an **unlimited** supply of each tire and after every lap, you may **change** to any given tire (including the current tire type) if you wait `changeTime` seconds.
12+
13+
Return _the **minimum** time to finish the race._
14+
15+
**Example 1:**
16+
17+
**Input:** tires = \[\[2,3],[3,4]], changeTime = 5, numLaps = 4
18+
19+
**Output:** 21
20+
21+
**Explanation:**
22+
23+
Lap 1: Start with tire 0 and finish the lap in 2 seconds.
24+
25+
Lap 2: Continue with tire 0 and finish the lap in 2 \* 3 = 6 seconds.
26+
27+
Lap 3: Change tires to a new tire 0 for 5 seconds and then finish the lap in another 2 seconds.
28+
29+
Lap 4: Continue with tire 0 and finish the lap in 2 \* 3 = 6 seconds.
30+
31+
Total time = 2 + 6 + 5 + 2 + 6 = 21 seconds.
32+
33+
The minimum time to complete the race is 21 seconds.
34+
35+
**Example 2:**
36+
37+
**Input:** tires = \[\[1,10],[2,2],[3,4]], changeTime = 6, numLaps = 5
38+
39+
**Output:** 25
40+
41+
**Explanation:**
42+
43+
Lap 1: Start with tire 1 and finish the lap in 2 seconds.
44+
45+
Lap 2: Continue with tire 1 and finish the lap in 2 \* 2 = 4 seconds.
46+
47+
Lap 3: Change tires to a new tire 1 for 6 seconds and then finish the lap in another 2 seconds.
48+
49+
Lap 4: Continue with tire 1 and finish the lap in 2 \* 2 = 4 seconds.
50+
51+
Lap 5: Change tires to tire 0 for 6 seconds then finish the lap in another 1 second.
52+
53+
Total time = 2 + 4 + 6 + 2 + 4 + 6 + 1 = 25 seconds.
54+
55+
The minimum time to complete the race is 25 seconds.
56+
57+
**Constraints:**
58+
59+
* <code>1 <= tires.length <= 10<sup>5</sup></code>
60+
* `tires[i].length == 2`
61+
* <code>1 <= f<sub>i</sub>, changeTime <= 10<sup>5</sup></code>
62+
* <code>2 <= r<sub>i</sub> <= 10<sup>5</sup></code>
63+
* `1 <= numLaps <= 1000`
64+
65+
## Solution
66+
67+
```java
68+
import java.util.Arrays;
69+
70+
public class Solution {
71+
public int minimumFinishTime(int[][] tires, int changeTime, int numLaps) {
72+
int minf = Integer.MAX_VALUE;
73+
// find the minimum of f, to deal with special case and stronger constraints later.
74+
for (int[] tire : tires) {
75+
minf = Math.min(minf, tire[0]);
76+
}
77+
// if min-f >= changeTime, we can return early
78+
if (minf >= changeTime) {
79+
return minf * numLaps + changeTime * (numLaps - 1);
80+
}
81+
// shortest[i] record shortest time that one single tire is worth to go the i-th laps
82+
// worth to go means the i-th lap time is shorter than changeTime + f
83+
int[] shortest = new int[numLaps + 1];
84+
Arrays.fill(shortest, Integer.MAX_VALUE);
85+
int len = 0;
86+
// traverse all tires, and update the shortest[i]
87+
// this shortest time is available from [1, len] in the array
88+
// len is updated in the traverse
89+
for (int[] tire : tires) {
90+
int f = tire[0];
91+
int r = tire[1];
92+
// index start from 1 to be consistent with numLaps
93+
int index = 1;
94+
int t = f;
95+
int sum = t;
96+
// use changeTime + minf here, which is a strong constraints than changeTime + f
97+
while (t <= changeTime + minf && index <= numLaps) {
98+
shortest[index] = Math.min(shortest[index], sum);
99+
t = t * r;
100+
sum += t;
101+
index++;
102+
}
103+
len = Math.max(len, index - 1);
104+
}
105+
for (int i = 2; i <= numLaps; i++) {
106+
// for j > Math.min(i/2, len), it's simply recombination of the values of shortest
107+
// [1:len]
108+
// it's ok to go furthur for the loop, just repeat the Math.min computation
109+
for (int j = 1; j <= Math.min(i / 2, len); j++) {
110+
shortest[i] = Math.min(shortest[i], shortest[j] + shortest[i - j] + changeTime);
111+
}
112+
}
113+
return shortest[numLaps];
114+
}
115+
}
116+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## 2190\. Most Frequent Number Following Key In an Array
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums`. You are also given an integer `key`, which is present in `nums`.
6+
7+
For every unique integer `target` in `nums`, **count** the number of times `target` immediately follows an occurrence of `key` in `nums`. In other words, count the number of indices `i` such that:
8+
9+
* `0 <= i <= nums.length - 2`,
10+
* `nums[i] == key` and,
11+
* `nums[i + 1] == target`.
12+
13+
Return _the_ `target` _with the **maximum** count_. The test cases will be generated such that the `target` with maximum count is unique.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1,100,200,1,100], key = 1
18+
19+
**Output:** 100
20+
21+
**Explanation:** For target = 100, there are 2 occurrences at indices 1 and 4 which follow an occurrence of key.
22+
23+
No other integers follow an occurrence of key, so we return 100.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [2,2,2,2,3], key = 2
28+
29+
**Output:** 2
30+
31+
**Explanation:** For target = 2, there are 3 occurrences at indices 1, 2, and 3 which follow an occurrence of key.
32+
33+
For target = 3, there is only one occurrence at index 4 which follows an occurrence of key.
34+
35+
target = 2 has the maximum number of occurrences following an occurrence of key, so we return 2.
36+
37+
**Constraints:**
38+
39+
* `2 <= nums.length <= 1000`
40+
* `1 <= nums[i] <= 1000`
41+
* The test cases will be generated such that the answer is unique.
42+
43+
## Solution
44+
45+
```java
46+
public class Solution {
47+
public int mostFrequent(int[] nums, int key) {
48+
int[] store = new int[1001];
49+
for (int i = 0; i < nums.length - 1; i++) {
50+
if (nums[i] == key) {
51+
store[nums[i + 1]]++;
52+
}
53+
}
54+
int res = 0;
55+
int count = store[0];
56+
for (int i = 1; i < 1001; i++) {
57+
if (count < store[i]) {
58+
count = store[i];
59+
res = i;
60+
}
61+
}
62+
return res;
63+
}
64+
}
65+
```

0 commit comments

Comments
 (0)