Skip to content

Commit 1202148

Browse files
authored
Added tasks 415, 417, 419, 420.
1 parent 7dba468 commit 1202148

File tree

13 files changed

+461
-0
lines changed

13 files changed

+461
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
407407

408408
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
409409
|-|-|-|-|-|-
410+
| 0417 |[Pacific Atlantic Water Flow](src/main/kotlin/g0401_0500/s0417_pacific_atlantic_water_flow/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 319 | 100.00
410411

411412
#### Day 5 Matrix Related Problems
412413

@@ -682,6 +683,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
682683
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
683684
|-|-|-|-|-|-
684685
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix | 308 | 57.93
686+
| 0417 |[Pacific Atlantic Water Flow](src/main/kotlin/g0401_0500/s0417_pacific_atlantic_water_flow/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 319 | 100.00
685687

686688
#### Day 11 Graph/BFS/DFS
687689

@@ -892,6 +894,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
892894
|-|-|-|-|-|-
893895
| 0200 |[Number of Islands](src.save/main/kotlin/g0101_0200/s0200_number_of_islands/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 252 | 95.41
894896
| 0133 |[Clone Graph](src.save/main/kotlin/g0101_0200/s0133_clone_graph/Solution.kt)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 351 | 60.91
897+
| 0417 |[Pacific Atlantic Water Flow](src/main/kotlin/g0401_0500/s0417_pacific_atlantic_water_flow/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 319 | 100.00
895898

896899
#### Udemy Dynamic Programming
897900

@@ -1084,6 +1087,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
10841087

10851088
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10861089
|-|-|-|-|-|-
1090+
| 0415 |[Add Strings](src/main/kotlin/g0401_0500/s0415_add_strings/Solution.kt)| Easy | String, Math, Simulation | 296 | 76.00
10871091
| 0409 |[Longest Palindrome](src/main/kotlin/g0401_0500/s0409_longest_palindrome/Solution.kt)| Easy | String, Hash_Table, Greedy | 259 | 60.71
10881092

10891093
#### Day 7 String
@@ -1623,7 +1627,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
16231627
| 0494 |[Target Sum](src/main/kotlin/g0401_0500/s0494_target_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Backtracking | 308 | 89.61
16241628
| 0438 |[Find All Anagrams in a String](src/main/kotlin/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Level_1_Day_12_Sliding_Window/Two_Pointer | 561 | 54.68
16251629
| 0437 |[Path Sum III](src/main/kotlin/g0401_0500/s0437_path_sum_iii/Solution.kt)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree | 403 | 54.12
1630+
| 0420 |[Strong Password Checker](src/main/kotlin/g0401_0500/s0420_strong_password_checker/Solution.kt)| Hard | String, Greedy, Heap_Priority_Queue | 157 | 80.00
1631+
| 0419 |[Battleships in a Board](src/main/kotlin/g0401_0500/s0419_battleships_in_a_board/Solution.kt)| Medium | Array, Depth_First_Search, Matrix | 273 | 76.92
1632+
| 0417 |[Pacific Atlantic Water Flow](src/main/kotlin/g0401_0500/s0417_pacific_atlantic_water_flow/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Graph_Theory_I_Day_4_Matrix_Related_Problems, Level_2_Day_10_Graph/BFS/DFS, Udemy_Graph | 319 | 100.00
16261633
| 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming | 509 | 57.56
1634+
| 0415 |[Add Strings](src/main/kotlin/g0401_0500/s0415_add_strings/Solution.kt)| Easy | String, Math, Simulation, Data_Structure_II_Day_6_String | 296 | 76.00
16271635
| 0414 |[Third Maximum Number](src/main/kotlin/g0401_0500/s0414_third_maximum_number/Solution.kt)| Easy | Array, Sorting | 317 | 73.00
16281636
| 0413 |[Arithmetic Slices](src/main/kotlin/g0401_0500/s0413_arithmetic_slices/Solution.kt)| Medium | Array, Dynamic_Programming, Algorithm_II_Day_14_Dynamic_Programming, Dynamic_Programming_I_Day_10 | 156 | 100.00
16291637
| 0412 |[Fizz Buzz](src/main/kotlin/g0401_0500/s0412_fizz_buzz/Solution.kt)| Easy | Top_Interview_Questions, String, Math, Simulation, Udemy_Integers | 307 | 71.81
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0401_0500.s0415_add_strings
2+
3+
// #Easy #String #Math #Simulation #Data_Structure_II_Day_6_String
4+
// #2022_12_06_Time_296_ms_(76.00%)_Space_36_MB_(84.00%)
5+
6+
class Solution {
7+
fun addStrings(num1: String, num2: String): String {
8+
var endNum1 = num1.length - 1
9+
var endNum2 = num2.length - 1
10+
val res = StringBuilder()
11+
var carry = 0
12+
var sum: Int
13+
while (endNum1 >= 0 || endNum2 >= 0) {
14+
val a = if (endNum1 >= 0) num1[endNum1] - '0' else 0
15+
val b = if (endNum2 >= 0) num2[endNum2] - '0' else 0
16+
sum = (a + b + carry) % 10
17+
carry = (a + b + carry) / 10
18+
res.append(sum)
19+
endNum1--
20+
endNum2--
21+
}
22+
if (carry != 0) {
23+
res.append(carry)
24+
}
25+
return res.reverse().toString()
26+
}
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
415\. Add Strings
2+
3+
Easy
4+
5+
Given two non-negative integers, `num1` and `num2` represented as string, return _the sum of_ `num1` _and_ `num2` _as a string_.
6+
7+
You must solve the problem without using any built-in library for handling large integers (such as `BigInteger`). You must also not convert the inputs to integers directly.
8+
9+
**Example 1:**
10+
11+
**Input:** num1 = "11", num2 = "123"
12+
13+
**Output:** "134"
14+
15+
**Example 2:**
16+
17+
**Input:** num1 = "456", num2 = "77"
18+
19+
**Output:** "533"
20+
21+
**Example 3:**
22+
23+
**Input:** num1 = "0", num2 = "0"
24+
25+
**Output:** "0"
26+
27+
**Constraints:**
28+
29+
* <code>1 <= num1.length, num2.length <= 10<sup>4</sup></code>
30+
* `num1` and `num2` consist of only digits.
31+
* `num1` and `num2` don't have any leading zeros except for the zero itself.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package g0401_0500.s0417_pacific_atlantic_water_flow
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix
4+
// #Graph_Theory_I_Day_4_Matrix_Related_Problems #Level_2_Day_10_Graph/BFS/DFS #Udemy_Graph
5+
// #2022_12_06_Time_319_ms_(100.00%)_Space_37.5_MB_(100.00%)
6+
7+
class Solution {
8+
private var col = 0
9+
private var row = 0
10+
fun pacificAtlantic(matrix: Array<IntArray>): List<List<Int>> {
11+
val res: MutableList<List<Int>> = ArrayList()
12+
if (matrix.size == 0) {
13+
return res
14+
}
15+
col = matrix.size
16+
row = matrix[0].size
17+
val pacific = Array(col) {
18+
BooleanArray(
19+
row
20+
)
21+
}
22+
val atlantic = Array(col) {
23+
BooleanArray(
24+
row
25+
)
26+
}
27+
for (i in 0 until col) {
28+
dfs(i, 0, matrix, pacific)
29+
dfs(i, row - 1, matrix, atlantic)
30+
}
31+
for (i in 0 until row) {
32+
dfs(0, i, matrix, pacific)
33+
dfs(col - 1, i, matrix, atlantic)
34+
}
35+
for (i in 0 until col) {
36+
for (j in 0 until row) {
37+
if (pacific[i][j] && atlantic[i][j]) {
38+
val temp: MutableList<Int> = ArrayList()
39+
temp.add(i)
40+
temp.add(j)
41+
res.add(temp)
42+
}
43+
}
44+
}
45+
return res
46+
}
47+
48+
private fun dfs(i: Int, j: Int, matrix: Array<IntArray>, visited: Array<BooleanArray>) {
49+
if (i < 0 || j < 0 || i >= matrix.size || j >= matrix[0].size || visited[i][j]) {
50+
return
51+
}
52+
visited[i][j] = true
53+
if (i < col - 1 && matrix[i][j] <= matrix[i + 1][j]) {
54+
dfs(i + 1, j, matrix, visited)
55+
}
56+
if (i > 0 && matrix[i][j] <= matrix[i - 1][j]) {
57+
dfs(i - 1, j, matrix, visited)
58+
}
59+
if (j < row - 1 && matrix[i][j] <= matrix[i][j + 1]) {
60+
dfs(i, j + 1, matrix, visited)
61+
}
62+
if (j > 0 && matrix[i][j] <= matrix[i][j - 1]) {
63+
dfs(i, j - 1, matrix, visited)
64+
}
65+
}
66+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
417\. Pacific Atlantic Water Flow
2+
3+
Medium
4+
5+
There is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges.
6+
7+
The island is partitioned into a grid of square cells. You are given an `m x n` integer matrix `heights` where `heights[r][c]` represents the **height above sea level** of the cell at coordinate `(r, c)`.
8+
9+
The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is **less than or equal to** the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.
10+
11+
Return _a **2D list** of grid coordinates_ `result` _where_ <code>result[i] = [r<sub>i</sub>, c<sub>i</sub>]</code> _denotes that rain water can flow from cell_ <code>(r<sub>i</sub>, c<sub>i</sub>)</code> _to **both** the Pacific and Atlantic oceans_.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/06/08/waterflow-grid.jpg)
16+
17+
**Input:** heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
18+
19+
**Output:** [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
20+
21+
**Explanation:** The following cells can flow to the Pacific and Atlantic oceans, as shown below: [0,4]: [0,4] -> Pacific Ocean [0,4] -> Atlantic Ocean [1,3]: [1,3] -> [0,3] -> Pacific Ocean [1,3] -> [1,4] -> Atlantic Ocean [1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean [1,4] -> Atlantic Ocean [2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean [2,2] -> [2,3] -> [2,4] -> Atlantic Ocean [3,0]: [3,0] -> Pacific Ocean [3,0] -> [4,0] -> Atlantic Ocean [3,1]: [3,1] -> [3,0] -> Pacific Ocean [3,1] -> [4,1] -> Atlantic Ocean [4,0]: [4,0] -> Pacific Ocean [4,0] -> Atlantic Ocean Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
22+
23+
**Example 2:**
24+
25+
**Input:** heights = [[1]]
26+
27+
**Output:** [[0,0]]
28+
29+
**Explanation:** The water can flow from the only cell to the Pacific and Atlantic oceans.
30+
31+
**Constraints:**
32+
33+
* `m == heights.length`
34+
* `n == heights[r].length`
35+
* `1 <= m, n <= 200`
36+
* <code>0 <= heights[r][c] <= 10<sup>5</sup></code>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0401_0500.s0419_battleships_in_a_board
2+
3+
// #Medium #Array #Depth_First_Search #Matrix
4+
// #2022_12_06_Time_273_ms_(76.92%)_Space_41.5_MB_(50.00%)
5+
6+
class Solution {
7+
fun countBattleships(board: Array<CharArray>?): Int {
8+
if (board == null || board.size == 0) {
9+
return 0
10+
}
11+
var count = 0
12+
val m = board.size
13+
val n = board[0].size
14+
for (i in 0 until m) {
15+
for (j in 0 until n) {
16+
if (board[i][j] != '.' && (j <= 0 || board[i][j - 1] != 'X') &&
17+
(i <= 0 || board[i - 1][j] != 'X')
18+
) {
19+
count++
20+
}
21+
}
22+
}
23+
return count
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
419\. Battleships in a Board
2+
3+
Medium
4+
5+
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`.
6+
7+
**Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, `1` column), where `k` can be of any size. At least one horizontal or vertical cell separates between two battleships (i.e., there are no adjacent battleships).
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/10/battelship-grid.jpg)
12+
13+
**Input:** board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
14+
15+
**Output:** 2
16+
17+
**Example 2:**
18+
19+
**Input:** board = [["."]]
20+
21+
**Output:** 0
22+
23+
**Constraints:**
24+
25+
* `m == board.length`
26+
* `n == board[i].length`
27+
* `1 <= m, n <= 200`
28+
* `board[i][j]` is either `'.'` or `'X'`.
29+
30+
**Follow up:** Could you do it in one-pass, using only `O(1)` extra memory and without modifying the values `board`?
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g0401_0500.s0420_strong_password_checker
2+
3+
// #Hard #String #Greedy #Heap_Priority_Queue
4+
// #2022_12_06_Time_157_ms_(80.00%)_Space_34.4_MB_(20.00%)
5+
6+
class Solution {
7+
fun strongPasswordChecker(s: String): Int {
8+
var res = 0
9+
var a1 = 1
10+
var a2 = 1
11+
var d = 1
12+
val carr = s.toCharArray()
13+
val arr = IntArray(carr.size)
14+
var i1 = 0
15+
while (i1 < arr.size) {
16+
if (Character.isLowerCase(carr[i1])) {
17+
a1 = 0
18+
}
19+
if (Character.isUpperCase(carr[i1])) {
20+
a2 = 0
21+
}
22+
if (Character.isDigit(carr[i1])) {
23+
d = 0
24+
}
25+
val j = i1
26+
while (i1 < carr.size && carr[i1] == carr[j]) {
27+
i1++
28+
}
29+
arr[j] = i1 - j
30+
}
31+
val totalMissing = a1 + a2 + d
32+
if (arr.size < 6) {
33+
res += totalMissing + Math.max(0, 6 - (arr.size + totalMissing))
34+
} else {
35+
var overLen = Math.max(arr.size - 20, 0)
36+
var leftOver = 0
37+
res += overLen
38+
for (k in 1..2) {
39+
var i = 0
40+
while (i < arr.size && overLen > 0) {
41+
if (arr[i] < 3 || arr[i] % 3 != k - 1) {
42+
i++
43+
continue
44+
}
45+
arr[i] -= Math.min(overLen, k)
46+
overLen -= k
47+
i++
48+
}
49+
}
50+
for (i in arr.indices) {
51+
if (arr[i] >= 3 && overLen > 0) {
52+
val need = arr[i] - 2
53+
arr[i] -= overLen
54+
overLen -= need
55+
}
56+
if (arr[i] >= 3) {
57+
leftOver += arr[i] / 3
58+
}
59+
}
60+
res += Math.max(totalMissing, leftOver)
61+
}
62+
return res
63+
}
64+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
420\. Strong Password Checker
2+
3+
Hard
4+
5+
A password is considered strong if the below conditions are all met:
6+
7+
* It has at least `6` characters and at most `20` characters.
8+
* It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**.
9+
* It does not contain three repeating characters in a row (i.e., <code>"B<ins>**aaa**</ins>bb0"</code> is weak, but <code>"B**<ins>aa</ins>**b<ins>**a**</ins>0"</code> is strong).
10+
11+
Given a string `password`, return _the minimum number of steps required to make `password` strong. if `password` is already strong, return `0`._
12+
13+
In one step, you can:
14+
15+
* Insert one character to `password`,
16+
* Delete one character from `password`, or
17+
* Replace one character of `password` with another character.
18+
19+
**Example 1:**
20+
21+
**Input:** password = "a"
22+
23+
**Output:** 5
24+
25+
**Example 2:**
26+
27+
**Input:** password = "aA1"
28+
29+
**Output:** 3
30+
31+
**Example 3:**
32+
33+
**Input:** password = "1337C0d3"
34+
35+
**Output:** 0
36+
37+
**Constraints:**
38+
39+
* `1 <= password.length <= 50`
40+
* `password` consists of letters, digits, dot `'.'` or exclamation mark `'!'`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0401_0500.s0415_add_strings
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 addStrings() {
10+
assertThat(Solution().addStrings("11", "123"), equalTo("134"))
11+
}
12+
13+
@Test
14+
fun addStrings2() {
15+
assertThat(Solution().addStrings("456", "77"), equalTo("533"))
16+
}
17+
}

0 commit comments

Comments
 (0)