Skip to content

Commit fed1bfe

Browse files
authored
Added tasks 932, 933, 934, 935
1 parent f3f00a5 commit fed1bfe

File tree

14 files changed

+669
-0
lines changed

14 files changed

+669
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
16731673

16741674
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16751675
|-|-|-|-|-|-
1676+
| 0934 |[Shortest Bridge](src/main/kotlin/g0901_1000/s0934_shortest_bridge/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 301 | 80.95
16761677

16771678
#### Day 7 Standard Traversal
16781679

@@ -1731,6 +1732,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17311732
|------|----------------|-------------|-------------|----------|---------
17321733
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
17331734
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
1735+
| 0935 |[Knight Dialer](src/main/kotlin/g0901_1000/s0935_knight_dialer/Solution.kt)| Medium | Dynamic_Programming | 160 | 100.00
1736+
| 0934 |[Shortest Bridge](src/main/kotlin/g0901_1000/s0934_shortest_bridge/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Graph_Theory_I_Day_6_Matrix_Related_Problems | 301 | 80.95
1737+
| 0933 |[Number of Recent Calls](src/main/kotlin/g0901_1000/s0933_number_of_recent_calls/RecentCounter.kt)| Easy | Design, Queue, Data_Stream | 476 | 82.50
1738+
| 0932 |[Beautiful Array](src/main/kotlin/g0901_1000/s0932_beautiful_array/Solution.kt)| Medium | Array, Math, Divide_and_Conquer | 145 | 100.00
17341739
| 0931 |[Minimum Falling Path Sum](src/main/kotlin/g0901_1000/s0931_minimum_falling_path_sum/Solution.kt)| Medium | Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_13 | 201 | 84.21
17351740
| 0930 |[Binary Subarrays With Sum](src/main/kotlin/g0901_1000/s0930_binary_subarrays_with_sum/Solution.kt)| Medium | Array, Hash_Table, Prefix_Sum, Sliding_Window | 250 | 93.75
17361741
| 0929 |[Unique Email Addresses](src/main/kotlin/g0901_1000/s0929_unique_email_addresses/Solution.kt)| Easy | Array, String, Hash_Table | 207 | 89.29
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0901_1000.s0932_beautiful_array
2+
3+
// #Medium #Array #Math #Divide_and_Conquer #2023_04_27_Time_145_ms_(100.00%)_Space_36.4_MB_(16.67%)
4+
5+
class Solution {
6+
private var memo: MutableMap<Int, IntArray>? = null
7+
fun beautifulArray(n: Int): IntArray? {
8+
memo = HashMap()
9+
return helper(n)
10+
}
11+
12+
private fun helper(n: Int): IntArray? {
13+
if (n == 1) {
14+
memo!![1] = intArrayOf(1)
15+
return intArrayOf(1)
16+
}
17+
if (memo!!.containsKey(n)) {
18+
return memo!![n]
19+
}
20+
val mid = (n + 1) / 2
21+
val left = helper(mid)
22+
val right = helper(n - mid)
23+
val rst = IntArray(n)
24+
for (i in 0 until mid) {
25+
rst[i] = left!![i] * 2 - 1
26+
}
27+
for (i in mid until n) {
28+
rst[i] = right!![i - mid] * 2
29+
}
30+
memo!![n] = rst
31+
return rst
32+
}
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
932\. Beautiful Array
2+
3+
Medium
4+
5+
An array `nums` of length `n` is **beautiful** if:
6+
7+
* `nums` is a permutation of the integers in the range `[1, n]`.
8+
* For every `0 <= i < j < n`, there is no index `k` with `i < k < j` where `2 * nums[k] == nums[i] + nums[j]`.
9+
10+
Given the integer `n`, return _any **beautiful** array_ `nums` _of length_ `n`. There will be at least one valid answer for the given `n`.
11+
12+
**Example 1:**
13+
14+
**Input:** n = 4
15+
16+
**Output:** [2,1,4,3]
17+
18+
**Example 2:**
19+
20+
**Input:** n = 5
21+
22+
**Output:** [3,1,2,5,4]
23+
24+
**Constraints:**
25+
26+
* `1 <= n <= 1000`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0901_1000.s0933_number_of_recent_calls
2+
3+
// #Easy #Design #Queue #Data_Stream #2023_04_27_Time_476_ms_(82.50%)_Space_107.1_MB_(5.00%)
4+
5+
import java.util.LinkedList
6+
import java.util.Queue
7+
8+
class RecentCounter {
9+
private val q: Queue<Int>
10+
11+
init {
12+
q = LinkedList()
13+
}
14+
15+
fun ping(t: Int): Int {
16+
q.offer(t)
17+
val min = t - 3000
18+
while (min > q.peek()) {
19+
q.poll()
20+
}
21+
return q.size
22+
}
23+
}
24+
25+
/*
26+
* Your RecentCounter object will be instantiated and called as such:
27+
* var obj = RecentCounter()
28+
* var param_1 = obj.ping(t)
29+
*/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
933\. Number of Recent Calls
2+
3+
Easy
4+
5+
You have a `RecentCounter` class which counts the number of recent requests within a certain time frame.
6+
7+
Implement the `RecentCounter` class:
8+
9+
* `RecentCounter()` Initializes the counter with zero recent requests.
10+
* `int ping(int t)` Adds a new request at time `t`, where `t` represents some time in milliseconds, and returns the number of requests that has happened in the past `3000` milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range `[t - 3000, t]`.
11+
12+
It is **guaranteed** that every call to `ping` uses a strictly larger value of `t` than the previous call.
13+
14+
**Example 1:**
15+
16+
**Input** ["RecentCounter", "ping", "ping", "ping", "ping"] [[], [1], [100], [3001], [3002]]
17+
18+
**Output:** [null, 1, 2, 3, 3]
19+
20+
**Explanation:**
21+
22+
RecentCounter recentCounter = new RecentCounter();
23+
24+
recentCounter.ping(1); // requests = [<ins>1</ins>], range is [-2999,1], return 1
25+
26+
recentCounter.ping(100); // requests = [<ins>1</ins>, <ins>100</ins>], range is [-2900,100], return 2
27+
28+
recentCounter.ping(3001); // requests = [<ins>1</ins>, <ins>100</ins>, <ins>3001</ins>], range is [1,3001], return 3
29+
30+
recentCounter.ping(3002); // requests = [1, <ins>100</ins>, <ins>3001</ins>, <ins>3002</ins>], range is [2,3002], return 3
31+
32+
**Constraints:**
33+
34+
* <code>1 <= t <= 10<sup>9</sup></code>
35+
* Each test case will call `ping` with **strictly increasing** values of `t`.
36+
* At most <code>10<sup>4</sup></code> calls will be made to `ping`.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package g0901_1000.s0934_shortest_bridge
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix
4+
// #Graph_Theory_I_Day_6_Matrix_Related_Problems
5+
// #2023_04_27_Time_301_ms_(80.95%)_Space_77.9_MB_(9.52%)
6+
7+
class Solution {
8+
private class Pair(var x: Int, var y: Int)
9+
10+
private val dirs = arrayOf(intArrayOf(1, 0), intArrayOf(0, 1), intArrayOf(-1, 0), intArrayOf(0, -1))
11+
12+
fun shortestBridge(grid: Array<IntArray>): Int {
13+
val q: ArrayDeque<Pair> = ArrayDeque()
14+
var flag = false
15+
val visited = Array(grid.size) {
16+
BooleanArray(
17+
grid[0].size
18+
)
19+
}
20+
var i = 0
21+
while (i < grid.size && !flag) {
22+
var j = 0
23+
while (j < grid[i].size && !flag) {
24+
if (grid[i][j] == 1) {
25+
dfs(grid, i, j, visited, q)
26+
flag = true
27+
}
28+
j++
29+
}
30+
i++
31+
}
32+
var level = -1
33+
while (!q.isEmpty()) {
34+
var size: Int = q.size
35+
level++
36+
while (size-- > 0) {
37+
val rem = q.removeFirst()
38+
for (dir in dirs) {
39+
val newrow = rem.x + dir[0]
40+
val newcol = rem.y + dir[1]
41+
if (newrow >= 0 && newcol >= 0 && newrow < grid.size && newcol < grid[0].size &&
42+
!visited[newrow][newcol]
43+
) {
44+
if (grid[newrow][newcol] == 1) {
45+
return level
46+
}
47+
q.add(Pair(newrow, newcol))
48+
visited[newrow][newcol] = true
49+
}
50+
}
51+
}
52+
}
53+
return -1
54+
}
55+
56+
private fun dfs(grid: Array<IntArray>, row: Int, col: Int, visited: Array<BooleanArray>, q: ArrayDeque<Pair>) {
57+
visited[row][col] = true
58+
q.add(Pair(row, col))
59+
for (dir in dirs) {
60+
val newrow = row + dir[0]
61+
val newcol = col + dir[1]
62+
if (newrow >= 0 && newcol >= 0 && newrow < grid.size && newcol < grid[0].size &&
63+
!visited[newrow][newcol] && grid[newrow][newcol] == 1
64+
) {
65+
dfs(grid, newrow, newcol, visited, q)
66+
}
67+
}
68+
}
69+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
934\. Shortest Bridge
2+
3+
Medium
4+
5+
You are given an `n x n` binary matrix `grid` where `1` represents land and `0` represents water.
6+
7+
An **island** is a 4-directionally connected group of `1`'s not connected to any other `1`'s. There are **exactly two islands** in `grid`.
8+
9+
You may change `0`'s to `1`'s to connect the two islands to form **one island**.
10+
11+
Return _the smallest number of_ `0`_'s you must flip to connect the two islands_.
12+
13+
**Example 1:**
14+
15+
**Input:** grid = [[0,1],[1,0]]
16+
17+
**Output:** 1
18+
19+
**Example 2:**
20+
21+
**Input:** grid = [[0,1,0],[0,0,0],[0,0,1]]
22+
23+
**Output:** 2
24+
25+
**Example 3:**
26+
27+
**Input:** grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
* `n == grid.length == grid[i].length`
34+
* `2 <= n <= 100`
35+
* `grid[i][j]` is either `0` or `1`.
36+
* There are exactly two islands in `grid`.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0901_1000.s0935_knight_dialer
2+
3+
// #Medium #Dynamic_Programming #2023_04_27_Time_160_ms_(100.00%)_Space_34.7_MB_(100.00%)
4+
5+
class Solution {
6+
fun knightDialer(n: Int): Int {
7+
if (n == 1) {
8+
return 10
9+
}
10+
val mod = 1000000007
11+
while (MEMO.size < n) {
12+
val cur = MEMO[MEMO.size - 1]
13+
val next = IntArray(10)
14+
for (i in 0..9) {
15+
for (d in MAP[i]!!) {
16+
next[d] = (next[d] + cur[i]) % mod
17+
}
18+
}
19+
MEMO.add(next)
20+
}
21+
var sum = 0
22+
for (x in MEMO[n - 1]) {
23+
sum = (sum + x) % mod
24+
}
25+
return sum
26+
}
27+
28+
companion object {
29+
private val MAP = arrayOfNulls<IntArray>(10)
30+
private val MEMO: MutableList<IntArray> = ArrayList()
31+
32+
init {
33+
MAP[0] = intArrayOf(4, 6)
34+
MAP[1] = intArrayOf(6, 8)
35+
MAP[2] = intArrayOf(7, 9)
36+
MAP[3] = intArrayOf(4, 8)
37+
MAP[4] = intArrayOf(3, 9, 0)
38+
MAP[5] = IntArray(0)
39+
MAP[6] = intArrayOf(1, 7, 0)
40+
MAP[7] = intArrayOf(2, 6)
41+
MAP[8] = intArrayOf(1, 3)
42+
MAP[9] = intArrayOf(2, 4)
43+
MEMO.add(intArrayOf(1, 1, 1, 1, 1, 0, 1, 1, 1, 1))
44+
}
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
935\. Knight Dialer
2+
3+
Medium
4+
5+
The chess knight has a **unique movement**, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an **L**). The possible movements of chess knight are shown in this diagaram:
6+
7+
A chess knight can move as indicated in the chess diagram below:
8+
9+
![](https://assets.leetcode.com/uploads/2020/08/18/chess.jpg)
10+
11+
We have a chess knight and a phone pad as shown below, the knight **can only stand on a numeric cell** (i.e. blue cell).
12+
13+
![](https://assets.leetcode.com/uploads/2020/08/18/phone.jpg)
14+
15+
Given an integer `n`, return how many distinct phone numbers of length `n` we can dial.
16+
17+
You are allowed to place the knight **on any numeric cell** initially and then you should perform `n - 1` jumps to dial a number of length `n`. All jumps should be **valid** knight jumps.
18+
19+
As the answer may be very large, **return the answer modulo** <code>10<sup>9</sup> + 7</code>.
20+
21+
**Example 1:**
22+
23+
**Input:** n = 1
24+
25+
**Output:** 10
26+
27+
**Explanation:** We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
28+
29+
**Example 2:**
30+
31+
**Input:** n = 2
32+
33+
**Output:** 20
34+
35+
**Explanation:** All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
36+
37+
**Example 3:**
38+
39+
**Input:** n = 3131
40+
41+
**Output:** 136006598
42+
43+
**Explanation:** Please take care of the mod.
44+
45+
**Constraints:**
46+
47+
* `1 <= n <= 5000`

0 commit comments

Comments
 (0)