Skip to content

Commit 13ddf25

Browse files
authored
Added tasks 697, 698, 699, 700
1 parent f5982f9 commit 13ddf25

File tree

13 files changed

+380
-0
lines changed

13 files changed

+380
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
620620
| 0448 |[Find All Numbers Disappeared in an Array](src.save/main/kotlin/g0401_0500/s0448_find_all_numbers_disappeared_in_an_array/Solution.kt)| Easy | Array, Hash_Table | 394 | 100.00
621621
| 0442 |[Find All Duplicates in an Array](src.save/main/kotlin/g0401_0500/s0442_find_all_duplicates_in_an_array/Solution.kt)| Medium | Array, Hash_Table | 480 | 73.81
622622
| 0041 |[First Missing Positive](src.save/main/kotlin/g0001_0100/s0041_first_missing_positive/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 345 | 100.00
623+
| 0697 |[Degree of an Array](src/main/kotlin/g0601_0700/s0697_degree_of_an_array/Solution.kt)| Easy | Array, Hash_Table | 289 | 84.62
623624
| 0532 |[K-diff Pairs in an Array](src.save/main/kotlin/g0501_0600/s0532_k_diff_pairs_in_an_array/Solution.kt)| Medium | Array, Hash_Table, Sorting, Binary_Search, Two_Pointers | 230 | 84.62
624625
| 0456 |[132 Pattern](src.save/main/kotlin/g0401_0500/s0456_132_pattern/Solution.kt)| Medium | Array, Binary_Search, Stack, Ordered_Set, Monotonic_Stack | 434 | 100.00
625626
| 0239 |[Sliding Window Maximum](src.save/main/kotlin/g0201_0300/s0239_sliding_window_maximum/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue | 1059 | 86.14
@@ -855,6 +856,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
855856

856857
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
857858
|-|-|-|-|-|-
859+
| 0700 |[Search in a Binary Search Tree](src/main/kotlin/g0601_0700/s0700_search_in_a_binary_search_tree/Solution.kt)| Easy | Tree, Binary_Tree, Binary_Search_Tree | 251 | 88.31
858860

859861
#### Day 14 Tree
860862

@@ -1679,6 +1681,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16791681
| 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
16801682
| 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
16811683
| 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
1684+
| 0700 |[Search in a Binary Search Tree](src/main/kotlin/g0601_0700/s0700_search_in_a_binary_search_tree/Solution.kt)| Easy | Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_13_Tree | 251 | 88.31
1685+
| 0699 |[Falling Squares](src/main/kotlin/g0601_0700/s0699_falling_squares/Solution.kt)| Hard | Array, Ordered_Set, Segment_Tree | 293 | 100.00
1686+
| 0698 |[Partition to K Equal Sum Subsets](src/main/kotlin/g0601_0700/s0698_partition_to_k_equal_sum_subsets/Solution.kt)| Medium | Array, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask, Memoization | 191 | 100.00
1687+
| 0697 |[Degree of an Array](src/main/kotlin/g0601_0700/s0697_degree_of_an_array/Solution.kt)| Easy | Array, Hash_Table, Udemy_Arrays | 289 | 84.62
16821688
| 0696 |[Count Binary Substrings](src/main/kotlin/g0601_0700/s0696_count_binary_substrings/Solution.kt)| Easy | String, Two_Pointers | 222 | 100.00
16831689
| 0695 |[Max Area of Island](src/main/kotlin/g0601_0700/s0695_max_area_of_island/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_I_Day_7_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_2_Matrix_Related_Problems | 324 | 24.06
16841690
| 0693 |[Binary Number with Alternating Bits](src/main/kotlin/g0601_0700/s0693_binary_number_with_alternating_bits/Solution.kt)| Easy | Bit_Manipulation | 129 | 100.00
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0601_0700.s0697_degree_of_an_array
2+
3+
// #Easy #Array #Hash_Table #Udemy_Arrays #2023_02_22_Time_289_ms_(84.62%)_Space_58.4_MB_(19.23%)
4+
5+
class Solution {
6+
private class Value(var count: Int, var start: Int, var end: Int)
7+
8+
fun findShortestSubArray(nums: IntArray): Int {
9+
var max = 1
10+
val map: MutableMap<Int, Value> = HashMap()
11+
for (i in nums.indices) {
12+
val j = nums[i]
13+
if (map.containsKey(j)) {
14+
val v = map[j]
15+
v!!.count++
16+
max = Math.max(max, v.count)
17+
v.end = i
18+
} else {
19+
map[j] = Value(1, i, i)
20+
}
21+
}
22+
var min = Int.MAX_VALUE
23+
for (entry in map.entries.iterator()) {
24+
val v: Value = entry.value
25+
if (v.count == max) {
26+
min = min.coerceAtMost(v.end - v.start)
27+
}
28+
}
29+
return min + 1
30+
}
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
697\. Degree of an Array
2+
3+
Easy
4+
5+
Given a non-empty array of non-negative integers `nums`, the **degree** of this array is defined as the maximum frequency of any one of its elements.
6+
7+
Your task is to find the smallest possible length of a (contiguous) subarray of `nums`, that has the same degree as `nums`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,2,3,1]
12+
13+
**Output:** 2
14+
15+
**Explanation:** The input array has a degree of 2 because both elements 1 and 2 appear twice. Of the subarrays that have the same degree: [1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2] The shortest length is 2. So return 2.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [1,2,2,3,1,4,2]
20+
21+
**Output:** 6
22+
23+
**Explanation:** The degree is 3 because the element 2 is repeated 3 times. So [2,2,3,1,4,2] is the shortest subarray, therefore returning 6.
24+
25+
**Constraints:**
26+
27+
* `nums.length` will be between 1 and 50,000.
28+
* `nums[i]` will be an integer between 0 and 49,999.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g0601_0700.s0698_partition_to_k_equal_sum_subsets
2+
3+
// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask #Memoization
4+
// #2023_02_22_Time_191_ms_(100.00%)_Space_45.5_MB_(50.00%)
5+
6+
import java.util.Arrays
7+
8+
class Solution {
9+
fun canPartitionKSubsets(nums: IntArray, k: Int): Boolean {
10+
if (nums.isEmpty()) {
11+
return false
12+
}
13+
val n = nums.size
14+
var sum = 0
15+
for (num in nums) {
16+
sum += num
17+
}
18+
if (sum % k != 0) {
19+
return false
20+
}
21+
// sum of each subset = sum / k
22+
sum /= k
23+
val dp = IntArray(1 shl n)
24+
Arrays.fill(dp, -1)
25+
dp[0] = 0
26+
for (i in 0 until (1 shl n)) {
27+
if (dp[i] == -1) {
28+
continue
29+
}
30+
val rem = sum - dp[i] % sum
31+
for (j in 0 until n) {
32+
// bitmask
33+
val tmp = i or (1 shl j)
34+
// skip if the bit is already taken
35+
if (tmp != i) {
36+
// num too big for current subset
37+
if (nums[j] > rem) {
38+
break
39+
}
40+
// cumulative sum
41+
dp[tmp] = dp[i] + nums[j]
42+
}
43+
}
44+
}
45+
// true if total sum of all nums is the same
46+
return dp[(1 shl n) - 1] == k * sum
47+
}
48+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
698\. Partition to K Equal Sum Subsets
2+
3+
Medium
4+
5+
Given an integer array `nums` and an integer `k`, return `true` if it is possible to divide this array into `k` non-empty subsets whose sums are all equal.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [4,3,2,3,5,2,1], k = 4
10+
11+
**Output:** true
12+
13+
**Explanation:** It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [1,2,3,4], k = 3
18+
19+
**Output:** false
20+
21+
**Constraints:**
22+
23+
* `1 <= k <= nums.length <= 16`
24+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
25+
* The frequency of each element is in the range `[1, 4]`.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g0601_0700.s0699_falling_squares
2+
3+
// #Hard #Array #Ordered_Set #Segment_Tree #2023_02_22_Time_293_ms_(100.00%)_Space_48.8_MB_(50.00%)
4+
5+
import java.util.Collections
6+
import java.util.TreeSet
7+
8+
class Solution {
9+
fun fallingSquares(positions: Array<IntArray>): List<Int> {
10+
// Coordinate compression using TreeSet
11+
val unique: MutableSet<Int> = TreeSet()
12+
for (square in positions) {
13+
unique.add(square[0])
14+
unique.add(square[0] + square[1] - 1)
15+
}
16+
// converted the TreeSet to a List
17+
val sorted: List<Int> = ArrayList(unique)
18+
// Storing the max heights for compressed coordinates
19+
val heights = IntArray(sorted.size)
20+
// Our answer list
21+
val list: MutableList<Int> = ArrayList(positions.size)
22+
// Global Max
23+
var max = 0
24+
for (square in positions) {
25+
// coordinate compression lookup
26+
val x1 = Collections.binarySearch(sorted, square[0])
27+
val x2 = Collections.binarySearch(sorted, square[0] + square[1] - 1)
28+
// get the current max for the interval between x1 and x2
29+
var current = 0
30+
for (i in x1..x2) {
31+
current = current.coerceAtLeast(heights[i])
32+
}
33+
// add the new square on the top
34+
current += square[1]
35+
// update the interval with the new value
36+
for (i in x1..x2) {
37+
heights[i] = current
38+
}
39+
// recalculate the global max
40+
max = max.coerceAtLeast(current)
41+
list.add(max)
42+
}
43+
return list
44+
}
45+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
699\. Falling Squares
2+
3+
Hard
4+
5+
There are several squares being dropped onto the X-axis of a 2D plane.
6+
7+
You are given a 2D integer array `positions` where <code>positions[i] = [left<sub>i</sub>, sideLength<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> square with a side length of <code>sideLength<sub>i</sub></code> that is dropped with its left edge aligned with X-coordinate <code>left<sub>i</sub></code>.
8+
9+
Each square is dropped one at a time from a height above any landed squares. It then falls downward (negative Y direction) until it either lands **on the top side of another square** or **on the X-axis**. A square brushing the left/right side of another square does not count as landing on it. Once it lands, it freezes in place and cannot be moved.
10+
11+
After each square is dropped, you must record the **height of the current tallest stack of squares**.
12+
13+
Return _an integer array_ `ans` _where_ `ans[i]` _represents the height described above after dropping the_ <code>i<sup>th</sup></code> _square_.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/04/28/fallingsq1-plane.jpg)
18+
19+
**Input:** positions = [[1,2],[2,3],[6,1]]
20+
21+
**Output:** [2,5,5]
22+
23+
**Explanation:**
24+
25+
After the first drop, the tallest stack is square 1 with a height of 2.
26+
27+
After the second drop, the tallest stack is squares 1 and 2 with a height of 5.
28+
29+
After the third drop, the tallest stack is still squares 1 and 2 with a height of 5.
30+
31+
Thus, we return an answer of [2, 5, 5].
32+
33+
**Example 2:**
34+
35+
**Input:** positions = [[100,100],[200,100]]
36+
37+
**Output:** [100,100]
38+
39+
**Explanation:**
40+
41+
After the first drop, the tallest stack is square 1 with a height of 100.
42+
43+
After the second drop, the tallest stack is either square 1 or square 2, both with heights of 100.
44+
45+
Thus, we return an answer of [100, 100].
46+
47+
Note that square 2 only brushes the right side of square 1, which does not count as landing on it.
48+
49+
**Constraints:**
50+
51+
* `1 <= positions.length <= 1000`
52+
* <code>1 <= left<sub>i</sub> <= 10<sup>8</sup></code>
53+
* <code>1 <= sideLength<sub>i</sub> <= 10<sup>6</sup></code>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0601_0700.s0700_search_in_a_binary_search_tree
2+
3+
// #Easy #Tree #Binary_Tree #Binary_Search_Tree #Data_Structure_I_Day_13_Tree
4+
// #2023_02_22_Time_251_ms_(88.31%)_Space_51_MB_(7.79%)
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+
@Suppress("NAME_SHADOWING")
19+
class Solution {
20+
fun searchBST(root: TreeNode?, `val`: Int): TreeNode? {
21+
var root: TreeNode? = root
22+
while (root != null && root.`val` != `val`) {
23+
root = if (root.`val` > `val`) {
24+
root.left
25+
} else {
26+
root.right
27+
}
28+
}
29+
return root
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
700\. Search in a Binary Search Tree
2+
3+
Easy
4+
5+
You are given the `root` of a binary search tree (BST) and an integer `val`.
6+
7+
Find the node in the BST that the node's value equals `val` and return the subtree rooted with that node. If such a node does not exist, return `null`.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/01/12/tree1.jpg)
12+
13+
**Input:** root = [4,2,7,1,3], val = 2
14+
15+
**Output:** [2,1,3]
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/01/12/tree2.jpg)
20+
21+
**Input:** root = [4,2,7,1,3], val = 5
22+
23+
**Output:** []
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the tree is in the range `[1, 5000]`.
28+
* <code>1 <= Node.val <= 10<sup>7</sup></code>
29+
* `root` is a binary search tree.
30+
* <code>1 <= val <= 10<sup>7</sup></code>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0601_0700.s0697_degree_of_an_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 findShortestSubArray() {
10+
assertThat(Solution().findShortestSubArray(intArrayOf(1, 2, 2, 3, 1)), equalTo(2))
11+
}
12+
13+
@Test
14+
fun findShortestSubArray2() {
15+
assertThat(
16+
Solution().findShortestSubArray(intArrayOf(1, 2, 2, 3, 1, 4, 2)), equalTo(6)
17+
)
18+
}
19+
}

0 commit comments

Comments
 (0)