Skip to content

Commit cc21c81

Browse files
committed
Added tasks 3498-3501
1 parent 3424093 commit cc21c81

File tree

12 files changed

+617
-0
lines changed

12 files changed

+617
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g3401_3500.s3498_reverse_degree_of_a_string;
2+
3+
// #Easy #2025_03_30_Time_1_ms_(100.00%)_Space_43.05_MB_(100.00%)
4+
5+
public class Solution {
6+
public int reverseDegree(String s) {
7+
int ans = 0;
8+
for (int i = 0; i < s.length(); ++i) {
9+
ans += (26 - (s.charAt(i) - 'a')) * (i + 1);
10+
}
11+
return ans;
12+
}
13+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3498\. Reverse Degree of a String
2+
3+
Easy
4+
5+
Given a string `s`, calculate its **reverse degree**.
6+
7+
The **reverse degree** is calculated as follows:
8+
9+
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)**.
10+
2. Sum these products for all characters in the string.
11+
12+
Return the **reverse degree** of `s`.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "abc"
17+
18+
**Output:** 148
19+
20+
**Explanation:**
21+
22+
| Letter | Index in Reversed Alphabet | Index in String | Product |
23+
|---------|----------------------------|----------------|---------|
24+
| `'a'` | 26 | 1 | 26 |
25+
| `'b'` | 25 | 2 | 50 |
26+
| `'c'` | 24 | 3 | 72 |
27+
28+
The reversed degree is `26 + 50 + 72 = 148`.
29+
30+
**Example 2:**
31+
32+
**Input:** s = "zaza"
33+
34+
**Output:** 160
35+
36+
**Explanation:**
37+
38+
| Letter | Index in Reversed Alphabet | Index in String | Product |
39+
|---------|----------------------------|----------------|---------|
40+
| `'z'` | 1 | 1 | 1 |
41+
| `'a'` | 26 | 2 | 52 |
42+
| `'z'` | 1 | 3 | 3 |
43+
| `'a'` | 26 | 4 | 104 |
44+
45+
The reverse degree is `1 + 52 + 3 + 104 = 160`.
46+
47+
**Constraints:**
48+
49+
* `1 <= s.length <= 1000`
50+
* `s` contains only lowercase English letters.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3401_3500.s3499_maximize_active_section_with_trade_i;
2+
3+
// #Medium #2025_03_30_Time_55_ms_(100.00%)_Space_45.50_MB_(100.00%)
4+
5+
public class Solution {
6+
public int maxActiveSectionsAfterTrade(String s) {
7+
int oneCount = 0;
8+
int convertedOne = 0;
9+
int curZeroCount = 0;
10+
int lastZeroCount = 0;
11+
for (int i = 0; i < s.length(); ++i) {
12+
if (s.charAt(i) == '0') {
13+
curZeroCount++;
14+
} else {
15+
if (curZeroCount != 0) {
16+
lastZeroCount = curZeroCount;
17+
}
18+
curZeroCount = 0;
19+
oneCount++;
20+
}
21+
convertedOne = Math.max(convertedOne, curZeroCount + lastZeroCount);
22+
}
23+
// corner case
24+
if (convertedOne == curZeroCount || convertedOne == lastZeroCount) {
25+
return oneCount;
26+
}
27+
return oneCount + convertedOne;
28+
}
29+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
3499\. Maximize Active Section with Trade I
2+
3+
Medium
4+
5+
You are given a binary string `s` of length `n`, where:
6+
7+
* `'1'` represents an **active** section.
8+
* `'0'` represents an **inactive** section.
9+
10+
You can perform **at most one trade** to maximize the number of active sections in `s`. In a trade, you:
11+
12+
* Convert a contiguous block of `'1'`s that is surrounded by `'0'`s to all `'0'`s.
13+
* Afterward, convert a contiguous block of `'0'`s that is surrounded by `'1'`s to all `'1'`s.
14+
15+
Return the **maximum** number of active sections in `s` after making the optimal trade.
16+
17+
**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.
18+
19+
**Example 1:**
20+
21+
**Input:** s = "01"
22+
23+
**Output:** 1
24+
25+
**Explanation:**
26+
27+
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.
28+
29+
**Example 2:**
30+
31+
**Input:** s = "0100"
32+
33+
**Output:** 4
34+
35+
**Explanation:**
36+
37+
* String `"0100"` → Augmented to `"101001"`.
38+
* Choose `"0100"`, convert <code>"10<ins>**1**</ins>001"</code> → <code>"1<ins>**0000**</ins>1"</code> → <code>"1<ins>**1111**</ins>1"</code>.
39+
* The final string without augmentation is `"1111"`. The maximum number of active sections is 4.
40+
41+
**Example 3:**
42+
43+
**Input:** s = "1000100"
44+
45+
**Output:** 7
46+
47+
**Explanation:**
48+
49+
* String `"1000100"` → Augmented to `"110001001"`.
50+
* Choose `"000100"`, convert <code>"11000<ins>**1**</ins>001"</code> → <code>"11<ins>**000000**</ins>1"</code> → <code>"11<ins>**111111**</ins>1"</code>.
51+
* The final string without augmentation is `"1111111"`. The maximum number of active sections is 7.
52+
53+
**Example 4:**
54+
55+
**Input:** s = "01010"
56+
57+
**Output:** 4
58+
59+
**Explanation:**
60+
61+
* String `"01010"` → Augmented to `"1010101"`.
62+
* Choose `"010"`, convert <code>"10<ins>**1**</ins>0101"</code> → <code>"1<ins>**000**</ins>101"</code> → <code>"1<ins>**111**</ins>101"</code>.
63+
* The final string without augmentation is `"11110"`. The maximum number of active sections is 4.
64+
65+
**Constraints:**
66+
67+
* <code>1 <= n == s.length <= 10<sup>5</sup></code>
68+
* `s[i]` is either `'0'` or `'1'`
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package g3401_3500.s3500_minimum_cost_to_divide_array_into_subarrays;
2+
3+
// #Hard #2025_03_30_Time_225_ms_(94.64%)_Space_62.54_MB_(58.93%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public long minimumCost(int[] nums, int[] cost, int k) {
9+
int n = nums.length;
10+
long[] prefixNums = new long[n];
11+
long total = 0;
12+
for (int i = 0; i < n; i++) {
13+
total += nums[i];
14+
prefixNums[i] = total;
15+
}
16+
long[] prefixCost = new long[n + 1];
17+
total = 0;
18+
for (int i = 0; i < n; i++) {
19+
total += cost[i];
20+
prefixCost[i + 1] = total;
21+
}
22+
long[][] memo = new long[n][n + 1];
23+
for (long[] row : memo) {
24+
Arrays.fill(row, -1);
25+
}
26+
int[] bestSplit = new int[n];
27+
Arrays.fill(bestSplit, -1);
28+
return rec(0, 1, nums, prefixNums, prefixCost, k, memo, bestSplit);
29+
}
30+
31+
private long rec(
32+
int index,
33+
int i,
34+
int[] nums,
35+
long[] prefixNums,
36+
long[] prefixCost,
37+
int k,
38+
long[][] memo,
39+
int[] bestSplit) {
40+
int n = nums.length;
41+
if (index == n) {
42+
return 0;
43+
}
44+
if (memo[index][i] != -1) {
45+
return memo[index][i];
46+
}
47+
if (bestSplit[index] != -1) {
48+
int j = bestSplit[index];
49+
long val =
50+
(prefixNums[j] + (long) k * i) * (prefixCost[j + 1] - prefixCost[index])
51+
+ rec(j + 1, i + 1, nums, prefixNums, prefixCost, k, memo, bestSplit);
52+
return memo[index][i] = val;
53+
}
54+
long best = Long.MAX_VALUE;
55+
int bestIndex = -1;
56+
for (int j = index; j < n; j++) {
57+
long val =
58+
(prefixNums[j] + (long) k * i) * (prefixCost[j + 1] - prefixCost[index])
59+
+ rec(j + 1, i + 1, nums, prefixNums, prefixCost, k, memo, bestSplit);
60+
if (val < best) {
61+
best = val;
62+
bestIndex = j;
63+
}
64+
}
65+
bestSplit[index] = bestIndex;
66+
memo[index][i] = best;
67+
return best;
68+
}
69+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3500\. Minimum Cost to Divide Array Into Subarrays
2+
3+
Hard
4+
5+
You are given two integer arrays, `nums` and `cost`, of the same size, and an integer `k`.
6+
7+
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:
8+
9+
* `(nums[0] + nums[1] + ... + nums[r] + k * i) * (cost[l] + cost[l + 1] + ... + cost[r])`.
10+
11+
**Note** that `i` represents the order of the subarray: 1 for the first subarray, 2 for the second, and so on.
12+
13+
Return the **minimum** total cost possible from any valid division.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [3,1,4], cost = [4,6,6], k = 1
18+
19+
**Output:** 110
20+
21+
**Explanation:**
22+
23+
The minimum total cost possible can be achieved by dividing `nums` into subarrays `[3, 1]` and `[4]`.
24+
25+
* The cost of the first subarray `[3,1]` is `(3 + 1 + 1 * 1) * (4 + 6) = 50`.
26+
* The cost of the second subarray `[4]` is `(3 + 1 + 4 + 1 * 2) * 6 = 60`.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [4,8,5,1,14,2,2,12,1], cost = [7,2,8,4,2,2,1,1,2], k = 7
31+
32+
**Output:** 985
33+
34+
**Explanation:**
35+
36+
The minimum total cost possible can be achieved by dividing `nums` into subarrays `[4, 8, 5, 1]`, `[14, 2, 2]`, and `[12, 1]`.
37+
38+
* The cost of the first subarray `[4, 8, 5, 1]` is `(4 + 8 + 5 + 1 + 7 * 1) * (7 + 2 + 8 + 4) = 525`.
39+
* The cost of the second subarray `[14, 2, 2]` is `(4 + 8 + 5 + 1 + 14 + 2 + 2 + 7 * 2) * (2 + 2 + 1) = 250`.
40+
* The cost of the third subarray `[12, 1]` is `(4 + 8 + 5 + 1 + 14 + 2 + 2 + 12 + 1 + 7 * 3) * (1 + 2) = 210`.
41+
42+
**Constraints:**
43+
44+
* `1 <= nums.length <= 1000`
45+
* `cost.length == nums.length`
46+
* `1 <= nums[i], cost[i] <= 1000`
47+
* `1 <= k <= 1000`

0 commit comments

Comments
 (0)