Skip to content

Commit 5d4c922

Browse files
authored
Added tasks 3184-3213
1 parent 30bd0f7 commit 5d4c922

File tree

25 files changed

+2273
-86
lines changed
  • src/main/kotlin
    • g3101_3200
      • s3184_count_pairs_that_form_a_complete_day_i
      • s3185_count_pairs_that_form_a_complete_day_ii
      • s3186_maximum_total_damage_with_spell_casting
      • s3187_peaks_in_array
      • s3190_find_minimum_operations_to_make_all_elements_divisible_by_three
      • s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i
      • s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii
      • s3193_count_the_number_of_inversions
      • s3194_minimum_average_of_smallest_and_largest_elements
      • s3195_find_the_minimum_area_to_cover_all_ones_i
      • s3196_maximize_total_cost_of_alternating_subarrays
      • s3197_find_the_minimum_area_to_cover_all_ones_ii
      • s3200_maximum_height_of_a_triangle
    • g3201_3300
      • s3201_find_the_maximum_length_of_valid_subsequence_i
      • s3202_find_the_maximum_length_of_valid_subsequence_ii
      • s3203_find_minimum_diameter_after_merging_two_trees
      • s3206_alternating_groups_i
      • s3207_maximum_points_after_enemy_battles
      • s3208_alternating_groups_ii
      • s3209_number_of_subarrays_with_and_value_of_k
      • s3210_find_the_encrypted_string
      • s3211_generate_binary_strings_without_adjacent_zeros
      • s3212_count_submatrices_with_equal_frequency_of_x_and_y
      • s3213_construct_string_with_minimum_cost

25 files changed

+2273
-86
lines changed

README.md

Lines changed: 110 additions & 86 deletions
Large diffs are not rendered by default.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
## 3184\. Count Pairs That Form a Complete Day I
5+
6+
Easy
7+
8+
Given an integer array `hours` representing times in **hours**, return an integer denoting the number of pairs `i`, `j` where `i < j` and `hours[i] + hours[j]` forms a **complete day**.
9+
10+
A **complete day** is defined as a time duration that is an **exact** **multiple** of 24 hours.
11+
12+
For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.
13+
14+
**Example 1:**
15+
16+
**Input:** hours = [12,12,30,24,24]
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
The pairs of indices that form a complete day are `(0, 1)` and `(3, 4)`.
23+
24+
**Example 2:**
25+
26+
**Input:** hours = [72,48,24,3]
27+
28+
**Output:** 3
29+
30+
**Explanation:**
31+
32+
The pairs of indices that form a complete day are `(0, 1)`, `(0, 2)`, and `(1, 2)`.
33+
34+
**Constraints:**
35+
36+
* `1 <= hours.length <= 100`
37+
* <code>1 <= hours[i] <= 10<sup>9</sup></code>
38+
39+
## Solution
40+
41+
```kotlin
42+
class Solution {
43+
fun countCompleteDayPairs(hours: IntArray): Int {
44+
val modular = IntArray(26)
45+
var ans = 0
46+
for (hour in hours) {
47+
val mod = hour % 24
48+
ans += modular[24 - mod]
49+
if (mod == 0) {
50+
modular[24]++
51+
} else {
52+
modular[mod]++
53+
}
54+
}
55+
return ans
56+
}
57+
}
58+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
## 3185\. Count Pairs That Form a Complete Day II
5+
6+
Medium
7+
8+
Given an integer array `hours` representing times in **hours**, return an integer denoting the number of pairs `i`, `j` where `i < j` and `hours[i] + hours[j]` forms a **complete day**.
9+
10+
A **complete day** is defined as a time duration that is an **exact** **multiple** of 24 hours.
11+
12+
For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.
13+
14+
**Example 1:**
15+
16+
**Input:** hours = [12,12,30,24,24]
17+
18+
**Output:** 2
19+
20+
**Explanation:** The pairs of indices that form a complete day are `(0, 1)` and `(3, 4)`.
21+
22+
**Example 2:**
23+
24+
**Input:** hours = [72,48,24,3]
25+
26+
**Output:** 3
27+
28+
**Explanation:** The pairs of indices that form a complete day are `(0, 1)`, `(0, 2)`, and `(1, 2)`.
29+
30+
**Constraints:**
31+
32+
* <code>1 <= hours.length <= 5 * 10<sup>5</sup></code>
33+
* <code>1 <= hours[i] <= 10<sup>9</sup></code>
34+
35+
## Solution
36+
37+
```kotlin
38+
class Solution {
39+
fun countCompleteDayPairs(hours: IntArray): Long {
40+
val hour = LongArray(24)
41+
for (j in hours) {
42+
hour[j % 24]++
43+
}
44+
var counter = hour[0] * (hour[0] - 1) / 2
45+
counter += hour[12] * (hour[12] - 1) / 2
46+
for (i in 1..11) {
47+
counter += hour[i] * hour[24 - i]
48+
}
49+
return counter
50+
}
51+
}
52+
```
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
## 3186\. Maximum Total Damage With Spell Casting
5+
6+
Medium
7+
8+
A magician has various spells.
9+
10+
You are given an array `power`, where each element represents the damage of a spell. Multiple spells can have the same damage value.
11+
12+
It is a known fact that if a magician decides to cast a spell with a damage of `power[i]`, they **cannot** cast any spell with a damage of `power[i] - 2`, `power[i] - 1`, `power[i] + 1`, or `power[i] + 2`.
13+
14+
Each spell can be cast **only once**.
15+
16+
Return the **maximum** possible _total damage_ that a magician can cast.
17+
18+
**Example 1:**
19+
20+
**Input:** power = [1,1,3,4]
21+
22+
**Output:** 6
23+
24+
**Explanation:**
25+
26+
The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.
27+
28+
**Example 2:**
29+
30+
**Input:** power = [7,1,6,6]
31+
32+
**Output:** 13
33+
34+
**Explanation:**
35+
36+
The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.
37+
38+
**Constraints:**
39+
40+
* <code>1 <= power.length <= 10<sup>5</sup></code>
41+
* <code>1 <= power[i] <= 10<sup>9</sup></code>
42+
43+
## Solution
44+
45+
```kotlin
46+
import kotlin.math.max
47+
import kotlin.math.min
48+
49+
class Solution {
50+
fun maximumTotalDamage(power: IntArray): Long {
51+
var maxPower = 0
52+
for (p in power) {
53+
if (p > maxPower) {
54+
maxPower = p
55+
}
56+
}
57+
return if ((maxPower <= 1000000)) smallPower(power, maxPower) else bigPower(power)
58+
}
59+
60+
private fun smallPower(power: IntArray, maxPower: Int): Long {
61+
val counts = IntArray(maxPower + 6)
62+
for (p in power) {
63+
counts[p]++
64+
}
65+
val dp = LongArray(maxPower + 6)
66+
dp[1] = counts[1].toLong()
67+
dp[2] = max((counts[2] * 2L).toDouble(), dp[1].toDouble()).toLong()
68+
for (i in 3..maxPower) {
69+
dp[i] = max((counts[i] * i + dp[i - 3]).toDouble(), max(dp[i - 1].toDouble(), dp[i - 2].toDouble()))
70+
.toLong()
71+
}
72+
return dp[maxPower]
73+
}
74+
75+
private fun bigPower(power: IntArray): Long {
76+
power.sort()
77+
val n = power.size
78+
val prevs = LongArray(4)
79+
var curPower = power[0]
80+
var count = 1
81+
var result: Long = 0
82+
for (i in 1..n) {
83+
val p = if ((i == n)) 1000000009 else power[i]
84+
if (p == curPower) {
85+
count++
86+
} else {
87+
val curVal = max(
88+
(curPower.toLong() * count + prevs[3]).toDouble(),
89+
max(prevs[1].toDouble(), prevs[2].toDouble())
90+
)
91+
.toLong()
92+
val diff = min((p - curPower).toDouble(), (prevs.size - 1).toDouble()).toInt()
93+
val nextCurVal =
94+
if ((diff == 1)) 0 else max(prevs[3].toDouble(), max(curVal.toDouble(), prevs[2].toDouble()))
95+
.toLong()
96+
// Shift the values in prevs[].
97+
var k = prevs.size - 1
98+
if (diff < prevs.size - 1) {
99+
while (k > diff) {
100+
prevs[k] = prevs[k-- - diff]
101+
}
102+
prevs[k--] = curVal
103+
}
104+
while (k > 0) {
105+
prevs[k--] = nextCurVal
106+
}
107+
curPower = p
108+
count = 1
109+
}
110+
}
111+
for (v in prevs) {
112+
if (v > result) {
113+
result = v
114+
}
115+
}
116+
return result
117+
}
118+
}
119+
```
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
## 3187\. Peaks in Array
5+
6+
Hard
7+
8+
A **peak** in an array `arr` is an element that is **greater** than its previous and next element in `arr`.
9+
10+
You are given an integer array `nums` and a 2D integer array `queries`.
11+
12+
You have to process queries of two types:
13+
14+
* <code>queries[i] = [1, l<sub>i</sub>, r<sub>i</sub>]</code>, determine the count of **peak** elements in the subarray <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.
15+
* <code>queries[i] = [2, index<sub>i</sub>, val<sub>i</sub>]</code>, change <code>nums[index<sub>i</sub>]</code> to <code>val<sub>i</sub></code>.
16+
17+
Return an array `answer` containing the results of the queries of the first type in order.
18+
19+
**Notes:**
20+
21+
* The **first** and the **last** element of an array or a subarray **cannot** be a peak.
22+
23+
**Example 1:**
24+
25+
**Input:** nums = [3,1,4,2,5], queries = \[\[2,3,4],[1,0,4]]
26+
27+
**Output:** [0]
28+
29+
**Explanation:**
30+
31+
First query: We change `nums[3]` to 4 and `nums` becomes `[3,1,4,4,5]`.
32+
33+
Second query: The number of peaks in the `[3,1,4,4,5]` is 0.
34+
35+
**Example 2:**
36+
37+
**Input:** nums = [4,1,4,2,1,5], queries = \[\[2,2,4],[1,0,2],[1,0,4]]
38+
39+
**Output:** [0,1]
40+
41+
**Explanation:**
42+
43+
First query: `nums[2]` should become 4, but it is already set to 4.
44+
45+
Second query: The number of peaks in the `[4,1,4]` is 0.
46+
47+
Third query: The second 4 is a peak in the `[4,1,4,2,1]`.
48+
49+
**Constraints:**
50+
51+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
52+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
53+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
54+
* `queries[i][0] == 1` or `queries[i][0] == 2`
55+
* For all `i` that:
56+
* `queries[i][0] == 1`: `0 <= queries[i][1] <= queries[i][2] <= nums.length - 1`
57+
* `queries[i][0] == 2`: `0 <= queries[i][1] <= nums.length - 1`, <code>1 <= queries[i][2] <= 10<sup>5</sup></code>
58+
59+
## Solution
60+
61+
```kotlin
62+
import kotlin.math.max
63+
64+
@Suppress("NAME_SHADOWING")
65+
class Solution {
66+
fun countOfPeaks(nums: IntArray, queries: Array<IntArray>): List<Int> {
67+
val peaks = BooleanArray(nums.size)
68+
val binaryIndexedTree = IntArray(Integer.highestOneBit(peaks.size) * 2 + 1)
69+
for (i in 1 until peaks.size - 1) {
70+
if (nums[i] > max(nums[i - 1], nums[i + 1])) {
71+
peaks[i] = true
72+
update(binaryIndexedTree, i + 1, 1)
73+
}
74+
}
75+
val result: MutableList<Int> = ArrayList()
76+
for (query in queries) {
77+
if (query[0] == 1) {
78+
val leftIndex = query[1]
79+
val rightIndex = query[2]
80+
result.add(computeRangeSum(binaryIndexedTree, leftIndex + 2, rightIndex))
81+
} else {
82+
val index = query[1]
83+
val value = query[2]
84+
nums[index] = value
85+
for (i in -1..1) {
86+
val affected = index + i
87+
if (affected >= 1 && affected <= nums.size - 2) {
88+
val peak =
89+
nums[affected] > max(nums[affected - 1], nums[affected + 1])
90+
if (peak != peaks[affected]) {
91+
if (peak) {
92+
update(binaryIndexedTree, affected + 1, 1)
93+
} else {
94+
update(binaryIndexedTree, affected + 1, -1)
95+
}
96+
peaks[affected] = peak
97+
}
98+
}
99+
}
100+
}
101+
}
102+
return result
103+
}
104+
105+
private fun computeRangeSum(binaryIndexedTree: IntArray, beginIndex: Int, endIndex: Int): Int {
106+
return if (beginIndex <= endIndex) query(binaryIndexedTree, endIndex) - query(binaryIndexedTree, beginIndex - 1)
107+
else 0
108+
}
109+
110+
private fun query(binaryIndexedTree: IntArray, index: Int): Int {
111+
var index = index
112+
var result = 0
113+
while (index != 0) {
114+
result += binaryIndexedTree[index]
115+
index -= index and -index
116+
}
117+
118+
return result
119+
}
120+
121+
private fun update(binaryIndexedTree: IntArray, index: Int, delta: Int) {
122+
var index = index
123+
while (index < binaryIndexedTree.size) {
124+
binaryIndexedTree[index] += delta
125+
index += index and -index
126+
}
127+
}
128+
}
129+
```

0 commit comments

Comments
 (0)