Skip to content

Commit 267c437

Browse files
committed
Added tasks 3692-3705
1 parent 8e4ab52 commit 267c437

File tree

14 files changed

+1342
-0
lines changed
  • src/main/kotlin
    • g3601_3700
      • s3692_majority_frequency_characters
      • s3693_climbing_stairs_ii
      • s3694_distinct_points_reachable_after_substring_removal
      • s3695_maximize_alternating_sum_using_swaps
      • s3697_compute_decimal_representation
      • s3698_split_array_with_minimum_difference
      • s3699_number_of_zigzag_arrays_i
      • s3700_number_of_zigzag_arrays_ii
    • g3701_3800
      • s3701_compute_alternating_sum
      • s3702_longest_subsequence_with_non_zero_bitwise_xor
      • s3703_remove_k_balanced_substrings
      • s3704_count_no_zero_pairs_that_sum_to_n
      • s3705_find_golden_hour_customers

14 files changed

+1342
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,19 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|---------
2091+
| 3705 |[Find Golden Hour Customers](src/main/kotlin/g3701_3800/s3705_find_golden_hour_customers)| Medium | Database | 281 | 71.26
2092+
| 3704 |[Count No-Zero Pairs That Sum to N](src/main/kotlin/g3701_3800/s3704_count_no_zero_pairs_that_sum_to_n)| Hard | Weekly_Contest_470 | 11 | 100.00
2093+
| 3703 |[Remove K-Balanced Substrings](src/main/kotlin/g3701_3800/s3703_remove_k_balanced_substrings)| Medium | Weekly_Contest_470 | 58 | 100.00
2094+
| 3702 |[Longest Subsequence With Non-Zero Bitwise XOR](src/main/kotlin/g3701_3800/s3702_longest_subsequence_with_non_zero_bitwise_xor)| Medium | Weekly_Contest_470 | 2 | 100.00
2095+
| 3701 |[Compute Alternating Sum](src/main/kotlin/g3701_3800/s3701_compute_alternating_sum)| Easy | Weekly_Contest_470 | 1 | 100.00
2096+
| 3700 |[Number of ZigZag Arrays II](src/main/kotlin/g3601_3700/s3700_number_of_zigzag_arrays_ii)| Hard | Dynamic_Programming, Math, Weekly_Contest_469 | 175 | 100.00
2097+
| 3699 |[Number of ZigZag Arrays I](src/main/kotlin/g3601_3700/s3699_number_of_zigzag_arrays_i)| Hard | Dynamic_Programming, Prefix_Sum, Weekly_Contest_469 | 227 | 78.57
2098+
| 3698 |[Split Array With Minimum Difference](src/main/kotlin/g3601_3700/s3698_split_array_with_minimum_difference)| Medium | Array, Prefix_Sum, Weekly_Contest_469 | 3 | 100.00
2099+
| 3697 |[Compute Decimal Representation](src/main/kotlin/g3601_3700/s3697_compute_decimal_representation)| Easy | Array, Math, Weekly_Contest_469 | 1 | 100.00
2100+
| 3695 |[Maximize Alternating Sum Using Swaps](src/main/kotlin/g3601_3700/s3695_maximize_alternating_sum_using_swaps)| Hard | Array, Sorting, Greedy, Union_Find, Biweekly_Contest_166 | 61 | 100.00
2101+
| 3694 |[Distinct Points Reachable After Substring Removal](src/main/kotlin/g3601_3700/s3694_distinct_points_reachable_after_substring_removal)| Medium | String, Hash_Table, Prefix_Sum, Sliding_Window, Biweekly_Contest_166 | 46 | 100.00
2102+
| 3693 |[Climbing Stairs II](src/main/kotlin/g3601_3700/s3693_climbing_stairs_ii)| Medium | Array, Dynamic_Programming, Biweekly_Contest_166 | 8 | 100.00
2103+
| 3692 |[Majority Frequency Characters](src/main/kotlin/g3601_3700/s3692_majority_frequency_characters)| Easy | String, Hash_Table, Counting, Biweekly_Contest_166 | 2 | 100.00
20912104
| 3691 |[Maximum Total Subarray Value II](src/main/kotlin/g3601_3700/s3691_maximum_total_subarray_value_ii)| Hard | Array, Greedy, Heap_Priority_Queue, Segment_Tree, Weekly_Contest_468 | 94 | 100.00
20922105
| 3690 |[Split and Merge Array Transformation](src/main/kotlin/g3601_3700/s3690_split_and_merge_array_transformation)| Medium | Array, Hash_Table, Breadth_First_Search, Weekly_Contest_468 | 12 | 100.00
20932106
| 3689 |[Maximum Total Subarray Value I](src/main/kotlin/g3601_3700/s3689_maximum_total_subarray_value_i)| Medium | Array, Greedy, Weekly_Contest_468 | 3 | 98.11
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
## 3692\. Majority Frequency Characters
5+
6+
Easy
7+
8+
You are given a string `s` consisting of lowercase English letters.
9+
10+
The **frequency group** for a value `k` is the set of characters that appear exactly `k` times in s.
11+
12+
The **majority frequency group** is the frequency group that contains the largest number of **distinct** characters.
13+
14+
Return a string containing all characters in the majority frequency group, in **any** order. If two or more frequency groups tie for that largest size, pick the group whose frequency `k` is **larger**.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "aaabbbccdddde"
19+
20+
**Output:** "ab"
21+
22+
**Explanation:**
23+
24+
| Frequency (k) | Distinct characters in group | Group size | Majority? |
25+
|---------------|------------------------------|------------|-----------|
26+
| 4 | {d} | 1 | No |
27+
| 3 | {a, b} | 2 | **Yes** |
28+
| 2 | {c} | 1 | No |
29+
| 1 | {e} | 1 | No |
30+
31+
Both characters `'a'` and `'b'` share the same frequency 3, they are in the majority frequency group. `"ba"` is also a valid answer.
32+
33+
**Example 2:**
34+
35+
**Input:** s = "abcd"
36+
37+
**Output:** "abcd"
38+
39+
**Explanation:**
40+
41+
| Frequency (k) | Distinct characters in group | Group size | Majority? |
42+
|---------------|------------------------------|------------|-----------|
43+
| 1 | {a, b, c, d} | 4 | **Yes** |
44+
45+
All characters share the same frequency 1, they are all in the majority frequency group.
46+
47+
**Example 3:**
48+
49+
**Input:** s = "pfpfgi"
50+
51+
**Output:** "fp"
52+
53+
**Explanation:**
54+
55+
| Frequency (k) | Distinct characters in group | Group size | Majority? |
56+
|---------------|------------------------------|------------|----------------------------------|
57+
| 2 | {p, f} | 2 | **Yes** |
58+
| 1 | {g, i} | 2 | No (tied size, lower frequency) |
59+
60+
Both characters `'p'` and `'f'` share the same frequency 2, they are in the majority frequency group. There is a tie in group size with frequency 1, but we pick the higher frequency: 2.
61+
62+
**Constraints:**
63+
64+
* `1 <= s.length <= 100`
65+
* `s` consists only of lowercase English letters.
66+
67+
## Solution
68+
69+
```kotlin
70+
class Solution {
71+
fun majorityFrequencyGroup(s: String): String {
72+
val cntArray = IntArray(26)
73+
for (i in 0..<s.length) {
74+
cntArray[s[i].code - 'a'.code]++
75+
}
76+
val freq = IntArray(s.length + 1)
77+
for (i in 0..25) {
78+
if (cntArray[i] > 0) {
79+
freq[cntArray[i]]++
80+
}
81+
}
82+
var size = 0
83+
var bfreq = 0
84+
for (i in 0..s.length) {
85+
val si = freq[i]
86+
if (si > size || (si == size && i > bfreq)) {
87+
size = si
88+
bfreq = i
89+
}
90+
}
91+
val sb = StringBuilder()
92+
for (i in 0..25) {
93+
if (cntArray[i] == bfreq) {
94+
sb.append((i + 'a'.code).toChar())
95+
}
96+
}
97+
return sb.toString()
98+
}
99+
}
100+
```
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
## 3693\. Climbing Stairs II
5+
6+
Medium
7+
8+
You are climbing a staircase with `n + 1` steps, numbered from 0 to `n`.
9+
10+
You are also given a **1-indexed** integer array `costs` of length `n`, where `costs[i]` is the cost of step `i`.
11+
12+
From step `i`, you can jump **only** to step `i + 1`, `i + 2`, or `i + 3`. The cost of jumping from step `i` to step `j` is defined as: <code>costs[j] + (j - i)<sup>2</sup></code>
13+
14+
You start from step 0 with `cost = 0`.
15+
16+
Return the **minimum** total cost to reach step `n`.
17+
18+
**Example 1:**
19+
20+
**Input:** n = 4, costs = [1,2,3,4]
21+
22+
**Output:** 13
23+
24+
**Explanation:**
25+
26+
One optimal path is `0 → 1 → 2 → 4`
27+
28+
Jump
29+
30+
Cost Calculation
31+
32+
Cost
33+
34+
0 → 1
35+
36+
<code>costs[1] + (1 - 0)<sup>2</sup> = 1 + 1</code>
37+
38+
2
39+
40+
1 → 2
41+
42+
<code>costs[2] + (2 - 1)<sup>2</sup> = 2 + 1</code>
43+
44+
3
45+
46+
2 → 4
47+
48+
<code>costs[4] + (4 - 2)<sup>2</sup> = 4 + 4</code>
49+
50+
8
51+
52+
Thus, the minimum total cost is `2 + 3 + 8 = 13`
53+
54+
**Example 2:**
55+
56+
**Input:** n = 4, costs = [5,1,6,2]
57+
58+
**Output:** 11
59+
60+
**Explanation:**
61+
62+
One optimal path is `0 → 2 → 4`
63+
64+
Jump
65+
66+
Cost Calculation
67+
68+
Cost
69+
70+
0 → 2
71+
72+
<code>costs[2] + (2 - 0)<sup>2</sup> = 1 + 4</code>
73+
74+
5
75+
76+
2 → 4
77+
78+
<code>costs[4] + (4 - 2)<sup>2</sup> = 2 + 4</code>
79+
80+
6
81+
82+
Thus, the minimum total cost is `5 + 6 = 11`
83+
84+
**Example 3:**
85+
86+
**Input:** n = 3, costs = [9,8,3]
87+
88+
**Output:** 12
89+
90+
**Explanation:**
91+
92+
The optimal path is `0 → 3` with total cost = <code>costs[3] + (3 - 0)<sup>2</sup> = 3 + 9 = 12</code>
93+
94+
**Constraints:**
95+
96+
* <code>1 <= n == costs.length <= 10<sup>5</sup></code>
97+
* <code>1 <= costs[i] <= 10<sup>4</sup></code>
98+
99+
## Solution
100+
101+
```kotlin
102+
import kotlin.math.min
103+
104+
@Suppress("unused")
105+
class Solution {
106+
fun climbStairs(n: Int, costs: IntArray): Int {
107+
if (costs.size == 1) {
108+
return costs[0] + 1
109+
}
110+
var one = costs[0] + 1
111+
var two = min(one + costs[1] + 1, costs[1] + 4)
112+
if (costs.size < 3) {
113+
return two
114+
}
115+
var three = min(one + costs[2] + 4, min(two + costs[2] + 1, costs[2] + 9))
116+
if (costs.size < 4) {
117+
return three
118+
}
119+
for (i in 3..<costs.size) {
120+
val four =
121+
(
122+
min(
123+
three + costs[i] + 1,
124+
min(two + costs[i] + 4, one + costs[i] + 9),
125+
)
126+
)
127+
one = two
128+
two = three
129+
three = four
130+
}
131+
return three
132+
}
133+
}
134+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
## 3694\. Distinct Points Reachable After Substring Removal
5+
6+
Medium
7+
8+
You are given a string `s` consisting of characters `'U'`, `'D'`, `'L'`, and `'R'`, representing moves on an infinite 2D Cartesian grid.
9+
10+
* `'U'`: Move from `(x, y)` to `(x, y + 1)`.
11+
* `'D'`: Move from `(x, y)` to `(x, y - 1)`.
12+
* `'L'`: Move from `(x, y)` to `(x - 1, y)`.
13+
* `'R'`: Move from `(x, y)` to `(x + 1, y)`.
14+
15+
You are also given a positive integer `k`.
16+
17+
You **must** choose and remove **exactly one** contiguous substring of length `k` from `s`. Then, start from coordinate `(0, 0)` and perform the remaining moves in order.
18+
19+
Return an integer denoting the number of **distinct** final coordinates reachable.
20+
21+
**Example 1:**
22+
23+
**Input:** s = "LUL", k = 1
24+
25+
**Output:** 2
26+
27+
**Explanation:**
28+
29+
After removing a substring of length 1, `s` can be `"UL"`, `"LL"` or `"LU"`. Following these moves, the final coordinates will be `(-1, 1)`, `(-2, 0)` and `(-1, 1)` respectively. There are two distinct points `(-1, 1)` and `(-2, 0)` so the answer is 2.
30+
31+
**Example 2:**
32+
33+
**Input:** s = "UDLR", k = 4
34+
35+
**Output:** 1
36+
37+
**Explanation:**
38+
39+
After removing a substring of length 4, `s` can only be the empty string. The final coordinates will be `(0, 0)`. There is only one distinct point `(0, 0)` so the answer is 1.
40+
41+
**Example 3:**
42+
43+
**Input:** s = "UU", k = 1
44+
45+
**Output:** 1
46+
47+
**Explanation:**
48+
49+
After removing a substring of length 1, `s` becomes `"U"`, which always ends at `(0, 1)`, so there is only one distinct final coordinate.
50+
51+
**Constraints:**
52+
53+
* <code>1 <= s.length <= 10<sup>5</sup></code>
54+
* `s` consists of only `'U'`, `'D'`, `'L'`, and `'R'`.
55+
* `1 <= k <= s.length`
56+
57+
## Solution
58+
59+
```kotlin
60+
class Solution {
61+
fun distinctPoints(s: String, k: Int): Int {
62+
val seen: MutableSet<Long> = HashSet()
63+
seen.add(0L)
64+
var x = 0
65+
var y = 0
66+
for (i in k..<s.length) {
67+
// add new step
68+
when (s[i]) {
69+
'U' -> y++
70+
'D' -> y--
71+
'L' -> x++
72+
'R' -> x--
73+
else -> x--
74+
}
75+
// remove old step
76+
when (s[i - k]) {
77+
'U' -> y--
78+
'D' -> y++
79+
'L' -> x--
80+
'R' -> x++
81+
else -> x++
82+
}
83+
seen.add(1000000L * x + y)
84+
}
85+
return seen.size
86+
}
87+
}
88+
```

0 commit comments

Comments
 (0)