Skip to content

Commit 36d73f2

Browse files
authored
Added tasks 50, 52, 57, 58.
1 parent 1baa1fb commit 36d73f2

File tree

13 files changed

+347
-0
lines changed

13 files changed

+347
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
574574
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
575575
|-|-|-|-|-|-
576576
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack | 936 | 80.54
577+
| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word/Solution.kt)| Easy | String | 243 | 63.33
577578

578579
#### Day 7
579580

@@ -976,6 +977,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
976977

977978
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
978979
|-|-|-|-|-|-
980+
| 0057 |[Insert Interval](src/main/kotlin/g0001_0100/s0057_insert_interval/Solution.kt)| Medium | Array | 257 | 99.52
979981
| 0056 |[Merge Intervals](src/main/kotlin/g0001_0100/s0056_merge_intervals/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting | 323 | 99.68
980982

981983
#### Day 18 Stack
@@ -1004,6 +1006,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
10041006
| 0136 |[Single Number](src/main/kotlin/g0101_0200/s0136_single_number/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation | 344 | 83.63
10051007
| 0007 |[Reverse Integer](src/main/kotlin/g0001_0100/s0007_reverse_integer/Solution.kt)| Medium | Top_Interview_Questions, Math | 230 | 57.36
10061008
| 0009 |[Palindrome Number](src/main/kotlin/g0001_0100/s0009_palindrome_number/Solution.kt)| Easy | Math | 208 | 94.88
1009+
| 0050 |[Pow(x, n)](src/main/kotlin/g0001_0100/s0050_powx_n/Solution.kt)| Medium | Top_Interview_Questions, Math, Recursion | 307 | 17.33
10071010

10081011
#### Udemy Strings
10091012

@@ -1030,6 +1033,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
10301033
| 0121 |[Best Time to Buy and Sell Stock](src/main/kotlin/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming | 609 | 94.06
10311034
| 0283 |[Move Zeroes](src/main/kotlin/g0201_0300/s0283_move_zeroes/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers | 516 | 79.07
10321035
| 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 234 | 92.75
1036+
| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word/Solution.kt)| Easy | String | 243 | 63.33
10331037
| 0189 |[Rotate Array](src/main/kotlin/g0101_0200/s0189_rotate_array/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers | 483 | 86.95
10341038
| 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 670 | 66.67
10351039
| 0075 |[Sort Colors](src/main/kotlin/g0001_0100/s0075_sort_colors/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 198 | 85.66
@@ -1522,11 +1526,15 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
15221526
| 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
15231527
| 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
15241528
| 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
1529+
| 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
1530+
| 0057 |[Insert Interval](src/main/kotlin/g0001_0100/s0057_insert_interval/Solution.kt)| Medium | Array, Level_2_Day_17_Interval | 257 | 99.52
15251531
| 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
15261532
| 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Algorithm_II_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_4, Udemy_Arrays | 670 | 66.67
15271533
| 0054 |[Spiral Matrix](src/main/kotlin/g0001_0100/s0054_spiral_matrix/Solution.kt)| Medium | Top_Interview_Questions, Array, Matrix, Simulation, Programming_Skills_II_Day_8, Level_2_Day_1_Implementation/Simulation, Udemy_2D_Arrays/Matrix | 224 | 62.50
15281534
| 0053 |[Maximum Subarray](src/main/kotlin/g0001_0100/s0053_maximum_subarray/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Data_Structure_I_Day_1_Array, Dynamic_Programming_I_Day_5, Udemy_Famous_Algorithm | 662 | 82.48
1535+
| 0052 |[N-Queens II](src/main/kotlin/g0001_0100/s0052_n_queens_ii/Solution.kt)| Hard | Backtracking | 231 | 80.00
15291536
| 0051 |[N-Queens](src/main/kotlin/g0001_0100/s0051_n_queens/Solution.kt)| Hard | Top_100_Liked_Questions, Array, Backtracking | 243 | 95.10
1537+
| 0050 |[Pow(x, n)](src/main/kotlin/g0001_0100/s0050_powx_n/Solution.kt)| Medium | Top_Interview_Questions, Math, Recursion, Udemy_Integers | 307 | 17.33
15301538
| 0049 |[Group Anagrams](src/main/kotlin/g0001_0100/s0049_group_anagrams/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Data_Structure_II_Day_8_String, Programming_Skills_II_Day_11, Udemy_Strings | 506 | 86.55
15311539
| 0048 |[Rotate Image](src/main/kotlin/g0001_0100/s0048_rotate_image/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Data_Structure_II_Day_3_Array, Programming_Skills_II_Day_7, Udemy_2D_Arrays/Matrix | 287 | 46.50
15321540
| 0047 |[Permutations II](src/main/kotlin/g0001_0100/s0047_permutations_ii/Solution.kt)| Medium | Array, Backtracking, Algorithm_II_Day_10_Recursion_Backtracking | 406 | 76.36
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0001_0100.s0050_powx_n
2+
3+
// #Medium #Top_Interview_Questions #Math #Recursion #Udemy_Integers
4+
// #2022_09_21_Time_307_ms_(17.33%)_Space_35.2_MB_(54.67%)
5+
6+
class Solution {
7+
fun myPow(x: Double, n: Int): Double {
8+
var x = x
9+
var nn = n.toLong()
10+
var res = 1.0
11+
if (n < 0) {
12+
nn *= -1
13+
}
14+
while (nn > 0) {
15+
if (nn % 2 == 1L) {
16+
nn--
17+
res *= x
18+
} else {
19+
x *= x
20+
nn /= 2
21+
}
22+
}
23+
return if (n < 0) {
24+
1.0 / res
25+
} else res
26+
}
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
50\. Pow(x, n)
2+
3+
Medium
4+
5+
Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which calculates `x` raised to the power `n` (i.e., <code>x<sup>n</sup></code>).
6+
7+
**Example 1:**
8+
9+
**Input:** x = 2.00000, n = 10
10+
11+
**Output:** 1024.00000
12+
13+
**Example 2:**
14+
15+
**Input:** x = 2.10000, n = 3
16+
17+
**Output:** 9.26100
18+
19+
**Example 3:**
20+
21+
**Input:** x = 2.00000, n = -2
22+
23+
**Output:** 0.25000
24+
25+
**Explanation:** 2<sup>\-2</sup> = 1/2<sup>2</sup> = 1/4 = 0.25
26+
27+
**Constraints:**
28+
29+
* `-100.0 < x < 100.0`
30+
* <code>-2<sup>31</sup> <= n <= 2<sup>31</sup>-1</code>
31+
* `n` is an integer.
32+
* <code>-10<sup>4</sup> <= x<sup>n</sup> <= 10<sup>4</sup></code>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g0001_0100.s0052_n_queens_ii
2+
3+
// #Hard #Backtracking #2022_09_21_Time_231_ms_(80.00%)_Space_33.7_MB_(94.29%)
4+
5+
class Solution {
6+
fun totalNQueens(n: Int): Int {
7+
val row = BooleanArray(n)
8+
val col = BooleanArray(n)
9+
val diagonal = BooleanArray(n + n - 1)
10+
val antiDiagonal = BooleanArray(n + n - 1)
11+
return totalNQueens(n, 0, row, col, diagonal, antiDiagonal)
12+
}
13+
14+
private fun totalNQueens(
15+
n: Int,
16+
r: Int,
17+
row: BooleanArray,
18+
col: BooleanArray,
19+
diagonal: BooleanArray,
20+
antiDiagonal: BooleanArray
21+
): Int {
22+
if (r == n) {
23+
return 1
24+
}
25+
var count = 0
26+
for (c in 0 until n) {
27+
if (!row[r] && !col[c] && !diagonal[r + c] && !antiDiagonal[r - c + n - 1]) {
28+
antiDiagonal[r - c + n - 1] = true
29+
diagonal[r + c] = antiDiagonal[r - c + n - 1]
30+
col[c] = diagonal[r + c]
31+
row[r] = col[c]
32+
count += totalNQueens(n, r + 1, row, col, diagonal, antiDiagonal)
33+
antiDiagonal[r - c + n - 1] = false
34+
diagonal[r + c] = antiDiagonal[r - c + n - 1]
35+
col[c] = diagonal[r + c]
36+
row[r] = col[c]
37+
}
38+
}
39+
return count
40+
}
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
52\. N-Queens II
2+
3+
Hard
4+
5+
The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
6+
7+
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
12+
13+
**Input:** n = 4
14+
15+
**Output:** 2
16+
17+
**Explanation:** There are two distinct solutions to the 4-queens puzzle as shown.
18+
19+
**Example 2:**
20+
21+
**Input:** n = 1
22+
23+
**Output:** 1
24+
25+
**Constraints:**
26+
27+
* `1 <= n <= 9`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0001_0100.s0057_insert_interval
2+
3+
// #Medium #Array #Level_2_Day_17_Interval #2022_09_21_Time_257_ms_(99.52%)_Space_37.1_MB_(99.52%)
4+
5+
import java.util.Arrays
6+
7+
class Solution {
8+
fun insert(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray> {
9+
val n = intervals.size
10+
var l = 0
11+
var r = n - 1
12+
while (l < n && newInterval[0] > intervals[l][1]) {
13+
l++
14+
}
15+
while (r >= 0 && newInterval[1] < intervals[r][0]) {
16+
r--
17+
}
18+
val res = Array(l + n - r) { IntArray(2) }
19+
for (i in 0 until l) {
20+
res[i] = Arrays.copyOf(intervals[i], intervals[i].size)
21+
}
22+
res[l][0] = Math.min(newInterval[0], if (l == n) newInterval[0] else intervals[l][0])
23+
res[l][1] = Math.max(newInterval[1], if (r == -1) newInterval[1] else intervals[r][1])
24+
var i = l + 1
25+
var j = r + 1
26+
while (j < n) {
27+
res[i] = intervals[j]
28+
i++
29+
j++
30+
}
31+
return res
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
57\. Insert Interval
2+
3+
Medium
4+
5+
You are given an array of non-overlapping intervals `intervals` where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> represent the start and the end of the <code>i<sup>th</sup></code> interval and `intervals` is sorted in ascending order by <code>start<sub>i</sub></code>. You are also given an interval `newInterval = [start, end]` that represents the start and end of another interval.
6+
7+
Insert `newInterval` into `intervals` such that `intervals` is still sorted in ascending order by <code>start<sub>i</sub></code> and `intervals` still does not have any overlapping intervals (merge overlapping intervals if necessary).
8+
9+
Return `intervals` _after the insertion_.
10+
11+
**Example 1:**
12+
13+
**Input:** intervals = [[1,3],[6,9]], newInterval = [2,5]
14+
15+
**Output:** [[1,5],[6,9]]
16+
17+
**Example 2:**
18+
19+
**Input:** intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
20+
21+
**Output:** [[1,2],[3,10],[12,16]]
22+
23+
**Explanation:** Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
24+
25+
**Constraints:**
26+
27+
* <code>0 <= intervals.length <= 10<sup>4</sup></code>
28+
* `intervals[i].length == 2`
29+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>5</sup></code>
30+
* `intervals` is sorted by <code>start<sub>i</sub></code> in **ascending** order.
31+
* `newInterval.length == 2`
32+
* <code>0 <= start <= end <= 10<sup>5</sup></code>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0001_0100.s0058_length_of_last_word
2+
3+
// #Easy #String #Programming_Skills_II_Day_6 #Udemy_Arrays
4+
// #2022_09_21_Time_243_ms_(63.33%)_Space_35.7_MB_(73.11%)
5+
6+
class Solution {
7+
fun lengthOfLastWord(str: String): Int {
8+
var len = 0
9+
for (i in str.length - 1 downTo 0) {
10+
val ch = str[i]
11+
if (ch == ' ' && len > 0) {
12+
break
13+
} else if (ch != ' ') {
14+
len++
15+
}
16+
}
17+
return len
18+
}
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
58\. Length of Last Word
2+
3+
Easy
4+
5+
Given a string `s` consisting of words and spaces, return _the length of the **last** word in the string._
6+
7+
A **word** is a maximal substring consisting of non-space characters only.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "Hello World"
12+
13+
**Output:** 5
14+
15+
**Explanation:** The last word is "World" with length 5.
16+
17+
**Example 2:**
18+
19+
**Input:** s = " fly me to the moon "
20+
21+
**Output:** 4
22+
23+
**Explanation:** The last word is "moon" with length 4.
24+
25+
**Example 3:**
26+
27+
**Input:** s = "luffy is still joyboy"
28+
29+
**Output:** 6
30+
31+
**Explanation:** The last word is "joyboy" with length 6.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= s.length <= 10<sup>4</sup></code>
36+
* `s` consists of only English letters and spaces `' '`.
37+
* There will be at least one word in `s`.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0050_powx_n
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 myPow() {
10+
assertThat(Solution().myPow(2.00000, 10), equalTo(1024.00000))
11+
}
12+
13+
@Test
14+
fun myPow2() {
15+
assertThat(Solution().myPow(2.10000, 3), equalTo(9.261000000000001))
16+
}
17+
18+
@Test
19+
fun myPow3() {
20+
assertThat(Solution().myPow(2.00000, -2), equalTo(0.25000))
21+
}
22+
}

0 commit comments

Comments
 (0)