Skip to content

Commit 696380d

Browse files
authored
Improved tasks 45-51.
1 parent 2b5a6f0 commit 696380d

File tree

10 files changed

+175
-152
lines changed

10 files changed

+175
-152
lines changed

src/main/kotlin/g0001_0100/s0045_jump_game_ii/Solution.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@ package g0001_0100.s0045_jump_game_ii
22

33
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Greedy
44
// #Algorithm_II_Day_13_Dynamic_Programming #Dynamic_Programming_I_Day_4
5-
// #2022_08_27_Time_231_ms_(98.08%)_Space_36.8_MB_(100.00%)
5+
// #2022_08_29_Time_227_ms_(98.14%)_Space_37.3_MB_(100.00%)
66

77
class Solution {
88
fun jump(nums: IntArray): Int {
9-
var jumps = 0
9+
var length = 0
10+
var maxLength = 0
1011
var minJump = 0
11-
var maxJump = 0
12-
while (maxJump < nums.lastIndex) {
13-
var nextJump = 0
14-
for (i in minJump..maxJump) { nextJump = maxOf(nextJump, i + nums[i]) }
15-
minJump = maxJump + 1
16-
maxJump = nextJump
17-
jumps++
12+
for (i in 0 until nums.size - 1) {
13+
length--
14+
maxLength--
15+
maxLength = Math.max(maxLength, nums[i])
16+
if (length <= 0) {
17+
length = maxLength
18+
minJump++
19+
}
20+
if (length >= nums.size - i - 1) {
21+
return minJump
22+
}
1823
}
19-
return jumps
24+
return minJump
2025
}
2126
}

src/main/kotlin/g0001_0100/s0046_permutations/Solution.kt

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,37 @@ package g0001_0100.s0046_permutations
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Backtracking
44
// #Algorithm_I_Day_11_Recursion_Backtracking #Level_2_Day_20_Brute_Force/Backtracking
5-
// #Udemy_Backtracking/Recursion #2022_08_28_Time_299_ms_(80.72%)_Space_39.8_MB_(85.67%)
5+
// #Udemy_Backtracking/Recursion #2022_08_29_Time_186_ms_(100.00%)_Space_36.9_MB_(98.90%)
66

77
class Solution {
88
fun permute(nums: IntArray): List<List<Int>> {
9-
var result = mutableListOf<MutableList<Int>>()
10-
val subResult = mutableListOf<Int>()
11-
subResult.add(nums[0])
12-
result.add(subResult)
13-
for (i in 1 until nums.size) {
14-
result = insert(nums[i], result)
9+
if (nums.isEmpty()) {
10+
return ArrayList()
1511
}
16-
return result
12+
val finalResult: MutableList<List<Int>> = ArrayList()
13+
permuteRecur(nums, finalResult, ArrayList(), BooleanArray(nums.size))
14+
return finalResult
1715
}
1816

19-
private fun insert(n: Int, arr: MutableList<MutableList<Int>>): MutableList<MutableList<Int>> {
20-
val result = mutableListOf<MutableList<Int>>()
21-
arr.forEach { p ->
22-
for (i in 0..p.size) {
23-
val subResult = mutableListOf<Int>()
24-
subResult.addAll(p)
25-
subResult.add(i, n)
26-
result.add(subResult)
17+
private fun permuteRecur(
18+
nums: IntArray,
19+
finalResult: MutableList<List<Int>>,
20+
currResult: MutableList<Int>,
21+
used: BooleanArray
22+
) {
23+
if (currResult.size == nums.size) {
24+
finalResult.add(ArrayList(currResult))
25+
return
26+
}
27+
for (i in nums.indices) {
28+
if (used[i]) {
29+
continue
2730
}
31+
currResult.add(nums[i])
32+
used[i] = true
33+
permuteRecur(nums, finalResult, currResult, used)
34+
used[i] = false
35+
currResult.removeAt(currResult.size - 1)
2836
}
29-
return result
3037
}
3138
}

src/main/kotlin/g0001_0100/s0048_rotate_image/Solution.kt

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,27 @@ package g0001_0100.s0048_rotate_image
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Math #Matrix
44
// #Data_Structure_II_Day_3_Array #Programming_Skills_II_Day_7 #Udemy_2D_Arrays/Matrix
5-
// #2022_08_28_Time_197_ms_(89.14%)_Space_35.5_MB_(83.90%)
5+
// #2022_08_29_Time_287_ms_(46.50%)_Space_35.9_MB_(45.39%)
66

77
class Solution {
88
fun rotate(matrix: Array<IntArray>) {
9-
val q = Queue()
10-
var l = 0
11-
var r = matrix.size - 1
12-
var u = 0
13-
var d = matrix.size - 1
14-
while (true) {
15-
if (l >= r) {
16-
break
9+
val n: Int = matrix.size
10+
for (i in 0 until n / 2) {
11+
for (j in i until n - i - 1) {
12+
val pos = arrayOf(
13+
intArrayOf(i, j),
14+
intArrayOf(j, n - 1 - i),
15+
intArrayOf(n - 1 - i, n - 1 - j),
16+
intArrayOf(n - 1 - j, i)
17+
)
18+
var t = matrix[pos[0][0]][pos[0][1]]
19+
for (k in 1 until pos.size) {
20+
val temp = matrix[pos[k][0]][pos[k][1]]
21+
matrix[pos[k][0]][pos[k][1]] = t
22+
t = temp
23+
}
24+
matrix[pos[0][0]][pos[0][1]] = t
1725
}
18-
q.initialize()
19-
for (i in l until r) {
20-
q.enq(matrix[u][i])
21-
}
22-
for (i in u until d) {
23-
q.enq(matrix[i][r])
24-
matrix[i][r] = q.deq()
25-
}
26-
for (i in r downTo l + 1) {
27-
q.enq(matrix[d][i])
28-
matrix[d][i] = q.deq()
29-
}
30-
for (i in d downTo u + 1) {
31-
q.enq(matrix[i][l])
32-
matrix[i][l] = q.deq()
33-
}
34-
for (i in l until r) {
35-
matrix[u][i] = q.deq()
36-
}
37-
l += 1
38-
r -= 1
39-
u += 1
40-
d -= 1
41-
}
42-
}
43-
44-
class Queue {
45-
var queue = MutableList(0) { 0 }
46-
fun initialize() {
47-
this.queue = MutableList(0) { 0 }
48-
}
49-
fun enq(i: Int) {
50-
this.queue.add(i)
51-
}
52-
fun deq(): Int {
53-
val r = this.queue[0]
54-
this.queue.removeAt(0)
55-
return r
5626
}
5727
}
5828
}

src/main/kotlin/g0001_0100/s0049_group_anagrams/Solution.kt

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,22 @@ package g0001_0100.s0049_group_anagrams
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #String #Hash_Table #Sorting
44
// #Data_Structure_II_Day_8_String #Programming_Skills_II_Day_11 #Udemy_Strings
5-
// #2022_08_28_Time_554_ms_(82.47%)_Space_74.6_MB_(66.07%)
5+
// #2022_08_29_Time_506_ms_(86.55%)_Space_72.5_MB_(81.04%)
6+
7+
import java.util.Arrays
68

79
class Solution {
810
fun groupAnagrams(strs: Array<String>): List<List<String>> {
9-
if (strs.isEmpty()) {
10-
return emptyList()
11-
}
12-
val hashMap = hashMapOf<String, MutableList<String>>()
13-
for (i in strs.indices) {
14-
val charArray = strs[i].toCharArray()
15-
charArray.sort()
16-
val sortedWord = String(charArray)
17-
if (hashMap.containsKey(sortedWord)) {
18-
hashMap[sortedWord]?.let {
19-
it.add(strs[i])
20-
hashMap[sortedWord] = it
21-
}
22-
} else {
23-
val list = mutableListOf<String>()
24-
list.add(strs[i])
25-
hashMap[sortedWord] = list
26-
}
27-
}
28-
val list = mutableListOf<List<String>>()
29-
hashMap.values.forEach {
30-
list.add(it)
11+
val hm: MutableMap<String, MutableList<String>> = HashMap()
12+
for (s in strs) {
13+
val ch = s.toCharArray()
14+
Arrays.sort(ch)
15+
val temp = String(ch)
16+
hm.computeIfAbsent(
17+
temp
18+
) { _: String? -> ArrayList() }
19+
hm[temp]!!.add(s)
3120
}
32-
return list
21+
return ArrayList<List<String>>(hm.values)
3322
}
3423
}
Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,48 @@
11
package g0001_0100.s0051_n_queens
22

33
// #Hard #Top_100_Liked_Questions #Array #Backtracking
4-
// #2022_08_28_Time_280_ms_(88.35%)_Space_41.3_MB_(81.55%)
4+
// #2022_08_29_Time_243_ms_(95.10%)_Space_39.7_MB_(91.18%)
5+
6+
import java.util.Arrays
57

68
class Solution {
79
fun solveNQueens(n: Int): List<List<String>> {
8-
val result = mutableListOf<List<String>>()
9-
val board = Array(n) { CharArray(n) { '.' } }
10-
// check only top rows and cols
11-
fun isValid(row: Int, col: Int): Boolean {
12-
for (i in 0 until row) {
13-
if (board[i][col] == 'Q')
14-
return false
15-
}
16-
// top right
17-
var (i, j) = row - 1 to col + 1
18-
while (i >= 0 && j < n) {
19-
if (board[i--][j++] == 'Q')
20-
return false
21-
}
22-
// top left
23-
i = row - 1
24-
j = col - 1
25-
while (i >= 0 && j >= 0) {
26-
if (board[i--][j--] == 'Q') {
27-
return false
28-
}
29-
}
30-
return true
31-
}
10+
val pos = BooleanArray(n + 2 * n - 1 + 2 * n - 1)
11+
val pos2 = IntArray(n)
12+
val ans: MutableList<List<String>> = ArrayList()
13+
helper(n, 0, pos, pos2, ans)
14+
return ans
15+
}
3216

33-
fun construct() {
34-
val list = mutableListOf<String>()
35-
for (row in board) list.add(String(row))
36-
result.add(list)
17+
private fun helper(n: Int, row: Int, pos: BooleanArray, pos2: IntArray, ans: MutableList<List<String>>) {
18+
if (row == n) {
19+
construct(n, pos2, ans)
20+
return
3721
}
38-
39-
fun backtrack(row: Int) {
40-
if (row == n) {
41-
construct()
42-
return
43-
}
44-
for (col in 0 until n) {
45-
if (isValid(row, col)) {
46-
board[row][col] = 'Q'
47-
backtrack(row + 1)
48-
board[row][col] = '.'
49-
}
22+
for (i in 0 until n) {
23+
val index = n + 2 * n - 1 + n - 1 + i - row
24+
if (pos[i] || pos[n + i + row] || pos[index]) {
25+
continue
5026
}
27+
pos[i] = true
28+
pos[n + i + row] = true
29+
pos[index] = true
30+
pos2[row] = i
31+
helper(n, row + 1, pos, pos2, ans)
32+
pos[i] = false
33+
pos[n + i + row] = false
34+
pos[index] = false
35+
}
36+
}
37+
38+
private fun construct(n: Int, pos: IntArray, ans: MutableList<List<String>>) {
39+
val sol: MutableList<String> = ArrayList()
40+
for (r in 0 until n) {
41+
val queenRow = CharArray(n)
42+
Arrays.fill(queenRow, '.')
43+
queenRow[pos[r]] = 'Q'
44+
sol.add(String(queenRow))
5145
}
52-
backtrack(0)
53-
return result
46+
ans.add(sol)
5447
}
5548
}

src/test/kotlin/g0001_0100/s0039_combination_sum/SolutionTest.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@ import org.junit.jupiter.api.Test
77
internal class SolutionTest {
88
@Test
99
fun combinationSum() {
10-
assertThat(Solution().combinationSum(intArrayOf(2, 3, 6, 7), 7), equalTo(arrayOf(intArrayOf(2, 2, 3).toList(), intArrayOf(7).toList()).toList()))
10+
assertThat(
11+
Solution().combinationSum(intArrayOf(2, 3, 6, 7), 7),
12+
equalTo(arrayOf(intArrayOf(2, 2, 3).toList(), intArrayOf(7).toList()).toList())
13+
)
1114
}
1215

1316
@Test
1417
fun combinationSum2() {
15-
assertThat(Solution().combinationSum(intArrayOf(2, 3, 5), 8), equalTo(arrayOf(intArrayOf(2, 2, 2, 2).toList(), intArrayOf(2, 3, 3).toList(), intArrayOf(3, 5).toList()).toList()))
18+
assertThat(
19+
Solution().combinationSum(intArrayOf(2, 3, 5), 8),
20+
equalTo(
21+
arrayOf(
22+
intArrayOf(2, 2, 2, 2).toList(),
23+
intArrayOf(2, 3, 3).toList(),
24+
intArrayOf(3, 5).toList()
25+
).toList()
26+
)
27+
)
1628
}
1729

1830
@Test
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
package g0001_0100.s0046_permutations
22

3+
import com_github_leetcode.ArrayUtils.getLists
34
import org.hamcrest.CoreMatchers.equalTo
45
import org.hamcrest.MatcherAssert.assertThat
56
import org.junit.jupiter.api.Test
67

78
internal class SolutionTest {
89
@Test
910
fun permute() {
10-
assertThat(Solution().permute(intArrayOf(1, 2, 3)), equalTo(arrayOf(intArrayOf(3, 2, 1).toList(), intArrayOf(2, 3, 1).toList(), intArrayOf(2, 1, 3).toList(), intArrayOf(3, 1, 2).toList(), intArrayOf(1, 3, 2).toList(), intArrayOf(1, 2, 3).toList()).toList()))
11+
val expected = arrayOf(
12+
intArrayOf(1, 2, 3),
13+
intArrayOf(1, 3, 2),
14+
intArrayOf(2, 1, 3),
15+
intArrayOf(2, 3, 1),
16+
intArrayOf(3, 1, 2),
17+
intArrayOf(3, 2, 1)
18+
)
19+
assertThat(
20+
Solution().permute(intArrayOf(1, 2, 3)),
21+
equalTo(getLists(expected))
22+
)
1123
}
1224

1325
@Test
1426
fun permute2() {
15-
assertThat(Solution().permute(intArrayOf(0, 1)), equalTo(arrayOf(intArrayOf(1, 0).toList(), intArrayOf(0, 1).toList()).toList()))
27+
val expected = arrayOf(intArrayOf(0, 1), intArrayOf(1, 0))
28+
assertThat(
29+
Solution().permute(intArrayOf(0, 1)), equalTo(getLists(expected))
30+
)
1631
}
1732

1833
@Test
1934
fun permute3() {
20-
assertThat(Solution().permute(intArrayOf(1)), equalTo(arrayOf(intArrayOf(1).toList()).toList()))
35+
val expected = arrayOf(intArrayOf(1))
36+
assertThat(Solution().permute(intArrayOf(1)), equalTo(getLists(expected)))
2137
}
2238
}

0 commit comments

Comments
 (0)