Skip to content

Added tasks 3349-3352 #722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package g3301_3400.s3349_adjacent_increasing_subarrays_detection_i

// #Easy #Array #2024_11_15_Time_179_ms_(97.92%)_Space_37.3_MB_(91.67%)

class Solution {
fun hasIncreasingSubarrays(nums: List<Int>, k: Int): Boolean {
val l = nums.size
if (l < k * 2) {
return false
}
for (i in 0.rangeUntil(l - 2 * k + 1)) {
if (check(i, k, nums) && check(i + k, k, nums)) {
return true
}
}
return false
}

private fun check(p: Int, k: Int, nums: List<Int>): Boolean {
for (i in p.rangeUntil(p + k - 1)) {
if (nums[i] >= nums[i + 1]) {
return false
}
}
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
3349\. Adjacent Increasing Subarrays Detection I

Easy

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:

* Both subarrays `nums[a..a + k - 1]` and `nums[b..b + k - 1]` are **strictly increasing**.
* The subarrays must be **adjacent**, meaning `b = a + k`.

Return `true` if it is _possible_ to find **two** such subarrays, and `false` otherwise.

**Example 1:**

**Input:** nums = [2,5,7,8,9,2,3,4,3,1], k = 3

**Output:** true

**Explanation:**

* The subarray starting at index `2` is `[7, 8, 9]`, which is strictly increasing.
* The subarray starting at index `5` is `[2, 3, 4]`, which is also strictly increasing.
* These two subarrays are adjacent, so the result is `true`.

**Example 2:**

**Input:** nums = [1,2,3,4,4,4,4,5,6,7], k = 5

**Output:** false

**Constraints:**

* `2 <= nums.length <= 100`
* `1 < 2 * k <= nums.length`
* `-1000 <= nums[i] <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package g3301_3400.s3350_adjacent_increasing_subarrays_detection_ii

// #Medium #Array #Binary_Search #2024_11_15_Time_947_ms_(48.57%)_Space_87.4_MB_(51.43%)

import kotlin.math.max
import kotlin.math.min

class Solution {
fun maxIncreasingSubarrays(nums: List<Int>): Int {
val n = nums.size
val a = IntArray(n)
for (i in 0.rangeUntil(n)) {
a[i] = nums[i]
}
var ans = 1
var previousLen = Int.Companion.MAX_VALUE
var i = 0
while (i < n) {
var j = i + 1
while (j < n && a[j - 1] < a[j]) {
++j
}
val len = j - i
ans = max(ans, (len / 2))
if (previousLen != Int.Companion.MAX_VALUE) {
ans = max(ans, min(previousLen, len))
}
previousLen = len
i = j
}
return ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
3350\. Adjacent Increasing Subarrays Detection II

Medium

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:

* Both subarrays `nums[a..a + k - 1]` and `nums[b..b + k - 1]` are **strictly increasing**.
* The subarrays must be **adjacent**, meaning `b = a + k`.

Return the **maximum** _possible_ value of `k`.

A **subarray** is a contiguous **non-empty** sequence of elements within an array.

**Example 1:**

**Input:** nums = [2,5,7,8,9,2,3,4,3,1]

**Output:** 3

**Explanation:**

* The subarray starting at index 2 is `[7, 8, 9]`, which is strictly increasing.
* The subarray starting at index 5 is `[2, 3, 4]`, which is also strictly increasing.
* These two subarrays are adjacent, and 3 is the **maximum** possible value of `k` for which two such adjacent strictly increasing subarrays exist.

**Example 2:**

**Input:** nums = [1,2,3,4,4,4,4,5,6,7]

**Output:** 2

**Explanation:**

* The subarray starting at index 0 is `[1, 2]`, which is strictly increasing.
* The subarray starting at index 2 is `[3, 4]`, which is also strictly increasing.
* These two subarrays are adjacent, and 2 is the **maximum** possible value of `k` for which two such adjacent strictly increasing subarrays exist.

**Constraints:**

* <code>2 <= nums.length <= 2 * 10<sup>5</sup></code>
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g3301_3400.s3351_sum_of_good_subsequences

// #Hard #Array #Hash_Table #Dynamic_Programming
// #2024_11_15_Time_16_ms_(100.00%)_Space_61.2_MB_(80.00%)

import kotlin.math.max

class Solution {
fun sumOfGoodSubsequences(nums: IntArray): Int {
var max = 0
for (x in nums) {
max = max(x, max)
}
val count = LongArray(max + 3)
val total = LongArray(max + 3)
val mod = (1e9 + 7).toInt().toLong()
var res: Long = 0
for (a in nums) {
count[a + 1] = (count[a] + count[a + 1] + count[a + 2] + 1) % mod
val cur = total[a] + total[a + 2] + a * (count[a] + count[a + 2] + 1)
total[a + 1] = (total[a + 1] + cur) % mod
res = (res + cur) % mod
}
return res.toInt()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
3351\. Sum of Good Subsequences

Hard

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.

Return the **sum** of all _possible_ **good subsequences** of `nums`.

Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

**Note** that a subsequence of size 1 is considered good by definition.

**Example 1:**

**Input:** nums = [1,2,1]

**Output:** 14

**Explanation:**

* Good subsequences are: `[1]`, `[2]`, `[1]`, `[1,2]`, `[2,1]`, `[1,2,1]`.
* The sum of elements in these subsequences is 14.

**Example 2:**

**Input:** nums = [3,4,5]

**Output:** 40

**Explanation:**

* Good subsequences are: `[3]`, `[4]`, `[5]`, `[3,4]`, `[4,5]`, `[3,4,5]`.
* The sum of elements in these subsequences is 40.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package g3301_3400.s3352_count_k_reducible_numbers_less_than_n

// #Hard #String #Dynamic_Programming #Math #Combinatorics
// #2024_11_15_Time_170_ms_(100.00%)_Space_34.9_MB_(100.00%)

class Solution {
fun countKReducibleNumbers(s: String, k: Int): Int {
val n = s.length
val reducible = IntArray(n + 1)
for (i in 2.rangeUntil(reducible.size)) {
reducible[i] = 1 + reducible[Integer.bitCount(i)]
}
val dp = LongArray(n + 1)
var curr = 0
for (i in 0.rangeUntil(n)) {
for (j in i - 1 downTo 0) {
dp[j + 1] += dp[j]
dp[j + 1] %= MOD.toLong()
}
if (s[i] == '1') {
dp[curr]++
dp[curr] %= MOD.toLong()
curr++
}
}
var result: Long = 0
for (i in 1..s.length) {
if (reducible[i] < k) {
result += dp[i]
result %= MOD.toLong()
}
}
return (result % MOD).toInt()
}

companion object {
private val MOD = (1e9 + 7).toInt()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
3352\. Count K-Reducible Numbers Less Than N

Hard

You are given a **binary** string `s` representing a number `n` in its binary form.

You are also given an integer `k`.

An integer `x` is called **k-reducible** if performing the following operation **at most** `k` times reduces it to 1:

* Replace `x` with the **count** of set bits in its binary representation.

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).

Return an integer denoting the number of positive integers **less** than `n` that are **k-reducible**.

Since the answer may be too large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

**Example 1:**

**Input:** s = "111", k = 1

**Output:** 3

**Explanation:**

`n = 7`. The 1-reducible integers less than 7 are 1, 2, and 4.

**Example 2:**

**Input:** s = "1000", k = 2

**Output:** 6

**Explanation:**

`n = 8`. The 2-reducible integers less than 8 are 1, 2, 3, 4, 5, and 6.

**Example 3:**

**Input:** s = "1", k = 3

**Output:** 0

**Explanation:**

There are no positive integers less than `n = 1`, so the answer is 0.

**Constraints:**

* `1 <= s.length <= 800`
* `s` has no leading zeros.
* `s` consists only of the characters `'0'` and `'1'`.
* `1 <= k <= 5`
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3301_3400.s3349_adjacent_increasing_subarrays_detection_i

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Test

internal class SolutionTest {
@Test
fun hasIncreasingSubarrays() {
assertThat<Boolean>(
Solution().hasIncreasingSubarrays(listOf<Int>(2, 5, 7, 8, 9, 2, 3, 4, 3, 1), 3),
equalTo<Boolean>(true)
)
}

@Test
fun hasIncreasingSubarrays2() {
assertThat<Boolean>(
Solution().hasIncreasingSubarrays(listOf<Int>(1, 2, 3, 4, 4, 4, 4, 5, 6, 7), 5),
equalTo<Boolean>(false)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3301_3400.s3350_adjacent_increasing_subarrays_detection_ii

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Test

internal class SolutionTest {
@Test
fun maxIncreasingSubarrays() {
assertThat<Int>(
Solution().maxIncreasingSubarrays(listOf<Int>(2, 5, 7, 8, 9, 2, 3, 4, 3, 1)),
equalTo<Int>(3)
)
}

@Test
fun maxIncreasingSubarrays2() {
assertThat<Int>(
Solution().maxIncreasingSubarrays(listOf<Int>(1, 2, 3, 4, 4, 4, 4, 5, 6, 7)),
equalTo<Int>(2)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3301_3400.s3351_sum_of_good_subsequences

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Test

internal class SolutionTest {
@Test
fun sumOfGoodSubsequences() {
assertThat<Int>(
Solution().sumOfGoodSubsequences(intArrayOf(1, 2, 1)),
equalTo<Int>(14)
)
}

@Test
fun sumOfGoodSubsequences2() {
assertThat<Int>(
Solution().sumOfGoodSubsequences(intArrayOf(3, 4, 5)),
equalTo<Int>(40)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g3301_3400.s3352_count_k_reducible_numbers_less_than_n

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Test

internal class SolutionTest {
@Test
fun countKReducibleNumbers() {
assertThat<Int>(Solution().countKReducibleNumbers("111", 1), equalTo<Int>(3))
}

@Test
fun countKReducibleNumbers2() {
assertThat<Int>(Solution().countKReducibleNumbers("1000", 2), equalTo<Int>(6))
}

@Test
fun countKReducibleNumbers3() {
assertThat<Int>(Solution().countKReducibleNumbers("1", 3), equalTo<Int>(0))
}
}