Skip to content

Commit dd6874a

Browse files
authored
Added tasks 3349-3352
1 parent 19c88b3 commit dd6874a

File tree

12 files changed

+383
-0
lines changed

12 files changed

+383
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3301_3400.s3349_adjacent_increasing_subarrays_detection_i
2+
3+
// #Easy #Array #2024_11_15_Time_179_ms_(97.92%)_Space_37.3_MB_(91.67%)
4+
5+
class Solution {
6+
fun hasIncreasingSubarrays(nums: List<Int>, k: Int): Boolean {
7+
val l = nums.size
8+
if (l < k * 2) {
9+
return false
10+
}
11+
for (i in 0.rangeUntil(l - 2 * k + 1)) {
12+
if (check(i, k, nums) && check(i + k, k, nums)) {
13+
return true
14+
}
15+
}
16+
return false
17+
}
18+
19+
private fun check(p: Int, k: Int, nums: List<Int>): Boolean {
20+
for (i in p.rangeUntil(p + k - 1)) {
21+
if (nums[i] >= nums[i + 1]) {
22+
return false
23+
}
24+
}
25+
return true
26+
}
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
3349\. Adjacent Increasing Subarrays Detection I
2+
3+
Easy
4+
5+
Given an array `nums` of `n` integers and an integer `k`, determine whether there exist **two** **adjacent** subarrays of length `k` such that both subarrays are **strictly** **increasing**. Specifically, check if there are **two** subarrays starting at indices `a` and `b` (`a < b`), where:
6+
7+
* Both subarrays `nums[a..a + k - 1]` and `nums[b..b + k - 1]` are **strictly increasing**.
8+
* The subarrays must be **adjacent**, meaning `b = a + k`.
9+
10+
Return `true` if it is _possible_ to find **two** such subarrays, and `false` otherwise.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [2,5,7,8,9,2,3,4,3,1], k = 3
15+
16+
**Output:** true
17+
18+
**Explanation:**
19+
20+
* The subarray starting at index `2` is `[7, 8, 9]`, which is strictly increasing.
21+
* The subarray starting at index `5` is `[2, 3, 4]`, which is also strictly increasing.
22+
* These two subarrays are adjacent, so the result is `true`.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [1,2,3,4,4,4,4,5,6,7], k = 5
27+
28+
**Output:** false
29+
30+
**Constraints:**
31+
32+
* `2 <= nums.length <= 100`
33+
* `1 < 2 * k <= nums.length`
34+
* `-1000 <= nums[i] <= 1000`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g3301_3400.s3350_adjacent_increasing_subarrays_detection_ii
2+
3+
// #Medium #Array #Binary_Search #2024_11_15_Time_947_ms_(48.57%)_Space_87.4_MB_(51.43%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun maxIncreasingSubarrays(nums: List<Int>): Int {
10+
val n = nums.size
11+
val a = IntArray(n)
12+
for (i in 0.rangeUntil(n)) {
13+
a[i] = nums[i]
14+
}
15+
var ans = 1
16+
var previousLen = Int.Companion.MAX_VALUE
17+
var i = 0
18+
while (i < n) {
19+
var j = i + 1
20+
while (j < n && a[j - 1] < a[j]) {
21+
++j
22+
}
23+
val len = j - i
24+
ans = max(ans, (len / 2))
25+
if (previousLen != Int.Companion.MAX_VALUE) {
26+
ans = max(ans, min(previousLen, len))
27+
}
28+
previousLen = len
29+
i = j
30+
}
31+
return ans
32+
}
33+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3350\. Adjacent Increasing Subarrays Detection II
2+
3+
Medium
4+
5+
Given an array `nums` of `n` integers, your task is to find the **maximum** value of `k` for which there exist **two** adjacent subarrays of length `k` each, such that both subarrays are **strictly** **increasing**. Specifically, check if there are **two** subarrays of length `k` starting at indices `a` and `b` (`a < b`), where:
6+
7+
* Both subarrays `nums[a..a + k - 1]` and `nums[b..b + k - 1]` are **strictly increasing**.
8+
* The subarrays must be **adjacent**, meaning `b = a + k`.
9+
10+
Return the **maximum** _possible_ value of `k`.
11+
12+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,5,7,8,9,2,3,4,3,1]
17+
18+
**Output:** 3
19+
20+
**Explanation:**
21+
22+
* The subarray starting at index 2 is `[7, 8, 9]`, which is strictly increasing.
23+
* The subarray starting at index 5 is `[2, 3, 4]`, which is also strictly increasing.
24+
* These two subarrays are adjacent, and 3 is the **maximum** possible value of `k` for which two such adjacent strictly increasing subarrays exist.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [1,2,3,4,4,4,4,5,6,7]
29+
30+
**Output:** 2
31+
32+
**Explanation:**
33+
34+
* The subarray starting at index 0 is `[1, 2]`, which is strictly increasing.
35+
* The subarray starting at index 2 is `[3, 4]`, which is also strictly increasing.
36+
* These two subarrays are adjacent, and 2 is the **maximum** possible value of `k` for which two such adjacent strictly increasing subarrays exist.
37+
38+
**Constraints:**
39+
40+
* <code>2 <= nums.length <= 2 * 10<sup>5</sup></code>
41+
* <code>-10<sup>9</sup> <= nums[i] <= 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 g3301_3400.s3351_sum_of_good_subsequences
2+
3+
// #Hard #Array #Hash_Table #Dynamic_Programming
4+
// #2024_11_15_Time_16_ms_(100.00%)_Space_61.2_MB_(80.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun sumOfGoodSubsequences(nums: IntArray): Int {
10+
var max = 0
11+
for (x in nums) {
12+
max = max(x, max)
13+
}
14+
val count = LongArray(max + 3)
15+
val total = LongArray(max + 3)
16+
val mod = (1e9 + 7).toInt().toLong()
17+
var res: Long = 0
18+
for (a in nums) {
19+
count[a + 1] = (count[a] + count[a + 1] + count[a + 2] + 1) % mod
20+
val cur = total[a] + total[a + 2] + a * (count[a] + count[a + 2] + 1)
21+
total[a + 1] = (total[a + 1] + cur) % mod
22+
res = (res + cur) % mod
23+
}
24+
return res.toInt()
25+
}
26+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3351\. Sum of Good Subsequences
2+
3+
Hard
4+
5+
You are given an integer array `nums`. A **good** subsequence is defined as a subsequence of `nums` where the absolute difference between any **two** consecutive elements in the subsequence is **exactly** 1.
6+
7+
Return the **sum** of all _possible_ **good subsequences** of `nums`.
8+
9+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
10+
11+
**Note** that a subsequence of size 1 is considered good by definition.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,1]
16+
17+
**Output:** 14
18+
19+
**Explanation:**
20+
21+
* Good subsequences are: `[1]`, `[2]`, `[1]`, `[1,2]`, `[2,1]`, `[1,2,1]`.
22+
* The sum of elements in these subsequences is 14.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [3,4,5]
27+
28+
**Output:** 40
29+
30+
**Explanation:**
31+
32+
* Good subsequences are: `[3]`, `[4]`, `[5]`, `[3,4]`, `[4,5]`, `[3,4,5]`.
33+
* The sum of elements in these subsequences is 40.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
38+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g3301_3400.s3352_count_k_reducible_numbers_less_than_n
2+
3+
// #Hard #String #Dynamic_Programming #Math #Combinatorics
4+
// #2024_11_15_Time_170_ms_(100.00%)_Space_34.9_MB_(100.00%)
5+
6+
class Solution {
7+
fun countKReducibleNumbers(s: String, k: Int): Int {
8+
val n = s.length
9+
val reducible = IntArray(n + 1)
10+
for (i in 2.rangeUntil(reducible.size)) {
11+
reducible[i] = 1 + reducible[Integer.bitCount(i)]
12+
}
13+
val dp = LongArray(n + 1)
14+
var curr = 0
15+
for (i in 0.rangeUntil(n)) {
16+
for (j in i - 1 downTo 0) {
17+
dp[j + 1] += dp[j]
18+
dp[j + 1] %= MOD.toLong()
19+
}
20+
if (s[i] == '1') {
21+
dp[curr]++
22+
dp[curr] %= MOD.toLong()
23+
curr++
24+
}
25+
}
26+
var result: Long = 0
27+
for (i in 1..s.length) {
28+
if (reducible[i] < k) {
29+
result += dp[i]
30+
result %= MOD.toLong()
31+
}
32+
}
33+
return (result % MOD).toInt()
34+
}
35+
36+
companion object {
37+
private val MOD = (1e9 + 7).toInt()
38+
}
39+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3352\. Count K-Reducible Numbers Less Than N
2+
3+
Hard
4+
5+
You are given a **binary** string `s` representing a number `n` in its binary form.
6+
7+
You are also given an integer `k`.
8+
9+
An integer `x` is called **k-reducible** if performing the following operation **at most** `k` times reduces it to 1:
10+
11+
* Replace `x` with the **count** of set bits in its binary representation.
12+
13+
For example, the binary representation of 6 is `"110"`. Applying the operation once reduces it to 2 (since `"110"` has two set bits). Applying the operation again to 2 (binary `"10"`) reduces it to 1 (since `"10"` has one set bit).
14+
15+
Return an integer denoting the number of positive integers **less** than `n` that are **k-reducible**.
16+
17+
Since the answer may be too large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
18+
19+
**Example 1:**
20+
21+
**Input:** s = "111", k = 1
22+
23+
**Output:** 3
24+
25+
**Explanation:**
26+
27+
`n = 7`. The 1-reducible integers less than 7 are 1, 2, and 4.
28+
29+
**Example 2:**
30+
31+
**Input:** s = "1000", k = 2
32+
33+
**Output:** 6
34+
35+
**Explanation:**
36+
37+
`n = 8`. The 2-reducible integers less than 8 are 1, 2, 3, 4, 5, and 6.
38+
39+
**Example 3:**
40+
41+
**Input:** s = "1", k = 3
42+
43+
**Output:** 0
44+
45+
**Explanation:**
46+
47+
There are no positive integers less than `n = 1`, so the answer is 0.
48+
49+
**Constraints:**
50+
51+
* `1 <= s.length <= 800`
52+
* `s` has no leading zeros.
53+
* `s` consists only of the characters `'0'` and `'1'`.
54+
* `1 <= k <= 5`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3301_3400.s3349_adjacent_increasing_subarrays_detection_i
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 hasIncreasingSubarrays() {
10+
assertThat<Boolean>(
11+
Solution().hasIncreasingSubarrays(listOf<Int>(2, 5, 7, 8, 9, 2, 3, 4, 3, 1), 3),
12+
equalTo<Boolean>(true)
13+
)
14+
}
15+
16+
@Test
17+
fun hasIncreasingSubarrays2() {
18+
assertThat<Boolean>(
19+
Solution().hasIncreasingSubarrays(listOf<Int>(1, 2, 3, 4, 4, 4, 4, 5, 6, 7), 5),
20+
equalTo<Boolean>(false)
21+
)
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3301_3400.s3350_adjacent_increasing_subarrays_detection_ii
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 maxIncreasingSubarrays() {
10+
assertThat<Int>(
11+
Solution().maxIncreasingSubarrays(listOf<Int>(2, 5, 7, 8, 9, 2, 3, 4, 3, 1)),
12+
equalTo<Int>(3)
13+
)
14+
}
15+
16+
@Test
17+
fun maxIncreasingSubarrays2() {
18+
assertThat<Int>(
19+
Solution().maxIncreasingSubarrays(listOf<Int>(1, 2, 3, 4, 4, 4, 4, 5, 6, 7)),
20+
equalTo<Int>(2)
21+
)
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3301_3400.s3351_sum_of_good_subsequences
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 sumOfGoodSubsequences() {
10+
assertThat<Int>(
11+
Solution().sumOfGoodSubsequences(intArrayOf(1, 2, 1)),
12+
equalTo<Int>(14)
13+
)
14+
}
15+
16+
@Test
17+
fun sumOfGoodSubsequences2() {
18+
assertThat<Int>(
19+
Solution().sumOfGoodSubsequences(intArrayOf(3, 4, 5)),
20+
equalTo<Int>(40)
21+
)
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3301_3400.s3352_count_k_reducible_numbers_less_than_n
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 countKReducibleNumbers() {
10+
assertThat<Int>(Solution().countKReducibleNumbers("111", 1), equalTo<Int>(3))
11+
}
12+
13+
@Test
14+
fun countKReducibleNumbers2() {
15+
assertThat<Int>(Solution().countKReducibleNumbers("1000", 2), equalTo<Int>(6))
16+
}
17+
18+
@Test
19+
fun countKReducibleNumbers3() {
20+
assertThat<Int>(Solution().countKReducibleNumbers("1", 3), equalTo<Int>(0))
21+
}
22+
}

0 commit comments

Comments
 (0)