Skip to content

Commit 3ea4d2f

Browse files
authoredJun 22, 2024··
Added tasks 3184-3187
1 parent 71eba20 commit 3ea4d2f

File tree

12 files changed

+437
-0
lines changed

12 files changed

+437
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3101_3200.s3184_count_pairs_that_form_a_complete_day_i
2+
3+
// #Easy #Array #Hash_Table #Counting #2024_06_22_Time_171_ms_(68.42%)_Space_35.1_MB_(91.58%)
4+
5+
class Solution {
6+
fun countCompleteDayPairs(hours: IntArray): Int {
7+
val modular = IntArray(26)
8+
var ans = 0
9+
for (hour in hours) {
10+
val mod = hour % 24
11+
ans += modular[24 - mod]
12+
if (mod == 0) {
13+
modular[24]++
14+
} else {
15+
modular[mod]++
16+
}
17+
}
18+
return ans
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
3184\. Count Pairs That Form a Complete Day I
2+
3+
Easy
4+
5+
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**.
6+
7+
A **complete day** is defined as a time duration that is an **exact** **multiple** of 24 hours.
8+
9+
For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.
10+
11+
**Example 1:**
12+
13+
**Input:** hours = [12,12,30,24,24]
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
The pairs of indices that form a complete day are `(0, 1)` and `(3, 4)`.
20+
21+
**Example 2:**
22+
23+
**Input:** hours = [72,48,24,3]
24+
25+
**Output:** 3
26+
27+
**Explanation:**
28+
29+
The pairs of indices that form a complete day are `(0, 1)`, `(0, 2)`, and `(1, 2)`.
30+
31+
**Constraints:**
32+
33+
* `1 <= hours.length <= 100`
34+
* <code>1 <= hours[i] <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3101_3200.s3185_count_pairs_that_form_a_complete_day_ii
2+
3+
// #Medium #Array #Hash_Table #Counting #2024_06_22_Time_578_ms_(78.33%)_Space_115_MB_(66.67%)
4+
5+
class Solution {
6+
fun countCompleteDayPairs(hours: IntArray): Long {
7+
val hour = LongArray(24)
8+
for (j in hours) {
9+
hour[j % 24]++
10+
}
11+
var counter = hour[0] * (hour[0] - 1) / 2
12+
counter += hour[12] * (hour[12] - 1) / 2
13+
for (i in 1..11) {
14+
counter += hour[i] * hour[24 - i]
15+
}
16+
return counter
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
3185\. Count Pairs That Form a Complete Day II
2+
3+
Medium
4+
5+
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**.
6+
7+
A **complete day** is defined as a time duration that is an **exact** **multiple** of 24 hours.
8+
9+
For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.
10+
11+
**Example 1:**
12+
13+
**Input:** hours = [12,12,30,24,24]
14+
15+
**Output:** 2
16+
17+
**Explanation:** The pairs of indices that form a complete day are `(0, 1)` and `(3, 4)`.
18+
19+
**Example 2:**
20+
21+
**Input:** hours = [72,48,24,3]
22+
23+
**Output:** 3
24+
25+
**Explanation:** The pairs of indices that form a complete day are `(0, 1)`, `(0, 2)`, and `(1, 2)`.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= hours.length <= 5 * 10<sup>5</sup></code>
30+
* <code>1 <= hours[i] <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package g3101_3200.s3186_maximum_total_damage_with_spell_casting
2+
3+
// #Medium #Array #Hash_Table #Dynamic_Programming #Sorting #Binary_Search #Two_Pointers #Counting
4+
// #2024_06_22_Time_1106_ms_(92.73%)_Space_81.1_MB_(41.82%)
5+
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
class Solution {
10+
fun maximumTotalDamage(power: IntArray): Long {
11+
var maxPower = 0
12+
for (p in power) {
13+
if (p > maxPower) {
14+
maxPower = p
15+
}
16+
}
17+
return if ((maxPower <= 1000000)) smallPower(power, maxPower) else bigPower(power)
18+
}
19+
20+
private fun smallPower(power: IntArray, maxPower: Int): Long {
21+
val counts = IntArray(maxPower + 6)
22+
for (p in power) {
23+
counts[p]++
24+
}
25+
val dp = LongArray(maxPower + 6)
26+
dp[1] = counts[1].toLong()
27+
dp[2] = max((counts[2] * 2L).toDouble(), dp[1].toDouble()).toLong()
28+
for (i in 3..maxPower) {
29+
dp[i] = max((counts[i] * i + dp[i - 3]).toDouble(), max(dp[i - 1].toDouble(), dp[i - 2].toDouble()))
30+
.toLong()
31+
}
32+
return dp[maxPower]
33+
}
34+
35+
private fun bigPower(power: IntArray): Long {
36+
power.sort()
37+
val n = power.size
38+
val prevs = LongArray(4)
39+
var curPower = power[0]
40+
var count = 1
41+
var result: Long = 0
42+
for (i in 1..n) {
43+
val p = if ((i == n)) 1000000009 else power[i]
44+
if (p == curPower) {
45+
count++
46+
} else {
47+
val curVal = max(
48+
(curPower.toLong() * count + prevs[3]).toDouble(),
49+
max(prevs[1].toDouble(), prevs[2].toDouble())
50+
)
51+
.toLong()
52+
val diff = min((p - curPower).toDouble(), (prevs.size - 1).toDouble()).toInt()
53+
val nextCurVal =
54+
if ((diff == 1)) 0 else max(prevs[3].toDouble(), max(curVal.toDouble(), prevs[2].toDouble()))
55+
.toLong()
56+
// Shift the values in prevs[].
57+
var k = prevs.size - 1
58+
if (diff < prevs.size - 1) {
59+
while (k > diff) {
60+
prevs[k] = prevs[k-- - diff]
61+
}
62+
prevs[k--] = curVal
63+
}
64+
while (k > 0) {
65+
prevs[k--] = nextCurVal
66+
}
67+
curPower = p
68+
count = 1
69+
}
70+
}
71+
for (v in prevs) {
72+
if (v > result) {
73+
result = v
74+
}
75+
}
76+
return result
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3186\. Maximum Total Damage With Spell Casting
2+
3+
Medium
4+
5+
A magician has various spells.
6+
7+
You are given an array `power`, where each element represents the damage of a spell. Multiple spells can have the same damage value.
8+
9+
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`.
10+
11+
Each spell can be cast **only once**.
12+
13+
Return the **maximum** possible _total damage_ that a magician can cast.
14+
15+
**Example 1:**
16+
17+
**Input:** power = [1,1,3,4]
18+
19+
**Output:** 6
20+
21+
**Explanation:**
22+
23+
The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.
24+
25+
**Example 2:**
26+
27+
**Input:** power = [7,1,6,6]
28+
29+
**Output:** 13
30+
31+
**Explanation:**
32+
33+
The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= power.length <= 10<sup>5</sup></code>
38+
* <code>1 <= power[i] <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package g3101_3200.s3187_peaks_in_array
2+
3+
// #Hard #Array #Segment_Tree #Binary_Indexed_Tree
4+
// #2024_06_22_Time_1339_ms_(80.00%)_Space_135.1_MB_(70.00%)
5+
6+
import kotlin.math.max
7+
8+
@Suppress("NAME_SHADOWING")
9+
class Solution {
10+
fun countOfPeaks(nums: IntArray, queries: Array<IntArray>): List<Int> {
11+
val peaks = BooleanArray(nums.size)
12+
val binaryIndexedTree = IntArray(Integer.highestOneBit(peaks.size) * 2 + 1)
13+
for (i in 1 until peaks.size - 1) {
14+
if (nums[i] > max(nums[i - 1], nums[i + 1])) {
15+
peaks[i] = true
16+
update(binaryIndexedTree, i + 1, 1)
17+
}
18+
}
19+
val result: MutableList<Int> = ArrayList()
20+
for (query in queries) {
21+
if (query[0] == 1) {
22+
val leftIndex = query[1]
23+
val rightIndex = query[2]
24+
result.add(computeRangeSum(binaryIndexedTree, leftIndex + 2, rightIndex))
25+
} else {
26+
val index = query[1]
27+
val value = query[2]
28+
nums[index] = value
29+
for (i in -1..1) {
30+
val affected = index + i
31+
if (affected >= 1 && affected <= nums.size - 2) {
32+
val peak =
33+
nums[affected] > max(nums[affected - 1], nums[affected + 1])
34+
if (peak != peaks[affected]) {
35+
if (peak) {
36+
update(binaryIndexedTree, affected + 1, 1)
37+
} else {
38+
update(binaryIndexedTree, affected + 1, -1)
39+
}
40+
peaks[affected] = peak
41+
}
42+
}
43+
}
44+
}
45+
}
46+
return result
47+
}
48+
49+
private fun computeRangeSum(binaryIndexedTree: IntArray, beginIndex: Int, endIndex: Int): Int {
50+
return if (beginIndex <= endIndex) query(binaryIndexedTree, endIndex) - query(binaryIndexedTree, beginIndex - 1)
51+
else 0
52+
}
53+
54+
private fun query(binaryIndexedTree: IntArray, index: Int): Int {
55+
var index = index
56+
var result = 0
57+
while (index != 0) {
58+
result += binaryIndexedTree[index]
59+
index -= index and -index
60+
}
61+
62+
return result
63+
}
64+
65+
private fun update(binaryIndexedTree: IntArray, index: Int, delta: Int) {
66+
var index = index
67+
while (index < binaryIndexedTree.size) {
68+
binaryIndexedTree[index] += delta
69+
index += index and -index
70+
}
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3187\. Peaks in Array
2+
3+
Hard
4+
5+
A **peak** in an array `arr` is an element that is **greater** than its previous and next element in `arr`.
6+
7+
You are given an integer array `nums` and a 2D integer array `queries`.
8+
9+
You have to process queries of two types:
10+
11+
* <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>.
12+
* <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>.
13+
14+
Return an array `answer` containing the results of the queries of the first type in order.
15+
16+
**Notes:**
17+
18+
* The **first** and the **last** element of an array or a subarray **cannot** be a peak.
19+
20+
**Example 1:**
21+
22+
**Input:** nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]]
23+
24+
**Output:** [0]
25+
26+
**Explanation:**
27+
28+
First query: We change `nums[3]` to 4 and `nums` becomes `[3,1,4,4,5]`.
29+
30+
Second query: The number of peaks in the `[3,1,4,4,5]` is 0.
31+
32+
**Example 2:**
33+
34+
**Input:** nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]]
35+
36+
**Output:** [0,1]
37+
38+
**Explanation:**
39+
40+
First query: `nums[2]` should become 4, but it is already set to 4.
41+
42+
Second query: The number of peaks in the `[4,1,4]` is 0.
43+
44+
Third query: The second 4 is a peak in the `[4,1,4,2,1]`.
45+
46+
**Constraints:**
47+
48+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
49+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
50+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
51+
* `queries[i][0] == 1` or `queries[i][0] == 2`
52+
* For all `i` that:
53+
* `queries[i][0] == 1`: `0 <= queries[i][1] <= queries[i][2] <= nums.length - 1`
54+
* `queries[i][0] == 2`: `0 <= queries[i][1] <= nums.length - 1`, <code>1 <= queries[i][2] <= 10<sup>5</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3101_3200.s3184_count_pairs_that_form_a_complete_day_i
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun countCompleteDayPairs() {
10+
assertThat(
11+
Solution().countCompleteDayPairs(intArrayOf(12, 12, 30, 24, 24)), equalTo(2)
12+
)
13+
}
14+
15+
@Test
16+
fun countCompleteDayPairs2() {
17+
assertThat(Solution().countCompleteDayPairs(intArrayOf(72, 48, 24, 3)), equalTo(3))
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3101_3200.s3185_count_pairs_that_form_a_complete_day_ii
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun countCompleteDayPairs() {
10+
assertThat(
11+
Solution().countCompleteDayPairs(intArrayOf(12, 12, 30, 24, 24)), equalTo(2L)
12+
)
13+
}
14+
15+
@Test
16+
fun countCompleteDayPairs2() {
17+
assertThat(Solution().countCompleteDayPairs(intArrayOf(72, 48, 24, 3)), equalTo(3L))
18+
}
19+
}

0 commit comments

Comments
 (0)
Please sign in to comment.