Skip to content

Commit e4457f6

Browse files
authored
Added tasks 3285-3292
1 parent 4fef2ec commit e4457f6

File tree

9 files changed

+753
-7
lines changed
  • src/main/kotlin/g3201_3300
    • s3285_find_indices_of_stable_mountains
    • s3286_find_a_safe_walk_through_a_grid
    • s3287_find_the_maximum_sequence_value_of_array
    • s3288_length_of_the_longest_increasing_path
    • s3289_the_two_sneaky_numbers_of_digitville
    • s3290_maximum_multiplication_score
    • s3291_minimum_number_of_valid_strings_to_form_target_i
    • s3292_minimum_number_of_valid_strings_to_form_target_ii

9 files changed

+753
-7
lines changed

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
|-|-|-|-|-|-
6161
| 0121 |[Best Time to Buy and Sell Stock](src/main/kotlin/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 609 | 94.06
6262
| 0283 |[Move Zeroes](src/main/kotlin/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 516 | 79.07
63-
| 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 202 | 91.18
63+
| 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 202 | 91.18
6464
| 0217 |[Contains Duplicate](src/main/kotlin/g0201_0300/s0217_contains_duplicate)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting | 719 | 73.49
6565
| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word)| Easy | String | 135 | 93.67
6666
| 0605 |[Can Place Flowers](src/main/kotlin/g0601_0700/s0605_can_place_flowers)| Easy | Array, Greedy | 209 | 85.71
@@ -244,7 +244,7 @@
244244

245245
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
246246
|-|-|-|-|-|-
247-
| 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 202 | 91.18
247+
| 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 202 | 91.18
248248
| 0088 |[Merge Sorted Array](src/main/kotlin/g0001_0100/s0088_merge_sorted_array)| Easy | Top_Interview_Questions, Array, Sorting, Two_Pointers | 170 | 69.74
249249

250250
#### Day 3 Array
@@ -409,7 +409,7 @@
409409

410410
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
411411
|-|-|-|-|-|-
412-
| 0002 |[Add Two Numbers](src/main/kotlin/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 203 | 96.13
412+
| 0002 |[Add Two Numbers](src/main/kotlin/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)), AI_can_be_used_to_solve_the_task | 203 | 96.13
413413
| 0142 |[Linked List Cycle II](src/main/kotlin/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 192 | 63.39
414414

415415
#### Day 11 Linked List
@@ -1330,7 +1330,7 @@
13301330

13311331
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13321332
|-|-|-|-|-|-
1333-
| 0002 |[Add Two Numbers](src/main/kotlin/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 203 | 96.13
1333+
| 0002 |[Add Two Numbers](src/main/kotlin/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)), AI_can_be_used_to_solve_the_task | 203 | 96.13
13341334
| 0445 |[Add Two Numbers II](src/main/kotlin/g0401_0500/s0445_add_two_numbers_ii)| Medium | Math, Stack, Linked_List | 240 | 82.61
13351335

13361336
#### Day 16
@@ -1648,7 +1648,7 @@
16481648

16491649
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16501650
|-|-|-|-|-|-
1651-
| 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 202 | 91.18
1651+
| 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 202 | 91.18
16521652
| 0299 |[Bulls and Cows](src/main/kotlin/g0201_0300/s0299_bulls_and_cows)| Medium | String, Hash_Table, Counting | 254 | 84.82
16531653

16541654
#### Day 14 Stack
@@ -1816,6 +1816,14 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3292 |[Minimum Number of Valid Strings to Form Target II](src/main/kotlin/g3201_3300/s3292_minimum_number_of_valid_strings_to_form_target_ii)| Hard | Array, String, Dynamic_Programming, Binary_Search, Segment_Tree, Hash_Function, String_Matching, Rolling_Hash | 674 | 50.00
1820+
| 3291 |[Minimum Number of Valid Strings to Form Target I](src/main/kotlin/g3201_3300/s3291_minimum_number_of_valid_strings_to_form_target_i)| Medium | Array, String, Dynamic_Programming, Binary_Search, Trie, Segment_Tree, Hash_Function, String_Matching, Rolling_Hash | 566 | 70.00
1821+
| 3290 |[Maximum Multiplication Score](src/main/kotlin/g3201_3300/s3290_maximum_multiplication_score)| Medium | Array, Dynamic_Programming | 749 | 66.67
1822+
| 3289 |[The Two Sneaky Numbers of Digitville](src/main/kotlin/g3201_3300/s3289_the_two_sneaky_numbers_of_digitville)| Easy | Array, Hash_Table, Math | 223 | 68.97
1823+
| 3288 |[Length of the Longest Increasing Path](src/main/kotlin/g3201_3300/s3288_length_of_the_longest_increasing_path)| Hard | Array, Sorting, Binary_Search | 984 | 83.33
1824+
| 3287 |[Find the Maximum Sequence Value of Array](src/main/kotlin/g3201_3300/s3287_find_the_maximum_sequence_value_of_array)| Hard | Array, Dynamic_Programming, Bit_Manipulation | 2893 | 33.33
1825+
| 3286 |[Find a Safe Walk Through a Grid](src/main/kotlin/g3201_3300/s3286_find_a_safe_walk_through_a_grid)| Medium | Array, Breadth_First_Search, Matrix, Heap_Priority_Queue, Graph, Shortest_Path | 357 | 48.28
1826+
| 3285 |[Find Indices of Stable Mountains](src/main/kotlin/g3201_3300/s3285_find_indices_of_stable_mountains)| Easy | Array | 195 | 92.68
18191827
| 3283 |[Maximum Number of Moves to Kill All Pawns](src/main/kotlin/g3201_3300/s3283_maximum_number_of_moves_to_kill_all_pawns)| Hard | Array, Math, Breadth_First_Search, Bit_Manipulation, Bitmask, Game_Theory | 638 | 100.00
18201828
| 3282 |[Reach End of Array With Max Score](src/main/kotlin/g3201_3300/s3282_reach_end_of_array_with_max_score)| Medium | Array, Greedy | 789 | 90.91
18211829
| 3281 |[Maximize Score of Numbers in Ranges](src/main/kotlin/g3201_3300/s3281_maximize_score_of_numbers_in_ranges)| Medium | Array, Sorting, Greedy, Binary_Search | 710 | 88.24
@@ -4425,6 +4433,6 @@
44254433
| 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Data_Structure_II_Day_9_String, Algorithm_II_Day_14_Dynamic_Programming, Dynamic_Programming_I_Day_17, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 162 | 99.00
44264434
| 0004 |[Median of Two Sorted Arrays](src/main/kotlin/g0001_0100/s0004_median_of_two_sorted_arrays)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Divide_and_Conquer, Big_O_Time_O(log(min(N,M)))_Space_O(1) | 293 | 75.96
44274435
| 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Algorithm_I_Day_6_Sliding_Window, Level_2_Day_14_Sliding_Window/Two_Pointer, Udemy_Strings, Big_O_Time_O(n)_Space_O(1) | 201 | 87.28
4428-
| 0002 |[Add Two Numbers](src/main/kotlin/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Data_Structure_II_Day_10_Linked_List, Programming_Skills_II_Day_15, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 203 | 96.13
4429-
| 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Data_Structure_I_Day_2_Array, Level_1_Day_13_Hashmap, Udemy_Arrays, Big_O_Time_O(n)_Space_O(n) | 202 | 91.18
4436+
| 0002 |[Add Two Numbers](src/main/kotlin/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Data_Structure_II_Day_10_Linked_List, Programming_Skills_II_Day_15, Big_O_Time_O(max(N,M))_Space_O(max(N,M)), AI_can_be_used_to_solve_the_task | 203 | 96.13
4437+
| 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Data_Structure_I_Day_2_Array, Level_1_Day_13_Hashmap, Udemy_Arrays, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 202 | 91.18
44304438

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3285\. Find Indices of Stable Mountains
5+
6+
Easy
7+
8+
There are `n` mountains in a row, and each mountain has a height. You are given an integer array `height` where `height[i]` represents the height of mountain `i`, and an integer `threshold`.
9+
10+
A mountain is called **stable** if the mountain just before it (**if it exists**) has a height **strictly greater** than `threshold`. **Note** that mountain 0 is **not** stable.
11+
12+
Return an array containing the indices of _all_ **stable** mountains in **any** order.
13+
14+
**Example 1:**
15+
16+
**Input:** height = [1,2,3,4,5], threshold = 2
17+
18+
**Output:** [3,4]
19+
20+
**Explanation:**
21+
22+
* Mountain 3 is stable because `height[2] == 3` is greater than `threshold == 2`.
23+
* Mountain 4 is stable because `height[3] == 4` is greater than `threshold == 2`.
24+
25+
**Example 2:**
26+
27+
**Input:** height = [10,1,10,1,10], threshold = 3
28+
29+
**Output:** [1,3]
30+
31+
**Example 3:**
32+
33+
**Input:** height = [10,1,10,1,10], threshold = 10
34+
35+
**Output:** []
36+
37+
**Constraints:**
38+
39+
* `2 <= n == height.length <= 100`
40+
* `1 <= height[i] <= 100`
41+
* `1 <= threshold <= 100`
42+
43+
## Solution
44+
45+
```kotlin
46+
class Solution {
47+
fun stableMountains(height: IntArray, threshold: Int): List<Int> {
48+
val n = height.size
49+
val list: MutableList<Int> = mutableListOf()
50+
for (i in 0 until n - 1) {
51+
if (height[i] > threshold) {
52+
list.add(i + 1)
53+
}
54+
}
55+
return list
56+
}
57+
}
58+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3286\. Find a Safe Walk Through a Grid
5+
6+
Medium
7+
8+
You are given an `m x n` binary matrix `grid` and an integer `health`.
9+
10+
You start on the upper-left corner `(0, 0)` and would like to get to the lower-right corner `(m - 1, n - 1)`.
11+
12+
You can move up, down, left, or right from one cell to another adjacent cell as long as your health _remains_ **positive**.
13+
14+
Cells `(i, j)` with `grid[i][j] = 1` are considered **unsafe** and reduce your health by 1.
15+
16+
Return `true` if you can reach the final cell with a health value of 1 or more, and `false` otherwise.
17+
18+
**Example 1:**
19+
20+
**Input:** grid = \[\[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]], health = 1
21+
22+
**Output:** true
23+
24+
**Explanation:**
25+
26+
The final cell can be reached safely by walking along the gray cells below.
27+
28+
![](https://assets.leetcode.com/uploads/2024/08/04/3868_examples_1drawio.png)
29+
30+
**Example 2:**
31+
32+
**Input:** grid = \[\[0,1,1,0,0,0],[1,0,1,0,0,0],[0,1,1,1,0,1],[0,0,1,0,1,0]], health = 3
33+
34+
**Output:** false
35+
36+
**Explanation:**
37+
38+
A minimum of 4 health points is needed to reach the final cell safely.
39+
40+
![](https://assets.leetcode.com/uploads/2024/08/04/3868_examples_2drawio.png)
41+
42+
**Example 3:**
43+
44+
**Input:** grid = \[\[1,1,1],[1,0,1],[1,1,1]], health = 5
45+
46+
**Output:** true
47+
48+
**Explanation:**
49+
50+
The final cell can be reached safely by walking along the gray cells below.
51+
52+
![](https://assets.leetcode.com/uploads/2024/08/04/3868_examples_3drawio.png)
53+
54+
Any path that does not go through the cell `(1, 1)` is unsafe since your health will drop to 0 when reaching the final cell.
55+
56+
**Constraints:**
57+
58+
* `m == grid.length`
59+
* `n == grid[i].length`
60+
* `1 <= m, n <= 50`
61+
* `2 <= m * n`
62+
* `1 <= health <= m + n`
63+
* `grid[i][j]` is either 0 or 1.
64+
65+
## Solution
66+
67+
```kotlin
68+
import java.util.LinkedList
69+
import java.util.Objects
70+
import java.util.Queue
71+
72+
class Solution {
73+
fun findSafeWalk(grid: List<List<Int>>, health: Int): Boolean {
74+
val n = grid.size
75+
val m = grid[0].size
76+
val dr = intArrayOf(0, 0, 1, -1)
77+
val dc = intArrayOf(1, -1, 0, 0)
78+
val visited = Array<Array<BooleanArray>?>(n) { Array<BooleanArray>(m) { BooleanArray(health + 1) } }
79+
val bfs: Queue<IntArray?> = LinkedList<IntArray>()
80+
bfs.add(intArrayOf(0, 0, health - grid[0][0]))
81+
visited[0]!![0][health - grid[0][0]] = true
82+
while (bfs.isNotEmpty()) {
83+
var size = bfs.size
84+
while (size-- > 0) {
85+
val currNode = bfs.poll()
86+
val r = Objects.requireNonNull<IntArray>(currNode)[0]
87+
val c = currNode!![1]
88+
val h = currNode[2]
89+
if (r == n - 1 && c == m - 1 && h > 0) {
90+
return true
91+
}
92+
for (k in 0..3) {
93+
val nr = r + dr[k]
94+
val nc = c + dc[k]
95+
if (isValidMove(nr, nc, n, m)) {
96+
val nh: Int = h - grid[nr][nc]
97+
if (nh >= 0 && !visited[nr]!![nc][nh]) {
98+
visited[nr]!![nc][nh] = true
99+
bfs.add(intArrayOf(nr, nc, nh))
100+
}
101+
}
102+
}
103+
}
104+
}
105+
return false
106+
}
107+
108+
private fun isValidMove(r: Int, c: Int, n: Int, m: Int): Boolean {
109+
return r >= 0 && c >= 0 && r < n && c < m
110+
}
111+
}
112+
```
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3287\. Find the Maximum Sequence Value of Array
5+
6+
Hard
7+
8+
You are given an integer array `nums` and a **positive** integer `k`.
9+
10+
The **value** of a sequence `seq` of size `2 * x` is defined as:
11+
12+
* `(seq[0] OR seq[1] OR ... OR seq[x - 1]) XOR (seq[x] OR seq[x + 1] OR ... OR seq[2 * x - 1])`.
13+
14+
Return the **maximum** **value** of any subsequence of `nums` having size `2 * k`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,6,7], k = 1
19+
20+
**Output:** 5
21+
22+
**Explanation:**
23+
24+
The subsequence `[2, 7]` has the maximum value of `2 XOR 7 = 5`.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [4,2,5,6,7], k = 2
29+
30+
**Output:** 2
31+
32+
**Explanation:**
33+
34+
The subsequence `[4, 5, 6, 7]` has the maximum value of `(4 OR 5) XOR (6 OR 7) = 2`.
35+
36+
**Constraints:**
37+
38+
* `2 <= nums.length <= 400`
39+
* <code>1 <= nums[i] < 2<sup>7</sup></code>
40+
* `1 <= k <= nums.length / 2`
41+
42+
## Solution
43+
44+
```kotlin
45+
import kotlin.math.max
46+
47+
class Solution {
48+
fun maxValue(nums: IntArray, k: Int): Int {
49+
val n = nums.size
50+
val left: Array<Array<MutableSet<Int>>> =
51+
Array<Array<MutableSet<Int>>>(n) { Array<MutableSet<Int>>(k + 1) { mutableSetOf() } }
52+
val right: Array<Array<MutableSet<Int>>> =
53+
Array<Array<MutableSet<Int>>>(n) { Array<MutableSet<Int>>(k + 1) { mutableSetOf() } }
54+
left[0][0].add(0)
55+
left[0][1].add(nums[0])
56+
for (i in 1 until n - k) {
57+
left[i][0].add(0)
58+
for (j in 1..k) {
59+
left[i][j].addAll(left[i - 1][j])
60+
for (v in left[i - 1][j - 1]) {
61+
left[i][j].add(v or nums[i])
62+
}
63+
}
64+
}
65+
right[n - 1][0].add(0)
66+
right[n - 1][1].add(nums[n - 1])
67+
var result = 0
68+
if (k == 1) {
69+
for (l in left[n - 2][k]) {
70+
result = max(result, (l xor nums[n - 1]))
71+
}
72+
}
73+
for (i in n - 2 downTo k) {
74+
right[i][0].add(0)
75+
for (j in 1..k) {
76+
right[i][j].addAll(right[i + 1][j])
77+
for (v in right[i + 1][j - 1]) {
78+
right[i][j].add(v or nums[i])
79+
}
80+
}
81+
for (l in left[i - 1][k]) {
82+
for (r in right[i][k]) {
83+
result = max(result, (l xor r))
84+
}
85+
}
86+
}
87+
return result
88+
}
89+
}
90+
```

0 commit comments

Comments
 (0)