Skip to content

Commit 7cfce7f

Browse files
authored
Added tasks 53, 54, 55, 56.
1 parent 696380d commit 7cfce7f

File tree

12 files changed

+346
-0
lines changed

12 files changed

+346
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0053_maximum_subarray
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
4+
// #Divide_and_Conquer #Data_Structure_I_Day_1_Array #Dynamic_Programming_I_Day_5
5+
// #Udemy_Famous_Algorithm #2022_08_29_Time_662_ms_(82.48%)_Space_97.8_MB_(24.28%)
6+
7+
class Solution {
8+
fun maxSubArray(nums: IntArray): Int {
9+
var nextMax = nums[0]
10+
var curMax = nums[0]
11+
for (i in 1 until nums.size) {
12+
nextMax += nums[i]
13+
if (nextMax < nums[i]) {
14+
nextMax = nums[i]
15+
}
16+
if (nextMax > curMax) {
17+
curMax = nextMax
18+
}
19+
}
20+
return curMax
21+
}
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
53\. Maximum Subarray
2+
3+
Medium
4+
5+
Given an integer array `nums`, find the contiguous subarray (containing at least one number) which has the largest sum and return _its sum_.
6+
7+
A **subarray** is a **contiguous** part of an array.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [-2,1,-3,4,-1,2,1,-5,4]
12+
13+
**Output:** 6
14+
15+
**Explanation:** [4,-1,2,1] has the largest sum = 6.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [1]
20+
21+
**Output:** 1
22+
23+
**Example 3:**
24+
25+
**Input:** nums = [5,4,-1,7,8]
26+
27+
**Output:** 23
28+
29+
**Constraints:**
30+
31+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
32+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
33+
34+
**Follow up:** If you have figured out the `O(n)` solution, try coding another solution using the **divide and conquer** approach, which is more subtle.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g0001_0100.s0054_spiral_matrix
2+
3+
// #Medium #Top_Interview_Questions #Array #Matrix #Simulation #Programming_Skills_II_Day_8
4+
// #Level_2_Day_1_Implementation/Simulation #Udemy_2D_Arrays/Matrix
5+
// #2022_08_29_Time_224_ms_(62.50%)_Space_34.4_MB_(63.36%)
6+
7+
class Solution {
8+
fun spiralOrder(matrix: Array<IntArray>): List<Int> {
9+
val list: MutableList<Int> = ArrayList()
10+
var r = 0
11+
var c = 0
12+
var bigR = matrix.size - 1
13+
var bigC: Int = matrix[0].size - 1
14+
while (r <= bigR && c <= bigC) {
15+
for (i in c..bigC) {
16+
list.add(matrix[r][i])
17+
}
18+
r++
19+
for (i in r..bigR) {
20+
list.add(matrix[i][bigC])
21+
}
22+
bigC--
23+
run {
24+
var i = bigC
25+
while (i >= c && r <= bigR) {
26+
list.add(matrix[bigR][i])
27+
i--
28+
}
29+
}
30+
bigR--
31+
var i = bigR
32+
while (i >= r && c <= bigC) {
33+
list.add(matrix[i][c])
34+
i--
35+
}
36+
c++
37+
}
38+
return list
39+
}
40+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
54\. Spiral Matrix
2+
3+
Medium
4+
5+
Given an `m x n` `matrix`, return _all elements of the_ `matrix` _in spiral order_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg)
10+
11+
**Input:** matrix = [[1,2,3],[4,5,6],[7,8,9]]
12+
13+
**Output:** [1,2,3,6,9,8,7,4,5]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/11/13/spiral.jpg)
18+
19+
**Input:** matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
20+
21+
**Output:** [1,2,3,4,8,12,11,10,9,5,6,7]
22+
23+
**Constraints:**
24+
25+
* `m == matrix.length`
26+
* `n == matrix[i].length`
27+
* `1 <= m, n <= 10`
28+
* `-100 <= matrix[i][j] <= 100`
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g0001_0100.s0055_jump_game
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming #Greedy
4+
// #Algorithm_II_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_4 #Udemy_Arrays
5+
// #2022_08_29_Time_670_ms_(66.67%)_Space_71.2_MB_(44.45%)
6+
7+
class Solution {
8+
fun canJump(nums: IntArray): Boolean {
9+
val sz = nums.size
10+
// we set 1 so it won't break on the first iteration
11+
var tmp = 1
12+
for (i in 0 until sz) {
13+
// we always deduct tmp for every iteration
14+
tmp--
15+
if (tmp < 0) {
16+
// if from previous iteration tmp is already 0, it will be <0 here
17+
// leading to false value
18+
return false
19+
}
20+
// we get the maximum value because this value is supposed
21+
// to be our iterator, if both values are 0, then the next
22+
// iteration we will return false
23+
// if either both or one of them are not 0 then we will keep doing this and check.
24+
25+
// We can stop the whole iteration with this condition. without this condition the code
26+
// runs in 2ms 79.6%, adding this condition improves the performance into 1ms 100%
27+
// because if the test case jump value is quite large, instead of just iterate, we can
28+
// just check using this condition
29+
// example: [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] -> we can just jump to the end without
30+
// iterating whole array
31+
tmp = Math.max(tmp, nums[i])
32+
if (i + tmp >= sz - 1) {
33+
return true
34+
}
35+
}
36+
// we can just return true at the end, because if tmp is 0 on previous
37+
// iteration,
38+
// even though the next iteration index is the last one, it will return false under the
39+
// tmp<0 condition
40+
return true
41+
}
42+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
55\. Jump Game
2+
3+
Medium
4+
5+
You are given an integer array `nums`. You are initially positioned at the array's **first index**, and each element in the array represents your maximum jump length at that position.
6+
7+
Return `true` _if you can reach the last index, or_ `false` _otherwise_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [2,3,1,1,4]
12+
13+
**Output:** true
14+
15+
**Explanation:** Jump 1 step from index 0 to 1, then 3 steps to the last index.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [3,2,1,0,4]
20+
21+
**Output:** false
22+
23+
**Explanation:** You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
24+
25+
**Constraints:**
26+
27+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
28+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0001_0100.s0056_merge_intervals
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting
4+
// #Data_Structure_II_Day_2_Array #Level_2_Day_17_Interval #Udemy_2D_Arrays/Matrix
5+
// #2022_08_29_Time_323_ms_(99.68%)_Space_43_MB_(99.04%)
6+
7+
import java.util.Arrays
8+
9+
class Solution {
10+
fun merge(intervals: Array<IntArray>): Array<IntArray> {
11+
Arrays.sort(
12+
intervals
13+
) { a: IntArray, b: IntArray ->
14+
Integer.compare(
15+
a[0],
16+
b[0]
17+
)
18+
}
19+
val list: MutableList<IntArray> = ArrayList()
20+
var current = intervals[0]
21+
list.add(current)
22+
for (next in intervals) {
23+
if (current[1] >= next[0]) {
24+
current[1] = Math.max(current[1], next[1])
25+
} else {
26+
current = next
27+
list.add(current)
28+
}
29+
}
30+
return list.toTypedArray()
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
56\. Merge Intervals
2+
3+
Medium
4+
5+
Given an array of `intervals` where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return _an array of the non-overlapping intervals that cover all the intervals in the input_.
6+
7+
**Example 1:**
8+
9+
**Input:** intervals = [[1,3],[2,6],[8,10],[15,18]]
10+
11+
**Output:** [[1,6],[8,10],[15,18]]
12+
13+
**Explanation:** Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
14+
15+
**Example 2:**
16+
17+
**Input:** intervals = [[1,4],[4,5]]
18+
19+
**Output:** [[1,5]]
20+
21+
**Explanation:** Intervals [1,4] and [4,5] are considered overlapping.
22+
23+
**Constraints:**
24+
25+
* <code>1 <= intervals.length <= 10<sup>4</sup></code>
26+
* `intervals[i].length == 2`
27+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0053_maximum_subarray
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 maxSubArray() {
10+
assertThat(Solution().maxSubArray(intArrayOf(-2, 1, -3, 4, -1, 2, 1, -5, 4)), equalTo(6))
11+
}
12+
13+
@Test
14+
fun maxSubArray2() {
15+
assertThat(Solution().maxSubArray(intArrayOf(1)), equalTo(1))
16+
}
17+
18+
@Test
19+
fun maxSubArray3() {
20+
assertThat(Solution().maxSubArray(intArrayOf(5, 4, -1, 7, 8)), equalTo(23))
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0001_0100.s0054_spiral_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 spiralOrder() {
10+
assertThat(
11+
Solution().spiralOrder(arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6), intArrayOf(7, 8, 9))),
12+
equalTo(
13+
intArrayOf(1, 2, 3, 6, 9, 8, 7, 4, 5).toList()
14+
)
15+
)
16+
}
17+
18+
@Test
19+
fun spiralOrder2() {
20+
assertThat(
21+
Solution().spiralOrder(arrayOf(intArrayOf(1, 2, 3, 4), intArrayOf(5, 6, 7, 8), intArrayOf(9, 10, 11, 12))),
22+
equalTo(
23+
intArrayOf(1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7).toList()
24+
)
25+
)
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0055_jump_game
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 canJump() {
10+
assertThat(Solution().canJump(intArrayOf(2, 3, 1, 1, 4)), equalTo(true))
11+
}
12+
13+
@Test
14+
fun canJump2() {
15+
assertThat(Solution().canJump(intArrayOf(3, 2, 1, 0, 4)), equalTo(false))
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0001_0100.s0056_merge_intervals
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 merge() {
10+
assertThat(
11+
Solution().merge(arrayOf(intArrayOf(1, 3), intArrayOf(2, 6), intArrayOf(8, 10), intArrayOf(15, 18))),
12+
equalTo(
13+
arrayOf(intArrayOf(1, 6), intArrayOf(8, 10), intArrayOf(15, 18))
14+
)
15+
)
16+
}
17+
18+
@Test
19+
fun merge2() {
20+
assertThat(
21+
Solution().merge(arrayOf(intArrayOf(1, 4), intArrayOf(4, 5))),
22+
equalTo(
23+
arrayOf(intArrayOf(1, 5))
24+
)
25+
)
26+
}
27+
}

0 commit comments

Comments
 (0)