Skip to content

Added tasks 3498-3505 #792

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 4 commits into from
Apr 1, 2025
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
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ repositories {

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:2.1.20")
testImplementation("org.junit.jupiter:junit-jupiter:[5.11.3,)")
testImplementation("org.junit.jupiter:junit-jupiter:[5.12.0,)")
testImplementation("org.hamcrest:hamcrest-core:[3.0,)")
testImplementation("org.zapodot:embedded-db-junit-jupiter:[2.2.0,)")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:[1.11.3,)")
testImplementation("org.zapodot:embedded-db-junit-jupiter:2.2.0")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:[1.12.0,)")
}

tasks.test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package g3401_3500.s3498_reverse_degree_of_a_string

// #Easy #String #Simulation #2025_04_01_Time_2_ms_(87.18%)_Space_42.65_MB_(89.74%)

class Solution {
fun reverseDegree(s: String): Int {
var ans = 0
for (i in 0..<s.length) {
ans += (26 - (s[i].code - 'a'.code)) * (i + 1)
}
return ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
3498\. Reverse Degree of a String

Easy

Given a string `s`, calculate its **reverse degree**.

The **reverse degree** is calculated as follows:

1. For each character, multiply its position in the _reversed_ alphabet (`'a'` = 26, `'b'` = 25, ..., `'z'` = 1) with its position in the string **(1-indexed)**.
2. Sum these products for all characters in the string.

Return the **reverse degree** of `s`.

**Example 1:**

**Input:** s = "abc"

**Output:** 148

**Explanation:**

| Letter | Index in Reversed Alphabet | Index in String | Product |
|---------|----------------------------|----------------|---------|
| `'a'` | 26 | 1 | 26 |
| `'b'` | 25 | 2 | 50 |
| `'c'` | 24 | 3 | 72 |

The reversed degree is `26 + 50 + 72 = 148`.

**Example 2:**

**Input:** s = "zaza"

**Output:** 160

**Explanation:**

| Letter | Index in Reversed Alphabet | Index in String | Product |
|---------|----------------------------|----------------|---------|
| `'z'` | 1 | 1 | 1 |
| `'a'` | 26 | 2 | 52 |
| `'z'` | 1 | 3 | 3 |
| `'a'` | 26 | 4 | 104 |

The reverse degree is `1 + 52 + 3 + 104 = 160`.

**Constraints:**

* `1 <= s.length <= 1000`
* `s` contains only lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g3401_3500.s3499_maximize_active_section_with_trade_i

// #Medium #String #Enumeration #2025_04_01_Time_52_ms_(73.08%)_Space_47.99_MB_(100.00%)

import kotlin.math.max

class Solution {
fun maxActiveSectionsAfterTrade(s: String): Int {
var oneCount = 0
var convertedOne = 0
var curZeroCount = 0
var lastZeroCount = 0
for (i in 0..<s.length) {
if (s[i] == '0') {
curZeroCount++
} else {
if (curZeroCount != 0) {
lastZeroCount = curZeroCount
}
curZeroCount = 0
oneCount++
}
convertedOne = max(convertedOne, curZeroCount + lastZeroCount)
}
// corner case
if (convertedOne == curZeroCount || convertedOne == lastZeroCount) {
return oneCount
}
return oneCount + convertedOne
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
3499\. Maximize Active Section with Trade I

Medium

You are given a binary string `s` of length `n`, where:

* `'1'` represents an **active** section.
* `'0'` represents an **inactive** section.

You can perform **at most one trade** to maximize the number of active sections in `s`. In a trade, you:

* Convert a contiguous block of `'1'`s that is surrounded by `'0'`s to all `'0'`s.
* Afterward, convert a contiguous block of `'0'`s that is surrounded by `'1'`s to all `'1'`s.

Return the **maximum** number of active sections in `s` after making the optimal trade.

**Note:** Treat `s` as if it is **augmented** with a `'1'` at both ends, forming `t = '1' + s + '1'`. The augmented `'1'`s **do not** contribute to the final count.

**Example 1:**

**Input:** s = "01"

**Output:** 1

**Explanation:**

Because there is no block of `'1'`s surrounded by `'0'`s, no valid trade is possible. The maximum number of active sections is 1.

**Example 2:**

**Input:** s = "0100"

**Output:** 4

**Explanation:**

* String `"0100"` → Augmented to `"101001"`.
* Choose `"0100"`, convert <code>"10<ins>**1**</ins>001"</code> → <code>"1<ins>**0000**</ins>1"</code> → <code>"1<ins>**1111**</ins>1"</code>.
* The final string without augmentation is `"1111"`. The maximum number of active sections is 4.

**Example 3:**

**Input:** s = "1000100"

**Output:** 7

**Explanation:**

* String `"1000100"` → Augmented to `"110001001"`.
* Choose `"000100"`, convert <code>"11000<ins>**1**</ins>001"</code> → <code>"11<ins>**000000**</ins>1"</code> → <code>"11<ins>**111111**</ins>1"</code>.
* The final string without augmentation is `"1111111"`. The maximum number of active sections is 7.

**Example 4:**

**Input:** s = "01010"

**Output:** 4

**Explanation:**

* String `"01010"` → Augmented to `"1010101"`.
* Choose `"010"`, convert <code>"10<ins>**1**</ins>0101"</code> → <code>"1<ins>**000**</ins>101"</code> → <code>"1<ins>**111**</ins>101"</code>.
* The final string without augmentation is `"11110"`. The maximum number of active sections is 4.

**Constraints:**

* <code>1 <= n == s.length <= 10<sup>5</sup></code>
* `s[i]` is either `'0'` or `'1'`
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g3401_3500.s3500_minimum_cost_to_divide_array_into_subarrays

// #Hard #Array #Dynamic_Programming #Prefix_Sum
// #2025_04_01_Time_28_ms_(92.31%)_Space_49.69_MB_(69.23%)

class Solution {
fun minimumCost(nums: IntArray, cost: IntArray, k: Int): Long {
val n = nums.size
val k = k.toLong()
val preNums = LongArray(n + 1)
val preCost = LongArray(n + 1)
for (i in 0..n - 1) {
preNums[i + 1] = preNums[i] + nums[i]
preCost[i + 1] = preCost[i] + cost[i]
}
val dp = LongArray(n + 1) {
Long.MAX_VALUE / 2
}.also { it[0] = 0L }
for (r in 1..n) for (l in 0..r - 1) {
val sumNums = preNums[r] * (preCost[r] - preCost[l])
val sumCost = k * (preCost[n] - preCost[l])
dp[r] = minOf(dp[r], dp[l] + sumNums + sumCost)
}
return dp[n]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
3500\. Minimum Cost to Divide Array Into Subarrays

Hard

You are given two integer arrays, `nums` and `cost`, of the same size, and an integer `k`.

You can divide `nums` into **non-empty subarrays**. The cost of the <code>i<sup>th</sup></code> subarray consisting of elements `nums[l..r]` is:

* `(nums[0] + nums[1] + ... + nums[r] + k * i) * (cost[l] + cost[l + 1] + ... + cost[r])`.

**Note** that `i` represents the order of the subarray: 1 for the first subarray, 2 for the second, and so on.

Return the **minimum** total cost possible from any valid division.

**Example 1:**

**Input:** nums = [3,1,4], cost = [4,6,6], k = 1

**Output:** 110

**Explanation:**

The minimum total cost possible can be achieved by dividing `nums` into subarrays `[3, 1]` and `[4]`.

* The cost of the first subarray `[3,1]` is `(3 + 1 + 1 * 1) * (4 + 6) = 50`.
* The cost of the second subarray `[4]` is `(3 + 1 + 4 + 1 * 2) * 6 = 60`.

**Example 2:**

**Input:** nums = [4,8,5,1,14,2,2,12,1], cost = [7,2,8,4,2,2,1,1,2], k = 7

**Output:** 985

**Explanation:**

The minimum total cost possible can be achieved by dividing `nums` into subarrays `[4, 8, 5, 1]`, `[14, 2, 2]`, and `[12, 1]`.

* The cost of the first subarray `[4, 8, 5, 1]` is `(4 + 8 + 5 + 1 + 7 * 1) * (7 + 2 + 8 + 4) = 525`.
* The cost of the second subarray `[14, 2, 2]` is `(4 + 8 + 5 + 1 + 14 + 2 + 2 + 7 * 2) * (2 + 2 + 1) = 250`.
* The cost of the third subarray `[12, 1]` is `(4 + 8 + 5 + 1 + 14 + 2 + 2 + 12 + 1 + 7 * 3) * (1 + 2) = 210`.

**Constraints:**

* `1 <= nums.length <= 1000`
* `cost.length == nums.length`
* `1 <= nums[i], cost[i] <= 1000`
* `1 <= k <= 1000`
Loading
Loading