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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)| |||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,64 +46,41 @@ Implement the `MyStack` class:

```kotlin
import java.util.LinkedList
import java.util.Queue

class MyStack {
private var queuePair = Pair(LinkedList<Int>(), LinkedList<Int>())
private var top: Int? = null
class MyStack() {
private val queue1: Queue<Int> = LinkedList()
private val queue2: Queue<Int> = 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<Int>, LinkedList<Int>>):
Pair<LinkedList<Int>, LinkedList<Int>> {
return if (queuePair.first.isNotEmpty()) {
Pair(queuePair.first, queuePair.second)
} else {
Pair(queuePair.second, queuePair.first)
}
return queue1.isEmpty()
}
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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`
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
* `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
}
}
```
Original file line number Diff line number Diff line change
@@ -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:**

* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
* `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
}
}
```
Original file line number Diff line number Diff line change
@@ -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`
* <code>-10<sup>9</sup> <= board[i][j] <= 10<sup>9</sup></code>

## Solution

```kotlin
import kotlin.math.max

class Solution {
fun maximumValueSum(board: Array<IntArray>): 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<IntArray> {
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
}
}
```
Loading