Skip to content

Commit

Permalink
Added tasks 3184-3187
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Jun 22, 2024
1 parent 71eba20 commit 3ea4d2f
Show file tree
Hide file tree
Showing 12 changed files with 437 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g3101_3200.s3184_count_pairs_that_form_a_complete_day_i

// #Easy #Array #Hash_Table #Counting #2024_06_22_Time_171_ms_(68.42%)_Space_35.1_MB_(91.58%)

class Solution {
fun countCompleteDayPairs(hours: IntArray): Int {
val modular = IntArray(26)
var ans = 0
for (hour in hours) {
val mod = hour % 24
ans += modular[24 - mod]
if (mod == 0) {
modular[24]++
} else {
modular[mod]++
}
}
return ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
3184\. Count Pairs That Form a Complete Day I

Easy

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**.

A **complete day** is defined as a time duration that is an **exact** **multiple** of 24 hours.

For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.

**Example 1:**

**Input:** hours = [12,12,30,24,24]

**Output:** 2

**Explanation:**

The pairs of indices that form a complete day are `(0, 1)` and `(3, 4)`.

**Example 2:**

**Input:** hours = [72,48,24,3]

**Output:** 3

**Explanation:**

The pairs of indices that form a complete day are `(0, 1)`, `(0, 2)`, and `(1, 2)`.

**Constraints:**

* `1 <= hours.length <= 100`
* <code>1 <= hours[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3101_3200.s3185_count_pairs_that_form_a_complete_day_ii

// #Medium #Array #Hash_Table #Counting #2024_06_22_Time_578_ms_(78.33%)_Space_115_MB_(66.67%)

class Solution {
fun countCompleteDayPairs(hours: IntArray): Long {
val hour = LongArray(24)
for (j in hours) {
hour[j % 24]++
}
var counter = hour[0] * (hour[0] - 1) / 2
counter += hour[12] * (hour[12] - 1) / 2
for (i in 1..11) {
counter += hour[i] * hour[24 - i]
}
return counter
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
3185\. Count Pairs That Form a Complete Day II

Medium

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**.

A **complete day** is defined as a time duration that is an **exact** **multiple** of 24 hours.

For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.

**Example 1:**

**Input:** hours = [12,12,30,24,24]

**Output:** 2

**Explanation:** The pairs of indices that form a complete day are `(0, 1)` and `(3, 4)`.

**Example 2:**

**Input:** hours = [72,48,24,3]

**Output:** 3

**Explanation:** The pairs of indices that form a complete day are `(0, 1)`, `(0, 2)`, and `(1, 2)`.

**Constraints:**

* <code>1 <= hours.length <= 5 * 10<sup>5</sup></code>
* <code>1 <= hours[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package g3101_3200.s3186_maximum_total_damage_with_spell_casting

// #Medium #Array #Hash_Table #Dynamic_Programming #Sorting #Binary_Search #Two_Pointers #Counting
// #2024_06_22_Time_1106_ms_(92.73%)_Space_81.1_MB_(41.82%)

import kotlin.math.max
import kotlin.math.min

class Solution {
fun maximumTotalDamage(power: IntArray): Long {
var maxPower = 0
for (p in power) {
if (p > maxPower) {
maxPower = p
}
}
return if ((maxPower <= 1000000)) smallPower(power, maxPower) else bigPower(power)
}

private fun smallPower(power: IntArray, maxPower: Int): Long {
val counts = IntArray(maxPower + 6)
for (p in power) {
counts[p]++
}
val dp = LongArray(maxPower + 6)
dp[1] = counts[1].toLong()
dp[2] = max((counts[2] * 2L).toDouble(), dp[1].toDouble()).toLong()
for (i in 3..maxPower) {
dp[i] = max((counts[i] * i + dp[i - 3]).toDouble(), max(dp[i - 1].toDouble(), dp[i - 2].toDouble()))
.toLong()
}
return dp[maxPower]
}

private fun bigPower(power: IntArray): Long {
power.sort()
val n = power.size
val prevs = LongArray(4)
var curPower = power[0]
var count = 1
var result: Long = 0
for (i in 1..n) {
val p = if ((i == n)) 1000000009 else power[i]
if (p == curPower) {
count++
} else {
val curVal = max(
(curPower.toLong() * count + prevs[3]).toDouble(),
max(prevs[1].toDouble(), prevs[2].toDouble())
)
.toLong()
val diff = min((p - curPower).toDouble(), (prevs.size - 1).toDouble()).toInt()
val nextCurVal =
if ((diff == 1)) 0 else max(prevs[3].toDouble(), max(curVal.toDouble(), prevs[2].toDouble()))
.toLong()
// Shift the values in prevs[].
var k = prevs.size - 1
if (diff < prevs.size - 1) {
while (k > diff) {
prevs[k] = prevs[k-- - diff]
}
prevs[k--] = curVal
}
while (k > 0) {
prevs[k--] = nextCurVal
}
curPower = p
count = 1
}
}
for (v in prevs) {
if (v > result) {
result = v
}
}
return result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
3186\. Maximum Total Damage With Spell Casting

Medium

A magician has various spells.

You are given an array `power`, where each element represents the damage of a spell. Multiple spells can have the same damage value.

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`.

Each spell can be cast **only once**.

Return the **maximum** possible _total damage_ that a magician can cast.

**Example 1:**

**Input:** power = [1,1,3,4]

**Output:** 6

**Explanation:**

The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.

**Example 2:**

**Input:** power = [7,1,6,6]

**Output:** 13

**Explanation:**

The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.

**Constraints:**

* <code>1 <= power.length <= 10<sup>5</sup></code>
* <code>1 <= power[i] <= 10<sup>9</sup></code>
72 changes: 72 additions & 0 deletions src/main/kotlin/g3101_3200/s3187_peaks_in_array/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package g3101_3200.s3187_peaks_in_array

// #Hard #Array #Segment_Tree #Binary_Indexed_Tree
// #2024_06_22_Time_1339_ms_(80.00%)_Space_135.1_MB_(70.00%)

import kotlin.math.max

@Suppress("NAME_SHADOWING")
class Solution {
fun countOfPeaks(nums: IntArray, queries: Array<IntArray>): List<Int> {
val peaks = BooleanArray(nums.size)
val binaryIndexedTree = IntArray(Integer.highestOneBit(peaks.size) * 2 + 1)
for (i in 1 until peaks.size - 1) {
if (nums[i] > max(nums[i - 1], nums[i + 1])) {
peaks[i] = true
update(binaryIndexedTree, i + 1, 1)
}
}
val result: MutableList<Int> = ArrayList()
for (query in queries) {
if (query[0] == 1) {
val leftIndex = query[1]
val rightIndex = query[2]
result.add(computeRangeSum(binaryIndexedTree, leftIndex + 2, rightIndex))
} else {
val index = query[1]
val value = query[2]
nums[index] = value
for (i in -1..1) {
val affected = index + i
if (affected >= 1 && affected <= nums.size - 2) {
val peak =
nums[affected] > max(nums[affected - 1], nums[affected + 1])
if (peak != peaks[affected]) {
if (peak) {
update(binaryIndexedTree, affected + 1, 1)
} else {
update(binaryIndexedTree, affected + 1, -1)
}
peaks[affected] = peak
}
}
}
}
}
return result
}

private fun computeRangeSum(binaryIndexedTree: IntArray, beginIndex: Int, endIndex: Int): Int {
return if (beginIndex <= endIndex) query(binaryIndexedTree, endIndex) - query(binaryIndexedTree, beginIndex - 1)
else 0
}

private fun query(binaryIndexedTree: IntArray, index: Int): Int {
var index = index
var result = 0
while (index != 0) {
result += binaryIndexedTree[index]
index -= index and -index
}

return result
}

private fun update(binaryIndexedTree: IntArray, index: Int, delta: Int) {
var index = index
while (index < binaryIndexedTree.size) {
binaryIndexedTree[index] += delta
index += index and -index
}
}
}
54 changes: 54 additions & 0 deletions src/main/kotlin/g3101_3200/s3187_peaks_in_array/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
3187\. Peaks in Array

Hard

A **peak** in an array `arr` is an element that is **greater** than its previous and next element in `arr`.

You are given an integer array `nums` and a 2D integer array `queries`.

You have to process queries of two types:

* <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>.
* <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>.

Return an array `answer` containing the results of the queries of the first type in order.

**Notes:**

* The **first** and the **last** element of an array or a subarray **cannot** be a peak.

**Example 1:**

**Input:** nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]]

**Output:** [0]

**Explanation:**

First query: We change `nums[3]` to 4 and `nums` becomes `[3,1,4,4,5]`.

Second query: The number of peaks in the `[3,1,4,4,5]` is 0.

**Example 2:**

**Input:** nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]]

**Output:** [0,1]

**Explanation:**

First query: `nums[2]` should become 4, but it is already set to 4.

Second query: The number of peaks in the `[4,1,4]` is 0.

Third query: The second 4 is a peak in the `[4,1,4,2,1]`.

**Constraints:**

* <code>3 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
* <code>1 <= queries.length <= 10<sup>5</sup></code>
* `queries[i][0] == 1` or `queries[i][0] == 2`
* For all `i` that:
* `queries[i][0] == 1`: `0 <= queries[i][1] <= queries[i][2] <= nums.length - 1`
* `queries[i][0] == 2`: `0 <= queries[i][1] <= nums.length - 1`, <code>1 <= queries[i][2] <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g3101_3200.s3184_count_pairs_that_form_a_complete_day_i

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Test

internal class SolutionTest {
@Test
fun countCompleteDayPairs() {
assertThat(
Solution().countCompleteDayPairs(intArrayOf(12, 12, 30, 24, 24)), equalTo(2)
)
}

@Test
fun countCompleteDayPairs2() {
assertThat(Solution().countCompleteDayPairs(intArrayOf(72, 48, 24, 3)), equalTo(3))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g3101_3200.s3185_count_pairs_that_form_a_complete_day_ii

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Test

internal class SolutionTest {
@Test
fun countCompleteDayPairs() {
assertThat(
Solution().countCompleteDayPairs(intArrayOf(12, 12, 30, 24, 24)), equalTo(2L)
)
}

@Test
fun countCompleteDayPairs2() {
assertThat(Solution().countCompleteDayPairs(intArrayOf(72, 48, 24, 3)), equalTo(3L))
}
}
Loading

0 comments on commit 3ea4d2f

Please sign in to comment.