Skip to content

Commit ff2dd03

Browse files
authored
Added tasks 73, 74, 75, 76.
1 parent bf91151 commit ff2dd03

File tree

12 files changed

+367
-0
lines changed

12 files changed

+367
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g0001_0100.s0073_set_matrix_zeroes
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Matrix
4+
// #Udemy_2D_Arrays/Matrix #2022_08_31_Time_255_ms_(100.00%)_Space_45.7_MB_(91.72%)
5+
6+
class Solution {
7+
// Approach: Use first row and first column for storing whether in future
8+
// the entire row or column needs to be marked 0
9+
fun setZeroes(matrix: Array<IntArray>) {
10+
val m = matrix.size
11+
val n: Int = matrix[0].size
12+
var row0 = false
13+
var col0 = false
14+
// Check if 0th col needs to be market all 0s in future
15+
for (ints in matrix) {
16+
if (ints[0] == 0) {
17+
col0 = true
18+
break
19+
}
20+
}
21+
// Check if 0th row needs to be market all 0s in future
22+
for (i in 0 until n) {
23+
if (matrix[0][i] == 0) {
24+
row0 = true
25+
break
26+
}
27+
}
28+
// Store the signals in 0th row and column
29+
for (i in 1 until m) {
30+
for (j in 1 until n) {
31+
if (matrix[i][j] == 0) {
32+
matrix[i][0] = 0
33+
matrix[0][j] = 0
34+
}
35+
}
36+
}
37+
// Mark 0 for all cells based on signal from 0th row and 0th column
38+
for (i in 1 until m) {
39+
for (j in 1 until n) {
40+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
41+
matrix[i][j] = 0
42+
}
43+
}
44+
}
45+
// Set 0th column
46+
for (i in 0 until m) {
47+
if (col0) {
48+
matrix[i][0] = 0
49+
}
50+
}
51+
// Set 0th row
52+
for (i in 0 until n) {
53+
if (row0) {
54+
matrix[0][i] = 0
55+
}
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
73\. Set Matrix Zeroes
2+
3+
Medium
4+
5+
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s.
6+
7+
You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm).
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/08/17/mat1.jpg)
12+
13+
**Input:** matrix = [[1,1,1],[1,0,1],[1,1,1]]
14+
15+
**Output:** [[1,0,1],[0,0,0],[1,0,1]]
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2020/08/17/mat2.jpg)
20+
21+
**Input:** matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
22+
23+
**Output:** [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
24+
25+
**Constraints:**
26+
27+
* `m == matrix.length`
28+
* `n == matrix[0].length`
29+
* `1 <= m, n <= 200`
30+
* <code>-2<sup>31</sup> <= matrix[i][j] <= 2<sup>31</sup> - 1</code>
31+
32+
**Follow up:**
33+
34+
* A straightforward solution using `O(mn)` space is probably a bad idea.
35+
* A simple improvement uses `O(m + n)` space, but still not the best solution.
36+
* Could you devise a constant space solution?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0001_0100.s0074_search_a_2d_matrix
2+
3+
// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Matrix #Data_Structure_I_Day_5_Array
4+
// #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_8 #Level_2_Day_8_Binary_Search
5+
// #Udemy_2D_Arrays/Matrix #2022_08_31_Time_290_ms_(40.17%)_Space_35.4_MB_(96.48%)
6+
7+
class Solution {
8+
fun searchMatrix(matrix: Array<IntArray>, target: Int): Boolean {
9+
val endRow = matrix.size
10+
val endCol: Int = matrix[0].size
11+
var targetRow = 0
12+
var result = false
13+
for (i in 0 until endRow) {
14+
if (matrix[i][endCol - 1] >= target) {
15+
targetRow = i
16+
break
17+
}
18+
}
19+
for (i in 0 until endCol) {
20+
if (matrix[targetRow][i] == target) {
21+
result = true
22+
break
23+
}
24+
}
25+
return result
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
74\. Search a 2D Matrix
2+
3+
Medium
4+
5+
Write an efficient algorithm that searches for a value `target` in an `m x n` integer matrix `matrix`. This matrix has the following properties:
6+
7+
* Integers in each row are sorted from left to right.
8+
* The first integer of each row is greater than the last integer of the previous row.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/10/05/mat.jpg)
13+
14+
**Input:** matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg)
21+
22+
**Input:** matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
23+
24+
**Output:** false
25+
26+
**Constraints:**
27+
28+
* `m == matrix.length`
29+
* `n == matrix[i].length`
30+
* `1 <= m, n <= 100`
31+
* <code>-10<sup>4</sup> <= matrix[i][j], target <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0001_0100.s0075_sort_colors
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting #Two_Pointers
4+
// #Data_Structure_II_Day_2_Array #Udemy_Arrays
5+
// #2022_08_31_Time_198_ms_(85.66%)_Space_34.8_MB_(84.84%)
6+
7+
class Solution {
8+
fun sortColors(nums: IntArray) {
9+
var zeroes = 0
10+
var ones = 0
11+
for (i in nums.indices) {
12+
if (nums[i] == 0) {
13+
nums[zeroes++] = 0
14+
} else if (nums[i] == 1) {
15+
ones++
16+
}
17+
}
18+
for (j in zeroes until zeroes + ones) {
19+
nums[j] = 1
20+
}
21+
for (k in zeroes + ones until nums.size) {
22+
nums[k] = 2
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
75\. Sort Colors
2+
3+
Medium
4+
5+
Given an array `nums` with `n` objects colored red, white, or blue, sort them **[in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** so that objects of the same color are adjacent, with the colors in the order red, white, and blue.
6+
7+
We will use the integers `0`, `1`, and `2` to represent the color red, white, and blue, respectively.
8+
9+
You must solve this problem without using the library's sort function.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,0,2,1,1,0]
14+
15+
**Output:** [0,0,1,1,2,2]
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [2,0,1]
20+
21+
**Output:** [0,1,2]
22+
23+
**Constraints:**
24+
25+
* `n == nums.length`
26+
* `1 <= n <= 300`
27+
* `nums[i]` is either `0`, `1`, or `2`.
28+
29+
**Follow up:** Could you come up with a one-pass algorithm using only constant extra space?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0001_0100.s0076_minimum_window_substring
2+
3+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
4+
// #Level_2_Day_14_Sliding_Window/Two_Pointer
5+
// #2022_08_31_Time_346_ms_(85.20%)_Space_39.3_MB_(93.88%)
6+
7+
class Solution {
8+
fun minWindow(s: String, t: String): String {
9+
val map = IntArray(128)
10+
for (i in 0 until t.length) {
11+
map[t[i] - 'A']++
12+
}
13+
var count = t.length
14+
var begin = 0
15+
var end = 0
16+
var d = Int.MAX_VALUE
17+
var head = 0
18+
while (end < s.length) {
19+
if (map[s[end++] - 'A']-- > 0) {
20+
count--
21+
}
22+
while (count == 0) {
23+
if (end - begin < d) {
24+
d = end - begin
25+
head = begin
26+
}
27+
if (map[s[begin++] - 'A']++ == 0) {
28+
count++
29+
}
30+
}
31+
}
32+
return if (d == Int.MAX_VALUE) "" else s.substring(head, head + d)
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
76\. Minimum Window Substring
2+
3+
Hard
4+
5+
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window substring** of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window. If there is no such substring__, return the empty string_ `""`_._
6+
7+
The testcases will be generated such that the answer is **unique**.
8+
9+
A **substring** is a contiguous sequence of characters within the string.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "ADOBECODEBANC", t = "ABC"
14+
15+
**Output:** "BANC"
16+
17+
**Explanation:** The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "a", t = "a"
22+
23+
**Output:** "a"
24+
25+
**Explanation:** The entire string s is the minimum window.
26+
27+
**Example 3:**
28+
29+
**Input:** s = "a", t = "aa"
30+
31+
**Output:** ""
32+
33+
**Explanation:** Both 'a's from t must be included in the window. Since the largest window of s only has one 'a', return empty string.
34+
35+
**Constraints:**
36+
37+
* `m == s.length`
38+
* `n == t.length`
39+
* <code>1 <= m, n <= 10<sup>5</sup></code>
40+
* `s` and `t` consist of uppercase and lowercase English letters.
41+
42+
**Follow up:** Could you find an algorithm that runs in `O(m + n)` time?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0001_0100.s0073_set_matrix_zeroes
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 setZeroes() {
10+
var array = arrayOf(intArrayOf(1, 1, 1), intArrayOf(1, 0, 1), intArrayOf(1, 1, 1))
11+
var expected = arrayOf(intArrayOf(1, 0, 1), intArrayOf(0, 0, 0), intArrayOf(1, 0, 1))
12+
Solution().setZeroes(array)
13+
assertThat(array, equalTo(expected))
14+
}
15+
16+
@Test
17+
fun setZeroes2() {
18+
var array = arrayOf(intArrayOf(0, 1, 2, 0), intArrayOf(3, 4, 5, 2), intArrayOf(1, 3, 1, 5))
19+
var expected = arrayOf(intArrayOf(0, 0, 0, 0), intArrayOf(0, 4, 5, 0), intArrayOf(0, 3, 1, 0))
20+
Solution().setZeroes(array)
21+
assertThat(array, equalTo(expected))
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0074_search_a_2d_matrix
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 searchMatrix() {
10+
assertThat(Solution().searchMatrix(arrayOf(intArrayOf(1, 3, 5, 7), intArrayOf(10, 11, 16, 20), intArrayOf(23, 30, 34, 60)), 3), equalTo(true))
11+
}
12+
13+
@Test
14+
fun searchMatrix2() {
15+
assertThat(Solution().searchMatrix(arrayOf(intArrayOf(1, 3, 5, 7), intArrayOf(10, 11, 16, 20), intArrayOf(23, 30, 34, 60)), 13), equalTo(false))
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0001_0100.s0075_sort_colors
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 sortColors() {
10+
var array = intArrayOf(2, 0, 2, 1, 1, 0)
11+
Solution().sortColors(array)
12+
var expected = intArrayOf(0, 0, 1, 1, 2, 2)
13+
assertThat(array, equalTo(expected))
14+
}
15+
16+
@Test
17+
fun sortColors2() {
18+
var array = intArrayOf(2, 0, 1)
19+
Solution().sortColors(array)
20+
var expected = intArrayOf(0, 1, 2)
21+
assertThat(array, equalTo(expected))
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0076_minimum_window_substring
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 minWindow() {
10+
assertThat(Solution().minWindow("ADOBECODEBANC", "ABC"), equalTo("BANC"))
11+
}
12+
13+
@Test
14+
fun minWindow2() {
15+
assertThat(Solution().minWindow("a", "a"), equalTo("a"))
16+
}
17+
18+
@Test
19+
fun minWindow3() {
20+
assertThat(Solution().minWindow("a", "aa"), equalTo(""))
21+
}
22+
}

0 commit comments

Comments
 (0)