Skip to content

Commit a38ee4c

Browse files
authored
Added tasks 908, 909, 910, 911
1 parent bfce334 commit a38ee4c

File tree

13 files changed

+416
-0
lines changed

13 files changed

+416
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
12731273

12741274
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12751275
|-|-|-|-|-|-
1276+
| 0911 |[Online Election](src/main/kotlin/g0901_1000/s0911_online_election/TopVotedCandidate.kt)| Medium | Array, Hash_Table, Binary_Search, Design | 766 | 83.33
12761277

12771278
### Dynamic Programming I
12781279

@@ -1583,6 +1584,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
15831584
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15841585
|-|-|-|-|-|-
15851586
| 0304 |[Range Sum Query 2D - Immutable](src.save/main/kotlin/g0301_0400/s0304_range_sum_query_2d_immutable/NumMatrix.kt)| Medium | Array, Matrix, Design, Prefix_Sum | 1373 | 85.71
1587+
| 0910 |[Smallest Range II](src/main/kotlin/g0901_1000/s0910_smallest_range_ii/Solution.kt)| Medium | Array, Math, Sorting, Greedy | 234 | 100.00
15861588

15871589
#### Day 14
15881590

@@ -1726,6 +1728,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
17261728
|------|----------------|-------------|-------------|----------|---------
17271729
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
17281730
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
1731+
| 0911 |[Online Election](src/main/kotlin/g0901_1000/s0911_online_election/TopVotedCandidate.kt)| Medium | Array, Hash_Table, Binary_Search, Design, Binary_Search_II_Day_20 | 766 | 83.33
1732+
| 0910 |[Smallest Range II](src/main/kotlin/g0901_1000/s0910_smallest_range_ii/Solution.kt)| Medium | Array, Math, Sorting, Greedy, Programming_Skills_II_Day_13 | 234 | 100.00
1733+
| 0909 |[Snakes and Ladders](src/main/kotlin/g0901_1000/s0909_snakes_and_ladders/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix | 203 | 100.00
1734+
| 0908 |[Smallest Range I](src/main/kotlin/g0901_1000/s0908_smallest_range_i/Solution.kt)| Easy | Array, Math | 202 | 87.50
17291735
| 0907 |[Sum of Subarray Minimums](src/main/kotlin/g0901_1000/s0907_sum_of_subarray_minimums/Solution.kt)| Medium | Array, Dynamic_Programming, Stack, Monotonic_Stack | 341 | 100.00
17301736
| 0906 |[Super Palindromes](src/main/kotlin/g0901_1000/s0906_super_palindromes/Solution.kt)| Hard | Math, Enumeration | 153 | 100.00
17311737
| 0905 |[Sort Array By Parity](src/main/kotlin/g0901_1000/s0905_sort_array_by_parity/Solution.kt)| Easy | Array, Sorting, Two_Pointers | 219 | 75.00
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0901_1000.s0908_smallest_range_i
2+
3+
// #Easy #Array #Math #2023_04_15_Time_202_ms_(87.50%)_Space_37.2_MB_(75.00%)
4+
5+
class Solution {
6+
fun smallestRangeI(nums: IntArray, k: Int): Int {
7+
var min = Int.MAX_VALUE
8+
var max = Int.MIN_VALUE
9+
for (num in nums) {
10+
min = min.coerceAtMost(num)
11+
max = max.coerceAtLeast(num)
12+
}
13+
return if (min + k >= max - k) {
14+
0
15+
} else max - k - (min + k)
16+
}
17+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
908\. Smallest Range I
2+
3+
Easy
4+
5+
You are given an integer array `nums` and an integer `k`.
6+
7+
In one operation, you can choose any index `i` where `0 <= i < nums.length` and change `nums[i]` to `nums[i] + x` where `x` is an integer from the range `[-k, k]`. You can apply this operation **at most once** for each index `i`.
8+
9+
The **score** of `nums` is the difference between the maximum and minimum elements in `nums`.
10+
11+
Return _the minimum **score** of_ `nums` _after applying the mentioned operation at most once for each index in it_.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1], k = 0
16+
17+
**Output:** 0
18+
19+
**Explanation:** The score is max(nums) - min(nums) = 1 - 1 = 0.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [0,10], k = 2
24+
25+
**Output:** 6
26+
27+
**Explanation:** Change nums to be [2, 8]. The score is max(nums) - min(nums) = 8 - 2 = 6.
28+
29+
**Example 3:**
30+
31+
**Input:** nums = [1,3,6], k = 3
32+
33+
**Output:** 0
34+
35+
**Explanation:** Change nums to be [4, 4, 4]. The score is max(nums) - min(nums) = 4 - 4 = 0.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
40+
* <code>0 <= nums[i] <= 10<sup>4</sup></code>
41+
* <code>0 <= k <= 10<sup>4</sup></code>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g0901_1000.s0909_snakes_and_ladders
2+
3+
// #Medium #Array #Breadth_First_Search #Matrix
4+
// #2023_04_15_Time_203_ms_(100.00%)_Space_36_MB_(100.00%)
5+
6+
import java.util.LinkedList
7+
import java.util.Queue
8+
9+
class Solution {
10+
private var size = 0
11+
fun snakesAndLadders(board: Array<IntArray>): Int {
12+
val queue: Queue<Int> = LinkedList()
13+
size = board.size
14+
val target = size * size
15+
val visited = BooleanArray(target)
16+
queue.add(1)
17+
visited[0] = true
18+
var step = 0
19+
while (!queue.isEmpty()) {
20+
val queueSize = queue.size
21+
for (i in 0 until queueSize) {
22+
val previousLabel = queue.poll()
23+
if (previousLabel == target) {
24+
return step
25+
}
26+
for (currentLabel in previousLabel + 1..Math.min(target, previousLabel + 6)) {
27+
if (visited[currentLabel - 1]) {
28+
continue
29+
}
30+
visited[currentLabel - 1] = true
31+
val position = indexToPosition(currentLabel)
32+
if (board[position[0]][position[1]] == -1) {
33+
queue.add(currentLabel)
34+
} else {
35+
queue.add(board[position[0]][position[1]])
36+
}
37+
}
38+
}
39+
step++
40+
}
41+
return -1
42+
}
43+
44+
private fun indexToPosition(index: Int): IntArray {
45+
val vertical = size - 1 - (index - 1) / size
46+
val horizontal: Int = if ((size - vertical) % 2 == 1) {
47+
(index - 1) % size
48+
} else {
49+
size - 1 - (index - 1) % size
50+
}
51+
return intArrayOf(vertical, horizontal)
52+
}
53+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
909\. Snakes and Ladders
2+
3+
Medium
4+
5+
You are given an `n x n` integer matrix `board` where the cells are labeled from `1` to <code>n<sup>2</sup></code> in a [**Boustrophedon style**](https://en.wikipedia.org/wiki/Boustrophedon) starting from the bottom left of the board (i.e. `board[n - 1][0]`) and alternating direction each row.
6+
7+
You start on square `1` of the board. In each move, starting from square `curr`, do the following:
8+
9+
* Choose a destination square `next` with a label in the range <code>[curr + 1, min(curr + 6, n<sup>2</sup>)]</code>.
10+
* This choice simulates the result of a standard **6-sided die roll**: i.e., there are always at most 6 destinations, regardless of the size of the board.
11+
* If `next` has a snake or ladder, you **must** move to the destination of that snake or ladder. Otherwise, you move to `next`.
12+
* The game ends when you reach the square <code>n<sup>2</sup></code>.
13+
14+
A board square on row `r` and column `c` has a snake or ladder if `board[r][c] != -1`. The destination of that snake or ladder is `board[r][c]`. Squares `1` and <code>n<sup>2</sup></code> do not have a snake or ladder.
15+
16+
Note that you only take a snake or ladder at most once per move. If the destination to a snake or ladder is the start of another snake or ladder, you do **not** follow the subsequent snake or ladder.
17+
18+
* For example, suppose the board is `[[-1,4],[-1,3]]`, and on the first move, your destination square is `2`. You follow the ladder to square `3`, but do **not** follow the subsequent ladder to `4`.
19+
20+
Return _the least number of moves required to reach the square_ <code>n<sup>2</sup></code>_. If it is not possible to reach the square, return_ `-1`.
21+
22+
**Example 1:**
23+
24+
![](https://assets.leetcode.com/uploads/2018/09/23/snakes.png)
25+
26+
**Input:** board = [[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,35,-1,-1,13,-1],[-1,-1,-1,-1,-1,-1],[-1,15,-1,-1,-1,-1]]
27+
28+
**Output:** 4
29+
30+
**Explanation:**
31+
32+
In the beginning, you start at square 1 (at row 5, column 0).
33+
34+
You decide to move to square 2 and must take the ladder to square 15.
35+
36+
You then decide to move to square 17 and must take the snake to square 13.
37+
38+
You then decide to move to square 14 and must take the ladder to square 35.
39+
40+
You then decide to move to square 36, ending the game.
41+
42+
This is the lowest possible number of moves to reach the last square, so return 4.
43+
44+
**Example 2:**
45+
46+
**Input:** board = [[-1,-1],[-1,3]]
47+
48+
**Output:** 1
49+
50+
**Constraints:**
51+
52+
* `n == board.length == board[i].length`
53+
* `2 <= n <= 20`
54+
* `grid[i][j]` is either `-1` or in the range <code>[1, n<sup>2</sup>]</code>.
55+
* The squares labeled `1` and <code>n<sup>2</sup></code> do not have any ladders or snakes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0901_1000.s0910_smallest_range_ii
2+
3+
// #Medium #Array #Math #Sorting #Greedy #Programming_Skills_II_Day_13
4+
// #2023_04_15_Time_234_ms_(100.00%)_Space_37.1_MB_(100.00%)
5+
6+
import java.util.Arrays
7+
8+
class Solution {
9+
fun smallestRangeII(nums: IntArray, k: Int): Int {
10+
Arrays.sort(nums)
11+
val n = nums.size
12+
var ans = nums[n - 1] - nums[0]
13+
val min = nums[0] + k
14+
val max = nums[n - 1] - k
15+
for (i in 0 until n - 1) {
16+
val mx = max.coerceAtLeast(nums[i] + k)
17+
val mi = min.coerceAtMost(nums[i + 1] - k)
18+
ans = ans.coerceAtMost(mx - mi)
19+
}
20+
return ans
21+
}
22+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
910\. Smallest Range II
2+
3+
Medium
4+
5+
You are given an integer array `nums` and an integer `k`.
6+
7+
For each index `i` where `0 <= i < nums.length`, change `nums[i]` to be either `nums[i] + k` or `nums[i] - k`.
8+
9+
The **score** of `nums` is the difference between the maximum and minimum elements in `nums`.
10+
11+
Return _the minimum **score** of_ `nums` _after changing the values at each index_.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1], k = 0
16+
17+
**Output:** 0
18+
19+
**Explanation:** The score is max(nums) - min(nums) = 1 - 1 = 0.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [0,10], k = 2
24+
25+
**Output:** 6
26+
27+
**Explanation:** Change nums to be [2, 8]. The score is max(nums) - min(nums) = 8 - 2 = 6.
28+
29+
**Example 3:**
30+
31+
**Input:** nums = [1,3,6], k = 3
32+
33+
**Output:** 3
34+
35+
**Explanation:** Change nums to be [4, 6, 3]. The score is max(nums) - min(nums) = 6 - 3 = 3.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
40+
* <code>0 <= nums[i] <= 10<sup>4</sup></code>
41+
* <code>0 <= k <= 10<sup>4</sup></code>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g0901_1000.s0911_online_election
2+
3+
// #Medium #Array #Hash_Table #Binary_Search #Design #Binary_Search_II_Day_20
4+
// #2023_04_15_Time_766_ms_(83.33%)_Space_66.7_MB_(100.00%)
5+
6+
class TopVotedCandidate(persons: IntArray, private val times: IntArray) {
7+
private val winnersAtTimeT: IntArray = IntArray(times.size)
8+
9+
init {
10+
val counterArray = IntArray(persons.size)
11+
var maxVote = 0
12+
var maxVotedPerson = 0
13+
for (i in persons.indices) {
14+
val person = persons[i]
15+
val voteCount = counterArray[person]
16+
if (voteCount + 1 >= maxVote) {
17+
maxVote = voteCount + 1
18+
maxVotedPerson = person
19+
}
20+
winnersAtTimeT[i] = maxVotedPerson
21+
counterArray[persons[i]] = voteCount + 1
22+
}
23+
}
24+
25+
fun q(t: Int): Int {
26+
var lo = 0
27+
var hi = times.size - 1
28+
if (t >= times[hi]) {
29+
lo = hi
30+
} else {
31+
while (lo < hi - 1) {
32+
val mid = lo + (hi - lo) / 2
33+
if (times[mid] == t) {
34+
lo = mid
35+
break
36+
} else if (times[mid] > t) {
37+
hi = mid
38+
} else {
39+
lo = mid
40+
}
41+
}
42+
}
43+
return winnersAtTimeT[lo]
44+
}
45+
}
46+
47+
/**
48+
* Your TopVotedCandidate object will be instantiated and called as such:
49+
* var obj = TopVotedCandidate(persons, times)
50+
* var param_1 = obj.q(t)
51+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
911\. Online Election
2+
3+
Medium
4+
5+
You are given two integer arrays `persons` and `times`. In an election, the <code>i<sup>th</sup></code> vote was cast for `persons[i]` at time `times[i]`.
6+
7+
For each query at a time `t`, find the person that was leading the election at time `t`. Votes cast at time `t` will count towards our query. In the case of a tie, the most recent vote (among tied candidates) wins.
8+
9+
Implement the `TopVotedCandidate` class:
10+
11+
* `TopVotedCandidate(int[] persons, int[] times)` Initializes the object with the `persons` and `times` arrays.
12+
* `int q(int t)` Returns the number of the person that was leading the election at time `t` according to the mentioned rules.
13+
14+
**Example 1:**
15+
16+
**Input** ["TopVotedCandidate", "q", "q", "q", "q", "q", "q"] [[[0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]], [3], [12], [25], [15], [24], [8]]
17+
18+
**Output:** [null, 0, 1, 1, 0, 0, 1]
19+
20+
**Explanation:**
21+
22+
TopVotedCandidate topVotedCandidate = new TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]);
23+
topVotedCandidate.q(3); // return 0, At time 3, the votes are [0], and 0 is leading.
24+
topVotedCandidate.q(12); // return 1, At time 12, the votes are [0,1,1], and 1 is leading.
25+
topVotedCandidate.q(25); // return 1, At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties go to the most recent vote.)
26+
topVotedCandidate.q(15); // return 0
27+
topVotedCandidate.q(24); // return 0
28+
topVotedCandidate.q(8); // return 1
29+
30+
**Constraints:**
31+
32+
* `1 <= persons.length <= 5000`
33+
* `times.length == persons.length`
34+
* `0 <= persons[i] < persons.length`
35+
* <code>0 <= times[i] <= 10<sup>9</sup></code>
36+
* `times` is sorted in a strictly increasing order.
37+
* <code>times[0] <= t <= 10<sup>9</sup></code>
38+
* At most <code>10<sup>4</sup></code> calls will be made to `q`.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0901_1000.s0908_smallest_range_i
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun smallestRangeI() {
10+
assertThat(Solution().smallestRangeI(intArrayOf(1), 0), equalTo(0))
11+
}
12+
13+
@Test
14+
fun smallestRangeI2() {
15+
assertThat(Solution().smallestRangeI(intArrayOf(0, 10), 2), equalTo(6))
16+
}
17+
18+
@Test
19+
fun smallestRangeI3() {
20+
assertThat(Solution().smallestRangeI(intArrayOf(1, 3, 6), 3), equalTo(0))
21+
}
22+
}

0 commit comments

Comments
 (0)