Skip to content

Commit c160295

Browse files
authored
Added tasks 797-805
1 parent 0717dfe commit c160295

File tree

11 files changed

+650
-6
lines changed

11 files changed

+650
-6
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
6060
|-|-|-|-|-|-
61+
| 0797 |[All Paths From Source to Target](src/main/kotlin/g0701_0800/s0797_all_paths_from_source_to_target)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Backtracking | 232 | 100.00
6162

6263
#### Day 8 Standard Traversal
6364

@@ -69,6 +70,7 @@
6970

7071
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
7172
|-|-|-|-|-|-
73+
| 0802 |[Find Eventual Safe States](src/main/kotlin/g0801_0900/s0802_find_eventual_safe_states)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 511 | 100.00
7274

7375
#### Day 10 Standard Traversal
7476

@@ -1028,6 +1030,7 @@
10281030
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10291031
|-|-|-|-|-|-
10301032
| 0130 |[Surrounded Regions](src/main/kotlin/g0101_0200/s0130_surrounded_regions)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 355 | 84.42
1033+
| 0797 |[All Paths From Source to Target](src/main/kotlin/g0701_0800/s0797_all_paths_from_source_to_target)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Backtracking | 232 | 100.00
10311034

10321035
#### Day 9 Recursion Backtracking
10331036

@@ -1676,6 +1679,14 @@
16761679
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence)| 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
16771680
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges)| 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
16781681
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
1682+
| 0805 |[Split Array With Same Average](src/main/kotlin/g0801_0900/s0805_split_array_with_same_average)| Hard | Array, Dynamic_Programming, Math, Bit_Manipulation, Bitmask | 142 | 100.00
1683+
| 0804 |[Unique Morse Code Words](src/main/kotlin/g0801_0900/s0804_unique_morse_code_words)| Easy | Array, String, Hash_Table | 158 | 80.00
1684+
| 0803 |[Bricks Falling When Hit](src/main/kotlin/g0801_0900/s0803_bricks_falling_when_hit)| Hard | Array, Matrix, Union_Find | 742 | 100.00
1685+
| 0802 |[Find Eventual Safe States](src/main/kotlin/g0801_0900/s0802_find_eventual_safe_states)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Graph_Theory_I_Day_9_Standard_Traversal | 511 | 100.00
1686+
| 0801 |[Minimum Swaps To Make Sequences Increasing](src/main/kotlin/g0801_0900/s0801_minimum_swaps_to_make_sequences_increasing)| Hard | Array, Dynamic_Programming | 617 | 83.33
1687+
| 0799 |[Champagne Tower](src/main/kotlin/g0701_0800/s0799_champagne_tower)| Medium | Dynamic_Programming | 153 | 100.00
1688+
| 0798 |[Smallest Rotation with Highest Score](src/main/kotlin/g0701_0800/s0798_smallest_rotation_with_highest_score)| Hard | Array, Prefix_Sum | 470 | 100.00
1689+
| 0797 |[All Paths From Source to Target](src/main/kotlin/g0701_0800/s0797_all_paths_from_source_to_target)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Backtracking, Algorithm_II_Day_8_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_7_Standard_Traversal | 232 | 100.00
16791690
| 0796 |[Rotate String](src/main/kotlin/g0701_0800/s0796_rotate_string)| Easy | String, String_Matching | 134 | 100.00
16801691
| 0795 |[Number of Subarrays with Bounded Maximum](src/main/kotlin/g0701_0800/s0795_number_of_subarrays_with_bounded_maximum)| Medium | Array, Two_Pointers | 361 | 66.67
16811692
| 0794 |[Valid Tic-Tac-Toe State](src/main/kotlin/g0701_0800/s0794_valid_tic_tac_toe_state)| Medium | Array, String | 138 | 100.00

src/main/kotlin/g0401_0500/s0493_reverse_pairs/readme.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ A **reverse pair** is a pair `(i, j)` where:
4646
## Solution
4747

4848
```kotlin
49-
import java.util.Arrays
50-
5149
class Solution {
5250
fun reversePairs(nums: IntArray): Int {
5351
return mergeSort(nums, 0, nums.size - 1)
@@ -68,7 +66,7 @@ class Solution {
6866
}
6967
cnt += j - (mid + 1)
7068
}
71-
Arrays.sort(nums, start, end + 1)
69+
nums.sort(start, end + 1)
7270
return cnt
7371
}
7472
}

src/main/kotlin/g0701_0800/s0787_cheapest_flights_within_k_stops/readme.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,13 @@ The optimal path with no stops from city 0 to 2 is marked in red and has cost 50
6868
## Solution
6969

7070
```kotlin
71-
import java.util.Arrays
72-
7371
class Solution {
7472
fun findCheapestPrice(n: Int, flights: Array<IntArray>, src: Int, dst: Int, k: Int): Int {
7573
// k + 2 becase there are total of k(intermediate stops) + 1(src) + 1(dst)
7674
// dp[i][j] = cost to reach j using atmost i edges from src
7775
val dp = Array(k + 2) { IntArray(n) }
7876
for (row in dp) {
79-
Arrays.fill(row, Int.MAX_VALUE)
77+
row.fill(Int.MAX_VALUE)
8078
}
8179
// cost to reach src is always 0
8280
for (i in 0..k + 1) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
## 797\. All Paths From Source to Target
5+
6+
Medium
7+
8+
Given a directed acyclic graph (**DAG**) of `n` nodes labeled from `0` to `n - 1`, find all possible paths from node `0` to node `n - 1` and return them in **any order**.
9+
10+
The graph is given as follows: `graph[i]` is a list of all nodes you can visit from node `i` (i.e., there is a directed edge from node `i` to node `graph[i][j]`).
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/09/28/all_1.jpg)
15+
16+
**Input:** graph = \[\[1,2],[3],[3],[]]
17+
18+
**Output:** [[0,1,3],[0,2,3]]
19+
20+
**Explanation:** There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
21+
22+
**Example 2:**
23+
24+
![](https://assets.leetcode.com/uploads/2020/09/28/all_2.jpg)
25+
26+
**Input:** graph = \[\[4,3,1],[3,2,4],[3],[4],[]]
27+
28+
**Output:** [[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]
29+
30+
**Constraints:**
31+
32+
* `n == graph.length`
33+
* `2 <= n <= 15`
34+
* `0 <= graph[i][j] < n`
35+
* `graph[i][j] != i` (i.e., there will be no self-loops).
36+
* All the elements of `graph[i]` are **unique**.
37+
* The input graph is **guaranteed** to be a **DAG**.
38+
39+
## Solution
40+
41+
```kotlin
42+
class Solution {
43+
private var res: MutableList<List<Int>>? = null
44+
fun allPathsSourceTarget(graph: Array<IntArray>): List<List<Int>> {
45+
res = ArrayList()
46+
val temp: MutableList<Int> = ArrayList()
47+
temp.add(0)
48+
// perform DFS
49+
solve(graph, temp, 0)
50+
return res as ArrayList<List<Int>>
51+
}
52+
53+
private fun solve(graph: Array<IntArray>, temp: MutableList<Int>, lastNode: Int) {
54+
if (lastNode == graph.size - 1) {
55+
res!!.add(ArrayList(temp))
56+
}
57+
for (link in graph[lastNode]) {
58+
temp.add(link)
59+
solve(graph, temp, link)
60+
temp.removeAt(temp.size - 1)
61+
}
62+
}
63+
}
64+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
## 798\. Smallest Rotation with Highest Score
5+
6+
Hard
7+
8+
You are given an array `nums`. You can rotate it by a non-negative integer `k` so that the array becomes `[nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1], ..., nums[k-1]]`. Afterward, any entries that are less than or equal to their index are worth one point.
9+
10+
* For example, if we have `nums = [2,4,1,3,0]`, and we rotate by `k = 2`, it becomes `[1,3,0,2,4]`. This is worth `3` points because `1 > 0` [no points], `3 > 1` [no points], `0 <= 2` [one point], `2 <= 3` [one point], `4 <= 4` [one point].
11+
12+
Return _the rotation index_ `k` _that corresponds to the highest score we can achieve if we rotated_ `nums` _by it_. If there are multiple answers, return the smallest such index `k`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,3,1,4,0]
17+
18+
**Output:** 3
19+
20+
**Explanation:** Scores for each k are listed below:
21+
22+
k = 0, nums = [2,3,1,4,0], score 2
23+
24+
k = 1, nums = [3,1,4,0,2], score 3
25+
26+
k = 2, nums = [1,4,0,2,3], score 3
27+
28+
k = 3, nums = [4,0,2,3,1], score 4
29+
30+
k = 4, nums = [0,2,3,1,4], score 3 So we should choose
31+
32+
k = 3, which has the highest score.
33+
34+
**Example 2:**
35+
36+
**Input:** nums = [1,3,0,2,4]
37+
38+
**Output:** 0
39+
40+
**Explanation:** nums will always have 3 points no matter how it shifts. So we will choose the smallest k, which is 0.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
45+
* `0 <= nums[i] < nums.length`
46+
47+
## Solution
48+
49+
```kotlin
50+
class Solution {
51+
// nums[i] will be in the range [0, nums.length].
52+
// At which positions will we lose points? The answer is k = i - nums[i] + 1.
53+
// We need to accumulate points we have lost from previous rotations using prefix sum except one
54+
// we did not lose.
55+
fun bestRotation(nums: IntArray): Int {
56+
val n = nums.size
57+
var res = 0
58+
val change = IntArray(n)
59+
for (i in 0 until n) {
60+
change[(i - nums[i] + 1 + n) % n]--
61+
}
62+
for (i in 1 until n) {
63+
change[i] += change[i - 1] + 1
64+
res = if (change[i] > change[res]) i else res
65+
}
66+
return res
67+
}
68+
}
69+
```
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+
## 799\. Champagne Tower
5+
6+
Medium
7+
8+
We stack glasses in a pyramid, where the **first** row has `1` glass, the **second** row has `2` glasses, and so on until the 100<sup>th</sup> row. Each glass holds one cup of champagne.
9+
10+
Then, some champagne is poured into the first glass at the top. When the topmost glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it. When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on. (A glass at the bottom row has its excess champagne fall on the floor.)
11+
12+
For example, after one cup of champagne is poured, the top most glass is full. After two cups of champagne are poured, the two glasses on the second row are half full. After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now. After four cups of champagne are poured, the third row has the middle glass half full, and the two outside glasses are a quarter full, as pictured below.
13+
14+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/09/tower.png)
15+
16+
Now after pouring some non-negative integer cups of champagne, return how full the <code>j<sup>th</sup></code> glass in the <code>i<sup>th</sup></code> row is (both `i` and `j` are 0-indexed.)
17+
18+
**Example 1:**
19+
20+
**Input:** poured = 1, query\_row = 1, query\_glass = 1
21+
22+
**Output:** 0.00000
23+
24+
**Explanation:** We poured 1 cup of champange to the top glass of the tower (which is indexed as (0, 0)). There will be no excess liquid so all the glasses under the top glass will remain empty.
25+
26+
**Example 2:**
27+
28+
**Input:** poured = 2, query\_row = 1, query\_glass = 1
29+
30+
**Output:** 0.50000
31+
32+
**Explanation:** We poured 2 cups of champange to the top glass of the tower (which is indexed as (0, 0)). There is one cup of excess liquid. The glass indexed as (1, 0) and the glass indexed as (1, 1) will share the excess liquid equally, and each will get half cup of champange.
33+
34+
**Example 3:**
35+
36+
**Input:** poured = 100000009, query\_row = 33, query\_glass = 17
37+
38+
**Output:** 1.00000
39+
40+
**Constraints:**
41+
42+
* <code>0 <= poured <= 10<sup>9</sup></code>
43+
* `0 <= query_glass <= query_row < 100`
44+
45+
## Solution
46+
47+
```kotlin
48+
class Solution {
49+
fun champagneTower(poured: Int, queryRow: Int, queryGlass: Int): Double {
50+
var curRow = 0
51+
// first row
52+
var cur = doubleArrayOf(poured.toDouble())
53+
// second row
54+
var next = DoubleArray(2)
55+
var spilled: Boolean
56+
do {
57+
spilled = false
58+
for (i in cur.indices) {
59+
// spilling, put the excess into the next row.
60+
if (cur[i] > 1) {
61+
val spilledAmount = cur[i] - 1
62+
cur[i] = 1.0
63+
next[i] += spilledAmount / 2
64+
next[i + 1] = spilledAmount / 2
65+
spilled = true
66+
}
67+
}
68+
// got to the desired row, return the glass amount
69+
if (curRow == queryRow) {
70+
return cur[queryGlass]
71+
}
72+
cur = next
73+
curRow++
74+
next = DoubleArray(curRow + 2)
75+
} while (spilled)
76+
// spill did not happen to the desired row
77+
return 0.0
78+
}
79+
}
80+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
## 801\. Minimum Swaps To Make Sequences Increasing
5+
6+
Hard
7+
8+
You are given two integer arrays of the same length `nums1` and `nums2`. In one operation, you are allowed to swap `nums1[i]` with `nums2[i]`.
9+
10+
* For example, if <code>nums1 = [1,2,3,<ins>8</ins>]</code>, and <code>nums2 = [5,6,7,<ins>4</ins>]</code>, you can swap the element at `i = 3` to obtain `nums1 = [1,2,3,4]` and `nums2 = [5,6,7,8]`.
11+
12+
Return _the minimum number of needed operations to make_ `nums1` _and_ `nums2` _**strictly increasing**_. The test cases are generated so that the given input always makes it possible.
13+
14+
An array `arr` is **strictly increasing** if and only if `arr[0] < arr[1] < arr[2] < ... < arr[arr.length - 1]`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums1 = [1,3,5,4], nums2 = [1,2,3,7]
19+
20+
**Output:** 1
21+
22+
**Explanation:** Swap nums1[3] and nums2[3]. Then the sequences are: nums1 = [1, 3, 5, 7] and nums2 = [1, 2, 3, 4] which are both strictly increasing.
23+
24+
**Example 2:**
25+
26+
**Input:** nums1 = [0,3,5,8,9], nums2 = [2,1,4,6,9]
27+
28+
**Output:** 1
29+
30+
**Constraints:**
31+
32+
* <code>2 <= nums1.length <= 10<sup>5</sup></code>
33+
* `nums2.length == nums1.length`
34+
* <code>0 <= nums1[i], nums2[i] <= 2 * 10<sup>5</sup></code>
35+
36+
## Solution
37+
38+
```kotlin
39+
class Solution {
40+
fun minSwap(listA: IntArray, listB: IntArray): Int {
41+
val dp = IntArray(2)
42+
dp[1] = 1
43+
for (i in 1 until listA.size) {
44+
var a = Int.MAX_VALUE
45+
var b = Int.MAX_VALUE
46+
if (listA[i] > listA[i - 1] && listB[i] > listB[i - 1]) {
47+
a = dp[0]
48+
b = dp[1]
49+
}
50+
if (listA[i] > listB[i - 1] && listB[i] > listA[i - 1]) {
51+
a = a.coerceAtMost(dp[1])
52+
b = b.coerceAtMost(dp[0])
53+
}
54+
dp[0] = a
55+
dp[1] = b + 1
56+
}
57+
return dp[0].coerceAtMost(dp[1])
58+
}
59+
}
60+
```

0 commit comments

Comments
 (0)