Skip to content

Commit 7580c5c

Browse files
authored
Added tasks 2398, 2399, 2400.
1 parent d600405 commit 7580c5c

File tree

10 files changed

+320
-1
lines changed

10 files changed

+320
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,9 @@ implementation 'com.github.javadev:leetcode-in-java:1.13'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2400 |[Number of Ways to Reach a Position After Exactly k Steps](src/main/java/g2301_2400/s2400_number_of_ways_to_reach_a_position_after_exactly_k_steps/Solution.java)| Medium | Dynamic_Programming, Math, Combinatorics | 1 | 99.66
1852+
| 2399 |[Check Distances Between Same Letters](src/main/java/g2301_2400/s2399_check_distances_between_same_letters/Solution.java)| Easy | Array, String, Hash_Table | 1 | 99.88
1853+
| 2398 |[Maximum Number of Robots Within Budget](src/main/java/g2301_2400/s2398_maximum_number_of_robots_within_budget/Solution.java)| Hard | Array, Binary_Search, Prefix_Sum, Sliding_Window, Queue, Heap_(Priority_Queue) | 15 | 99.75
18511854
| 2397 |[Maximum Rows Covered by Columns](src/main/java/g2301_2400/s2397_maximum_rows_covered_by_columns/Solution.java)| Medium | Array, Matrix, Bit_Manipulation, Backtracking, Enumeration | 1 | 100.00
18521855
| 2396 |[Strictly Palindromic Number](src/main/java/g2301_2400/s2396_strictly_palindromic_number/Solution.java)| Medium | Math, Two_Pointers, Brainteaser | 0 | 100.00
18531856
| 2395 |[Find Subarrays With Equal Sum](src/main/java/g2301_2400/s2395_find_subarrays_with_equal_sum/Solution.java)| Easy | Array, Hash_Table | 0 | 100.00
@@ -1856,7 +1859,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.13'
18561859
| 2390 |[Removing Stars From a String](src/main/java/g2301_2400/s2390_removing_stars_from_a_string/Solution.java)| Medium | String, Stack, Simulation | 31 | 90.55
18571860
| 2389 |[Longest Subsequence With Limited Sum](src/main/java/g2301_2400/s2389_longest_subsequence_with_limited_sum/Solution.java)| Easy | Array, Sorting, Greedy, Binary_Search, Prefix_Sum | 4 | 99.97
18581861
| 2386 |[Find the K-Sum of an Array](src/main/java/g2301_2400/s2386_find_the_k_sum_of_an_array/Solution.java)| Hard | Array, Sorting, Heap_Priority_Queue | 75 | 100.00
1859-
| 2385 |[Amount of Time for Binary Tree to Be Infected](src/main/java/g2301_2400/s2385_amount_of_time_for_binary_tree_to_be_infected/Solution.java)| Medium | Tree, Binary_Tree, Depth_First_Search, Breadth_First_Search | 20 | 100.00
1862+
| 2385 |[Amount of Time for Binary Tree to Be Infected](src/main/java/g2301_2400/s2385_amount_of_time_for_binary_tree_to_be_infected/Solution.java)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 20 | 100.00
18601863
| 2384 |[Largest Palindromic Number](src/main/java/g2301_2400/s2384_largest_palindromic_number/Solution.java)| Medium | String, Hash_Table, Greedy | 26 | 100.00
18611864
| 2383 |[Minimum Hours of Training to Win a Competition](src/main/java/g2301_2400/s2383_minimum_hours_of_training_to_win_a_competition/Solution.java)| Easy | Array, Greedy | 0 | 100.00
18621865
| 2382 |[Maximum Segment Sum After Removals](src/main/java/g2301_2400/s2382_maximum_segment_sum_after_removals/Solution.java)| Hard | Array, Prefix_Sum, Union_Find, Ordered_Set | 28 | 100.00
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g2301_2400.s2398_maximum_number_of_robots_within_budget;
2+
3+
// #Hard #Array #Binary_Search #Prefix_Sum #Sliding_Window #Queue #Heap_(Priority_Queue)
4+
// #2022_09_19_Time_15_ms_(99.75%)_Space_105.9_MB_(85.13%)
5+
6+
public class Solution {
7+
8+
// use sliding window to track the largest in a way that the sliding window only grows.
9+
// then the maximum size is the size of the sliding window at the end.
10+
// if condition is met, we just grow the sliding window.
11+
// if condition is not met, we shift the sliding window with the same size to the next position.
12+
// e.g., if [0,3] is valid, next time we will try [0,4].
13+
// if [0,3] is invalid, next time we will try [1,4],
14+
// by adjusting the window to [1,3] first in the current round.
15+
public int maximumRobots(int[] chargeTimes, int[] runningCosts, long budget) {
16+
int n = chargeTimes.length;
17+
// [front, end).
18+
int[] deque = new int[n];
19+
int front = 0;
20+
int end = 0;
21+
long sum = 0;
22+
int left = 0;
23+
int right = 0;
24+
for (; right < n; ++right) {
25+
// add right into the sliding window, so the window becomes [left, right].
26+
// update sliding window max and window sum.
27+
while (end - front > 0 && chargeTimes[deque[end - 1]] <= chargeTimes[right]) {
28+
--end;
29+
}
30+
deque[end++] = right;
31+
sum += runningCosts[right];
32+
// if the condition is met in the window, do nothing,
33+
// so the next window size will become one larger.
34+
// if the condition is not met in the window, shrink one from the front,
35+
// so the next window size will stay the same.
36+
if (chargeTimes[deque[front]] + (right - left + 1) * sum > budget) {
37+
while (end - front > 0 && deque[front] <= left) {
38+
++front;
39+
}
40+
sum -= runningCosts[left];
41+
++left;
42+
}
43+
}
44+
return right - left;
45+
}
46+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2398\. Maximum Number of Robots Within Budget
2+
3+
Hard
4+
5+
You have `n` robots. You are given two **0-indexed** integer arrays, `chargeTimes` and `runningCosts`, both of length `n`. The <code>i<sup>th</sup></code> robot costs `chargeTimes[i]` units to charge and costs `runningCosts[i]` units to run. You are also given an integer `budget`.
6+
7+
The **total cost** of running `k` chosen robots is equal to `max(chargeTimes) + k * sum(runningCosts)`, where `max(chargeTimes)` is the largest charge cost among the `k` robots and `sum(runningCosts)` is the sum of running costs among the `k` robots.
8+
9+
Return _the **maximum** number of **consecutive** robots you can run such that the total cost **does not** exceed_ `budget`.
10+
11+
**Example 1:**
12+
13+
**Input:** chargeTimes = [3,6,1,3,4], runningCosts = [2,1,3,4,5], budget = 25
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
It is possible to run all individual and consecutive pairs of robots within budget.
20+
21+
To obtain answer 3, consider the first 3 robots. The total cost will be max(3,6,1) + 3 \* sum(2,1,3) = 6 + 3 \* 6 = 24 which is less than 25.
22+
23+
It can be shown that it is not possible to run more than 3 consecutive robots within budget, so we return 3.
24+
25+
**Example 2:**
26+
27+
**Input:** chargeTimes = [11,12,19], runningCosts = [10,8,7], budget = 19
28+
29+
**Output:** 0
30+
31+
**Explanation:** No robot can be run that does not exceed the budget, so we return 0.
32+
33+
**Constraints:**
34+
35+
* `chargeTimes.length == runningCosts.length == n`
36+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
37+
* <code>1 <= chargeTimes[i], runningCosts[i] <= 10<sup>5</sup></code>
38+
* <code>1 <= budget <= 10<sup>15</sup></code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2301_2400.s2399_check_distances_between_same_letters;
2+
3+
// #Easy #Array #String #Hash_Table #2022_09_19_Time_1_ms_(99.88%)_Space_43.8_MB_(22.33%)
4+
5+
public class Solution {
6+
public boolean checkDistances(String s, int[] distance) {
7+
boolean[] seenFirstIndexYet = new boolean[26];
8+
for (int idxIntoS = 0; idxIntoS < s.length(); ++idxIntoS) {
9+
char c = s.charAt(idxIntoS);
10+
if (!seenFirstIndexYet[c - 'a']) {
11+
seenFirstIndexYet[c - 'a'] = true;
12+
distance[c - 'a'] += idxIntoS;
13+
} else {
14+
// seenFirstIndexYet[c - 'a']
15+
distance[c - 'a'] -= idxIntoS;
16+
if (distance[c - 'a'] != -1) {
17+
// early return
18+
return false;
19+
}
20+
}
21+
}
22+
return true;
23+
}
24+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2399\. Check Distances Between Same Letters
2+
3+
Easy
4+
5+
You are given a **0-indexed** string `s` consisting of only lowercase English letters, where each letter in `s` appears **exactly** **twice**. You are also given a **0-indexed** integer array `distance` of length `26`.
6+
7+
Each letter in the alphabet is numbered from `0` to `25` (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, ... , `'z' -> 25`).
8+
9+
In a **well-spaced** string, the number of letters between the two occurrences of the <code>i<sup>th</sup></code> letter is `distance[i]`. If the <code>i<sup>th</sup></code> letter does not appear in `s`, then `distance[i]` can be **ignored**.
10+
11+
Return `true` _if_ `s` _is a **well-spaced** string, otherwise return_ `false`.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "abaccb", distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
16+
17+
**Output:** true
18+
19+
**Explanation:**
20+
21+
- 'a' appears at indices 0 and 2 so it satisfies distance[0] = 1.
22+
23+
- 'b' appears at indices 1 and 5 so it satisfies distance[1] = 3.
24+
25+
- 'c' appears at indices 3 and 4 so it satisfies distance[2] = 0.
26+
27+
Note that distance[3] = 5, but since 'd' does not appear in s, it can be ignored.
28+
29+
Return true because s is a well-spaced string.
30+
31+
**Example 2:**
32+
33+
**Input:** s = "aa", distance = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
34+
35+
**Output:** false
36+
37+
**Explanation:**
38+
39+
- 'a' appears at indices 0 and 1 so there are zero letters between them. Because distance[0] = 1, s is not a well-spaced string.
40+
41+
**Constraints:**
42+
43+
* `2 <= s.length <= 52`
44+
* `s` consists only of lowercase English letters.
45+
* Each letter appears in `s` exactly twice.
46+
* `distance.length == 26`
47+
* `0 <= distance[i] <= 50`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g2301_2400.s2400_number_of_ways_to_reach_a_position_after_exactly_k_steps;
2+
3+
// #Medium #Dynamic_Programming #Math #Combinatorics
4+
// #2022_09_19_Time_1_ms_(99.66%)_Space_41.7_MB_(91.83%)
5+
6+
public class Solution {
7+
private int mod = 1000000007;
8+
9+
public int numberOfWays(int startPos, int endPos, int k) {
10+
if (Math.abs(endPos - startPos) > k) {
11+
return 0;
12+
}
13+
if (Math.abs(endPos - startPos + k) % 2 != 0) {
14+
return 0;
15+
}
16+
int t = endPos - startPos;
17+
int right = (k + t) / 2;
18+
int min = Math.min(right, k - right);
19+
if (min == 0) {
20+
return 1;
21+
}
22+
int[] rev = new int[min + 1];
23+
rev[1] = 1;
24+
int ans = k;
25+
for (int i = 2; i <= min; i++) {
26+
rev[i] = (int) ((long) (mod - mod / i) * (long) rev[mod % i] % mod);
27+
ans = (int) ((long) ans * (long) (k - i + 1) % mod);
28+
ans = (int) ((long) ans * (long) rev[i] % mod);
29+
}
30+
return ans;
31+
}
32+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2400\. Number of Ways to Reach a Position After Exactly k Steps
2+
3+
Medium
4+
5+
You are given two **positive** integers `startPos` and `endPos`. Initially, you are standing at position `startPos` on an **infinite** number line. With one step, you can move either one position to the left, or one position to the right.
6+
7+
Given a positive integer `k`, return _the number of **different** ways to reach the position_ `endPos` _starting from_ `startPos`_, such that you perform **exactly**_ `k` _steps_. Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
8+
9+
Two ways are considered different if the order of the steps made is not exactly the same.
10+
11+
**Note** that the number line includes negative integers.
12+
13+
**Example 1:**
14+
15+
**Input:** startPos = 1, endPos = 2, k = 3
16+
17+
**Output:** 3
18+
19+
**Explanation:** We can reach position 2 from 1 in exactly 3 steps in three ways:
20+
21+
- 1 -> 2 -> 3 -> 2.
22+
23+
- 1 -> 2 -> 1 -> 2.
24+
25+
- 1 -> 0 -> 1 -> 2.
26+
27+
It can be proven that no other way is possible, so we return 3.
28+
29+
**Example 2:**
30+
31+
**Input:** startPos = 2, endPos = 5, k = 10
32+
33+
**Output:** 0
34+
35+
**Explanation:** It is impossible to reach position 5 from position 2 in exactly 10 steps.
36+
37+
**Constraints:**
38+
39+
* `1 <= startPos, endPos, k <= 1000`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2301_2400.s2398_maximum_number_of_robots_within_budget;
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 maximumRobots() {
11+
assertThat(
12+
new Solution()
13+
.maximumRobots(new int[] {3, 6, 1, 3, 4}, new int[] {2, 1, 3, 4, 5}, 25),
14+
equalTo(3));
15+
}
16+
17+
@Test
18+
void maximumRobots2() {
19+
assertThat(
20+
new Solution().maximumRobots(new int[] {11, 12, 19}, new int[] {10, 8, 7}, 19),
21+
equalTo(0));
22+
}
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g2301_2400.s2399_check_distances_between_same_letters;
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 checkDistances() {
11+
assertThat(
12+
new Solution()
13+
.checkDistances(
14+
"abaccb",
15+
new int[] {
16+
1, 3, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17+
0, 0, 0, 0, 0
18+
}),
19+
equalTo(true));
20+
}
21+
22+
@Test
23+
void checkDistances2() {
24+
assertThat(
25+
new Solution()
26+
.checkDistances(
27+
"aa",
28+
new int[] {
29+
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30+
0, 0, 0, 0, 0
31+
}),
32+
equalTo(false));
33+
}
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g2301_2400.s2400_number_of_ways_to_reach_a_position_after_exactly_k_steps;
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 numberOfWays() {
11+
assertThat(new Solution().numberOfWays(1, 2, 3), equalTo(3));
12+
}
13+
14+
@Test
15+
void numberOfWays2() {
16+
assertThat(new Solution().numberOfWays(2, 5, 10), equalTo(0));
17+
}
18+
19+
@Test
20+
void numberOfWays3() {
21+
assertThat(new Solution().numberOfWays(1, 10, 3), equalTo(0));
22+
}
23+
24+
@Test
25+
void numberOfWays4() {
26+
assertThat(new Solution().numberOfWays(1, 1000, 999), equalTo(1));
27+
}
28+
29+
@Test
30+
void numberOfWays5() {
31+
assertThat(new Solution().numberOfWays(272, 270, 6), equalTo(15));
32+
}
33+
}

0 commit comments

Comments
 (0)