Skip to content

Commit 19734f2

Browse files
committed
Added tasks 3498-3505
1 parent 4720597 commit 19734f2

File tree

10 files changed

+1195
-165
lines changed
  • src/main/kotlin
    • g0201_0300/s0218_the_skyline_problem
    • g3401_3500
      • s3498_reverse_degree_of_a_string
      • s3499_maximize_active_section_with_trade_i
      • s3500_minimum_cost_to_divide_array_into_subarrays
    • g3501_3600
      • s3501_maximize_active_section_with_trade_ii
      • s3502_minimum_cost_to_reach_every_position
      • s3503_longest_palindrome_after_substring_concatenation_i
      • s3504_longest_palindrome_after_substring_concatenation_ii
      • s3505_minimum_operations_to_make_elements_within_k_subarrays_equal

10 files changed

+1195
-165
lines changed

README.md

+164-156
Large diffs are not rendered by default.

src/main/kotlin/g0201_0300/s0218_the_skyline_problem/readme.md

+6-9
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,18 @@ class Solution {
5454
return ans
5555
}
5656
val totalBuildings = blds.size
57-
val buildings = Array<Building?>(totalBuildings * 2) { null }
58-
var idx = 0
59-
for (building in blds) {
60-
buildings[idx] = Building(building[0], building[2], true)
61-
buildings[idx + 1] = Building(building[1], building[2], false)
62-
idx += 2
57+
val buildings = Array(totalBuildings * 2) { i ->
58+
if (i % 2 == 0) {
59+
Building(blds[i / 2][0], blds[i / 2][2], true)
60+
} else {
61+
Building(blds[i / 2][1], blds[i / 2][2], false)
62+
}
6363
}
6464
buildings.sort()
6565
val skyline = TreeMap<Int, Int>()
6666
skyline[0] = 1
6767
var prevMaxHeight = 0
6868
for (building in buildings) {
69-
if (building == null) {
70-
continue
71-
}
7269
val height = building.height
7370
if (building.isStart) {
7471
skyline[height] = 1 + (skyline[height] ?: 0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3498\. Reverse Degree of a String
5+
6+
Easy
7+
8+
Given a string `s`, calculate its **reverse degree**.
9+
10+
The **reverse degree** is calculated as follows:
11+
12+
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)**.
13+
2. Sum these products for all characters in the string.
14+
15+
Return the **reverse degree** of `s`.
16+
17+
**Example 1:**
18+
19+
**Input:** s = "abc"
20+
21+
**Output:** 148
22+
23+
**Explanation:**
24+
25+
| Letter | Index in Reversed Alphabet | Index in String | Product |
26+
|---------|----------------------------|----------------|---------|
27+
| `'a'` | 26 | 1 | 26 |
28+
| `'b'` | 25 | 2 | 50 |
29+
| `'c'` | 24 | 3 | 72 |
30+
31+
The reversed degree is `26 + 50 + 72 = 148`.
32+
33+
**Example 2:**
34+
35+
**Input:** s = "zaza"
36+
37+
**Output:** 160
38+
39+
**Explanation:**
40+
41+
| Letter | Index in Reversed Alphabet | Index in String | Product |
42+
|---------|----------------------------|----------------|---------|
43+
| `'z'` | 1 | 1 | 1 |
44+
| `'a'` | 26 | 2 | 52 |
45+
| `'z'` | 1 | 3 | 3 |
46+
| `'a'` | 26 | 4 | 104 |
47+
48+
The reverse degree is `1 + 52 + 3 + 104 = 160`.
49+
50+
**Constraints:**
51+
52+
* `1 <= s.length <= 1000`
53+
* `s` contains only lowercase English letters.
54+
55+
## Solution
56+
57+
```kotlin
58+
class Solution {
59+
fun reverseDegree(s: String): Int {
60+
var ans = 0
61+
for (i in 0..<s.length) {
62+
ans += (26 - (s[i].code - 'a'.code)) * (i + 1)
63+
}
64+
return ans
65+
}
66+
}
67+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3499\. Maximize Active Section with Trade I
5+
6+
Medium
7+
8+
You are given a binary string `s` of length `n`, where:
9+
10+
* `'1'` represents an **active** section.
11+
* `'0'` represents an **inactive** section.
12+
13+
You can perform **at most one trade** to maximize the number of active sections in `s`. In a trade, you:
14+
15+
* Convert a contiguous block of `'1'`s that is surrounded by `'0'`s to all `'0'`s.
16+
* Afterward, convert a contiguous block of `'0'`s that is surrounded by `'1'`s to all `'1'`s.
17+
18+
Return the **maximum** number of active sections in `s` after making the optimal trade.
19+
20+
**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.
21+
22+
**Example 1:**
23+
24+
**Input:** s = "01"
25+
26+
**Output:** 1
27+
28+
**Explanation:**
29+
30+
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.
31+
32+
**Example 2:**
33+
34+
**Input:** s = "0100"
35+
36+
**Output:** 4
37+
38+
**Explanation:**
39+
40+
* String `"0100"` → Augmented to `"101001"`.
41+
* Choose `"0100"`, convert <code>"10<ins>**1**</ins>001"</code> → <code>"1<ins>**0000**</ins>1"</code> → <code>"1<ins>**1111**</ins>1"</code>.
42+
* The final string without augmentation is `"1111"`. The maximum number of active sections is 4.
43+
44+
**Example 3:**
45+
46+
**Input:** s = "1000100"
47+
48+
**Output:** 7
49+
50+
**Explanation:**
51+
52+
* String `"1000100"` → Augmented to `"110001001"`.
53+
* Choose `"000100"`, convert <code>"11000<ins>**1**</ins>001"</code> → <code>"11<ins>**000000**</ins>1"</code> → <code>"11<ins>**111111**</ins>1"</code>.
54+
* The final string without augmentation is `"1111111"`. The maximum number of active sections is 7.
55+
56+
**Example 4:**
57+
58+
**Input:** s = "01010"
59+
60+
**Output:** 4
61+
62+
**Explanation:**
63+
64+
* String `"01010"` → Augmented to `"1010101"`.
65+
* Choose `"010"`, convert <code>"10<ins>**1**</ins>0101"</code> → <code>"1<ins>**000**</ins>101"</code> → <code>"1<ins>**111**</ins>101"</code>.
66+
* The final string without augmentation is `"11110"`. The maximum number of active sections is 4.
67+
68+
**Constraints:**
69+
70+
* <code>1 <= n == s.length <= 10<sup>5</sup></code>
71+
* `s[i]` is either `'0'` or `'1'`
72+
73+
## Solution
74+
75+
```kotlin
76+
import kotlin.math.max
77+
78+
class Solution {
79+
fun maxActiveSectionsAfterTrade(s: String): Int {
80+
var oneCount = 0
81+
var convertedOne = 0
82+
var curZeroCount = 0
83+
var lastZeroCount = 0
84+
for (i in 0..<s.length) {
85+
if (s[i] == '0') {
86+
curZeroCount++
87+
} else {
88+
if (curZeroCount != 0) {
89+
lastZeroCount = curZeroCount
90+
}
91+
curZeroCount = 0
92+
oneCount++
93+
}
94+
convertedOne = max(convertedOne, curZeroCount + lastZeroCount)
95+
}
96+
// corner case
97+
if (convertedOne == curZeroCount || convertedOne == lastZeroCount) {
98+
return oneCount
99+
}
100+
return oneCount + convertedOne
101+
}
102+
}
103+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3500\. Minimum Cost to Divide Array Into Subarrays
5+
6+
Hard
7+
8+
You are given two integer arrays, `nums` and `cost`, of the same size, and an integer `k`.
9+
10+
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:
11+
12+
* `(nums[0] + nums[1] + ... + nums[r] + k * i) * (cost[l] + cost[l + 1] + ... + cost[r])`.
13+
14+
**Note** that `i` represents the order of the subarray: 1 for the first subarray, 2 for the second, and so on.
15+
16+
Return the **minimum** total cost possible from any valid division.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [3,1,4], cost = [4,6,6], k = 1
21+
22+
**Output:** 110
23+
24+
**Explanation:**
25+
26+
The minimum total cost possible can be achieved by dividing `nums` into subarrays `[3, 1]` and `[4]`.
27+
28+
* The cost of the first subarray `[3,1]` is `(3 + 1 + 1 * 1) * (4 + 6) = 50`.
29+
* The cost of the second subarray `[4]` is `(3 + 1 + 4 + 1 * 2) * 6 = 60`.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [4,8,5,1,14,2,2,12,1], cost = [7,2,8,4,2,2,1,1,2], k = 7
34+
35+
**Output:** 985
36+
37+
**Explanation:**
38+
39+
The minimum total cost possible can be achieved by dividing `nums` into subarrays `[4, 8, 5, 1]`, `[14, 2, 2]`, and `[12, 1]`.
40+
41+
* The cost of the first subarray `[4, 8, 5, 1]` is `(4 + 8 + 5 + 1 + 7 * 1) * (7 + 2 + 8 + 4) = 525`.
42+
* The cost of the second subarray `[14, 2, 2]` is `(4 + 8 + 5 + 1 + 14 + 2 + 2 + 7 * 2) * (2 + 2 + 1) = 250`.
43+
* The cost of the third subarray `[12, 1]` is `(4 + 8 + 5 + 1 + 14 + 2 + 2 + 12 + 1 + 7 * 3) * (1 + 2) = 210`.
44+
45+
**Constraints:**
46+
47+
* `1 <= nums.length <= 1000`
48+
* `cost.length == nums.length`
49+
* `1 <= nums[i], cost[i] <= 1000`
50+
* `1 <= k <= 1000`
51+
52+
## Solution
53+
54+
```kotlin
55+
class Solution {
56+
fun minimumCost(nums: IntArray, cost: IntArray, k: Int): Long {
57+
val n = nums.size
58+
val k = k.toLong()
59+
val preNums = LongArray(n + 1)
60+
val preCost = LongArray(n + 1)
61+
for (i in 0..n - 1) {
62+
preNums[i + 1] = preNums[i] + nums[i]
63+
preCost[i + 1] = preCost[i] + cost[i]
64+
}
65+
val dp = LongArray(n + 1) {
66+
Long.MAX_VALUE / 2
67+
}.also { it[0] = 0L }
68+
for (r in 1..n) for (l in 0..r - 1) {
69+
val sumNums = preNums[r] * (preCost[r] - preCost[l])
70+
val sumCost = k * (preCost[n] - preCost[l])
71+
dp[r] = minOf(dp[r], dp[l] + sumNums + sumCost)
72+
}
73+
return dp[n]
74+
}
75+
}
76+
```

0 commit comments

Comments
 (0)