Skip to content

Commit ed0cada

Browse files
authored
Added tasks 659, 661, 662, 664
1 parent 00a7df4 commit ed0cada

File tree

13 files changed

+442
-0
lines changed

13 files changed

+442
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16751675
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys/Solution.kt)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
16761676
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16771677
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
1678+
| 0664 |[Strange Printer](src/main/kotlin/g0601_0700/s0664_strange_printer/Solution.kt)| Hard | String, Dynamic_Programming | 196 | 100.00
1679+
| 0662 |[Maximum Width of Binary Tree](src/main/kotlin/g0601_0700/s0662_maximum_width_of_binary_tree/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 189 | 75.00
1680+
| 0661 |[Image Smoother](src/main/kotlin/g0601_0700/s0661_image_smoother/Solution.kt)| Easy | Array, Matrix | 352 | 100.00
1681+
| 0659 |[Split Array into Consecutive Subsequences](src/main/kotlin/g0601_0700/s0659_split_array_into_consecutive_subsequences/Solution.kt)| Medium | Array, Hash_Table, Greedy, Heap_Priority_Queue | 352 | 100.00
16781682
| 0658 |[Find K Closest Elements](src/main/kotlin/g0601_0700/s0658_find_k_closest_elements/Solution.kt)| Medium | Array, Sorting, Binary_Search, Two_Pointers, Heap_Priority_Queue, Binary_Search_II_Day_2 | 375 | 95.16
16791683
| 0657 |[Robot Return to Origin](src/main/kotlin/g0601_0700/s0657_robot_return_to_origin/Solution.kt)| Easy | String, Simulation | 186 | 100.00
16801684
| 0655 |[Print Binary Tree](src/main/kotlin/g0601_0700/s0655_print_binary_tree/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 176 | 100.00
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0601_0700.s0659_split_array_into_consecutive_subsequences
2+
3+
// #Medium #Array #Hash_Table #Greedy #Heap_Priority_Queue
4+
// #2023_02_14_Time_352_ms_(100.00%)_Space_38.5_MB_(100.00%)
5+
6+
class Solution {
7+
fun isPossible(nums: IntArray): Boolean {
8+
val element = IntArray(2001)
9+
for (num in nums) {
10+
element[num + 1000] += 1
11+
}
12+
for (i in element.indices) {
13+
while (element[i] > 0) {
14+
var length = 1
15+
while (i + length < element.size &&
16+
element[i + length] >= element[i + length - 1]
17+
) {
18+
length += 1
19+
}
20+
if (length < 3) {
21+
return false
22+
} else {
23+
for (j in i until i + length) {
24+
element[j] -= 1
25+
}
26+
}
27+
}
28+
}
29+
return true
30+
}
31+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
659\. Split Array into Consecutive Subsequences
2+
3+
Medium
4+
5+
You are given an integer array `nums` that is **sorted in non-decreasing order**.
6+
7+
Determine if it is possible to split `nums` into **one or more subsequences** such that **both** of the following conditions are true:
8+
9+
* Each subsequence is a **consecutive increasing sequence** (i.e. each integer is **exactly one** more than the previous integer).
10+
* All subsequences have a length of `3` **or more**.
11+
12+
Return `true` _if you can split_ `nums` _according to the above conditions, or_ `false` _otherwise_.
13+
14+
A **subsequence** of an array is a new array that is formed from the original array by deleting some (can be none) of the elements without disturbing the relative positions of the remaining elements. (i.e., `[1,3,5]` is a subsequence of <code>[<ins>1</ins>,2,<ins>3</ins>,4,<ins>5</ins>]</code> while `[1,3,2]` is not).
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,2,3,3,4,5]
19+
20+
**Output:** true
21+
22+
**Explanation:** nums can be split into the following subsequences:
23+
24+
[**<ins>1</ins>**,**<ins>2</ins>**,**<ins>3</ins>**,3,4,5] --> 1, 2, 3
25+
26+
[1,2,3,**<ins>3</ins>**,**<ins>4</ins>**,**<ins>5</ins>**] --> 3, 4, 5
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [1,2,3,3,4,4,5,5]
31+
32+
**Output:** true
33+
34+
**Explanation:** nums can be split into the following subsequences:
35+
36+
[**<ins>1</ins>**,**<ins>2</ins>**,**<ins>3</ins>**,3,**<ins>4</ins>**,4,**<ins>5</ins>**,5] --> 1, 2, 3, 4, 5
37+
38+
[1,2,3,**<ins>3</ins>**,4,**<ins>4</ins>**,5,**<ins>5</ins>**] --> 3, 4, 5
39+
40+
**Example 3:**
41+
42+
**Input:** nums = [1,2,3,4,4,5]
43+
44+
**Output:** false
45+
46+
**Explanation:** It is impossible to split nums into consecutive increasing subsequences of length 3 or more.
47+
48+
**Constraints:**
49+
50+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
51+
* `-1000 <= nums[i] <= 1000`
52+
* `nums` is sorted in **non-decreasing** order.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g0601_0700.s0661_image_smoother
2+
3+
// #Easy #Array #Matrix #2023_02_14_Time_352_ms_(100.00%)_Space_44.4_MB_(33.33%)
4+
5+
class Solution {
6+
fun imageSmoother(matrix: Array<IntArray>?): Array<IntArray>? {
7+
if (matrix.isNullOrEmpty()) {
8+
return matrix
9+
}
10+
val m = matrix.size
11+
val n = matrix[0].size
12+
val result = Array(m) { IntArray(n) }
13+
for (i in 0 until m) {
14+
for (j in 0 until n) {
15+
bfs(matrix, i, j, result, m, n)
16+
}
17+
}
18+
return result
19+
}
20+
21+
private fun bfs(matrix: Array<IntArray>, i: Int, j: Int, result: Array<IntArray>, m: Int, n: Int) {
22+
var sum = matrix[i][j]
23+
var denominator = 1
24+
if (j + 1 < n) {
25+
sum += matrix[i][j + 1]
26+
denominator++
27+
}
28+
if (i + 1 < m && j + 1 < n) {
29+
sum += matrix[i + 1][j + 1]
30+
denominator++
31+
}
32+
if (i + 1 < m) {
33+
sum += matrix[i + 1][j]
34+
denominator++
35+
}
36+
if (i + 1 < m && j - 1 >= 0) {
37+
sum += matrix[i + 1][j - 1]
38+
denominator++
39+
}
40+
if (j - 1 >= 0) {
41+
sum += matrix[i][j - 1]
42+
denominator++
43+
}
44+
if (i - 1 >= 0 && j - 1 >= 0) {
45+
sum += matrix[i - 1][j - 1]
46+
denominator++
47+
}
48+
if (i - 1 >= 0) {
49+
sum += matrix[i - 1][j]
50+
denominator++
51+
}
52+
if (i - 1 >= 0 && j + 1 < n) {
53+
sum += matrix[i - 1][j + 1]
54+
denominator++
55+
}
56+
result[i][j] = sum / denominator
57+
}
58+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
661\. Image Smoother
2+
3+
Easy
4+
5+
An **image smoother** is a filter of the size `3 x 3` that can be applied to each cell of an image by rounding down the average of the cell and the eight surrounding cells (i.e., the average of the nine cells in the blue smoother). If one or more of the surrounding cells of a cell is not present, we do not consider it in the average (i.e., the average of the four cells in the red smoother).
6+
7+
![](https://assets.leetcode.com/uploads/2021/05/03/smoother-grid.jpg)
8+
9+
Given an `m x n` integer matrix `img` representing the grayscale of an image, return _the image after applying the smoother on each cell of it_.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/05/03/smooth-grid.jpg)
14+
15+
**Input:** img = [[1,1,1],[1,0,1],[1,1,1]]
16+
17+
**Output:** [[0,0,0],[0,0,0],[0,0,0]]
18+
19+
**Explanation:**
20+
21+
For the points (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
22+
23+
For the points (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
24+
25+
For the point (1,1): floor(8/9) = floor(0.88888889) = 0
26+
27+
**Example 2:**
28+
29+
![](https://assets.leetcode.com/uploads/2021/05/03/smooth2-grid.jpg)
30+
31+
**Input:** img = [[100,200,100],[200,50,200],[100,200,100]]
32+
33+
**Output:** [[137,141,137],[141,138,141],[137,141,137]]
34+
35+
**Explanation:**
36+
37+
For the points (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137
38+
39+
For the points (0,1), (1,0), (1,2), (2,1): floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141
40+
41+
For the point (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.888889) = 138
42+
43+
**Constraints:**
44+
45+
* `m == img.length`
46+
* `n == img[i].length`
47+
* `1 <= m, n <= 200`
48+
* `0 <= img[i][j] <= 255`
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package g0601_0700.s0662_maximum_width_of_binary_tree
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
4+
// #2023_02_14_Time_189_ms_(75.00%)_Space_35.6_MB_(100.00%)
5+
6+
import com_github_leetcode.TreeNode
7+
import java.util.LinkedList
8+
import java.util.Objects
9+
import java.util.Queue
10+
11+
/*
12+
* Example:
13+
* var ti = TreeNode(5)
14+
* var v = ti.`val`
15+
* Definition for a binary tree node.
16+
* class TreeNode(var `val`: Int) {
17+
* var left: TreeNode? = null
18+
* var right: TreeNode? = null
19+
* }
20+
*/
21+
class Solution {
22+
internal class Pair(node: TreeNode, idx: Int) {
23+
var node: TreeNode
24+
var idx: Int
25+
26+
init {
27+
this.node = node
28+
this.idx = idx
29+
}
30+
}
31+
32+
fun widthOfBinaryTree(root: TreeNode): Int {
33+
val q: Queue<Pair> = LinkedList()
34+
q.add(Pair(root, 0))
35+
var res = 1
36+
while (!q.isEmpty()) {
37+
val qSize = q.size
38+
var lastIdx = 0
39+
var firstIdx = 0
40+
for (i in 0 until qSize) {
41+
val temp = q.poll()
42+
if (i == 0) {
43+
firstIdx = temp.idx
44+
}
45+
if (i == qSize - 1) {
46+
lastIdx = Objects.requireNonNull(temp).idx
47+
}
48+
if (Objects.requireNonNull(temp).node.left != null) {
49+
q.add(Pair(temp.node.left!!, 2 * temp.idx + 1))
50+
}
51+
if (temp.node.right != null) {
52+
q.add(Pair(temp.node.right!!, 2 * temp.idx + 2))
53+
}
54+
}
55+
res = (lastIdx - firstIdx + 1).coerceAtLeast(res)
56+
}
57+
return res
58+
}
59+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
662\. Maximum Width of Binary Tree
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, return _the **maximum width** of the given tree_.
6+
7+
The **maximum width** of a tree is the maximum **width** among all levels.
8+
9+
The **width** of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes that would be present in a complete binary tree extending down to that level are also counted into the length calculation.
10+
11+
It is **guaranteed** that the answer will in the range of a **32-bit** signed integer.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/05/03/width1-tree.jpg)
16+
17+
**Input:** root = [1,3,2,5,3,null,9]
18+
19+
**Output:** 4
20+
21+
**Explanation:** The maximum width exists in the third level with length 4 (5,3,null,9).
22+
23+
**Example 2:**
24+
25+
![](https://assets.leetcode.com/uploads/2022/03/14/maximum-width-of-binary-tree-v3.jpg)
26+
27+
**Input:** root = [1,3,2,5,null,null,9,6,null,7]
28+
29+
**Output:** 7
30+
31+
**Explanation:** The maximum width exists in the fourth level with length 7 (6,null,null,null,null,null,7).
32+
33+
**Example 3:**
34+
35+
![](https://assets.leetcode.com/uploads/2021/05/03/width3-tree.jpg)
36+
37+
**Input:** root = [1,3,2,5]
38+
39+
**Output:** 2
40+
41+
**Explanation:** The maximum width exists in the second level with length 2 (3,2).
42+
43+
**Constraints:**
44+
45+
* The number of nodes in the tree is in the range `[1, 3000]`.
46+
* `-100 <= Node.val <= 100`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0601_0700.s0664_strange_printer
2+
3+
// #Hard #String #Dynamic_Programming #2023_02_14_Time_196_ms_(100.00%)_Space_35.3_MB_(100.00%)
4+
5+
class Solution {
6+
fun strangePrinter(s: String): Int {
7+
if (s.isEmpty()) {
8+
return 0
9+
}
10+
val dp = Array(s.length) { IntArray(s.length) }
11+
for (i in s.length - 1 downTo 0) {
12+
for (j in i until s.length) {
13+
if (i == j) {
14+
dp[i][j] = 1
15+
} else if (s[i] == s[i + 1]) {
16+
dp[i][j] = dp[i + 1][j]
17+
} else {
18+
dp[i][j] = dp[i + 1][j] + 1
19+
for (k in i + 1..j) {
20+
if (s[k] == s[i]) {
21+
dp[i][j] = dp[i][j].coerceAtMost(dp[i + 1][k - 1] + dp[k][j])
22+
}
23+
}
24+
}
25+
}
26+
}
27+
return dp[0][s.length - 1]
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
664\. Strange Printer
2+
3+
Hard
4+
5+
There is a strange printer with the following two special properties:
6+
7+
* The printer can only print a sequence of **the same character** each time.
8+
* At each turn, the printer can print new characters starting from and ending at any place and will cover the original existing characters.
9+
10+
Given a string `s`, return _the minimum number of turns the printer needed to print it_.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "aaabbb"
15+
16+
**Output:** 2
17+
18+
**Explanation:** Print "aaa" first and then print "bbb".
19+
20+
**Example 2:**
21+
22+
**Input:** s = "aba"
23+
24+
**Output:** 2
25+
26+
**Explanation:** Print "aaa" first and then print "b" from the second place of the string, which will cover the existing character 'a'.
27+
28+
**Constraints:**
29+
30+
* `1 <= s.length <= 100`
31+
* `s` consists of lowercase English letters.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0601_0700.s0659_split_array_into_consecutive_subsequences
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 isPossible() {
10+
assertThat(Solution().isPossible(intArrayOf(1, 2, 3, 3, 4, 5)), equalTo(true))
11+
}
12+
13+
@Test
14+
fun isPossible2() {
15+
assertThat(Solution().isPossible(intArrayOf(1, 2, 3, 3, 4, 4, 5, 5)), equalTo(true))
16+
}
17+
}

0 commit comments

Comments
 (0)