Skip to content

Commit aa759e1

Browse files
authored
Added tasks 59, 60, 61, 63.
1 parent c61b09e commit aa759e1

File tree

13 files changed

+391
-0
lines changed

13 files changed

+391
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
439439
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
440440
|-|-|-|-|-|-
441441
| 0062 |[Unique Paths](src/main/kotlin/g0001_0100/s0062_unique_paths/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics | 209 | 49.18
442+
| 0063 |[Unique Paths II](src/main/kotlin/g0001_0100/s0063_unique_paths_ii/Solution.kt)| Medium | Array, Dynamic_Programming, Matrix | 187 | 84.62
442443

443444
#### Day 16
444445

@@ -631,6 +632,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
631632

632633
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
633634
|-|-|-|-|-|-
635+
| 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list/Solution.kt)| Medium | Two_Pointers, Linked_List | 355 | 13.54
634636

635637
#### Day 17
636638

@@ -1076,6 +1078,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
10761078
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10771079
|-|-|-|-|-|-
10781080
| 0114 |[Flatten Binary Tree to Linked List](src/main/kotlin/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution.kt)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List | 305 | 26.30
1081+
| 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list/Solution.kt)| Medium | Two_Pointers, Linked_List | 355 | 13.54
10791082
| 0024 |[Swap Nodes in Pairs](src/main/kotlin/g0001_0100/s0024_swap_nodes_in_pairs/Solution.kt)| Medium | Top_100_Liked_Questions, Linked_List, Recursion | 149 | 99.39
10801083
| 0142 |[Linked List Cycle II](src/main/kotlin/g0101_0200/s0142_linked_list_cycle_ii/Solution.kt)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List | 192 | 63.39
10811084
| 0141 |[Linked List Cycle](src/main/kotlin/g0101_0200/s0141_linked_list_cycle/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List | 223 | 91.85
@@ -1257,6 +1260,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
12571260
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12581261
|-|-|-|-|-|-
12591262
| 0048 |[Rotate Image](src/main/kotlin/g0001_0100/s0048_rotate_image/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix | 287 | 46.50
1263+
| 0059 |[Spiral Matrix II](src/main/kotlin/g0001_0100/s0059_spiral_matrix_ii/Solution.kt)| Medium | Array, Matrix, Simulation | 292 | 18.33
12601264

12611265
#### Day 4 Array
12621266

@@ -1525,7 +1529,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
15251529
| 0072 |[Edit Distance](src/main/kotlin/g0001_0100/s0072_edit_distance/Solution.kt)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 320 | 63.53
15261530
| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming | 127 | 97.06
15271531
| 0064 |[Minimum Path Sum](src/main/kotlin/g0001_0100/s0064_minimum_path_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Udemy_Dynamic_Programming | 222 | 95.70
1532+
| 0063 |[Unique Paths II](src/main/kotlin/g0001_0100/s0063_unique_paths_ii/Solution.kt)| Medium | Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_15 | 187 | 84.62
15281533
| 0062 |[Unique Paths](src/main/kotlin/g0001_0100/s0062_unique_paths/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, Algorithm_II_Day_13_Dynamic_Programming, Dynamic_Programming_I_Day_15, Level_1_Day_11_Dynamic_Programming | 209 | 49.18
1534+
| 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list/Solution.kt)| Medium | Two_Pointers, Linked_List, Programming_Skills_II_Day_16, Udemy_Linked_List | 355 | 13.54
1535+
| 0060 |[Permutation Sequence](src/main/kotlin/g0001_0100/s0060_permutation_sequence/Solution.kt)| Hard | Math, Recursion | 293 | 27.78
1536+
| 0059 |[Spiral Matrix II](src/main/kotlin/g0001_0100/s0059_spiral_matrix_ii/Solution.kt)| Medium | Array, Matrix, Simulation, Data_Structure_II_Day_3_Array | 292 | 18.33
15291537
| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word/Solution.kt)| Easy | String, Programming_Skills_II_Day_6, Udemy_Arrays | 243 | 63.33
15301538
| 0057 |[Insert Interval](src/main/kotlin/g0001_0100/s0057_insert_interval/Solution.kt)| Medium | Array, Level_2_Day_17_Interval | 257 | 99.52
15311539
| 0056 |[Merge Intervals](src/main/kotlin/g0001_0100/s0056_merge_intervals/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Data_Structure_II_Day_2_Array, Level_2_Day_17_Interval, Udemy_2D_Arrays/Matrix | 323 | 99.68
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0001_0100.s0059_spiral_matrix_ii
2+
3+
// #Medium #Array #Matrix #Simulation #Data_Structure_II_Day_3_Array
4+
// #2022_09_22_Time_292_ms_(18.33%)_Space_34.7_MB_(71.67%)
5+
6+
class Solution {
7+
fun generateMatrix(n: Int): Array<IntArray> {
8+
var num = 1
9+
var rStart = 0
10+
var rEnd = n - 1
11+
var cStart = 0
12+
var cEnd = n - 1
13+
val spiral = Array(n) { IntArray(n) }
14+
while (rStart <= rEnd && cStart <= cEnd) {
15+
for (k in cStart..cEnd) {
16+
spiral[rStart][k] = num++
17+
}
18+
rStart++
19+
for (k in rStart..rEnd) {
20+
spiral[k][cEnd] = num++
21+
}
22+
cEnd--
23+
if (rStart <= rEnd) {
24+
for (k in cEnd downTo cStart) {
25+
spiral[rEnd][k] = num++
26+
}
27+
}
28+
rEnd--
29+
if (cStart <= cEnd) {
30+
for (k in rEnd downTo rStart) {
31+
spiral[k][cStart] = num++
32+
}
33+
}
34+
cStart++
35+
}
36+
return spiral
37+
}
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
59\. Spiral Matrix II
2+
3+
Medium
4+
5+
Given a positive integer `n`, generate an `n x n` `matrix` filled with elements from `1` to <code>n<sup>2</sup></code> in spiral order.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/11/13/spiraln.jpg)
10+
11+
**Input:** n = 3
12+
13+
**Output:** [[1,2,3],[8,9,4],[7,6,5]]
14+
15+
**Example 2:**
16+
17+
**Input:** n = 1
18+
19+
**Output:** [[1]]
20+
21+
**Constraints:**
22+
23+
* `1 <= n <= 20`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0001_0100.s0060_permutation_sequence
2+
3+
// #Hard #Math #Recursion #2022_09_22_Time_293_ms_(27.78%)_Space_34.2_MB_(61.11%)
4+
5+
class Solution {
6+
fun getPermutation(n: Int, k: Int): String {
7+
var k = k
8+
val res = CharArray(n)
9+
// We want the permutation sequence to be zero-indexed
10+
k = k - 1
11+
// The set bits indicate the available digits
12+
var a = (1 shl n) - 1
13+
var m = 1
14+
for (i in 2 until n) {
15+
// m = (n - 1)!
16+
m *= i
17+
}
18+
for (i in 0 until n) {
19+
var b = a
20+
for (j in 0 until k / m) {
21+
b = b and b - 1
22+
}
23+
// b is the bit corresponding to the digit we want
24+
b = b and b.inv() + 1
25+
res[i] = ('1'.code + Integer.bitCount(b - 1)).toChar()
26+
// Remove b from the set of available digits
27+
a = a and b.inv()
28+
k %= m
29+
m /= Math.max(1, n - i - 1)
30+
}
31+
return String(res)
32+
}
33+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
60\. Permutation Sequence
2+
3+
Hard
4+
5+
The set `[1, 2, 3, ..., n]` contains a total of `n!` unique permutations.
6+
7+
By listing and labeling all of the permutations in order, we get the following sequence for `n = 3`:
8+
9+
1. `"123"`
10+
2. `"132"`
11+
3. `"213"`
12+
4. `"231"`
13+
5. `"312"`
14+
6. `"321"`
15+
16+
Given `n` and `k`, return the <code>k<sup>th</sup></code> permutation sequence.
17+
18+
**Example 1:**
19+
20+
**Input:** n = 3, k = 3
21+
22+
**Output:** "213"
23+
24+
**Example 2:**
25+
26+
**Input:** n = 4, k = 9
27+
28+
**Output:** "2314"
29+
30+
**Example 3:**
31+
32+
**Input:** n = 3, k = 1
33+
34+
**Output:** "123"
35+
36+
**Constraints:**
37+
38+
* `1 <= n <= 9`
39+
* `1 <= k <= n!`
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g0001_0100.s0061_rotate_list
2+
3+
// #Medium #Two_Pointers #Linked_List #Programming_Skills_II_Day_16 #Udemy_Linked_List
4+
// #2022_09_22_Time_355_ms_(13.54%)_Space_35.5_MB_(85.42%)
5+
6+
import com_github_leetcode.ListNode
7+
8+
/**
9+
* Example:
10+
* var li = ListNode(5)
11+
* var v = li.`val`
12+
* Definition for singly-linked list.
13+
* class ListNode(var `val`: Int) {
14+
* var next: ListNode? = null
15+
* }
16+
*/
17+
class Solution {
18+
fun rotateRight(head: ListNode?, k: Int): ListNode? {
19+
if (head == null || k == 0) {
20+
return head
21+
}
22+
var tail = head
23+
// find the count and let tail points to last node
24+
var count = 1
25+
while (tail != null && tail.next != null) {
26+
count++
27+
tail = tail.next
28+
}
29+
// calculate number of times to rotate by count modulas
30+
val times = k % count
31+
if (times == 0) {
32+
return head
33+
}
34+
var temp = head
35+
// iterate and go to the K+1 th node from the end or count - K - 1 node from
36+
// start
37+
var i = 1
38+
while (i <= count - times - 1 && temp != null) {
39+
temp = temp.next
40+
i++
41+
}
42+
var newHead: ListNode? = null
43+
if (temp != null && tail != null) {
44+
newHead = temp.next
45+
temp.next = null
46+
tail.next = head
47+
}
48+
return newHead
49+
}
50+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
61\. Rotate List
2+
3+
Medium
4+
5+
Given the `head` of a linked list, rotate the list to the right by `k` places.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg)
10+
11+
**Input:** head = [1,2,3,4,5], k = 2
12+
13+
**Output:** [4,5,1,2,3]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg)
18+
19+
**Input:** head = [0,1,2], k = 4
20+
21+
**Output:** [2,0,1]
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range `[0, 500]`.
26+
* `-100 <= Node.val <= 100`
27+
* <code>0 <= k <= 2 * 10<sup>9</sup></code>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g0001_0100.s0063_unique_paths_ii
2+
3+
// #Medium #Array #Dynamic_Programming #Matrix #Dynamic_Programming_I_Day_15
4+
// #2022_09_22_Time_187_ms_(84.62%)_Space_43.5_MB_(6.15%)
5+
6+
class Solution {
7+
fun uniquePathsWithObstacles(obstacleGrid: Array<IntArray>): Int {
8+
// if start point has obstacle, there's no path
9+
if (obstacleGrid[0][0] == 1) {
10+
return 0
11+
}
12+
obstacleGrid[0][0] = 1
13+
val m = obstacleGrid.size
14+
val n: Int = obstacleGrid[0].size
15+
for (i in 1 until m) {
16+
if (obstacleGrid[i][0] == 1) {
17+
obstacleGrid[i][0] = 0
18+
} else {
19+
obstacleGrid[i][0] = obstacleGrid[i - 1][0]
20+
}
21+
}
22+
for (j in 1 until n) {
23+
if (obstacleGrid[0][j] == 1) {
24+
obstacleGrid[0][j] = 0
25+
} else {
26+
obstacleGrid[0][j] = obstacleGrid[0][j - 1]
27+
}
28+
}
29+
for (i in 1 until m) {
30+
for (j in 1 until n) {
31+
if (obstacleGrid[i][j] == 1) {
32+
obstacleGrid[i][j] = 0
33+
} else {
34+
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]
35+
}
36+
}
37+
}
38+
return obstacleGrid[m - 1][n - 1]
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
63\. Unique Paths II
2+
3+
Medium
4+
5+
You are given an `m x n` integer array `grid`. There is a robot initially located at the **top-left corner** (i.e., `grid[0][0]`). The robot tries to move to the **bottom-right corner** (i.e., `grid[m-1][n-1]`). The robot can only move either down or right at any point in time.
6+
7+
An obstacle and space are marked as `1` or `0` respectively in `grid`. A path that the robot takes cannot include **any** square that is an obstacle.
8+
9+
Return _the number of possible unique paths that the robot can take to reach the bottom-right corner_.
10+
11+
The testcases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/11/04/robot1.jpg)
16+
17+
**Input:** obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
18+
19+
**Output:** 2
20+
21+
**Explanation:** There is one obstacle in the middle of the 3x3 grid above.
22+
23+
There are two ways to reach the bottom-right corner:
24+
25+
1. Right -> Right -> Down -> Down
26+
27+
2. Down -> Down -> Right -> Right
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2020/11/04/robot2.jpg)
32+
33+
**Input:** obstacleGrid = [[0,1],[0,0]]
34+
35+
**Output:** 1
36+
37+
**Constraints:**
38+
39+
* `m == obstacleGrid.length`
40+
* `n == obstacleGrid[i].length`
41+
* `1 <= m, n <= 100`
42+
* `obstacleGrid[i][j]` is `0` or `1`.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0001_0100.s0059_spiral_matrix_ii
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 generateMatrix() {
10+
assertThat(
11+
Solution().generateMatrix(3),
12+
equalTo(arrayOf(intArrayOf(1, 2, 3), intArrayOf(8, 9, 4), intArrayOf(7, 6, 5)))
13+
)
14+
}
15+
16+
@Test
17+
fun generateMatrix2() {
18+
assertThat(Solution().generateMatrix(1), equalTo(arrayOf(intArrayOf(1))))
19+
}
20+
}

0 commit comments

Comments
 (0)