diff --git a/src/main/kotlin/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/Solution.kt b/src/main/kotlin/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/Solution.kt new file mode 100644 index 000000000..d85e92bd2 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/Solution.kt @@ -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 + } +} diff --git a/src/main/kotlin/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/readme.md b/src/main/kotlin/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/readme.md new file mode 100644 index 000000000..178dab189 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/readme.md @@ -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` +* 1 <= hours[i] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/Solution.kt b/src/main/kotlin/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/Solution.kt new file mode 100644 index 000000000..ccadadb11 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/Solution.kt @@ -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 + } +} diff --git a/src/main/kotlin/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/readme.md b/src/main/kotlin/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/readme.md new file mode 100644 index 000000000..187c79898 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/readme.md @@ -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:** + +* 1 <= hours.length <= 5 * 105 +* 1 <= hours[i] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3186_maximum_total_damage_with_spell_casting/Solution.kt b/src/main/kotlin/g3101_3200/s3186_maximum_total_damage_with_spell_casting/Solution.kt new file mode 100644 index 000000000..9c6b9eff8 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3186_maximum_total_damage_with_spell_casting/Solution.kt @@ -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 + } +} diff --git a/src/main/kotlin/g3101_3200/s3186_maximum_total_damage_with_spell_casting/readme.md b/src/main/kotlin/g3101_3200/s3186_maximum_total_damage_with_spell_casting/readme.md new file mode 100644 index 000000000..c841f31de --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3186_maximum_total_damage_with_spell_casting/readme.md @@ -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:** + +* 1 <= power.length <= 105 +* 1 <= power[i] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3187_peaks_in_array/Solution.kt b/src/main/kotlin/g3101_3200/s3187_peaks_in_array/Solution.kt new file mode 100644 index 000000000..240388acf --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3187_peaks_in_array/Solution.kt @@ -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): List { + 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 = 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 + } + } +} diff --git a/src/main/kotlin/g3101_3200/s3187_peaks_in_array/readme.md b/src/main/kotlin/g3101_3200/s3187_peaks_in_array/readme.md new file mode 100644 index 000000000..4b9bdd7db --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3187_peaks_in_array/readme.md @@ -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: + +* queries[i] = [1, li, ri], determine the count of **peak** elements in the subarray nums[li..ri]. +* queries[i] = [2, indexi, vali], change nums[indexi] to vali. + +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:** + +* 3 <= nums.length <= 105 +* 1 <= nums[i] <= 105 +* 1 <= queries.length <= 105 +* `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`, 1 <= queries[i][2] <= 105 \ No newline at end of file diff --git a/src/test/kotlin/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/SolutionTest.kt new file mode 100644 index 000000000..36338054c --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/SolutionTest.kt @@ -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)) + } +} diff --git a/src/test/kotlin/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/SolutionTest.kt new file mode 100644 index 000000000..2ccd7cbec --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/SolutionTest.kt @@ -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)) + } +} diff --git a/src/test/kotlin/g3101_3200/s3186_maximum_total_damage_with_spell_casting/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3186_maximum_total_damage_with_spell_casting/SolutionTest.kt new file mode 100644 index 000000000..4c21719e2 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3186_maximum_total_damage_with_spell_casting/SolutionTest.kt @@ -0,0 +1,25 @@ +package g3101_3200.s3186_maximum_total_damage_with_spell_casting + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumTotalDamage() { + assertThat(Solution().maximumTotalDamage(intArrayOf(1, 1, 3, 4)), equalTo(6L)) + } + + @Test + fun maximumTotalDamage2() { + assertThat(Solution().maximumTotalDamage(intArrayOf(7, 1, 6, 6)), equalTo(13L)) + } + + @Test + fun maximumTotalDamage3() { + assertThat( + Solution().maximumTotalDamage(intArrayOf(1000001, 1, 6, 6)), + equalTo(1000014L) + ) + } +} diff --git a/src/test/kotlin/g3101_3200/s3187_peaks_in_array/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3187_peaks_in_array/SolutionTest.kt new file mode 100644 index 000000000..c89599928 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3187_peaks_in_array/SolutionTest.kt @@ -0,0 +1,30 @@ +package g3101_3200.s3187_peaks_in_array + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countOfPeaks() { + assertThat( + Solution() + .countOfPeaks( + intArrayOf(3, 1, 4, 2, 5), arrayOf(intArrayOf(2, 3, 4), intArrayOf(1, 0, 4)) + ), + equalTo(listOf(0)) + ) + } + + @Test + fun countOfPeaks2() { + assertThat( + Solution() + .countOfPeaks( + intArrayOf(4, 1, 4, 2, 1, 5), + arrayOf(intArrayOf(2, 2, 4), intArrayOf(1, 0, 2), intArrayOf(1, 0, 4)) + ), + equalTo(listOf(0, 1)) + ) + } +}