Skip to content

Commit bf91151

Browse files
authored
Added tasks 62, 64, 70, 72.
1 parent 7cfce7f commit bf91151

File tree

12 files changed

+325
-0
lines changed

12 files changed

+325
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0001_0100.s0062_unique_paths
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math
4+
// #Combinatorics #Algorithm_II_Day_13_Dynamic_Programming #Dynamic_Programming_I_Day_15
5+
// #Level_1_Day_11_Dynamic_Programming #2022_08_30_Time_209_ms_(49.18%)_Space_33.5_MB_(60.44%)
6+
7+
class Solution {
8+
fun uniquePaths(m: Int, n: Int): Int {
9+
val dp = Array(m) { IntArray(n) }
10+
for (i in 0 until m) {
11+
dp[i][0] = 1
12+
}
13+
for (j in 0 until n) {
14+
dp[0][j] = 1
15+
}
16+
for (i in 1 until m) {
17+
for (j in 1 until n) {
18+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
19+
}
20+
}
21+
return dp[m - 1][n - 1]
22+
}
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
62\. Unique Paths
2+
3+
Medium
4+
5+
There is a robot on an `m x n` grid. The robot is initially located at the **top-left corner** (i.e., `grid[0][0]`). The robot tries to move to the **bottom-right corner** (i.e., `grid[m - 1][n - 1]`). The robot can only move either down or right at any point in time.
6+
7+
Given the two integers `m` and `n`, return _the number of possible unique paths that the robot can take to reach the bottom-right corner_.
8+
9+
The test cases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
14+
15+
**Input:** m = 3, n = 7
16+
17+
**Output:** 28
18+
19+
**Example 2:**
20+
21+
**Input:** m = 3, n = 2
22+
23+
**Output:** 3
24+
25+
**Explanation:** From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
26+
27+
1. Right -> Down -> Down
28+
29+
2. Down -> Down -> Right
30+
31+
3. Down -> Right -> Down
32+
33+
**Constraints:**
34+
35+
* `1 <= m, n <= 100`
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0001_0100.s0064_minimum_path_sum
2+
3+
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Matrix
4+
// #Dynamic_Programming_I_Day_16 #Udemy_Dynamic_Programming
5+
// #2022_08_30_Time_222_ms_(95.70%)_Space_38_MB_(98.92%)
6+
7+
class Solution {
8+
fun minPathSum(grid: Array<IntArray>): Int {
9+
val m = grid.size
10+
val n = grid[0].size
11+
val dp = Array(m) { Array(n) { 0 } }
12+
for (i in 0 until m) {
13+
for (j in 0 until n) {
14+
if (i == 0 && j == 0) {
15+
dp[i][j] = grid[i][j]
16+
} else if (i == 0) {
17+
dp[i][j] = dp[i][j - 1] + grid[i][j]
18+
} else if (j == 0) {
19+
dp[i][j] = dp[i - 1][j] + grid[i][j]
20+
} else {
21+
dp[i][j] = minOf(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
22+
}
23+
}
24+
}
25+
return dp[m - 1][n - 1]
26+
}
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
64\. Minimum Path Sum
2+
3+
Medium
4+
5+
Given a `m x n` `grid` filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
6+
7+
**Note:** You can only move either down or right at any point in time.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/11/05/minpath.jpg)
12+
13+
**Input:** grid = [[1,3,1],[1,5,1],[4,2,1]]
14+
15+
**Output:** 7
16+
17+
**Explanation:** Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
18+
19+
**Example 2:**
20+
21+
**Input:** grid = [[1,2,3],[4,5,6]]
22+
23+
**Output:** 12
24+
25+
**Constraints:**
26+
27+
* `m == grid.length`
28+
* `n == grid[i].length`
29+
* `1 <= m, n <= 200`
30+
* `0 <= grid[i][j] <= 100`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0001_0100.s0070_climbing_stairs
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math #Memoization
4+
// #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_2
5+
// #Level_1_Day_10_Dynamic_Programming #Udemy_Dynamic_Programming
6+
// #2022_08_30_Time_127_ms_(97.06%)_Space_32.7_MB_(97.70%)
7+
8+
class Solution {
9+
fun climbStairs(n: Int): Int {
10+
if (n < 2) {
11+
return n
12+
}
13+
val cache = IntArray(n)
14+
// creating a cache or DP to store the result
15+
// so that we dont have to iterate multiple times
16+
// for the same values;
17+
18+
// for 0 and 1 the result array i.e cache values would be 1 and 2
19+
// in loop we are just getting ith values i.e 5th step values from
20+
// i-1 and i-2 which are 4th step and 3rd step values.
21+
cache[0] = 1
22+
cache[1] = 2
23+
for (i in 2 until n) {
24+
cache[i] = cache[i - 1] + cache[i - 2]
25+
}
26+
return cache[n - 1]
27+
}
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
70\. Climbing Stairs
2+
3+
Easy
4+
5+
You are climbing a staircase. It takes `n` steps to reach the top.
6+
7+
Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
8+
9+
**Example 1:**
10+
11+
**Input:** n = 2
12+
13+
**Output:** 2
14+
15+
**Explanation:** There are two ways to climb to the top.
16+
17+
1. 1 step + 1 step
18+
19+
2. 2 steps
20+
21+
**Example 2:**
22+
23+
**Input:** n = 3
24+
25+
**Output:** 3
26+
27+
**Explanation:** There are three ways to climb to the top.
28+
29+
1. 1 step + 1 step + 1 step
30+
31+
2. 1 step + 2 steps
32+
33+
3. 2 steps + 1 step
34+
35+
**Constraints:**
36+
37+
* `1 <= n <= 45`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0001_0100.s0072_edit_distance
2+
3+
// #Hard #Top_100_Liked_Questions #String #Dynamic_Programming
4+
// #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_19
5+
// #Udemy_Dynamic_Programming #2022_08_30_Time_320_ms_(63.53%)_Space_37.2_MB_(83.53%)
6+
7+
class Solution {
8+
fun minDistance(w1: String, w2: String): Int {
9+
val n1 = w1.length
10+
val n2 = w2.length
11+
if (n2 > n1) {
12+
return minDistance(w2, w1)
13+
}
14+
val dp = IntArray(n2 + 1)
15+
for (j in 0..n2) {
16+
dp[j] = j
17+
}
18+
for (i in 1..n1) {
19+
var pre = dp[0]
20+
dp[0] = i
21+
for (j in 1..n2) {
22+
val tmp = dp[j]
23+
dp[j] = if (w1[i - 1] != w2[j - 1]) 1 + Math.min(pre, Math.min(dp[j], dp[j - 1])) else pre
24+
pre = tmp
25+
}
26+
}
27+
return dp[n2]
28+
}
29+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
72\. Edit Distance
2+
3+
Hard
4+
5+
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_.
6+
7+
You have the following three operations permitted on a word:
8+
9+
* Insert a character
10+
* Delete a character
11+
* Replace a character
12+
13+
**Example 1:**
14+
15+
**Input:** word1 = "horse", word2 = "ros"
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
horse -> rorse (replace 'h' with 'r')
22+
23+
rorse -> rose (remove 'r')
24+
25+
rose -> ros (remove 'e')
26+
27+
**Example 2:**
28+
29+
**Input:** word1 = "intention", word2 = "execution"
30+
31+
**Output:** 5
32+
33+
**Explanation:**
34+
35+
intention -> inention (remove 't')
36+
37+
inention -> enention (replace 'i' with 'e')
38+
39+
enention -> exention (replace 'n' with 'x')
40+
41+
exention -> exection (replace 'n' with 'c')
42+
43+
exection -> execution (insert 'u')
44+
45+
**Constraints:**
46+
47+
* `0 <= word1.length, word2.length <= 500`
48+
* `word1` and `word2` consist of lowercase English letters.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0062_unique_paths
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 uniquePaths() {
10+
assertThat(Solution().uniquePaths(3, 7), equalTo(28))
11+
}
12+
13+
@Test
14+
fun uniquePaths2() {
15+
assertThat(Solution().uniquePaths(3, 2), equalTo(3))
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0064_minimum_path_sum
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 minPathSum() {
10+
assertThat(Solution().minPathSum(arrayOf(intArrayOf(1, 3, 1), intArrayOf(1, 5, 1), intArrayOf(4, 2, 1))), equalTo(7))
11+
}
12+
13+
@Test
14+
fun minPathSum2() {
15+
assertThat(Solution().minPathSum(arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6))), equalTo(12))
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0070_climbing_stairs
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 climbStairs() {
10+
assertThat(Solution().climbStairs(2), equalTo(2))
11+
}
12+
13+
@Test
14+
fun climbStairs2() {
15+
assertThat(Solution().climbStairs(3), equalTo(3))
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0072_edit_distance
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 minDistance() {
10+
assertThat(Solution().minDistance("horse", "ros"), equalTo(3))
11+
}
12+
13+
@Test
14+
fun minDistance2() {
15+
assertThat(Solution().minDistance("intention", "execution"), equalTo(5))
16+
}
17+
}

0 commit comments

Comments
 (0)