Skip to content

Commit

Permalink
Added tasks 3360-3373
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Dec 3, 2024
1 parent bf2b4dc commit 8eec1ea
Show file tree
Hide file tree
Showing 36 changed files with 1,581 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g3301_3400.s3360_stone_removal_game

// #Easy #Math #Simulation #2024_12_03_Time_0_ms_(100.00%)_Space_34.3_MB_(6.00%)

class Solution {
fun canAliceWin(n: Int): Boolean {
if (n < 10) {
return false
}
var stonesRemaining = n - 10
var stonesToBeRemoved = 9
var i = 1
while (stonesRemaining >= stonesToBeRemoved) {
stonesRemaining -= stonesToBeRemoved
i++
stonesToBeRemoved--
}
return i % 2 != 0
}
}
37 changes: 37 additions & 0 deletions src/main/kotlin/g3301_3400/s3360_stone_removal_game/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
3360\. Stone Removal Game

Easy

Alice and Bob are playing a game where they take turns removing stones from a pile, with _Alice going first_.

* Alice starts by removing **exactly** 10 stones on her first turn.
* For each subsequent turn, each player removes **exactly** 1 fewer stone than the previous opponent.

The player who cannot make a move loses the game.

Given a positive integer `n`, return `true` if Alice wins the game and `false` otherwise.

**Example 1:**

**Input:** n = 12

**Output:** true

**Explanation:**

* Alice removes 10 stones on her first turn, leaving 2 stones for Bob.
* Bob cannot remove 9 stones, so Alice wins.

**Example 2:**

**Input:** n = 1

**Output:** false

**Explanation:**

* Alice cannot remove 10 stones, so Alice loses.

**Constraints:**

* `1 <= n <= 50`
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package g3301_3400.s3361_shift_distance_between_two_strings

// #Medium #Array #String #Prefix_Sum #2024_12_03_Time_350_ms_(82.50%)_Space_41.7_MB_(57.50%)

import kotlin.math.min

class Solution {
fun shiftDistance(s: String, t: String, nextCost: IntArray, previousCost: IntArray): Long {
val costs = Array<LongArray?>(26) { LongArray(26) }
var cost: Long
for (i in 0..25) {
cost = nextCost[i].toLong()
var j = if (i == 25) 0 else i + 1
while (j != i) {
costs[i]!![j] = cost
cost += nextCost[j].toLong()
if (j == 25) {
j = -1
}
j++
}
}
for (i in 0..25) {
cost = previousCost[i].toLong()
var j = if (i == 0) 25 else i - 1
while (j != i) {
costs[i]!![j] = min(costs[i]!![j].toDouble(), cost.toDouble()).toLong()
cost += previousCost[j].toLong()
if (j == 0) {
j = 26
}
j--
}
}
val n = s.length
var ans: Long = 0
for (i in 0..<n) {
ans += costs[s[i].code - 'a'.code]!![t[i].code - 'a'.code]
}
return ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
3361\. Shift Distance Between Two Strings

Medium

You are given two strings `s` and `t` of the same length, and two integer arrays `nextCost` and `previousCost`.

In one operation, you can pick any index `i` of `s`, and perform **either one** of the following actions:

* Shift `s[i]` to the next letter in the alphabet. If `s[i] == 'z'`, you should replace it with `'a'`. This operation costs `nextCost[j]` where `j` is the index of `s[i]` in the alphabet.
* Shift `s[i]` to the previous letter in the alphabet. If `s[i] == 'a'`, you should replace it with `'z'`. This operation costs `previousCost[j]` where `j` is the index of `s[i]` in the alphabet.

The **shift distance** is the **minimum** total cost of operations required to transform `s` into `t`.

Return the **shift distance** from `s` to `t`.

**Example 1:**

**Input:** s = "abab", t = "baba", nextCost = [100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [1,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

**Output:** 2

**Explanation:**

* We choose index `i = 0` and shift `s[0]` 25 times to the previous character for a total cost of 1.
* We choose index `i = 1` and shift `s[1]` 25 times to the next character for a total cost of 0.
* We choose index `i = 2` and shift `s[2]` 25 times to the previous character for a total cost of 1.
* We choose index `i = 3` and shift `s[3]` 25 times to the next character for a total cost of 0.

**Example 2:**

**Input:** s = "leet", t = "code", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]

**Output:** 31

**Explanation:**

* We choose index `i = 0` and shift `s[0]` 9 times to the previous character for a total cost of 9.
* We choose index `i = 1` and shift `s[1]` 10 times to the next character for a total cost of 10.
* We choose index `i = 2` and shift `s[2]` 1 time to the previous character for a total cost of 1.
* We choose index `i = 3` and shift `s[3]` 11 times to the next character for a total cost of 11.

**Constraints:**

* <code>1 <= s.length == t.length <= 10<sup>5</sup></code>
* `s` and `t` consist only of lowercase English letters.
* `nextCost.length == previousCost.length == 26`
* <code>0 <= nextCost[i], previousCost[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g3301_3400.s3362_zero_array_transformation_iii

// #Medium #Array #Sorting #Greedy #Heap_Priority_Queue #Prefix_Sum
// #2024_12_03_Time_195_ms_(81.82%)_Space_106.3_MB_(72.73%)

import java.util.PriorityQueue

class Solution {
fun maxRemoval(nums: IntArray, queries: Array<IntArray>): Int {
queries.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] }
val last = PriorityQueue<Int?>(Comparator { a: Int?, b: Int? -> b!! - a!! })
val diffs = IntArray(nums.size + 1)
var idx = 0
var cur = 0
for (i in nums.indices) {
while (idx < queries.size && queries[idx][0] == i) {
last.add(queries[idx][1])
idx++
}
cur += diffs[i]
while (cur < nums[i] && last.isNotEmpty() && last.peek()!! >= i) {
cur++
diffs[last.poll()!! + 1]--
}
if (cur < nums[i]) {
return -1
}
}
return last.size
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
3362\. Zero Array Transformation III

Medium

You are given an integer array `nums` of length `n` and a 2D array `queries` where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.

Each `queries[i]` represents the following action on `nums`:

* Decrement the value at each index in the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> in `nums` by **at most** 1.
* The amount by which the value is decremented can be chosen **independently** for each index.

A **Zero Array** is an array with all its elements equal to 0.

Return the **maximum** number of elements that can be removed from `queries`, such that `nums` can still be converted to a **zero array** using the _remaining_ queries. If it is not possible to convert `nums` to a **zero array**, return -1.

**Example 1:**

**Input:** nums = [2,0,2], queries = [[0,2],[0,2],[1,1]]

**Output:** 1

**Explanation:**

After removing `queries[2]`, `nums` can still be converted to a zero array.

* Using `queries[0]`, decrement `nums[0]` and `nums[2]` by 1 and `nums[1]` by 0.
* Using `queries[1]`, decrement `nums[0]` and `nums[2]` by 1 and `nums[1]` by 0.

**Example 2:**

**Input:** nums = [1,1,1,1], queries = [[1,3],[0,2],[1,3],[1,2]]

**Output:** 2

**Explanation:**

We can remove `queries[2]` and `queries[3]`.

**Example 3:**

**Input:** nums = [1,2,3,4], queries = [[0,3]]

**Output:** \-1

**Explanation:**

`nums` cannot be converted to a zero array even after using all the queries.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
* <code>1 <= queries.length <= 10<sup>5</sup></code>
* `queries[i].length == 2`
* <code>0 <= l<sub>i</sub> <= r<sub>i</sub> < nums.length</code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected

// #Hard #Array #Dynamic_Programming #Matrix
// #2024_12_03_Time_39_ms_(88.89%)_Space_161.2_MB_(100.00%)

import kotlin.math.max

class Solution {
fun maxCollectedFruits(fruits: Array<IntArray>): Int {
val n = fruits.size
// Set inaccessible cells to 0
for (i in 0..<n) {
for (j in 0..<n) {
if (i < j && j < n - 1 - i) {
fruits[i][j] = 0
}
if (j < i && i < n - 1 - j) {
fruits[i][j] = 0
}
}
}
var res = 0
for (i in 0..<n) {
res += fruits[i][i]
}
for (i in 1..<n) {
for (j in i + 1..<n) {
fruits[i][j] = (
fruits[i][j] + max(
fruits[i - 1][j - 1],
max(fruits[i - 1][j], if (j + 1 < n) fruits[i - 1][j + 1] else 0),
)
)
}
}
for (j in 1..<n) {
for (i in j + 1..<n) {
fruits[i][j] = (
fruits[i][j] + max(
fruits[i - 1][j - 1],
max(fruits[i][j - 1], if (i + 1 < n) fruits[i + 1][j - 1] else 0),
)
)
}
}
return res + fruits[n - 1][n - 2] + fruits[n - 2][n - 1]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
3363\. Find the Maximum Number of Fruits Collected

Hard

There is a game dungeon comprised of `n x n` rooms arranged in a grid.

You are given a 2D array `fruits` of size `n x n`, where `fruits[i][j]` represents the number of fruits in the room `(i, j)`. Three children will play in the game dungeon, with **initial** positions at the corner rooms `(0, 0)`, `(0, n - 1)`, and `(n - 1, 0)`.

The children will make **exactly** `n - 1` moves according to the following rules to reach the room `(n - 1, n - 1)`:

* The child starting from `(0, 0)` must move from their current room `(i, j)` to one of the rooms `(i + 1, j + 1)`, `(i + 1, j)`, and `(i, j + 1)` if the target room exists.
* The child starting from `(0, n - 1)` must move from their current room `(i, j)` to one of the rooms `(i + 1, j - 1)`, `(i + 1, j)`, and `(i + 1, j + 1)` if the target room exists.
* The child starting from `(n - 1, 0)` must move from their current room `(i, j)` to one of the rooms `(i - 1, j + 1)`, `(i, j + 1)`, and `(i + 1, j + 1)` if the target room exists.

When a child enters a room, they will collect all the fruits there. If two or more children enter the same room, only one child will collect the fruits, and the room will be emptied after they leave.

Return the **maximum** number of fruits the children can collect from the dungeon.

**Example 1:**

**Input:** fruits = [[1,2,3,4],[5,6,8,7],[9,10,11,12],[13,14,15,16]]

**Output:** 100

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/10/15/example_1.gif)

In this example:

* The 1<sup>st</sup> child (green) moves on the path `(0,0) -> (1,1) -> (2,2) -> (3, 3)`.
* The 2<sup>nd</sup> child (red) moves on the path `(0,3) -> (1,2) -> (2,3) -> (3, 3)`.
* The 3<sup>rd</sup> child (blue) moves on the path `(3,0) -> (3,1) -> (3,2) -> (3, 3)`.

In total they collect `1 + 6 + 11 + 1 + 4 + 8 + 12 + 13 + 14 + 15 = 100` fruits.

**Example 2:**

**Input:** fruits = [[1,1],[1,1]]

**Output:** 4

**Explanation:**

In this example:

* The 1<sup>st</sup> child moves on the path `(0,0) -> (1,1)`.
* The 2<sup>nd</sup> child moves on the path `(0,1) -> (1,1)`.
* The 3<sup>rd</sup> child moves on the path `(1,0) -> (1,1)`.

In total they collect `1 + 1 + 1 + 1 = 4` fruits.

**Constraints:**

* `2 <= n == fruits.length == fruits[i].length <= 1000`
* `0 <= fruits[i][j] <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g3301_3400.s3364_minimum_positive_sum_subarray

// #Easy #Array #Prefix_Sum #Sliding_Window #2024_12_03_Time_3_ms_(98.15%)_Space_38.1_MB_(33.33%)

import kotlin.math.min

class Solution {
fun minimumSumSubarray(li: List<Int>, l: Int, r: Int): Int {
val n = li.size
var min = Int.Companion.MAX_VALUE
val a = IntArray(n + 1)
for (i in 1..n) {
a[i] = a[i - 1] + li[i - 1]
}
for (size in l..r) {
for (i in size - 1..<n) {
val sum = a[i + 1] - a[i + 1 - size]
if (sum > 0) {
min = min(min, sum)
}
}
}
return if (min == Int.Companion.MAX_VALUE) {
-1
} else {
min
}
}
}
Loading

0 comments on commit 8eec1ea

Please sign in to comment.