From 3c39c8bfd2325717be56cb1807e3d46f41925d44 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 21 Aug 2024 15:41:32 +0300 Subject: [PATCH] Added tasks 3248-3261 --- README.md | 10 +- .../readme.md | 65 +++----- .../s3248_snake_in_matrix/image01.png | Bin 0 -> 3609 bytes .../s3248_snake_in_matrix/image02.png | Bin 0 -> 7586 bytes .../readme.md | 79 +++++++++ .../readme.md | 80 +++++++++ .../readme.md | 113 +++++++++++++ .../readme.md | 113 +++++++++++++ .../readme.md | 86 ++++++++++ .../readme.md | 67 ++++++++ .../readme.md | 157 ++++++++++++++++++ .../readme.md | 103 ++++++++++++ 12 files changed, 828 insertions(+), 45 deletions(-) create mode 100644 src/main/kotlin/g3201_3300/s3248_snake_in_matrix/image01.png create mode 100644 src/main/kotlin/g3201_3300/s3248_snake_in_matrix/image02.png create mode 100644 src/main/kotlin/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3259_maximum_energy_boost_from_two_drinks/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii/readme.md diff --git a/README.md b/README.md index 2248824b..b3cf0429 100644 --- a/README.md +++ b/README.md @@ -1816,6 +1816,14 @@ | # | Title | Difficulty | Tag | Time, ms | Time, % |------|----------------|-------------|-------------|----------|-------- +| 3261 |[Count Substrings That Satisfy K-Constraint II](src/main/kotlin/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii)| Hard | Array, String, Binary_Search, Prefix_Sum, Sliding_Window | 1005 | 100.00 +| 3260 |[Find the Largest Palindrome Divisible by K](src/main/kotlin/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k)| Hard | String, Dynamic_Programming, Math, Greedy, Number_Theory | 211 | 100.00 +| 3259 |[Maximum Energy Boost From Two Drinks](src/main/kotlin/g3201_3300/s3259_maximum_energy_boost_from_two_drinks)| Medium | Array, Dynamic_Programming | 811 | 96.88 +| 3258 |[Count Substrings That Satisfy K-Constraint I](src/main/kotlin/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i)| Easy | String, Sliding_Window | 155 | 92.86 +| 3257 |[Maximum Value Sum by Placing Three Rooks II](src/main/kotlin/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii)| Hard | Array, Dynamic_Programming, Matrix, Enumeration | 770 | 100.00 +| 3256 |[Maximum Value Sum by Placing Three Rooks I](src/main/kotlin/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i)| Hard | Array, Dynamic_Programming, Matrix, Enumeration | 279 | 100.00 +| 3255 |[Find the Power of K-Size Subarrays II](src/main/kotlin/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii)| Medium | Array, Sliding_Window | 892 | 89.36 +| 3254 |[Find the Power of K-Size Subarrays I](src/main/kotlin/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i)| Medium | Array, Sliding_Window | 245 | 92.59 | 3251 |[Find the Count of Monotonic Pairs II](src/main/kotlin/g3201_3300/s3251_find_the_count_of_monotonic_pairs_ii)| Hard | Array, Dynamic_Programming, Math, Prefix_Sum, Combinatorics | 291 | 100.00 | 3250 |[Find the Count of Monotonic Pairs I](src/main/kotlin/g3201_3300/s3250_find_the_count_of_monotonic_pairs_i)| Hard | Array, Dynamic_Programming, Math, Prefix_Sum, Combinatorics | 241 | 100.00 | 3249 |[Count the Number of Good Nodes](src/main/kotlin/g3201_3300/s3249_count_the_number_of_good_nodes)| Medium | Depth_First_Search, Tree | 1190 | 100.00 @@ -4186,7 +4194,7 @@ | 0228 |[Summary Ranges](src/main/kotlin/g0201_0300/s0228_summary_ranges)| Easy | Array | 169 | 91.89 | 0227 |[Basic Calculator II](src/main/kotlin/g0201_0300/s0227_basic_calculator_ii)| Medium | String, Math, Stack, Level_2_Day_18_Stack | 383 | 62.50 | 0226 |[Invert Binary Tree](src/main/kotlin/g0201_0300/s0226_invert_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_12_Tree, Level_2_Day_6_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 233 | 54.90 -| 0225 |[Implement Stack using Queues](src/main/kotlin/g0201_0300/s0225_implement_stack_using_queues)| Easy | Stack, Design, Queue | 248 | 73.44 +| 0225 |[Implement Stack using Queues](src/main/kotlin/g0201_0300/s0225_implement_stack_using_queues)| Easy | Stack, Design, Queue | 147 | 88.57 | 0224 |[Basic Calculator](src/main/kotlin/g0201_0300/s0224_basic_calculator)| Hard | String, Math, Stack, Recursion | 294 | 93.33 | 0223 |[Rectangle Area](src/main/kotlin/g0201_0300/s0223_rectangle_area)| Medium | Math, Geometry | 291 | 66.67 | 0222 |[Count Complete Tree Nodes](src/main/kotlin/g0201_0300/s0222_count_complete_tree_nodes)| ||| diff --git a/src/main/kotlin/g0201_0300/s0225_implement_stack_using_queues/readme.md b/src/main/kotlin/g0201_0300/s0225_implement_stack_using_queues/readme.md index 2e9eb2ae..10eb7be9 100644 --- a/src/main/kotlin/g0201_0300/s0225_implement_stack_using_queues/readme.md +++ b/src/main/kotlin/g0201_0300/s0225_implement_stack_using_queues/readme.md @@ -46,64 +46,41 @@ Implement the `MyStack` class: ```kotlin import java.util.LinkedList +import java.util.Queue -class MyStack { - private var queuePair = Pair(LinkedList(), LinkedList()) - private var top: Int? = null +class MyStack() { + private val queue1: Queue = LinkedList() + private val queue2: Queue = LinkedList() fun push(x: Int) { - queuePair.first.addLast(x) - top = x + queue1.add(x) } fun pop(): Int { - if (isQueuesEmpty()) { - throw Exception() + while (queue1.size > 1) { + queue2.add(queue1.remove()) } - val queuePair = selectSourceAndDestinationQueues(queuePair) - var value = 0 - repeat(queuePair.first.size) { - when (queuePair.first.size) { - 2 -> { - top = queuePair.first.removeFirst() - queuePair.second.addLast(top) - } - 1 -> { - value = queuePair.first.removeFirst() - } - else -> { - queuePair.second.addLast(queuePair.first.removeFirst()) - } - } - } - return value + val top = queue1.remove() + queue1.clear() + queue1.addAll(queue2) + queue2.clear() + return top } fun top(): Int { - if (isQueuesEmpty()) { - throw Exception() + while (queue1.size > 1) { + queue2.add(queue1.remove()) } - return top!! + val top = queue1.remove() + queue2.add(top) + queue1.clear() + queue1.addAll(queue2) + queue2.clear() + return top } fun empty(): Boolean { - return isQueuesEmpty() - } - - private fun isQueuesEmpty(): Boolean { - if (queuePair.first.isEmpty() && queuePair.second.isEmpty()) { - return true - } - return false - } - - private fun selectSourceAndDestinationQueues(queuePair: Pair, LinkedList>): - Pair, LinkedList> { - return if (queuePair.first.isNotEmpty()) { - Pair(queuePair.first, queuePair.second) - } else { - Pair(queuePair.second, queuePair.first) - } + return queue1.isEmpty() } } diff --git a/src/main/kotlin/g3201_3300/s3248_snake_in_matrix/image01.png b/src/main/kotlin/g3201_3300/s3248_snake_in_matrix/image01.png new file mode 100644 index 0000000000000000000000000000000000000000..d32550a7821d484c9940ef913bd1cc28e8b64f99 GIT binary patch literal 3609 zcmeHKXHXN^77j5i5D^kkks3h}L22SjFAFh%h;$JMO;PEIbSa?;2||!6O%ezq7@7zO zQbKVRgau?1T3iUq!+;?m3PFsK7k>Ev&Ayp8^JezXom)1ik3X!8hyv$Z4E3a0zT9ce)4Orbvo5P7w{Ny{7}Lr;CtYJNH0V6_Uv|DxSl6 z0FmxLF~LBIWkeE(l};EqZpLjEvZ|03pf}0Jg7X3FL4~8qp&&}G$&z*bmX5~vee z!RxH7(Gq!sSrzzO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0|yG7)8x=iV_Oh;qRekFr6-rCEj z2Cb!YBEnjbNh%n&_D#(&$W8<4|b*n|JH7MY^#hK;M1Dan} zT$Hza(mH;XvOJ&<{MOEP;Jv#*JTB@%9uS&Nint#MmTc~(PI?5Li^L`<1H z^!Zv`ITSyVU+1G;X2MN_l)uJIkE4w>eF8HYLY9ka>RJcyTph@(Hu}xBjR)~G-Kk#> z5HE=c79<6gHy%PpI&5`$3zAx9nn;^V5+2$fRQ08gee}UXnkJ#oBP1GrO(5HHbJ4Yy zwV1`*H-45&a%Rlg84brm%Lg!%G+O6U$TB=p7^vyIXU0ylu^?$^|784J_E75bH^}l8 zvs)aGSyX&h7!3{N>T?wyx3w}#;#hLHZkw*<0PM;K3wciXCGW~iGR%ZX7&P_1{o^kN z1M}jc0b@HyeysW0GXgBs;q2F^Cc0V2p1Qsjnphkk|9ZxV64gmyp#$ZR3-*SjcPoIp zLy31FA!%j942dM=^UTnO9JH zojl5rifz@Y>w%6ESMUY(VHt08akSt|)W8I%L)CQlw%mCd0~7Gu{3-Wn*F4k@pVsnujVp`&-e^(?tf)?*zG;eURfHU$8Pq*Su zUcRsdsF{l5io!kQUNwTUZnNJ|mQ%JI3*g`q&F zG|Rj1hl{~zLCedAQc=%E8G?qZHxGN^HR|FzQbATeH-W8;v7{0LXo}y?u!1|Vpr{jO sT()TOinUKUfZaZp9``>J>e~ez)clg~p?~BQzvTm1pT7ieH1|pQHzhRR1poj5 literal 0 HcmV?d00001 diff --git a/src/main/kotlin/g3201_3300/s3248_snake_in_matrix/image02.png b/src/main/kotlin/g3201_3300/s3248_snake_in_matrix/image02.png new file mode 100644 index 0000000000000000000000000000000000000000..00305b02682e7176ef3e0538b531ed0ed685ed46 GIT binary patch literal 7586 zcmeHMcTiK?x(C4^LIeerB1J@q$Accgh=_!MG(Gex5{h&Tf)J1rdazNXh$4an&q3*- zDufo1B81SSuH&?u^oT_ztazafNwpLLjcgQqfB)**>IgFXMqb&Hw^;~Hn!3P z9>(p1z%}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2}IBe&t1Wyv1ziQ>7qHwezWWi!pT=y zFOPkm-1VFm`Ruu>(42oKrFc#h!u1Dy&T65q9Mi|L912**n4{_0GnT1sq|CJQiTrnc z))JXz2-cB|m&S_(*5S*A_6IDL0lqOjC#@jI`uqG>kAFLVu#_(r?_qI~%p?sOdgE#G zz|P-uA(ke#e-BhdAKcf(e&}TKQ-04_T!_T^zdCCCrw0FGn6t-qAw2TKhu@~6fpb;1 N>)M7|I8BGB{{VF*1poj5 literal 0 HcmV?d00001 diff --git a/src/main/kotlin/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i/readme.md b/src/main/kotlin/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i/readme.md new file mode 100644 index 00000000..453bd0d8 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i/readme.md @@ -0,0 +1,79 @@ +[![](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) + +## 3254\. Find the Power of K-Size Subarrays I + +Medium + +You are given an array of integers `nums` of length `n` and a _positive_ integer `k`. + +The **power** of an array is defined as: + +* Its **maximum** element if _all_ of its elements are **consecutive** and **sorted** in **ascending** order. +* \-1 otherwise. + +You need to find the **power** of all subarrays of `nums` of size `k`. + +Return an integer array `results` of size `n - k + 1`, where `results[i]` is the _power_ of `nums[i..(i + k - 1)]`. + +**Example 1:** + +**Input:** nums = [1,2,3,4,3,2,5], k = 3 + +**Output:** [3,4,-1,-1,-1] + +**Explanation:** + +There are 5 subarrays of `nums` of size 3: + +* `[1, 2, 3]` with the maximum element 3. +* `[2, 3, 4]` with the maximum element 4. +* `[3, 4, 3]` whose elements are **not** consecutive. +* `[4, 3, 2]` whose elements are **not** sorted. +* `[3, 2, 5]` whose elements are **not** consecutive. + +**Example 2:** + +**Input:** nums = [2,2,2,2,2], k = 4 + +**Output:** [-1,-1] + +**Example 3:** + +**Input:** nums = [3,2,3,2,3,2], k = 2 + +**Output:** [-1,3,-1,3,-1] + +**Constraints:** + +* `1 <= n == nums.length <= 500` +* 1 <= nums[i] <= 105 +* `1 <= k <= n` + +## Solution + +```kotlin +class Solution { + fun resultsArray(nums: IntArray, k: Int): IntArray { + val n = nums.size + val arr = IntArray(n - k + 1) + var count = 0 + for (i in 1 until k) { + if (nums[i] == nums[i - 1] + 1) { + count++ + } + } + arr[0] = if ((count == k - 1)) nums[k - 1] else -1 + for (i in 1..n - k) { + if (nums[i] == nums[i - 1] + 1) { + count-- + } + if (nums[i + k - 1] == nums[i + k - 2] + 1) { + count++ + } + arr[i] = if ((count == k - 1)) nums[i + k - 1] else -1 + } + return arr + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii/readme.md b/src/main/kotlin/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii/readme.md new file mode 100644 index 00000000..255cfcbd --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii/readme.md @@ -0,0 +1,80 @@ +[![](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) + +## 3255\. Find the Power of K-Size Subarrays II + +Medium + +You are given an array of integers `nums` of length `n` and a _positive_ integer `k`. + +The **power** of an array is defined as: + +* Its **maximum** element if _all_ of its elements are **consecutive** and **sorted** in **ascending** order. +* \-1 otherwise. + +You need to find the **power** of all subarrays of `nums` of size `k`. + +Return an integer array `results` of size `n - k + 1`, where `results[i]` is the _power_ of `nums[i..(i + k - 1)]`. + +**Example 1:** + +**Input:** nums = [1,2,3,4,3,2,5], k = 3 + +**Output:** [3,4,-1,-1,-1] + +**Explanation:** + +There are 5 subarrays of `nums` of size 3: + +* `[1, 2, 3]` with the maximum element 3. +* `[2, 3, 4]` with the maximum element 4. +* `[3, 4, 3]` whose elements are **not** consecutive. +* `[4, 3, 2]` whose elements are **not** sorted. +* `[3, 2, 5]` whose elements are **not** consecutive. + +**Example 2:** + +**Input:** nums = [2,2,2,2,2], k = 4 + +**Output:** [-1,-1] + +**Example 3:** + +**Input:** nums = [3,2,3,2,3,2], k = 2 + +**Output:** [-1,3,-1,3,-1] + +**Constraints:** + +* 1 <= n == nums.length <= 105 +* 1 <= nums[i] <= 106 +* `1 <= k <= n` + +## Solution + +```kotlin +class Solution { + fun resultsArray(nums: IntArray, k: Int): IntArray { + if (k == 1) { + return nums + } + var start = 0 + val n = nums.size + val output = IntArray(n - k + 1) + for (i in 1 until n) { + if (nums[i] != nums[i - 1] + 1) { + start = i + } + val index = i - k + 1 + if (index >= 0) { + if (start > index) { + output[index] = -1 + } else { + output[index] = nums[i] + } + } + } + return output + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i/readme.md b/src/main/kotlin/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i/readme.md new file mode 100644 index 00000000..9f263262 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i/readme.md @@ -0,0 +1,113 @@ +[![](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) + +## 3256\. Maximum Value Sum by Placing Three Rooks I + +Hard + +You are given a `m x n` 2D array `board` representing a chessboard, where `board[i][j]` represents the **value** of the cell `(i, j)`. + +Rooks in the **same** row or column **attack** each other. You need to place _three_ rooks on the chessboard such that the rooks **do not** **attack** each other. + +Return the **maximum** sum of the cell **values** on which the rooks are placed. + +**Example 1:** + +**Input:** board = \[\[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]] + +**Output:** 4 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/08/08/rooks2.png) + +We can place the rooks in the cells `(0, 2)`, `(1, 3)`, and `(2, 1)` for a sum of `1 + 1 + 2 = 4`. + +**Example 2:** + +**Input:** board = \[\[1,2,3],[4,5,6],[7,8,9]] + +**Output:** 15 + +**Explanation:** + +We can place the rooks in the cells `(0, 0)`, `(1, 1)`, and `(2, 2)` for a sum of `1 + 5 + 9 = 15`. + +**Example 3:** + +**Input:** board = \[\[1,1,1],[1,1,1],[1,1,1]] + +**Output:** 3 + +**Explanation:** + +We can place the rooks in the cells `(0, 2)`, `(1, 1)`, and `(2, 0)` for a sum of `1 + 1 + 1 = 3`. + +**Constraints:** + +* `3 <= m == board.length <= 100` +* `3 <= n == board[i].length <= 100` +* -109 <= board[i][j] <= 109 + +## Solution + +```kotlin +import kotlin.math.max + +class Solution { + fun maximumValueSum(board: Array): Long { + val n = board.size + val m = board[0].size + val tb = Array(n) { IntArray(m) } + tb[0] = board[0].copyOf(m) + for (i in 1 until n) { + for (j in 0 until m) { + tb[i][j] = max(tb[i - 1][j], board[i][j]) + } + } + val bt = Array(n) { IntArray(m) } + bt[n - 1] = board[n - 1].copyOf(m) + for (i in n - 2 downTo 0) { + for (j in 0 until m) { + bt[i][j] = max(bt[i + 1][j], board[i][j]) + } + } + var ans = Long.MIN_VALUE + for (i in 1 until n - 1) { + val max3Top = getMax3(tb[i - 1]) + val max3Cur = getMax3(board[i]) + val max3Bottom = getMax3(bt[i + 1]) + for (topCand in max3Top) { + for (curCand in max3Cur) { + for (bottomCand in max3Bottom) { + if (topCand[1] != curCand[1] && topCand[1] != bottomCand[1] && curCand[1] != bottomCand[1]) { + val cand = topCand[0].toLong() + curCand[0] + bottomCand[0] + ans = max(ans, cand) + } + } + } + } + } + return ans + } + + private fun getMax3(row: IntArray): Array { + val m = row.size + val ans = Array(3) { IntArray(2) } + ans.fill(intArrayOf(Int.MIN_VALUE, -1)) + for (j in 0 until m) { + if (row[j] >= ans[0][0]) { + ans[2] = ans[1] + ans[1] = ans[0] + ans[0] = intArrayOf(row[j], j) + } else if (row[j] >= ans[1][0]) { + ans[2] = ans[1] + ans[1] = intArrayOf(row[j], j) + } else if (row[j] > ans[2][0]) { + ans[2] = intArrayOf(row[j], j) + } + } + return ans + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii/readme.md b/src/main/kotlin/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii/readme.md new file mode 100644 index 00000000..ddc8faaf --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii/readme.md @@ -0,0 +1,113 @@ +[![](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) + +## 3257\. Maximum Value Sum by Placing Three Rooks II + +Hard + +You are given a `m x n` 2D array `board` representing a chessboard, where `board[i][j]` represents the **value** of the cell `(i, j)`. + +Rooks in the **same** row or column **attack** each other. You need to place _three_ rooks on the chessboard such that the rooks **do not** **attack** each other. + +Return the **maximum** sum of the cell **values** on which the rooks are placed. + +**Example 1:** + +**Input:** board = \[\[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]] + +**Output:** 4 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/08/08/rooks2.png) + +We can place the rooks in the cells `(0, 2)`, `(1, 3)`, and `(2, 1)` for a sum of `1 + 1 + 2 = 4`. + +**Example 2:** + +**Input:** board = \[\[1,2,3],[4,5,6],[7,8,9]] + +**Output:** 15 + +**Explanation:** + +We can place the rooks in the cells `(0, 0)`, `(1, 1)`, and `(2, 2)` for a sum of `1 + 5 + 9 = 15`. + +**Example 3:** + +**Input:** board = \[\[1,1,1],[1,1,1],[1,1,1]] + +**Output:** 3 + +**Explanation:** + +We can place the rooks in the cells `(0, 2)`, `(1, 1)`, and `(2, 0)` for a sum of `1 + 1 + 1 = 3`. + +**Constraints:** + +* `3 <= m == board.length <= 500` +* `3 <= n == board[i].length <= 500` +* -109 <= board[i][j] <= 109 + +## Solution + +```kotlin +import kotlin.math.max + +class Solution { + fun maximumValueSum(board: Array): Long { + val n = board.size + val m = board[0].size + val tb = Array(n) { IntArray(m) } + tb[0] = board[0].copyOf(m) + for (i in 1 until n) { + for (j in 0 until m) { + tb[i][j] = max(tb[i - 1][j], board[i][j]) + } + } + val bt = Array(n) { IntArray(m) } + bt[n - 1] = board[n - 1].copyOf(m) + for (i in n - 2 downTo 0) { + for (j in 0 until m) { + bt[i][j] = max(bt[i + 1][j], board[i][j]) + } + } + var ans = Long.MIN_VALUE + for (i in 1 until n - 1) { + val max3Top = getMax3(tb[i - 1]) + val max3Cur = getMax3(board[i]) + val max3Bottom = getMax3(bt[i + 1]) + for (topCand in max3Top) { + for (curCand in max3Cur) { + for (bottomCand in max3Bottom) { + if (topCand[1] != curCand[1] && topCand[1] != bottomCand[1] && curCand[1] != bottomCand[1]) { + val cand = topCand[0].toLong() + curCand[0] + bottomCand[0] + ans = max(ans, cand) + } + } + } + } + } + return ans + } + + private fun getMax3(row: IntArray): Array { + val m = row.size + val ans = Array(3) { IntArray(2) } + ans.fill(intArrayOf(Int.MIN_VALUE, -1)) + for (j in 0 until m) { + if (row[j] >= ans[0][0]) { + ans[2] = ans[1] + ans[1] = ans[0] + ans[0] = intArrayOf(row[j], j) + } else if (row[j] >= ans[1][0]) { + ans[2] = ans[1] + ans[1] = intArrayOf(row[j], j) + } else if (row[j] > ans[2][0]) { + ans[2] = intArrayOf(row[j], j) + } + } + return ans + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i/readme.md b/src/main/kotlin/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i/readme.md new file mode 100644 index 00000000..ed087ccb --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i/readme.md @@ -0,0 +1,86 @@ +[![](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) + +## 3258\. Count Substrings That Satisfy K-Constraint I + +Easy + +You are given a **binary** string `s` and an integer `k`. + +A **binary string** satisfies the **k-constraint** if **either** of the following conditions holds: + +* The number of `0`'s in the string is at most `k`. +* The number of `1`'s in the string is at most `k`. + +Return an integer denoting the number of substrings of `s` that satisfy the **k-constraint**. + +**Example 1:** + +**Input:** s = "10101", k = 1 + +**Output:** 12 + +**Explanation:** + +Every substring of `s` except the substrings `"1010"`, `"10101"`, and `"0101"` satisfies the k-constraint. + +**Example 2:** + +**Input:** s = "1010101", k = 2 + +**Output:** 25 + +**Explanation:** + +Every substring of `s` except the substrings with a length greater than 5 satisfies the k-constraint. + +**Example 3:** + +**Input:** s = "11111", k = 1 + +**Output:** 15 + +**Explanation:** + +All substrings of `s` satisfy the k-constraint. + +**Constraints:** + +* `1 <= s.length <= 50` +* `1 <= k <= s.length` +* `s[i]` is either `'0'` or `'1'`. + +## Solution + +```kotlin +class Solution { + fun countKConstraintSubstrings(s: String, k: Int): Int { + val n = s.length + var sum = n + var i = 0 + var j = 0 + var one = 0 + var zero = 0 + var ch: Char + while (j < n) { + ch = s[j++] + if (ch == '0') { + zero++ + } else { + one++ + } + while (i <= j && one > k && zero > k) { + ch = s[i++] + if (ch == '0') { + zero-- + } else { + one-- + } + } + val len = (zero + one - 1) + sum += len + } + return sum + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3259_maximum_energy_boost_from_two_drinks/readme.md b/src/main/kotlin/g3201_3300/s3259_maximum_energy_boost_from_two_drinks/readme.md new file mode 100644 index 00000000..31c46c31 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3259_maximum_energy_boost_from_two_drinks/readme.md @@ -0,0 +1,67 @@ +[![](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) + +## 3259\. Maximum Energy Boost From Two Drinks + +Medium + +You are given two integer arrays `energyDrinkA` and `energyDrinkB` of the same length `n` by a futuristic sports scientist. These arrays represent the energy boosts per hour provided by two different energy drinks, A and B, respectively. + +You want to _maximize_ your total energy boost by drinking one energy drink _per hour_. However, if you want to switch from consuming one energy drink to the other, you need to wait for _one hour_ to cleanse your system (meaning you won't get any energy boost in that hour). + +Return the **maximum** total energy boost you can gain in the next `n` hours. + +**Note** that you can start consuming _either_ of the two energy drinks. + +**Example 1:** + +**Input:** energyDrinkA = [1,3,1], energyDrinkB = [3,1,1] + +**Output:** 5 + +**Explanation:** + +To gain an energy boost of 5, drink only the energy drink A (or only B). + +**Example 2:** + +**Input:** energyDrinkA = [4,1,1], energyDrinkB = [1,1,3] + +**Output:** 7 + +**Explanation:** + +To gain an energy boost of 7: + +* Drink the energy drink A for the first hour. +* Switch to the energy drink B and we lose the energy boost of the second hour. +* Gain the energy boost of the drink B in the third hour. + +**Constraints:** + +* `n == energyDrinkA.length == energyDrinkB.length` +* 3 <= n <= 105 +* 1 <= energyDrinkA[i], energyDrinkB[i] <= 105 + +## Solution + +```kotlin +import kotlin.math.max + +class Solution { + fun maxEnergyBoost(energyDrinkA: IntArray, energyDrinkB: IntArray): Long { + var a0: Long = 0 + var a1: Long = 0 + var b0: Long = 0 + var b1: Long = 0 + val n = energyDrinkA.size + for (i in 0 until n) { + a1 = max((a0 + energyDrinkA[i]), b0) + b1 = max((b0 + energyDrinkB[i]), a0) + a0 = a1 + b0 = b1 + } + return max(a1, b1) + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k/readme.md b/src/main/kotlin/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k/readme.md new file mode 100644 index 00000000..baa46a82 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k/readme.md @@ -0,0 +1,157 @@ +[![](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) + +## 3260\. Find the Largest Palindrome Divisible by K + +Hard + +You are given two **positive** integers `n` and `k`. + +An integer `x` is called **k-palindromic** if: + +* `x` is a palindrome. +* `x` is divisible by `k`. + +Return the **largest** integer having `n` digits (as a string) that is **k-palindromic**. + +**Note** that the integer must **not** have leading zeros. + +**Example 1:** + +**Input:** n = 3, k = 5 + +**Output:** "595" + +**Explanation:** + +595 is the largest k-palindromic integer with 3 digits. + +**Example 2:** + +**Input:** n = 1, k = 4 + +**Output:** "8" + +**Explanation:** + +4 and 8 are the only k-palindromic integers with 1 digit. + +**Example 3:** + +**Input:** n = 5, k = 6 + +**Output:** "89898" + +**Constraints:** + +* 1 <= n <= 105 +* `1 <= k <= 9` + +## Solution + +```kotlin +class Solution { + fun largestPalindrome(n: Int, k: Int): String { + val sc = CharArray(n) + if (k == 1 || k == 3 || k == 9) { + sc.fill('9') + } else if (k == 7) { + if (n == 1) { + return "7" + } else if (n == 2) { + return "77" + } + val mod = n % 12 + checkValues(n, mod, sc) + } else if (k == 2) { + sc.fill('9') + sc[0] = '8' + sc[n - 1] = '8' + } else if (k == 4) { + sc.fill('8') + var i = 2 + var j = n - 3 + while (i <= j) { + sc[i] = '9' + sc[j] = '9' + ++i + --j + } + } else if (k == 5) { + sc.fill('9') + sc[0] = '5' + sc[n - 1] = '5' + } else if (k == 6) { + val number = getString(n, sc) + if (number != null) { + return number + } + } else if (k == 8) { + sc.fill('8') + var i = 3 + var j = n - 4 + while (i <= j) { + sc[i] = '9' + sc[j] = '9' + ++i + --j + } + } + return String(sc) + } + + private fun checkValues(n: Int, mod: Int, sc: CharArray) { + if (mod == 6 || mod == 0) { + sc.fill('9') + } else if (mod == 3) { + sc.fill('9') + sc[n / 2] = '5' + } else if (mod == 4 || mod == 5 || mod == 1 || mod == 2) { + sc.fill('7') + var i = 0 + var j = n - 1 + while (i + 1 < j) { + sc[i] = '9' + sc[j] = '9' + ++i + --j + } + } else if (mod == 7 || mod == 8 || mod == 10 || mod == 11) { + sc.fill('4') + var i = 0 + var j = n - 1 + while (i + 1 < j) { + sc[i] = '9' + sc[j] = '9' + ++i + --j + } + } else if (mod == 9) { + sc.fill('9') + sc[n / 2] = '6' + } + } + + private fun getString(n: Int, sc: CharArray): String? { + if (n == 1) { + return "6" + } else if (n == 2) { + return "66" + } else { + if (n % 2 == 1) { + sc.fill('9') + sc[0] = '8' + sc[n - 1] = '8' + sc[n / 2] = '8' + } else { + sc.fill('9') + sc[0] = '8' + sc[n - 1] = '8' + sc[n / 2] = '7' + sc[n / 2 - 1] = '7' + } + } + return null + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii/readme.md b/src/main/kotlin/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii/readme.md new file mode 100644 index 00000000..4897eee5 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii/readme.md @@ -0,0 +1,103 @@ +[![](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) + +## 3261\. Count Substrings That Satisfy K-Constraint II + +Hard + +You are given a **binary** string `s` and an integer `k`. + +You are also given a 2D integer array `queries`, where queries[i] = [li, ri]. + +A **binary string** satisfies the **k-constraint** if **either** of the following conditions holds: + +* The number of `0`'s in the string is at most `k`. +* The number of `1`'s in the string is at most `k`. + +Return an integer array `answer`, where `answer[i]` is the number of substrings of s[li..ri] that satisfy the **k-constraint**. + +**Example 1:** + +**Input:** s = "0001111", k = 2, queries = \[\[0,6]] + +**Output:** [26] + +**Explanation:** + +For the query `[0, 6]`, all substrings of `s[0..6] = "0001111"` satisfy the k-constraint except for the substrings `s[0..5] = "000111"` and `s[0..6] = "0001111"`. + +**Example 2:** + +**Input:** s = "010101", k = 1, queries = \[\[0,5],[1,4],[2,3]] + +**Output:** [15,9,3] + +**Explanation:** + +The substrings of `s` with a length greater than 3 do not satisfy the k-constraint. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s[i]` is either `'0'` or `'1'`. +* `1 <= k <= s.length` +* 1 <= queries.length <= 105 +* queries[i] == [li, ri] +* 0 <= li <= ri < s.length +* All queries are distinct. + +## Solution + +```kotlin +class Solution { + fun countKConstraintSubstrings(s: String, k: Int, queries: Array): LongArray { + val current = s.toCharArray() + val n = current.size + val prefix = LongArray(n) + val index = IntArray(n) + var i = 0 + var count = 0 + var count1 = 0 + var count0 = 0 + for (j in 0 until n) { + if (current[j] == '0') { + count0++ + } + if (current[j] == '1') { + count1++ + } + while (count0 > k && count1 > k) { + if (current[i] == '0') { + count0-- + } + if (current[i] == '1') { + count1-- + } + i++ + index[i] = j - 1 + } + count += j - i + 1 + index[i] = j + prefix[j] = count.toLong() + } + while (i < n) { + index[i++] = n - 1 + } + val result = LongArray(queries.size) + i = 0 + while (i < queries.size) { + val indexFirst = index[queries[i][0]] + if (indexFirst > queries[i][1]) { + val num = queries[i][1] - queries[i][0] + 1L + result[i] = ((num) * (num + 1)) / 2 + } else { + result[i] = prefix[queries[i][1]] - prefix[indexFirst] + val num = indexFirst - queries[i][0] + 1L + result[i] += ((num) * (num + 1)) / 2 + } + i++ + } + return result + } +} +``` \ No newline at end of file