Skip to content

Commit 9807670

Browse files
authored
Updated readme
1 parent 2dbbde6 commit 9807670

File tree

11 files changed

+761
-77
lines changed

11 files changed

+761
-77
lines changed

README.md

Lines changed: 112 additions & 77 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+
## 42\. Trapping Rain Water
5+
6+
Hard
7+
8+
Given `n` non-negative integers representing an elevation map where the width of each bar is `1`, compute how much water it can trap after raining.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png)
13+
14+
**Input:** height = [0,1,0,2,1,0,1,3,2,1,2,1]
15+
16+
**Output:** 6
17+
18+
**Explanation:** The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
19+
20+
**Example 2:**
21+
22+
**Input:** height = [4,2,0,3,2,5]
23+
24+
**Output:** 9
25+
26+
**Constraints:**
27+
28+
* `n == height.length`
29+
* <code>1 <= n <= 2 * 10<sup>4</sup></code>
30+
* <code>0 <= height[i] <= 10<sup>5</sup></code>
31+
32+
## Solution
33+
34+
```scala
35+
object Solution {
36+
def trap(height: Array[Int]): Int = {
37+
var left = 0
38+
var right = height.length - 1
39+
var result = 0
40+
var lowerWall = 0
41+
42+
while (left < right) {
43+
val leftValue = height(left)
44+
val rightValue = height(right)
45+
46+
if (leftValue < rightValue) {
47+
lowerWall = Math.max(leftValue, lowerWall)
48+
result += lowerWall - leftValue
49+
left += 1
50+
} else {
51+
lowerWall = Math.max(rightValue, lowerWall)
52+
result += lowerWall - rightValue
53+
right -= 1
54+
}
55+
}
56+
57+
result
58+
}
59+
}
60+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
## 45\. Jump Game II
5+
6+
Medium
7+
8+
Given an array of non-negative integers `nums`, you are initially positioned at the first index of the array.
9+
10+
Each element in the array represents your maximum jump length at that position.
11+
12+
Your goal is to reach the last index in the minimum number of jumps.
13+
14+
You can assume that you can always reach the last index.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,3,1,1,4]
19+
20+
**Output:** 2
21+
22+
**Explanation:** The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [2,3,0,1,4]
27+
28+
**Output:** 2
29+
30+
**Constraints:**
31+
32+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
33+
* `0 <= nums[i] <= 1000`
34+
35+
## Solution
36+
37+
```scala
38+
object Solution {
39+
def jump(nums: Array[Int]): Int = {
40+
var length = 0
41+
var maxLength = 0
42+
var minJump = 0
43+
44+
for (i <- 0 until nums.length - 1) {
45+
length -= 1
46+
maxLength -= 1
47+
maxLength = math.max(maxLength, nums(i))
48+
49+
if (length <= 0) {
50+
length = maxLength
51+
minJump += 1
52+
}
53+
54+
if (length >= nums.length - i - 1) {
55+
return minJump
56+
}
57+
}
58+
59+
minJump
60+
}
61+
}
62+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
## 46\. Permutations
5+
6+
Medium
7+
8+
Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
9+
10+
**Example 1:**
11+
12+
**Input:** nums = [1,2,3]
13+
14+
**Output:** [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
15+
16+
**Example 2:**
17+
18+
**Input:** nums = [0,1]
19+
20+
**Output:** [[0,1],[1,0]]
21+
22+
**Example 3:**
23+
24+
**Input:** nums = [1]
25+
26+
**Output:** [[1]]
27+
28+
**Constraints:**
29+
30+
* `1 <= nums.length <= 6`
31+
* `-10 <= nums[i] <= 10`
32+
* All the integers of `nums` are **unique**.
33+
34+
## Solution
35+
36+
```scala
37+
object Solution {
38+
def permute(nums: Array[Int]): List[List[Int]] = {
39+
if (nums == null || nums.isEmpty) {
40+
return List()
41+
}
42+
43+
val finalResult = new scala.collection.mutable.ListBuffer[List[Int]]()
44+
val used = Array.fill[Boolean](nums.length)(false)
45+
46+
def permuteRecur(currResult: scala.collection.mutable.ListBuffer[Int]): Unit = {
47+
if (currResult.size == nums.length) {
48+
finalResult += currResult.toList
49+
return
50+
}
51+
52+
for (i <- nums.indices) {
53+
if (!used(i)) {
54+
currResult += nums(i)
55+
used(i) = true
56+
permuteRecur(currResult)
57+
used(i) = false
58+
currResult.remove(currResult.size - 1)
59+
}
60+
}
61+
}
62+
63+
permuteRecur(new scala.collection.mutable.ListBuffer[Int]())
64+
finalResult.toList
65+
}
66+
}
67+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
## 48\. Rotate Image
5+
6+
Medium
7+
8+
You are given an `n x n` 2D `matrix` representing an image, rotate the image by **90** degrees (clockwise).
9+
10+
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.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/08/28/mat1.jpg)
15+
16+
**Input:** matrix = \[\[1,2,3],[4,5,6],[7,8,9]]
17+
18+
**Output:** [[7,4,1],[8,5,2],[9,6,3]]
19+
20+
**Example 2:**
21+
22+
![](https://assets.leetcode.com/uploads/2020/08/28/mat2.jpg)
23+
24+
**Input:** matrix = \[\[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
25+
26+
**Output:** [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
27+
28+
**Example 3:**
29+
30+
**Input:** matrix = \[\[1]]
31+
32+
**Output:** [[1]]
33+
34+
**Example 4:**
35+
36+
**Input:** matrix = \[\[1,2],[3,4]]
37+
38+
**Output:** [[3,1],[4,2]]
39+
40+
**Constraints:**
41+
42+
* `matrix.length == n`
43+
* `matrix[i].length == n`
44+
* `1 <= n <= 20`
45+
* `-1000 <= matrix[i][j] <= 1000`
46+
47+
## Solution
48+
49+
```scala
50+
object Solution {
51+
def rotate(matrix: Array[Array[Int]]): Unit = {
52+
val n = matrix.length
53+
54+
for (i <- 0 until n / 2) {
55+
for (j <- i until n - i - 1) {
56+
val positions = Array(
57+
(i, j), (j, n - 1 - i), (n - 1 - i, n - 1 - j), (n - 1 - j, i)
58+
)
59+
var temp = matrix(positions(0)._1)(positions(0)._2)
60+
for (k <- 1 until positions.length) {
61+
val nextTemp = matrix(positions(k)._1)(positions(k)._2)
62+
matrix(positions(k)._1)(positions(k)._2) = temp
63+
temp = nextTemp
64+
}
65+
matrix(positions(0)._1)(positions(0)._2) = temp
66+
}
67+
}
68+
}
69+
}
70+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
## 49\. Group Anagrams
5+
6+
Medium
7+
8+
Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
9+
10+
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.
11+
12+
**Example 1:**
13+
14+
**Input:** strs = ["eat","tea","tan","ate","nat","bat"]
15+
16+
**Output:** [["bat"],["nat","tan"],["ate","eat","tea"]]
17+
18+
**Example 2:**
19+
20+
**Input:** strs = [""]
21+
22+
**Output:** [[""]]
23+
24+
**Example 3:**
25+
26+
**Input:** strs = ["a"]
27+
28+
**Output:** [["a"]]
29+
30+
**Constraints:**
31+
32+
* <code>1 <= strs.length <= 10<sup>4</sup></code>
33+
* `0 <= strs[i].length <= 100`
34+
* `strs[i]` consists of lowercase English letters.
35+
36+
## Solution
37+
38+
```scala
39+
import scala.collection.mutable.{ArrayBuffer, Map}
40+
41+
object Solution {
42+
def groupAnagrams(strs: Array[String]): List[List[String]] = {
43+
val hm = Map[String, ArrayBuffer[String]]()
44+
45+
for (s <- strs) {
46+
val ch = s.toCharArray
47+
ch.sortInPlace()
48+
val temp = new String(ch)
49+
hm.getOrElseUpdate(temp, ArrayBuffer.empty[String]) += s
50+
}
51+
52+
hm.values.map(_.toList).toList
53+
}
54+
}
55+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
## 51\. N-Queens
5+
6+
Hard
7+
8+
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.
9+
10+
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
11+
12+
Each solution contains a distinct board configuration of the n-queens' placement, where `'Q'` and `'.'` both indicate a queen and an empty space, respectively.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
17+
18+
**Input:** n = 4
19+
20+
**Output:** [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
21+
22+
**Explanation:** There exist two distinct solutions to the 4-queens puzzle as shown above
23+
24+
**Example 2:**
25+
26+
**Input:** n = 1
27+
28+
**Output:** [["Q"]]
29+
30+
**Constraints:**
31+
32+
* `1 <= n <= 9`
33+
34+
## Solution
35+
36+
```scala
37+
object Solution {
38+
def solveNQueens(n: Int): List[List[String]] = {
39+
val rst = scala.collection.mutable.ListBuffer[List[String]]()
40+
dfs(n, List[Int](), rst)
41+
rst.toList
42+
}
43+
44+
@SuppressWarnings(Array("scala:S3776"))
45+
private def dfs(n: Int, oneSol: List[Int], rst: scala.collection.mutable.ListBuffer[List[String]]): Unit = {
46+
if (oneSol.length == n) {
47+
val line = for (idx <- oneSol) yield ("." * idx + "Q" + "." * (n - idx - 1))
48+
rst += line
49+
return
50+
}
51+
for (col <- 0 until n) {
52+
if (!oneSol.contains(col)) {
53+
var i = oneSol.length - 1
54+
var j = col - 1
55+
var k = col + 1
56+
var continue = true
57+
while (continue && i >= 0 && (j >= 0 || k < n)) {
58+
if (oneSol(i) == j || oneSol(i) == k) {
59+
continue = false
60+
}
61+
i -= 1
62+
j -= 1
63+
k += 1
64+
}
65+
if (continue) {
66+
dfs(n, oneSol :+ col, rst)
67+
}
68+
}
69+
}
70+
}
71+
}
72+
```

0 commit comments

Comments
 (0)