Skip to content

Commit 56c9205

Browse files
authored
Added tasks 46, 48, 49, 51.
1 parent eee7c8c commit 56c9205

File tree

12 files changed

+382
-0
lines changed

12 files changed

+382
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0001_0100.s0046_permutations
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Backtracking
4+
// #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%)
6+
7+
class Solution {
8+
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)
15+
}
16+
return result
17+
}
18+
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)
27+
}
28+
}
29+
return result
30+
}
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
46\. Permutations
2+
3+
Medium
4+
5+
Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,2,3]
10+
11+
**Output:** [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
12+
13+
**Example 2:**
14+
15+
**Input:** nums = [0,1]
16+
17+
**Output:** [[0,1],[1,0]]
18+
19+
**Example 3:**
20+
21+
**Input:** nums = [1]
22+
23+
**Output:** [[1]]
24+
25+
**Constraints:**
26+
27+
* `1 <= nums.length <= 6`
28+
* `-10 <= nums[i] <= 10`
29+
* All the integers of `nums` are **unique**.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g0001_0100.s0048_rotate_image
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Math #Matrix
4+
// #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%)
6+
7+
class Solution {
8+
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
17+
}
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
56+
}
57+
}
58+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
48\. Rotate Image
2+
3+
Medium
4+
5+
You are given an `n x n` 2D `matrix` representing an image, rotate the image by **90** degrees (clockwise).
6+
7+
You have to rotate the image [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm), which means you have to modify the input 2D matrix directly. **DO NOT** allocate another 2D matrix and do the rotation.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/08/28/mat1.jpg)
12+
13+
**Input:** matrix = [[1,2,3],[4,5,6],[7,8,9]]
14+
15+
**Output:** [[7,4,1],[8,5,2],[9,6,3]]
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2020/08/28/mat2.jpg)
20+
21+
**Input:** matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
22+
23+
**Output:** [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
24+
25+
**Constraints:**
26+
27+
* `n == matrix.length == matrix[i].length`
28+
* `1 <= n <= 20`
29+
* `-1000 <= matrix[i][j] <= 1000`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0001_0100.s0049_group_anagrams
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #String #Hash_Table #Sorting
4+
// #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%)
6+
7+
class Solution {
8+
fun groupAnagrams(strs: Array<String>): List<List<String>> {
9+
if (strs.isEmpty()) {
10+
return emptyList()
11+
}
12+
13+
val hashMap = hashMapOf<String, MutableList<String>>()
14+
15+
for (i in strs.indices) {
16+
val charArray = strs[i].toCharArray()
17+
charArray.sort()
18+
val sortedWord = String(charArray)
19+
if (hashMap.containsKey(sortedWord)) {
20+
hashMap[sortedWord]?.let {
21+
it.add(strs[i])
22+
hashMap[sortedWord] = it
23+
}
24+
} else {
25+
val list = mutableListOf<String>()
26+
list.add(strs[i])
27+
hashMap[sortedWord] = list
28+
}
29+
}
30+
val list = mutableListOf<List<String>>()
31+
hashMap.values.forEach {
32+
list.add(it)
33+
}
34+
return list
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
49\. Group Anagrams
2+
3+
Medium
4+
5+
Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
6+
7+
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
8+
9+
**Example 1:**
10+
11+
**Input:** strs = ["eat","tea","tan","ate","nat","bat"]
12+
13+
**Output:** [["bat"],["nat","tan"],["ate","eat","tea"]]
14+
15+
**Example 2:**
16+
17+
**Input:** strs = [""]
18+
19+
**Output:** [[""]]
20+
21+
**Example 3:**
22+
23+
**Input:** strs = ["a"]
24+
25+
**Output:** [["a"]]
26+
27+
**Constraints:**
28+
29+
* <code>1 <= strs.length <= 10<sup>4</sup></code>
30+
* `0 <= strs[i].length <= 100`
31+
* `strs[i]` consists of lowercase English letters.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g0001_0100.s0051_n_queens
2+
3+
// #Hard #Top_100_Liked_Questions #Array #Backtracking
4+
// #2022_08_28_Time_280_ms_(88.35%)_Space_41.3_MB_(81.55%)
5+
6+
class Solution {
7+
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+
}
32+
33+
fun construct() {
34+
val list = mutableListOf<String>()
35+
for (row in board) list.add(String(row))
36+
result.add(list)
37+
}
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+
}
50+
}
51+
}
52+
backtrack(0)
53+
return result
54+
}
55+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
51\. N-Queens
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 _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
8+
9+
Each solution contains a distinct board configuration of the n-queens' placement, where `'Q'` and `'.'` both indicate a queen and an empty space, respectively.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
14+
15+
**Input:** n = 4
16+
17+
**Output:** [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
18+
19+
**Explanation:** There exist two distinct solutions to the 4-queens puzzle as shown above
20+
21+
**Example 2:**
22+
23+
**Input:** n = 1
24+
25+
**Output:** [["Q"]]
26+
27+
**Constraints:**
28+
29+
* `1 <= n <= 9`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0046_permutations
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 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+
}
12+
13+
@Test
14+
fun permute2() {
15+
assertThat(Solution().permute(intArrayOf(0, 1)), equalTo(arrayOf(intArrayOf(1, 0).toList(), intArrayOf(0, 1).toList()).toList()))
16+
}
17+
18+
@Test
19+
fun permute3() {
20+
assertThat(Solution().permute(intArrayOf(1)), equalTo(arrayOf(intArrayOf(1).toList()).toList()))
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0001_0100.s0048_rotate_image
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 rotate() {
10+
var matrix = arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6), intArrayOf(7, 8, 9))
11+
var expected = arrayOf(intArrayOf(7, 4, 1).toTypedArray(), intArrayOf(8, 5, 2).toTypedArray(), intArrayOf(9, 6, 3).toTypedArray())
12+
Solution().rotate(matrix)
13+
assertThat(matrix, equalTo(expected))
14+
}
15+
16+
@Test
17+
fun rotate2() {
18+
var matrix = arrayOf(intArrayOf(5, 1, 9, 11), intArrayOf(2, 4, 8, 10), intArrayOf(13, 3, 6, 7), intArrayOf(15, 14, 12, 16))
19+
var expected = arrayOf(intArrayOf(15, 13, 2, 5).toTypedArray(), intArrayOf(14, 3, 4, 1).toTypedArray(), intArrayOf(12, 6, 8, 9).toTypedArray(), intArrayOf(16, 7, 10, 11).toTypedArray())
20+
Solution().rotate(matrix)
21+
assertThat(matrix, equalTo(expected))
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0049_group_anagrams
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 groupAnagrams() {
10+
assertThat(Solution().groupAnagrams(arrayOf("eat", "tea", "tan", "ate", "nat", "bat")), equalTo(arrayOf(arrayOf("eat", "tea", "ate").toList(), arrayOf("bat").toList(), arrayOf("tan", "nat").toList(),).toList()))
11+
}
12+
13+
@Test
14+
fun groupAnagrams2() {
15+
assertThat(Solution().groupAnagrams(arrayOf("")), equalTo(arrayOf(arrayOf("").toList()).toList()))
16+
}
17+
18+
@Test
19+
fun groupAnagrams3() {
20+
assertThat(Solution().groupAnagrams(arrayOf("a")), equalTo(arrayOf(arrayOf("a").toList()).toList()))
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0051_n_queens
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 solveNQueens() {
10+
assertThat(Solution().solveNQueens(4), equalTo(arrayOf(arrayOf(".Q..", "...Q", "Q...", "..Q.").toList(), arrayOf("..Q.", "Q...", "...Q", ".Q..").toList()).toList()))
11+
}
12+
13+
@Test
14+
fun solveNQueens2() {
15+
assertThat(Solution().solveNQueens(1), equalTo(arrayOf(arrayOf("Q").toList()).toList()))
16+
}
17+
}

0 commit comments

Comments
 (0)