Skip to content

Commit ae98082

Browse files
authored
Added tasks 665, 667, 668, 669
1 parent ed0cada commit ae98082

File tree

13 files changed

+314
-0
lines changed

13 files changed

+314
-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+
| 0669 |[Trim a Binary Search Tree](src/main/kotlin/g0601_0700/s0669_trim_a_binary_search_tree/Solution.kt)| Medium | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 195 | 100.00
1679+
| 0668 |[Kth Smallest Number in Multiplication Table](src/main/kotlin/g0601_0700/s0668_kth_smallest_number_in_multiplication_table/Solution.kt)| Hard | Math, Binary_Search | 151 | 100.00
1680+
| 0667 |[Beautiful Arrangement II](src/main/kotlin/g0601_0700/s0667_beautiful_arrangement_ii/Solution.kt)| Medium | Array, Math | 175 | 100.00
1681+
| 0665 |[Non-decreasing Array](src/main/kotlin/g0601_0700/s0665_non_decreasing_array/Solution.kt)| Medium | Array | 256 | 85.71
16781682
| 0664 |[Strange Printer](src/main/kotlin/g0601_0700/s0664_strange_printer/Solution.kt)| Hard | String, Dynamic_Programming | 196 | 100.00
16791683
| 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
16801684
| 0661 |[Image Smoother](src/main/kotlin/g0601_0700/s0661_image_smoother/Solution.kt)| Easy | Array, Matrix | 352 | 100.00
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0601_0700.s0665_non_decreasing_array
2+
3+
// #Medium #Array #2023_02_14_Time_256_ms_(85.71%)_Space_39.8_MB_(28.57%)
4+
5+
class Solution {
6+
fun checkPossibility(nums: IntArray): Boolean {
7+
val n = nums.size
8+
if (n <= 2) {
9+
return true
10+
}
11+
12+
var isModified = false
13+
for (i in 0..(n - 2)) {
14+
if (nums[i] <= nums[i + 1]) {
15+
continue
16+
}
17+
if (isModified) {
18+
return false
19+
}
20+
21+
when {
22+
i == 0 || nums[i - 1] <= nums[i + 1] -> {
23+
nums[i] = nums[i + 1]
24+
isModified = true
25+
}
26+
i == n - 2 || nums[i] <= nums[i + 2] -> {
27+
nums[i + 1] = nums[i]
28+
isModified = true
29+
}
30+
else -> {
31+
return false
32+
}
33+
}
34+
}
35+
return true
36+
}
37+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
665\. Non-decreasing Array
2+
3+
Medium
4+
5+
Given an array `nums` with `n` integers, your task is to check if it could become non-decreasing by modifying **at most one element**.
6+
7+
We define an array is non-decreasing if `nums[i] <= nums[i + 1]` holds for every `i` (**0-based**) such that (`0 <= i <= n - 2`).
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [4,2,3]
12+
13+
**Output:** true
14+
15+
**Explanation:** You could modify the first 4 to 1 to get a non-decreasing array.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [4,2,1]
20+
21+
**Output:** false
22+
23+
**Explanation:** You cannot get a non-decreasing array by modifying at most one element.
24+
25+
**Constraints:**
26+
27+
* `n == nums.length`
28+
* <code>1 <= n <= 10<sup>4</sup></code>
29+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0601_0700.s0667_beautiful_arrangement_ii
2+
3+
// #Medium #Array #Math #2023_02_14_Time_175_ms_(100.00%)_Space_36.1_MB_(100.00%)
4+
5+
class Solution {
6+
fun constructArray(n: Int, k: Int): IntArray {
7+
var k = k
8+
val res = IntArray(n)
9+
var left = 1
10+
var right = n
11+
for (i in 0 until n) {
12+
res[i] = if (k % 2 == 0) left++ else right--
13+
if (k > 1) {
14+
k--
15+
}
16+
}
17+
return res
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
667\. Beautiful Arrangement II
2+
3+
Medium
4+
5+
Given two integers `n` and `k`, construct a list `answer` that contains `n` different positive integers ranging from `1` to `n` and obeys the following requirement:
6+
7+
* Suppose this list is <code>answer = [a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub>, ... , a<sub>n</sub>]</code>, then the list <code>[|a<sub>1</sub> - a<sub>2</sub>|, |a<sub>2</sub> - a<sub>3</sub>|, |a<sub>3</sub> - a<sub>4</sub>|, ... , |a<sub>n-1</sub> - a<sub>n</sub>|]</code> has exactly `k` distinct integers.
8+
9+
Return _the list_ `answer`. If there multiple valid answers, return **any of them**.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 3, k = 1
14+
15+
**Output:** [1,2,3] Explanation: The [1,2,3] has three different positive integers ranging from 1 to 3, and the [1,1] has exactly 1 distinct integer: 1
16+
17+
**Example 2:**
18+
19+
**Input:** n = 3, k = 2
20+
21+
**Output:** [1,3,2] Explanation: The [1,3,2] has three different positive integers ranging from 1 to 3, and the [2,1] has exactly 2 distinct integers: 1 and 2.
22+
23+
**Constraints:**
24+
25+
* <code>1 <= k < n <= 10<sup>4</sup></code>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0601_0700.s0668_kth_smallest_number_in_multiplication_table
2+
3+
// #Hard #Math #Binary_Search #2023_02_14_Time_151_ms_(100.00%)_Space_33_MB_(50.00%)
4+
5+
class Solution {
6+
fun findKthNumber(m: Int, n: Int, k: Int): Int {
7+
var lo = 1
8+
var hi = m * n
9+
while (lo < hi) {
10+
val mid = lo + (hi - lo) / 2
11+
var col = n
12+
var row = 1
13+
var count = 0
14+
while (row <= m && col >= 1) {
15+
val `val` = row * col
16+
if (`val` > mid) {
17+
col--
18+
} else {
19+
count += col
20+
row++
21+
}
22+
}
23+
if (count < k) {
24+
lo = mid + 1
25+
} else {
26+
hi = mid
27+
}
28+
}
29+
return lo
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
668\. Kth Smallest Number in Multiplication Table
2+
3+
Hard
4+
5+
Nearly everyone has used the [Multiplication Table](https://en.wikipedia.org/wiki/Multiplication_table). The multiplication table of size `m x n` is an integer matrix `mat` where `mat[i][j] == i * j` (**1-indexed**).
6+
7+
Given three integers `m`, `n`, and `k`, return _the_ <code>k<sup>th</sup></code> _smallest element in the_ `m x n` _multiplication table_.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/05/02/multtable1-grid.jpg)
12+
13+
**Input:** m = 3, n = 3, k = 5
14+
15+
**Output:** 3
16+
17+
**Explanation:** The 5<sup>th</sup> smallest number is 3.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2021/05/02/multtable2-grid.jpg)
22+
23+
**Input:** m = 2, n = 3, k = 6
24+
25+
**Output:** 6
26+
27+
**Explanation:** The 6<sup>th</sup> smallest number is 6.
28+
29+
**Constraints:**
30+
31+
* <code>1 <= m, n <= 3 * 10<sup>4</sup></code>
32+
* `1 <= k <= m * n`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0601_0700.s0669_trim_a_binary_search_tree
2+
3+
// #Medium #Depth_First_Search #Tree #Binary_Tree #Binary_Search_Tree
4+
// #2023_02_14_Time_195_ms_(100.00%)_Space_36.3_MB_(100.00%)
5+
6+
import com_github_leetcode.TreeNode
7+
8+
/*
9+
* Example:
10+
* var ti = TreeNode(5)
11+
* var v = ti.`val`
12+
* Definition for a binary tree node.
13+
* class TreeNode(var `val`: Int) {
14+
* var left: TreeNode? = null
15+
* var right: TreeNode? = null
16+
* }
17+
*/
18+
class Solution {
19+
fun trimBST(root: TreeNode?, l: Int, r: Int): TreeNode? {
20+
if (root == null) {
21+
return root
22+
}
23+
if (root.`val` > r) {
24+
return trimBST(root.left, l, r)
25+
}
26+
if (root.`val` < l) {
27+
return trimBST(root.right, l, r)
28+
}
29+
root.left = trimBST(root.left, l, r)
30+
root.right = trimBST(root.right, l, r)
31+
return root
32+
}
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
669\. Trim a Binary Search Tree
2+
3+
Medium
4+
5+
Given the `root` of a binary search tree and the lowest and highest boundaries as `low` and `high`, trim the tree so that all its elements lies in `[low, high]`. Trimming the tree should **not** change the relative structure of the elements that will remain in the tree (i.e., any node's descendant should remain a descendant). It can be proven that there is a **unique answer**.
6+
7+
Return _the root of the trimmed binary search tree_. Note that the root may change depending on the given bounds.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/09/09/trim1.jpg)
12+
13+
**Input:** root = [1,0,2], low = 1, high = 2
14+
15+
**Output:** [1,null,2]
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2020/09/09/trim2.jpg)
20+
21+
**Input:** root = [3,0,4,null,2,null,null,1], low = 1, high = 3
22+
23+
**Output:** [3,2,null,1]
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
28+
* <code>0 <= Node.val <= 10<sup>4</sup></code>
29+
* The value of each node in the tree is **unique**.
30+
* `root` is guaranteed to be a valid binary search tree.
31+
* <code>0 <= low <= high <= 10<sup>4</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0601_0700.s0665_non_decreasing_array
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 checkPossibility() {
10+
assertThat(Solution().checkPossibility(intArrayOf(4, 2, 3)), equalTo(true))
11+
}
12+
13+
@Test
14+
fun checkPossibility2() {
15+
assertThat(Solution().checkPossibility(intArrayOf(4, 2, 1)), equalTo(false))
16+
}
17+
}

0 commit comments

Comments
 (0)