Skip to content

Commit 3fbf09b

Browse files
authored
Added tasks 3248-3261
1 parent 81f9c9c commit 3fbf09b

File tree

12 files changed

+828
-45
lines changed
  • src/main/kotlin
    • g0201_0300/s0225_implement_stack_using_queues
    • g3201_3300
      • s3248_snake_in_matrix
      • s3254_find_the_power_of_k_size_subarrays_i
      • s3255_find_the_power_of_k_size_subarrays_ii
      • s3256_maximum_value_sum_by_placing_three_rooks_i
      • s3257_maximum_value_sum_by_placing_three_rooks_ii
      • s3258_count_substrings_that_satisfy_k_constraint_i
      • s3259_maximum_energy_boost_from_two_drinks
      • s3260_find_the_largest_palindrome_divisible_by_k
      • s3261_count_substrings_that_satisfy_k_constraint_ii

12 files changed

+828
-45
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,14 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 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
1820+
| 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
1821+
| 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
1822+
| 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
1823+
| 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
1824+
| 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
1825+
| 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
1826+
| 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
18191827
| 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
18201828
| 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
18211829
| 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 @@
41864194
| 0228 |[Summary Ranges](src/main/kotlin/g0201_0300/s0228_summary_ranges)| Easy | Array | 169 | 91.89
41874195
| 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
41884196
| 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
4189-
| 0225 |[Implement Stack using Queues](src/main/kotlin/g0201_0300/s0225_implement_stack_using_queues)| Easy | Stack, Design, Queue | 248 | 73.44
4197+
| 0225 |[Implement Stack using Queues](src/main/kotlin/g0201_0300/s0225_implement_stack_using_queues)| Easy | Stack, Design, Queue | 147 | 88.57
41904198
| 0224 |[Basic Calculator](src/main/kotlin/g0201_0300/s0224_basic_calculator)| Hard | String, Math, Stack, Recursion | 294 | 93.33
41914199
| 0223 |[Rectangle Area](src/main/kotlin/g0201_0300/s0223_rectangle_area)| Medium | Math, Geometry | 291 | 66.67
41924200
| 0222 |[Count Complete Tree Nodes](src/main/kotlin/g0201_0300/s0222_count_complete_tree_nodes)| |||

src/main/kotlin/g0201_0300/s0225_implement_stack_using_queues/readme.md

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -46,64 +46,41 @@ Implement the `MyStack` class:
4646

4747
```kotlin
4848
import java.util.LinkedList
49+
import java.util.Queue
4950

50-
class MyStack {
51-
private var queuePair = Pair(LinkedList<Int>(), LinkedList<Int>())
52-
private var top: Int? = null
51+
class MyStack() {
52+
private val queue1: Queue<Int> = LinkedList()
53+
private val queue2: Queue<Int> = LinkedList()
5354

5455
fun push(x: Int) {
55-
queuePair.first.addLast(x)
56-
top = x
56+
queue1.add(x)
5757
}
5858

5959
fun pop(): Int {
60-
if (isQueuesEmpty()) {
61-
throw Exception()
60+
while (queue1.size > 1) {
61+
queue2.add(queue1.remove())
6262
}
63-
val queuePair = selectSourceAndDestinationQueues(queuePair)
64-
var value = 0
65-
repeat(queuePair.first.size) {
66-
when (queuePair.first.size) {
67-
2 -> {
68-
top = queuePair.first.removeFirst()
69-
queuePair.second.addLast(top)
70-
}
71-
1 -> {
72-
value = queuePair.first.removeFirst()
73-
}
74-
else -> {
75-
queuePair.second.addLast(queuePair.first.removeFirst())
76-
}
77-
}
78-
}
79-
return value
63+
val top = queue1.remove()
64+
queue1.clear()
65+
queue1.addAll(queue2)
66+
queue2.clear()
67+
return top
8068
}
8169

8270
fun top(): Int {
83-
if (isQueuesEmpty()) {
84-
throw Exception()
71+
while (queue1.size > 1) {
72+
queue2.add(queue1.remove())
8573
}
86-
return top!!
74+
val top = queue1.remove()
75+
queue2.add(top)
76+
queue1.clear()
77+
queue1.addAll(queue2)
78+
queue2.clear()
79+
return top
8780
}
8881

8982
fun empty(): Boolean {
90-
return isQueuesEmpty()
91-
}
92-
93-
private fun isQueuesEmpty(): Boolean {
94-
if (queuePair.first.isEmpty() && queuePair.second.isEmpty()) {
95-
return true
96-
}
97-
return false
98-
}
99-
100-
private fun selectSourceAndDestinationQueues(queuePair: Pair<LinkedList<Int>, LinkedList<Int>>):
101-
Pair<LinkedList<Int>, LinkedList<Int>> {
102-
return if (queuePair.first.isNotEmpty()) {
103-
Pair(queuePair.first, queuePair.second)
104-
} else {
105-
Pair(queuePair.second, queuePair.first)
106-
}
83+
return queue1.isEmpty()
10784
}
10885
}
10986

Loading
Loading
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 3254\. Find the Power of K-Size Subarrays I
5+
6+
Medium
7+
8+
You are given an array of integers `nums` of length `n` and a _positive_ integer `k`.
9+
10+
The **power** of an array is defined as:
11+
12+
* Its **maximum** element if _all_ of its elements are **consecutive** and **sorted** in **ascending** order.
13+
* \-1 otherwise.
14+
15+
You need to find the **power** of all subarrays of `nums` of size `k`.
16+
17+
Return an integer array `results` of size `n - k + 1`, where `results[i]` is the _power_ of `nums[i..(i + k - 1)]`.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [1,2,3,4,3,2,5], k = 3
22+
23+
**Output:** [3,4,-1,-1,-1]
24+
25+
**Explanation:**
26+
27+
There are 5 subarrays of `nums` of size 3:
28+
29+
* `[1, 2, 3]` with the maximum element 3.
30+
* `[2, 3, 4]` with the maximum element 4.
31+
* `[3, 4, 3]` whose elements are **not** consecutive.
32+
* `[4, 3, 2]` whose elements are **not** sorted.
33+
* `[3, 2, 5]` whose elements are **not** consecutive.
34+
35+
**Example 2:**
36+
37+
**Input:** nums = [2,2,2,2,2], k = 4
38+
39+
**Output:** [-1,-1]
40+
41+
**Example 3:**
42+
43+
**Input:** nums = [3,2,3,2,3,2], k = 2
44+
45+
**Output:** [-1,3,-1,3,-1]
46+
47+
**Constraints:**
48+
49+
* `1 <= n == nums.length <= 500`
50+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
51+
* `1 <= k <= n`
52+
53+
## Solution
54+
55+
```kotlin
56+
class Solution {
57+
fun resultsArray(nums: IntArray, k: Int): IntArray {
58+
val n = nums.size
59+
val arr = IntArray(n - k + 1)
60+
var count = 0
61+
for (i in 1 until k) {
62+
if (nums[i] == nums[i - 1] + 1) {
63+
count++
64+
}
65+
}
66+
arr[0] = if ((count == k - 1)) nums[k - 1] else -1
67+
for (i in 1..n - k) {
68+
if (nums[i] == nums[i - 1] + 1) {
69+
count--
70+
}
71+
if (nums[i + k - 1] == nums[i + k - 2] + 1) {
72+
count++
73+
}
74+
arr[i] = if ((count == k - 1)) nums[i + k - 1] else -1
75+
}
76+
return arr
77+
}
78+
}
79+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 3255\. Find the Power of K-Size Subarrays II
5+
6+
Medium
7+
8+
You are given an array of integers `nums` of length `n` and a _positive_ integer `k`.
9+
10+
The **power** of an array is defined as:
11+
12+
* Its **maximum** element if _all_ of its elements are **consecutive** and **sorted** in **ascending** order.
13+
* \-1 otherwise.
14+
15+
You need to find the **power** of all subarrays of `nums` of size `k`.
16+
17+
Return an integer array `results` of size `n - k + 1`, where `results[i]` is the _power_ of `nums[i..(i + k - 1)]`.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [1,2,3,4,3,2,5], k = 3
22+
23+
**Output:** [3,4,-1,-1,-1]
24+
25+
**Explanation:**
26+
27+
There are 5 subarrays of `nums` of size 3:
28+
29+
* `[1, 2, 3]` with the maximum element 3.
30+
* `[2, 3, 4]` with the maximum element 4.
31+
* `[3, 4, 3]` whose elements are **not** consecutive.
32+
* `[4, 3, 2]` whose elements are **not** sorted.
33+
* `[3, 2, 5]` whose elements are **not** consecutive.
34+
35+
**Example 2:**
36+
37+
**Input:** nums = [2,2,2,2,2], k = 4
38+
39+
**Output:** [-1,-1]
40+
41+
**Example 3:**
42+
43+
**Input:** nums = [3,2,3,2,3,2], k = 2
44+
45+
**Output:** [-1,3,-1,3,-1]
46+
47+
**Constraints:**
48+
49+
* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
50+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
51+
* `1 <= k <= n`
52+
53+
## Solution
54+
55+
```kotlin
56+
class Solution {
57+
fun resultsArray(nums: IntArray, k: Int): IntArray {
58+
if (k == 1) {
59+
return nums
60+
}
61+
var start = 0
62+
val n = nums.size
63+
val output = IntArray(n - k + 1)
64+
for (i in 1 until n) {
65+
if (nums[i] != nums[i - 1] + 1) {
66+
start = i
67+
}
68+
val index = i - k + 1
69+
if (index >= 0) {
70+
if (start > index) {
71+
output[index] = -1
72+
} else {
73+
output[index] = nums[i]
74+
}
75+
}
76+
}
77+
return output
78+
}
79+
}
80+
```
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 3256\. Maximum Value Sum by Placing Three Rooks I
5+
6+
Hard
7+
8+
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)`.
9+
10+
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.
11+
12+
Return the **maximum** sum of the cell **values** on which the rooks are placed.
13+
14+
**Example 1:**
15+
16+
**Input:** board = \[\[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]]
17+
18+
**Output:** 4
19+
20+
**Explanation:**
21+
22+
![](https://assets.leetcode.com/uploads/2024/08/08/rooks2.png)
23+
24+
We can place the rooks in the cells `(0, 2)`, `(1, 3)`, and `(2, 1)` for a sum of `1 + 1 + 2 = 4`.
25+
26+
**Example 2:**
27+
28+
**Input:** board = \[\[1,2,3],[4,5,6],[7,8,9]]
29+
30+
**Output:** 15
31+
32+
**Explanation:**
33+
34+
We can place the rooks in the cells `(0, 0)`, `(1, 1)`, and `(2, 2)` for a sum of `1 + 5 + 9 = 15`.
35+
36+
**Example 3:**
37+
38+
**Input:** board = \[\[1,1,1],[1,1,1],[1,1,1]]
39+
40+
**Output:** 3
41+
42+
**Explanation:**
43+
44+
We can place the rooks in the cells `(0, 2)`, `(1, 1)`, and `(2, 0)` for a sum of `1 + 1 + 1 = 3`.
45+
46+
**Constraints:**
47+
48+
* `3 <= m == board.length <= 100`
49+
* `3 <= n == board[i].length <= 100`
50+
* <code>-10<sup>9</sup> <= board[i][j] <= 10<sup>9</sup></code>
51+
52+
## Solution
53+
54+
```kotlin
55+
import kotlin.math.max
56+
57+
class Solution {
58+
fun maximumValueSum(board: Array<IntArray>): Long {
59+
val n = board.size
60+
val m = board[0].size
61+
val tb = Array(n) { IntArray(m) }
62+
tb[0] = board[0].copyOf(m)
63+
for (i in 1 until n) {
64+
for (j in 0 until m) {
65+
tb[i][j] = max(tb[i - 1][j], board[i][j])
66+
}
67+
}
68+
val bt = Array(n) { IntArray(m) }
69+
bt[n - 1] = board[n - 1].copyOf(m)
70+
for (i in n - 2 downTo 0) {
71+
for (j in 0 until m) {
72+
bt[i][j] = max(bt[i + 1][j], board[i][j])
73+
}
74+
}
75+
var ans = Long.MIN_VALUE
76+
for (i in 1 until n - 1) {
77+
val max3Top = getMax3(tb[i - 1])
78+
val max3Cur = getMax3(board[i])
79+
val max3Bottom = getMax3(bt[i + 1])
80+
for (topCand in max3Top) {
81+
for (curCand in max3Cur) {
82+
for (bottomCand in max3Bottom) {
83+
if (topCand[1] != curCand[1] && topCand[1] != bottomCand[1] && curCand[1] != bottomCand[1]) {
84+
val cand = topCand[0].toLong() + curCand[0] + bottomCand[0]
85+
ans = max(ans, cand)
86+
}
87+
}
88+
}
89+
}
90+
}
91+
return ans
92+
}
93+
94+
private fun getMax3(row: IntArray): Array<IntArray> {
95+
val m = row.size
96+
val ans = Array(3) { IntArray(2) }
97+
ans.fill(intArrayOf(Int.MIN_VALUE, -1))
98+
for (j in 0 until m) {
99+
if (row[j] >= ans[0][0]) {
100+
ans[2] = ans[1]
101+
ans[1] = ans[0]
102+
ans[0] = intArrayOf(row[j], j)
103+
} else if (row[j] >= ans[1][0]) {
104+
ans[2] = ans[1]
105+
ans[1] = intArrayOf(row[j], j)
106+
} else if (row[j] > ans[2][0]) {
107+
ans[2] = intArrayOf(row[j], j)
108+
}
109+
}
110+
return ans
111+
}
112+
}
113+
```

0 commit comments

Comments
 (0)