Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added tasks 3184-3213
  • Loading branch information
javadev committed Jul 15, 2024
commit 0d48e40cbde99726dc65688c5399211cd9538a6a
196 changes: 110 additions & 86 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 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>

## Solution

```kotlin
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,52 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 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>

## Solution

```kotlin
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,119 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 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>

## Solution

```kotlin
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
}
}
```
129 changes: 129 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,129 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 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>

## Solution

```kotlin
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
}
}
}
```
Loading