Skip to content

Commit e9c5eb7

Browse files
committed
Added tasks 3498-3505
1 parent 1170f0c commit e9c5eb7

File tree

9 files changed

+1165
-155
lines changed
  • src/main/java
    • 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

9 files changed

+1165
-155
lines changed

README.md

Lines changed: 163 additions & 155 deletions
Large diffs are not rendered by default.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
58+
public class Solution {
59+
public int reverseDegree(String s) {
60+
int ans = 0;
61+
for (int i = 0; i < s.length(); ++i) {
62+
ans += (26 - (s.charAt(i) - 'a')) * (i + 1);
63+
}
64+
return ans;
65+
}
66+
}
67+
```
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
76+
public class Solution {
77+
public int maxActiveSectionsAfterTrade(String s) {
78+
int oneCount = 0;
79+
int convertedOne = 0;
80+
int curZeroCount = 0;
81+
int lastZeroCount = 0;
82+
for (int i = 0; i < s.length(); ++i) {
83+
if (s.charAt(i) == '0') {
84+
curZeroCount++;
85+
} else {
86+
if (curZeroCount != 0) {
87+
lastZeroCount = curZeroCount;
88+
}
89+
curZeroCount = 0;
90+
oneCount++;
91+
}
92+
convertedOne = Math.max(convertedOne, curZeroCount + lastZeroCount);
93+
}
94+
// corner case
95+
if (convertedOne == curZeroCount || convertedOne == lastZeroCount) {
96+
return oneCount;
97+
}
98+
return oneCount + convertedOne;
99+
}
100+
}
101+
```
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
55+
@SuppressWarnings("java:S107")
56+
public class Solution {
57+
public long minimumCost(int[] nums, int[] cost, int k) {
58+
int n = nums.length;
59+
long kLong = k;
60+
long[] preNums = new long[n + 1];
61+
long[] preCost = new long[n + 1];
62+
for (int i = 0; i < n; i++) {
63+
preNums[i + 1] = preNums[i] + nums[i];
64+
preCost[i + 1] = preCost[i] + cost[i];
65+
}
66+
long[] dp = new long[n + 1];
67+
for (int i = 0; i <= n; i++) {
68+
dp[i] = Long.MAX_VALUE / 2;
69+
}
70+
dp[0] = 0L;
71+
for (int r = 1; r <= n; r++) {
72+
for (int l = 0; l < r; l++) {
73+
long sumNums = preNums[r] * (preCost[r] - preCost[l]);
74+
long sumCost = kLong * (preCost[n] - preCost[l]);
75+
dp[r] = Math.min(dp[r], dp[l] + sumNums + sumCost);
76+
}
77+
}
78+
return dp[n];
79+
}
80+
}
81+
```

0 commit comments

Comments
 (0)