Skip to content

Commit 132c6b5

Browse files
authored
Added tasks 64-208
1 parent 9807670 commit 132c6b5

File tree

41 files changed

+3110
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3110
-0
lines changed

README.md

Lines changed: 127 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Scala/LeetCode-in-Scala?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Scala/LeetCode-in-Scala?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala/fork)
3+
4+
## 64\. Minimum Path Sum
5+
6+
Medium
7+
8+
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.
9+
10+
**Note:** You can only move either down or right at any point in time.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/05/minpath.jpg)
15+
16+
**Input:** grid = \[\[1,3,1],[1,5,1],[4,2,1]]
17+
18+
**Output:** 7
19+
20+
**Explanation:** Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
21+
22+
**Example 2:**
23+
24+
**Input:** grid = \[\[1,2,3],[4,5,6]]
25+
26+
**Output:** 12
27+
28+
**Constraints:**
29+
30+
* `m == grid.length`
31+
* `n == grid[i].length`
32+
* `1 <= m, n <= 200`
33+
* `0 <= grid[i][j] <= 100`
34+
35+
## Solution
36+
37+
```scala
38+
object Solution {
39+
def minPathSum(grid: Array[Array[Int]]): Int = {
40+
val numRows = grid.length
41+
val numCols = grid(0).length
42+
val dp = Array.ofDim[Int](numRows, numCols)
43+
44+
for (r <- numRows - 1 to 0 by -1) {
45+
for (c <- numCols - 1 to 0 by -1) {
46+
if (r == numRows - 1 && c == numCols - 1) {
47+
dp(r)(c) = grid(r)(c)
48+
} else if (r == numRows - 1) {
49+
dp(r)(c) = grid(r)(c) + dp(r)(c + 1)
50+
} else if (c == numCols - 1) {
51+
dp(r)(c) = grid(r)(c) + dp(r + 1)(c)
52+
} else {
53+
dp(r)(c) = grid(r)(c) + Math.min(dp(r + 1)(c), dp(r)(c + 1))
54+
}
55+
}
56+
}
57+
dp(0)(0)
58+
}
59+
}
60+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Scala/LeetCode-in-Scala?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Scala/LeetCode-in-Scala?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala/fork)
3+
4+
## 70\. Climbing Stairs
5+
6+
Easy
7+
8+
You are climbing a staircase. It takes `n` steps to reach the top.
9+
10+
Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
11+
12+
**Example 1:**
13+
14+
**Input:** n = 2
15+
16+
**Output:** 2
17+
18+
**Explanation:** There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps
19+
20+
**Example 2:**
21+
22+
**Input:** n = 3
23+
24+
**Output:** 3
25+
26+
**Explanation:** There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step
27+
28+
**Constraints:**
29+
30+
* `1 <= n <= 45`
31+
32+
## Solution
33+
34+
```scala
35+
object Solution {
36+
def climbStairs(n: Int): Int = {
37+
if (n < 2) {
38+
return n
39+
}
40+
val cache = new Array[Int](n)
41+
42+
// Initialize the cache for the first two steps
43+
cache(0) = 1
44+
cache(1) = 2
45+
46+
// Fill in the cache using a loop
47+
for (i <- 2 until n) {
48+
cache(i) = cache(i - 1) + cache(i - 2)
49+
}
50+
51+
cache(n - 1)
52+
}
53+
}
54+
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Scala/LeetCode-in-Scala?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Scala/LeetCode-in-Scala?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala/fork)
3+
4+
## 72\. Edit Distance
5+
6+
Hard
7+
8+
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_.
9+
10+
You have the following three operations permitted on a word:
11+
12+
* Insert a character
13+
* Delete a character
14+
* Replace a character
15+
16+
**Example 1:**
17+
18+
**Input:** word1 = "horse", word2 = "ros"
19+
20+
**Output:** 3
21+
22+
**Explanation:** horse -> rorse (replace 'h' with 'r') rorse -> rose (remove 'r') rose -> ros (remove 'e')
23+
24+
**Example 2:**
25+
26+
**Input:** word1 = "intention", word2 = "execution"
27+
28+
**Output:** 5
29+
30+
**Explanation:** intention -> inention (remove 't') inention -> enention (replace 'i' with 'e') enention -> exention (replace 'n' with 'x') exention -> exection (replace 'n' with 'c') exection -> execution (insert 'u')
31+
32+
**Constraints:**
33+
34+
* `0 <= word1.length, word2.length <= 500`
35+
* `word1` and `word2` consist of lowercase English letters.
36+
37+
## Solution
38+
39+
```scala
40+
object Solution {
41+
def minDistance(w1: String, w2: String): Int = {
42+
val n1 = w1.length
43+
val n2 = w2.length
44+
45+
if (n2 > n1) {
46+
return minDistance(w2, w1)
47+
}
48+
49+
var dp = Array.range(0, n2 + 1)
50+
51+
for (j <- 0 to n2) {
52+
dp(j) = j
53+
}
54+
55+
for (i <- 1 to n1) {
56+
var pre = dp(0)
57+
dp(0) = i
58+
59+
for (j <- 1 to n2) {
60+
val tmp = dp(j)
61+
dp(j) =
62+
if (w1(i - 1) != w2(j - 1))
63+
1 + Math.min(pre, Math.min(dp(j), dp(j - 1)))
64+
else
65+
pre
66+
pre = tmp
67+
}
68+
}
69+
70+
dp(n2)
71+
}
72+
}
73+
```
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Scala/LeetCode-in-Scala?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Scala/LeetCode-in-Scala?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala/fork)
3+
4+
## 73\. Set Matrix Zeroes
5+
6+
Medium
7+
8+
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s, and return _the matrix_.
9+
10+
You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm).
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/08/17/mat1.jpg)
15+
16+
**Input:** matrix = \[\[1,1,1],[1,0,1],[1,1,1]]
17+
18+
**Output:** [[1,0,1],[0,0,0],[1,0,1]]
19+
20+
**Example 2:**
21+
22+
![](https://assets.leetcode.com/uploads/2020/08/17/mat2.jpg)
23+
24+
**Input:** matrix = \[\[0,1,2,0],[3,4,5,2],[1,3,1,5]]
25+
26+
**Output:** [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
27+
28+
**Constraints:**
29+
30+
* `m == matrix.length`
31+
* `n == matrix[0].length`
32+
* `1 <= m, n <= 200`
33+
* <code>-2<sup>31</sup> <= matrix[i][j] <= 2<sup>31</sup> - 1</code>
34+
35+
**Follow up:**
36+
37+
* A straightforward solution using `O(mn)` space is probably a bad idea.
38+
* A simple improvement uses `O(m + n)` space, but still not the best solution.
39+
* Could you devise a constant space solution?
40+
41+
## Solution
42+
43+
```scala
44+
object Solution {
45+
@SuppressWarnings(Array("scala:S3776"))
46+
def setZeroes(matrix: Array[Array[Int]]): Unit = {
47+
val m = matrix.length
48+
val n = matrix(0).length
49+
var row0 = false
50+
var col0 = false
51+
52+
// Check if the 0th column needs to be marked as all 0s in the future
53+
for (row <- matrix) {
54+
if (row(0) == 0) {
55+
col0 = true
56+
}
57+
}
58+
59+
// Check if the 0th row needs to be marked as all 0s in the future
60+
for (i <- 0 until n) {
61+
if (matrix(0)(i) == 0) {
62+
row0 = true
63+
}
64+
}
65+
66+
// Store the signals in the 0th row and column
67+
for (i <- 1 until m) {
68+
for (j <- 1 until n) {
69+
if (matrix(i)(j) == 0) {
70+
matrix(i)(0) = 0
71+
matrix(0)(j) = 0
72+
}
73+
}
74+
}
75+
76+
// Mark 0 for all cells based on signals from the 0th row and 0th column
77+
for (i <- 1 until m) {
78+
for (j <- 1 until n) {
79+
if (matrix(i)(0) == 0 || matrix(0)(j) == 0) {
80+
matrix(i)(j) = 0
81+
}
82+
}
83+
}
84+
85+
// Set the 0th column
86+
for (i <- 0 until m) {
87+
if (col0) {
88+
matrix(i)(0) = 0
89+
}
90+
}
91+
92+
// Set the 0th row
93+
for (i <- 0 until n) {
94+
if (row0) {
95+
matrix(0)(i) = 0
96+
}
97+
}
98+
}
99+
}
100+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Scala/LeetCode-in-Scala?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Scala/LeetCode-in-Scala?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala/fork)
3+
4+
## 74\. Search a 2D Matrix
5+
6+
Medium
7+
8+
Write an efficient algorithm that searches for a value in an `m x n` matrix. This matrix has the following properties:
9+
10+
* Integers in each row are sorted from left to right.
11+
* The first integer of each row is greater than the last integer of the previous row.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/10/05/mat.jpg)
16+
17+
**Input:** matrix = \[\[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
18+
19+
**Output:** true
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg)
24+
25+
**Input:** matrix = \[\[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
26+
27+
**Output:** false
28+
29+
**Constraints:**
30+
31+
* `m == matrix.length`
32+
* `n == matrix[i].length`
33+
* `1 <= m, n <= 100`
34+
* <code>-10<sup>4</sup> <= matrix[i][j], target <= 10<sup>4</sup></code>
35+
36+
## Solution
37+
38+
```scala
39+
object Solution {
40+
def searchMatrix(matrix: Array[Array[Int]], target: Int): Boolean = {
41+
var i = matrix.length - 1
42+
val n = matrix.last.length - 1
43+
var j = 0
44+
while (i >= 0 && j <= n) {
45+
if (matrix(i)(j) == target) return true
46+
else if (matrix(i)(j) > target) i -= 1
47+
else j += 1
48+
}
49+
false
50+
}
51+
}
52+
```

0 commit comments

Comments
 (0)