Skip to content

Commit 93fc4b1

Browse files
ThanhNITjavadev
authored andcommitted
Added tasks 912, 913, 914, 915
1 parent a38ee4c commit 93fc4b1

File tree

13 files changed

+420
-0
lines changed

13 files changed

+420
-0
lines changed

README.md

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

448448
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
449449
|-|-|-|-|-|-
450+
| 0912 |[Sort an Array](src/main/kotlin/g0901_1000/s0912_sort_an_array/Solution.kt)| Medium | Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Merge_Sort, Bucket_Sort, Counting_Sort, Radix_Sort | 606 | 98.48
450451

451452
#### Udemy 2D Arrays/Matrix
452453

@@ -1728,6 +1729,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
17281729
|------|----------------|-------------|-------------|----------|---------
17291730
| 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
17301731
| 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
1732+
| 0915 |[Partition Array into Disjoint Intervals](src/main/kotlin/g0901_1000/s0915_partition_array_into_disjoint_intervals/Solution.kt)| Medium | Array | 510 | 76.92
1733+
| 0914 |[X of a Kind in a Deck of Cards](src/main/kotlin/g0901_1000/s0914_x_of_a_kind_in_a_deck_of_cards/Solution.kt)| Easy | Array, Hash_Table, Math, Counting, Number_Theory | 238 | 70.00
1734+
| 0913 |[Cat and Mouse](src/main/kotlin/g0901_1000/s0913_cat_and_mouse/Solution.kt)| Hard | Dynamic_Programming, Math, Graph, Memoization, Topological_Sort, Game_Theory | 211 | 100.00
1735+
| 0912 |[Sort an Array](src/main/kotlin/g0901_1000/s0912_sort_an_array/Solution.kt)| Medium | Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Merge_Sort, Bucket_Sort, Counting_Sort, Radix_Sort, Udemy_Sorting_Algorithms | 606 | 98.48
17311736
| 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
17321737
| 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
17331738
| 0909 |[Snakes and Ladders](src/main/kotlin/g0901_1000/s0909_snakes_and_ladders/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix | 203 | 100.00
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g0901_1000.s0912_sort_an_array
2+
3+
// #Medium #Array #Sorting #Heap_Priority_Queue #Divide_and_Conquer #Merge_Sort #Bucket_Sort
4+
// #Counting_Sort #Radix_Sort #Udemy_Sorting_Algorithms
5+
// #2023_04_16_Time_606_ms_(98.48%)_Space_47.6_MB_(57.11%)
6+
7+
class Solution {
8+
fun sortArray(nums: IntArray): IntArray {
9+
return mergeSort(nums, 0, nums.size - 1)
10+
}
11+
12+
private fun mergeSort(arr: IntArray, lo: Int, hi: Int): IntArray {
13+
if (lo == hi) {
14+
val sortedArr = IntArray(1)
15+
sortedArr[0] = arr[lo]
16+
return sortedArr
17+
}
18+
val mid = (lo + hi) / 2
19+
val leftArray = mergeSort(arr, lo, mid)
20+
val rightArray = mergeSort(arr, mid + 1, hi)
21+
return mergeSortedArray(leftArray, rightArray)
22+
}
23+
24+
private fun mergeSortedArray(a: IntArray, b: IntArray): IntArray {
25+
val ans = IntArray(a.size + b.size)
26+
var i = 0
27+
var j = 0
28+
var k = 0
29+
while (i < a.size && j < b.size) {
30+
if (a[i] < b[j]) {
31+
ans[k++] = a[i++]
32+
} else {
33+
ans[k++] = b[j++]
34+
}
35+
}
36+
while (i < a.size) {
37+
ans[k++] = a[i++]
38+
}
39+
while (j < b.size) {
40+
ans[k++] = b[j++]
41+
}
42+
return ans
43+
}
44+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
912\. Sort an Array
2+
3+
Medium
4+
5+
Given an array of integers `nums`, sort the array in ascending order and return it.
6+
7+
You must solve the problem **without using any built-in** functions in `O(nlog(n))` time complexity and with the smallest space complexity possible.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [5,2,3,1]
12+
13+
**Output:** [1,2,3,5]
14+
15+
**Explanation:** After sorting the array, the positions of some numbers are not changed (for example, 2 and 3), while the positions of other numbers are changed (for example, 1 and 5).
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [5,1,1,2,0,0]
20+
21+
**Output:** [0,0,1,1,2,5]
22+
23+
**Explanation:** Note that the values of nums are not necessairly unique.
24+
25+
**Constraints:**
26+
27+
* <code>1 <= nums.length <= 5 * 10<sup>4</sup></code>
28+
* <code>-5 * 10<sup>4</sup> <= nums[i] <= 5 * 10<sup>4</sup></code>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package g0901_1000.s0913_cat_and_mouse
2+
3+
// #Hard #Dynamic_Programming #Math #Graph #Memoization #Topological_Sort #Game_Theory
4+
// #2023_04_16_Time_211_ms_(100.00%)_Space_37.1_MB_(100.00%)
5+
6+
import java.util.LinkedList
7+
import java.util.Queue
8+
9+
class Solution {
10+
fun catMouseGame(graph: Array<IntArray>): Int {
11+
val n = graph.size
12+
val states = Array(n) {
13+
Array(n) {
14+
IntArray(
15+
2
16+
)
17+
}
18+
}
19+
val degree = Array(n) {
20+
Array(n) {
21+
IntArray(
22+
2
23+
)
24+
}
25+
}
26+
for (m in 0 until n) {
27+
for (c in 0 until n) {
28+
degree[m][c][MOUSE] = graph[m].size
29+
degree[m][c][CAT] = graph[c].size
30+
for (node in graph[c]) {
31+
if (node == 0) {
32+
--degree[m][c][CAT]
33+
break
34+
}
35+
}
36+
}
37+
}
38+
val q: Queue<IntArray> = LinkedList()
39+
for (i in 1 until n) {
40+
states[0][i][MOUSE] = MOUSE_WIN
41+
states[0][i][CAT] = MOUSE_WIN
42+
states[i][i][MOUSE] = CAT_WIN
43+
states[i][i][CAT] = CAT_WIN
44+
q.offer(intArrayOf(0, i, MOUSE, MOUSE_WIN))
45+
q.offer(intArrayOf(i, i, MOUSE, CAT_WIN))
46+
q.offer(intArrayOf(0, i, CAT, MOUSE_WIN))
47+
q.offer(intArrayOf(i, i, CAT, CAT_WIN))
48+
}
49+
while (!q.isEmpty()) {
50+
val state = q.poll()
51+
val mouse = state[0]
52+
val cat = state[1]
53+
val turn = state[2]
54+
val result = state[3]
55+
if (mouse == 1 && cat == 2 && turn == MOUSE) {
56+
return result
57+
}
58+
val prevTurn = 1 - turn
59+
for (prev in graph[if (prevTurn == MOUSE) mouse else cat]) {
60+
val prevMouse = if (prevTurn == MOUSE) prev else mouse
61+
val prevCat = if (prevTurn == CAT) prev else cat
62+
if (prevCat != 0 && states[prevMouse][prevCat][prevTurn] == DRAW &&
63+
(
64+
prevTurn == MOUSE && result == MOUSE_WIN || prevTurn == CAT && result == CAT_WIN ||
65+
--degree[prevMouse][prevCat][prevTurn] == 0
66+
)
67+
) {
68+
states[prevMouse][prevCat][prevTurn] = result
69+
q.offer(intArrayOf(prevMouse, prevCat, prevTurn, result))
70+
}
71+
}
72+
}
73+
return DRAW
74+
}
75+
76+
companion object {
77+
private const val DRAW = 0
78+
private const val MOUSE_WIN = 1
79+
private const val CAT_WIN = 2
80+
private const val MOUSE = 0
81+
private const val CAT = 1
82+
}
83+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
913\. Cat and Mouse
2+
3+
Hard
4+
5+
A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns.
6+
7+
The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph.
8+
9+
The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at node `0`.
10+
11+
During each player's turn, they **must** travel along one edge of the graph that meets where they are. For example, if the Mouse is at node 1, it **must** travel to any node in `graph[1]`.
12+
13+
Additionally, it is not allowed for the Cat to travel to the Hole (node 0.)
14+
15+
Then, the game can end in three ways:
16+
17+
* If ever the Cat occupies the same node as the Mouse, the Cat wins.
18+
* If ever the Mouse reaches the Hole, the Mouse wins.
19+
* If ever a position is repeated (i.e., the players are in the same position as a previous turn, and it is the same player's turn to move), the game is a draw.
20+
21+
Given a `graph`, and assuming both players play optimally, return
22+
23+
* `1` if the mouse wins the game,
24+
* `2` if the cat wins the game, or
25+
* `0` if the game is a draw.
26+
27+
**Example 1:**
28+
29+
![](https://assets.leetcode.com/uploads/2020/11/17/cat1.jpg)
30+
31+
**Input:** graph = [[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]]
32+
33+
**Output:** 0
34+
35+
**Example 2:**
36+
37+
![](https://assets.leetcode.com/uploads/2020/11/17/cat2.jpg)
38+
39+
**Input:** graph = [[1,3],[0],[3],[0,2]]
40+
41+
**Output:** 1
42+
43+
**Constraints:**
44+
45+
* `3 <= graph.length <= 50`
46+
* `1 <= graph[i].length < graph.length`
47+
* `0 <= graph[i][j] < graph.length`
48+
* `graph[i][j] != i`
49+
* `graph[i]` is unique.
50+
* The mouse and the cat can always move.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0901_1000.s0914_x_of_a_kind_in_a_deck_of_cards
2+
3+
// #Easy #Array #Hash_Table #Math #Counting #Number_Theory
4+
// #2023_04_16_Time_238_ms_(70.00%)_Space_37.6_MB_(60.00%)
5+
6+
class Solution {
7+
fun hasGroupsSizeX(deck: IntArray): Boolean {
8+
val map: HashMap<Int, Int> = HashMap()
9+
for (j in deck) {
10+
if (map.containsKey(j)) {
11+
map[j] = map[j]!! + 1
12+
} else {
13+
map[j] = 1
14+
}
15+
}
16+
var x = map[deck[0]]!!
17+
18+
for (entry in map.entries.iterator()) {
19+
x = gcd(x, entry.value)
20+
}
21+
return x >= 2
22+
}
23+
24+
private fun gcd(a: Int, b: Int): Int {
25+
return if (b == 0) {
26+
a
27+
} else gcd(b, a % b)
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
914\. X of a Kind in a Deck of Cards
2+
3+
Easy
4+
5+
You are given an integer array `deck` where `deck[i]` represents the number written on the <code>i<sup>th</sup></code> card.
6+
7+
Partition the cards into **one or more groups** such that:
8+
9+
* Each group has **exactly** `x` cards where `x > 1`, and
10+
* All the cards in one group have the same integer written on them.
11+
12+
Return `true` _if such partition is possible, or_ `false` _otherwise_.
13+
14+
**Example 1:**
15+
16+
**Input:** deck = [1,2,3,4,4,3,2,1]
17+
18+
**Output:** true
19+
20+
**Explanation:**: Possible partition [1,1],[2,2],[3,3],[4,4].
21+
22+
**Example 2:**
23+
24+
**Input:** deck = [1,1,1,2,2,2,3,3]
25+
26+
**Output:** false
27+
28+
**Explanation:**: No possible partition.
29+
30+
**Constraints:**
31+
32+
* <code>1 <= deck.length <= 10<sup>4</sup></code>
33+
* <code>0 <= deck[i] < 10<sup>4</sup></code>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0901_1000.s0915_partition_array_into_disjoint_intervals
2+
3+
// #Medium #Array #2023_04_16_Time_510_ms_(76.92%)_Space_53.2_MB_(69.23%)
4+
5+
class Solution {
6+
fun partitionDisjoint(nums: IntArray): Int {
7+
var res = 0
8+
var leftMax = nums[0]
9+
var greater = nums[0]
10+
for (i in 1 until nums.size) {
11+
if (greater <= nums[i]) {
12+
greater = nums[i]
13+
} else if (nums[i] < leftMax) {
14+
res = i
15+
leftMax = greater
16+
}
17+
}
18+
return res + 1
19+
}
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
915\. Partition Array into Disjoint Intervals
2+
3+
Medium
4+
5+
Given an integer array `nums`, partition it into two (contiguous) subarrays `left` and `right` so that:
6+
7+
* Every element in `left` is less than or equal to every element in `right`.
8+
* `left` and `right` are non-empty.
9+
* `left` has the smallest possible size.
10+
11+
Return _the length of_ `left` _after such a partitioning_.
12+
13+
Test cases are generated such that partitioning exists.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [5,0,3,8,6]
18+
19+
**Output:** 3
20+
21+
**Explanation:** left = [5,0,3], right = [8,6]
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [1,1,1,0,6,12]
26+
27+
**Output:** 4
28+
29+
**Explanation:** left = [1,1,1,0], right = [6,12]
30+
31+
**Constraints:**
32+
33+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
34+
* <code>0 <= nums[i] <= 10<sup>6</sup></code>
35+
* There is at least one valid answer for the given input.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0901_1000.s0912_sort_an_array
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 sortArray() {
10+
assertThat(
11+
Solution().sortArray(intArrayOf(5, 2, 3, 1)), equalTo(intArrayOf(1, 2, 3, 5))
12+
)
13+
}
14+
15+
@Test
16+
fun sortArray2() {
17+
assertThat(
18+
Solution().sortArray(intArrayOf(5, 1, 1, 2, 0, 0)),
19+
equalTo(intArrayOf(0, 0, 1, 1, 2, 5))
20+
)
21+
}
22+
}

0 commit comments

Comments
 (0)