Skip to content

Commit 234447c

Browse files
authored
Added tasks 2769-2773
1 parent fa5e761 commit 234447c

File tree

12 files changed

+380
-0
lines changed

12 files changed

+380
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package g2701_2800.s2769_find_the_maximum_achievable_number
2+
3+
// #Easy #Math #2023_08_11_Time_134_ms_(97.89%)_Space_33.8_MB_(81.05%)
4+
5+
class Solution {
6+
fun theMaximumAchievableX(num: Int, t: Int): Int {
7+
return num + t * 2
8+
}
9+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2769\. Find the Maximum Achievable Number
2+
3+
Easy
4+
5+
You are given two integers, `num` and `t`.
6+
7+
An integer `x` is called **achievable** if it can become equal to `num` after applying the following operation no more than `t` times:
8+
9+
* Increase or decrease `x` by `1`, and simultaneously increase or decrease `num` by `1`.
10+
11+
Return _the maximum possible achievable number_. It can be proven that there exists at least one achievable number.
12+
13+
**Example 1:**
14+
15+
**Input:** num = 4, t = 1
16+
17+
**Output:** 6
18+
19+
**Explanation:**
20+
21+
The maximum achievable number is x = 6; it can become equal to num after performing this operation:
22+
23+
1- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5.
24+
25+
It can be proven that there is no achievable number larger than 6.
26+
27+
**Example 2:**
28+
29+
**Input:** num = 3, t = 2
30+
31+
**Output:** 7
32+
33+
**Explanation:**
34+
35+
The maximum achievable number is x = 7; after performing these operations, x will equal num:
36+
37+
1- Decrease x by 1, and increase num by 1. Now, x = 6 and num = 4.
38+
39+
2- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5.
40+
41+
It can be proven that there is no achievable number larger than 7.
42+
43+
**Constraints:**
44+
45+
* `1 <= num, t <= 50`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2701_2800.s2770_maximum_number_of_jumps_to_reach_the_last_index
2+
3+
// #Medium #Array #Dynamic_Programming #2023_08_11_Time_325_ms_(51.16%)_Space_49.1_MB_(13.95%)
4+
5+
class Solution {
6+
private class Pair(var prev: Int, var len: Int)
7+
8+
fun maximumJumps(nums: IntArray, target: Int): Int {
9+
val arr = arrayOfNulls<Pair>(nums.size)
10+
arr[0] = Pair(0, 0)
11+
for (i in 1 until nums.size) {
12+
arr[i] = Pair(-1, 0)
13+
for (j in i - 1 downTo 0) {
14+
if (Math.abs(nums[i] - nums[j]) <= target &&
15+
arr[j]!!.prev != -1 && arr[j]!!.len + 1 > arr[i]!!.len
16+
) {
17+
arr[i]!!.prev = j
18+
arr[i]!!.len = arr[j]!!.len + 1
19+
}
20+
}
21+
}
22+
return if (arr[nums.size - 1]!!.len > 0) arr[nums.size - 1]!!.len else -1
23+
}
24+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2770\. Maximum Number of Jumps to Reach the Last Index
2+
3+
Medium
4+
5+
You are given a **0-indexed** array `nums` of `n` integers and an integer `target`.
6+
7+
You are initially positioned at index `0`. In one step, you can jump from index `i` to any index `j` such that:
8+
9+
* `0 <= i < j < n`
10+
* `-target <= nums[j] - nums[i] <= target`
11+
12+
Return _the **maximum number of jumps** you can make to reach index_ `n - 1`.
13+
14+
If there is no way to reach index `n - 1`, return `-1`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,3,6,4,1,2], target = 2
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:
25+
26+
- Jump from index 0 to index 1.
27+
28+
- Jump from index 1 to index 3.
29+
30+
- Jump from index 3 to index 5.
31+
32+
It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps.
33+
34+
Hence, the answer is 3.
35+
36+
**Example 2:**
37+
38+
**Input:** nums = [1,3,6,4,1,2], target = 3
39+
40+
**Output:** 5
41+
42+
**Explanation:**
43+
44+
To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:
45+
46+
- Jump from index 0 to index 1.
47+
48+
- Jump from index 1 to index 2.
49+
50+
- Jump from index 2 to index 3.
51+
52+
- Jump from index 3 to index 4.
53+
54+
- Jump from index 4 to index 5.
55+
56+
It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 5 jumps.
57+
58+
Hence, the answer is 5.
59+
60+
**Example 3:**
61+
62+
**Input:** nums = [1,3,6,4,1,2], target = 0
63+
64+
**Output:** -1
65+
66+
**Explanation:** It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1.
67+
68+
**Constraints:**
69+
70+
* `2 <= nums.length == n <= 1000`
71+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
72+
* <code>0 <= target <= 2 * 10<sup>9</sup></code>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g2701_2800.s2771_longest_non_decreasing_subarray_from_two_arrays
2+
3+
// #Medium #Array #Dynamic_Programming #2023_08_11_Time_665_ms_(96.88%)_Space_77.8_MB_(53.13%)
4+
5+
class Solution {
6+
fun maxNonDecreasingLength(nums1: IntArray, nums2: IntArray): Int {
7+
var res = 1
8+
var dp1 = 1
9+
var dp2 = 1
10+
val n = nums1.size
11+
var t11: Int
12+
var t12: Int
13+
var t21: Int
14+
var t22: Int
15+
for (i in 1 until n) {
16+
t11 = if (nums1[i - 1] <= nums1[i]) dp1 + 1 else 1
17+
t12 = if (nums1[i - 1] <= nums2[i]) dp1 + 1 else 1
18+
t21 = if (nums2[i - 1] <= nums1[i]) dp2 + 1 else 1
19+
t22 = if (nums2[i - 1] <= nums2[i]) dp2 + 1 else 1
20+
dp1 = Math.max(t11, t21)
21+
dp2 = Math.max(t12, t22)
22+
res = Math.max(res, Math.max(dp1, dp2))
23+
}
24+
return res
25+
}
26+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2771\. Longest Non-decreasing Subarray From Two Arrays
2+
3+
Medium
4+
5+
You are given two **0-indexed** integer arrays `nums1` and `nums2` of length `n`.
6+
7+
Let's define another **0-indexed** integer array, `nums3`, of length `n`. For each index `i` in the range `[0, n - 1]`, you can assign either `nums1[i]` or `nums2[i]` to `nums3[i]`.
8+
9+
Your task is to maximize the length of the **longest non-decreasing subarray** in `nums3` by choosing its values optimally.
10+
11+
Return _an integer representing the length of the **longest non-decreasing** subarray in_ `nums3`.
12+
13+
**Note:** A **subarray** is a contiguous **non-empty** sequence of elements within an array.
14+
15+
**Example 1:**
16+
17+
**Input:** nums1 = [2,3,1], nums2 = [1,2,1]
18+
19+
**Output:** 2
20+
21+
**Explanation:**
22+
23+
One way to construct nums3 is:
24+
25+
nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1].
26+
27+
The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2.
28+
29+
We can show that 2 is the maximum achievable length.
30+
31+
**Example 2:**
32+
33+
**Input:** nums1 = [1,3,2,1], nums2 = [2,2,3,4]
34+
35+
**Output:** 4
36+
37+
**Explanation:**
38+
39+
One way to construct nums3 is:
40+
41+
nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4].
42+
43+
The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length.
44+
45+
**Example 3:**
46+
47+
**Input:** nums1 = [1,1], nums2 = [2,2]
48+
49+
**Output:** 2
50+
51+
**Explanation:**
52+
53+
One way to construct nums3 is:
54+
55+
nums3 = [nums1[0], nums1[1]] => [1,1].
56+
57+
The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length.
58+
59+
**Constraints:**
60+
61+
* <code>1 <= nums1.length == nums2.length == n <= 10<sup>5</sup></code>
62+
* <code>1 <= nums1[i], nums2[i] <= 10<sup>9</sup></code>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2701_2800.s2772_apply_operations_to_make_all_array_elements_equal_to_zero
2+
3+
// #Medium #Array #Prefix_Sum #2023_08_11_Time_578_ms_(97.06%)_Space_71.5_MB_(20.59%)
4+
5+
class Solution {
6+
fun checkArray(nums: IntArray, k: Int): Boolean {
7+
var cur = 0
8+
val n = nums.size
9+
for (i in 0 until n) {
10+
if (cur > nums[i]) {
11+
return false
12+
}
13+
nums[i] -= cur
14+
cur += nums[i]
15+
if (i >= k - 1) {
16+
cur -= nums[i - k + 1]
17+
}
18+
}
19+
return cur == 0
20+
}
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2772\. Apply Operations to Make All Array Elements Equal to Zero
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums` and a positive integer `k`.
6+
7+
You can apply the following operation on the array **any** number of times:
8+
9+
* Choose **any** subarray of size `k` from the array and **decrease** all its elements by `1`.
10+
11+
Return `true` _if you can make all the array elements equal to_ `0`_, or_ `false` _otherwise_.
12+
13+
A **subarray** is a contiguous non-empty part of an array.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [2,2,3,1,1,0], k = 3
18+
19+
**Output:** true
20+
21+
**Explanation:**
22+
23+
We can do the following operations:
24+
25+
- Choose the subarray [2,2,3]. The resulting array will be nums = [**<ins>1</ins>**,**<ins>1</ins>**,**<ins>2</ins>**,1,1,0].
26+
27+
- Choose the subarray [2,1,1]. The resulting array will be nums = [1,1,**<ins>1</ins>**,**<ins>0</ins>**,**<ins>0</ins>**,0].
28+
29+
- Choose the subarray [1,1,1]. The resulting array will be nums = [<ins>**0**</ins>,<ins>**0**</ins>,<ins>**0**</ins>,0,0,0].
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [1,3,1,1], k = 2
34+
35+
**Output:** false
36+
37+
**Explanation:** It is not possible to make all the array elements equal to 0.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= k <= nums.length <= 10<sup>5</sup></code>
42+
* <code>0 <= nums[i] <= 10<sup>6</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2701_2800.s2769_find_the_maximum_achievable_number
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun theMaximumAchievableX() {
10+
assertThat(Solution().theMaximumAchievableX(4, 1), equalTo(6))
11+
}
12+
13+
@Test
14+
fun theMaximumAchievableX2() {
15+
assertThat(Solution().theMaximumAchievableX(3, 2), equalTo(7))
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g2701_2800.s2770_maximum_number_of_jumps_to_reach_the_last_index
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun maximumJumps() {
10+
assertThat(Solution().maximumJumps(intArrayOf(1, 3, 6, 4, 1, 2), 2), equalTo(3))
11+
}
12+
13+
@Test
14+
fun maximumJumps2() {
15+
assertThat(Solution().maximumJumps(intArrayOf(1, 3, 6, 4, 1, 2), 3), equalTo(5))
16+
}
17+
18+
@Test
19+
fun maximumJumps3() {
20+
assertThat(Solution().maximumJumps(intArrayOf(1, 3, 6, 4, 1, 2), 0), equalTo(-1))
21+
}
22+
}

0 commit comments

Comments
 (0)