Skip to content

Commit d30505e

Browse files
authored
Added tasks 697-715
1 parent 12d8db0 commit d30505e

File tree

17 files changed

+1313
-0
lines changed

17 files changed

+1313
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9595
|-|-|-|-|-|-
9696
| 0438 |[Find All Anagrams in a String](src/main/kotlin/g0401_0500/s0438_find_all_anagrams_in_a_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window | 561 | 54.68
97+
| 0713 |[Subarray Product Less Than K](src/main/kotlin/g0701_0800/s0713_subarray_product_less_than_k)| Medium | Array, Sliding_Window | 336 | 92.11
9798

9899
#### Day 13
99100

@@ -138,6 +139,7 @@
138139

139140
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
140141
|-|-|-|-|-|-
142+
| 0707 |[Design Linked List](src/main/kotlin/g0701_0800/s0707_design_linked_list)| Medium | Design, Linked_List | 243 | 100.00
141143

142144
#### Day 20
143145

@@ -336,6 +338,7 @@
336338

337339
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
338340
|-|-|-|-|-|-
341+
| 0704 |[Binary Search](src/main/kotlin/g0701_0800/s0704_binary_search)| Easy | Array, Binary_Search | 261 | 77.91
339342
| 0278 |[First Bad Version](src/main/kotlin/g0201_0300/s0278_first_bad_version)| Easy | Binary_Search, Interactive | 349 | 76.86
340343

341344
#### Day 8 Binary Search Tree
@@ -565,6 +568,7 @@
565568

566569
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
567570
|-|-|-|-|-|-
571+
| 0704 |[Binary Search](src/main/kotlin/g0701_0800/s0704_binary_search)| Easy | Array, Binary_Search | 261 | 77.91
568572
| 0033 |[Search in Rotated Sorted Array](src/main/kotlin/g0001_0100/s0033_search_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search | 184 | 86.08
569573
| 0153 |[Find Minimum in Rotated Sorted Array](src/main/kotlin/g0101_0200/s0153_find_minimum_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Array, Binary_Search | 262 | 60.96
570574

@@ -588,7 +592,9 @@
588592
| 0448 |[Find All Numbers Disappeared in an Array](src/main/kotlin/g0401_0500/s0448_find_all_numbers_disappeared_in_an_array)| Easy | Array, Hash_Table | 394 | 100.00
589593
| 0442 |[Find All Duplicates in an Array](src/main/kotlin/g0401_0500/s0442_find_all_duplicates_in_an_array)| Medium | Array, Hash_Table | 480 | 73.81
590594
| 0041 |[First Missing Positive](src/main/kotlin/g0001_0100/s0041_first_missing_positive)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 345 | 100.00
595+
| 0697 |[Degree of an Array](src/main/kotlin/g0601_0700/s0697_degree_of_an_array)| Easy | Array, Hash_Table | 289 | 84.62
591596
| 0532 |[K-diff Pairs in an Array](src/main/kotlin/g0501_0600/s0532_k_diff_pairs_in_an_array)| Medium | Array, Hash_Table, Sorting, Binary_Search, Two_Pointers | 230 | 84.62
597+
| 0713 |[Subarray Product Less Than K](src/main/kotlin/g0701_0800/s0713_subarray_product_less_than_k)| Medium | Array, Sliding_Window | 336 | 92.11
592598
| 0456 |[132 Pattern](src/main/kotlin/g0401_0500/s0456_132_pattern)| Medium | Array, Binary_Search, Stack, Ordered_Set, Monotonic_Stack | 434 | 100.00
593599
| 0239 |[Sliding Window Maximum](src/main/kotlin/g0201_0300/s0239_sliding_window_maximum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue | 1059 | 86.14
594600

@@ -643,6 +649,7 @@
643649
| 0138 |[Copy List with Random Pointer](src/main/kotlin/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List | 274 | 80.58
644650
| 0025 |[Reverse Nodes in k-Group](src/main/kotlin/g0001_0100/s0025_reverse_nodes_in_k_group)| Hard | Top_100_Liked_Questions, Linked_List, Recursion | 194 | 87.72
645651
| 0146 |[LRU Cache](src/main/kotlin/g0101_0200/s0146_lru_cache)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List | 1116 | 97.93
652+
| 0707 |[Design Linked List](src/main/kotlin/g0701_0800/s0707_design_linked_list)| Medium | Design, Linked_List | 243 | 100.00
646653

647654
#### Udemy Tree Stack Queue
648655

@@ -660,6 +667,7 @@
660667
| 0111 |[Minimum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0111_minimum_depth_of_binary_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 525 | 90.51
661668
| 0104 |[Maximum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 236 | 83.39
662669
| 0110 |[Balanced Binary Tree](src/main/kotlin/g0101_0200/s0110_balanced_binary_tree)| Easy | Depth_First_Search, Tree, Binary_Tree | 310 | 63.63
670+
| 0701 |[Insert into a Binary Search Tree](src/main/kotlin/g0701_0800/s0701_insert_into_a_binary_search_tree)| Medium | Tree, Binary_Tree, Binary_Search_Tree | 311 | 79.03
663671
| 0297 |[Serialize and Deserialize Binary Tree](src/main/kotlin/g0201_0300/s0297_serialize_and_deserialize_binary_tree)| Hard | Top_Interview_Questions, String, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Design | 475 | 78.85
664672
| 0124 |[Binary Tree Maximum Path Sum](src/main/kotlin/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree | 331 | 74.42
665673
| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 330 | 41.38
@@ -823,6 +831,8 @@
823831

824832
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
825833
|-|-|-|-|-|-
834+
| 0700 |[Search in a Binary Search Tree](src/main/kotlin/g0601_0700/s0700_search_in_a_binary_search_tree)| Easy | Tree, Binary_Tree, Binary_Search_Tree | 251 | 88.31
835+
| 0701 |[Insert into a Binary Search Tree](src/main/kotlin/g0701_0800/s0701_insert_into_a_binary_search_tree)| Medium | Tree, Binary_Tree, Binary_Search_Tree | 311 | 79.03
826836

827837
#### Day 14 Tree
828838

@@ -848,6 +858,7 @@
848858
|-|-|-|-|-|-
849859
| 0075 |[Sort Colors](src/main/kotlin/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 198 | 85.66
850860
| 0056 |[Merge Intervals](src/main/kotlin/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting | 323 | 99.68
861+
| 0706 |[Design HashMap](src/main/kotlin/g0701_0800/s0706_design_hashmap)| Easy | Array, Hash_Table, Design, Linked_List, Hash_Function | 405 | 92.11
851862

852863
#### Day 3 Array
853864

@@ -919,6 +930,7 @@
919930
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
920931
|-|-|-|-|-|-
921932
| 0024 |[Swap Nodes in Pairs](src/main/kotlin/g0001_0100/s0024_swap_nodes_in_pairs)| Medium | Top_100_Liked_Questions, Linked_List, Recursion | 149 | 99.39
933+
| 0707 |[Design Linked List](src/main/kotlin/g0701_0800/s0707_design_linked_list)| Medium | Design, Linked_List | 243 | 100.00
922934

923935
#### Day 13 Linked List
924936

@@ -987,6 +999,7 @@
987999

9881000
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9891001
|-|-|-|-|-|-
1002+
| 0704 |[Binary Search](src/main/kotlin/g0701_0800/s0704_binary_search)| Easy | Array, Binary_Search | 261 | 77.91
9901003
| 0278 |[First Bad Version](src/main/kotlin/g0201_0300/s0278_first_bad_version)| Easy | Binary_Search, Interactive | 349 | 76.86
9911004
| 0035 |[Search Insert Position](src/main/kotlin/g0001_0100/s0035_search_insert_position)| Easy | Top_100_Liked_Questions, Array, Binary_Search | 267 | 50.32
9921005

@@ -1114,6 +1127,7 @@
11141127
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11151128
|-|-|-|-|-|-
11161129
| 0438 |[Find All Anagrams in a String](src/main/kotlin/g0401_0500/s0438_find_all_anagrams_in_a_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window | 561 | 54.68
1130+
| 0713 |[Subarray Product Less Than K](src/main/kotlin/g0701_0800/s0713_subarray_product_less_than_k)| Medium | Array, Sliding_Window | 336 | 92.11
11171131
| 0209 |[Minimum Size Subarray Sum](src/main/kotlin/g0201_0300/s0209_minimum_size_subarray_sum)| Medium | Array, Binary_Search, Prefix_Sum, Sliding_Window | 315 | 96.73
11181132

11191133
#### Day 6 Breadth First Search Depth First Search
@@ -1234,6 +1248,7 @@
12341248

12351249
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12361250
|-|-|-|-|-|-
1251+
| 0704 |[Binary Search](src/main/kotlin/g0701_0800/s0704_binary_search)| Easy | Array, Binary_Search | 261 | 77.91
12371252
| 0374 |[Guess Number Higher or Lower](src/main/kotlin/g0301_0400/s0374_guess_number_higher_or_lower)| Easy | Binary_Search, Interactive | 134 | 94.19
12381253

12391254
#### Day 2
@@ -1471,6 +1486,7 @@
14711486
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14721487
|-|-|-|-|-|-
14731488
| 0309 |[Best Time to Buy and Sell Stock with Cooldown](src/main/kotlin/g0301_0400/s0309_best_time_to_buy_and_sell_stock_with_cooldown)| Medium | Array, Dynamic_Programming | 272 | 73.33
1489+
| 0714 |[Best Time to Buy and Sell Stock with Transaction Fee](src/main/kotlin/g0701_0800/s0714_best_time_to_buy_and_sell_stock_with_transaction_fee)| Medium | Array, Dynamic_Programming, Greedy | 417 | 90.91
14741490

14751491
#### Day 9
14761492

@@ -1616,6 +1632,7 @@
16161632

16171633
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16181634
|-|-|-|-|-|-
1635+
| 0709 |[To Lower Case](src/main/kotlin/g0701_0800/s0709_to_lower_case)| Easy | String | 142 | 98.68
16191636

16201637
#### Day 10 Linked List and Tree
16211638

@@ -1647,6 +1664,22 @@
16471664
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
16481665
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16491666
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
1667+
| 0715 |[Range Module](src/main/kotlin/g0701_0800/s0715_range_module)| Hard | Design, Ordered_Set, Segment_Tree | 638 | 58.33
1668+
| 0714 |[Best Time to Buy and Sell Stock with Transaction Fee](src/main/kotlin/g0701_0800/s0714_best_time_to_buy_and_sell_stock_with_transaction_fee)| Medium | Array, Dynamic_Programming, Greedy, Dynamic_Programming_I_Day_8 | 417 | 90.91
1669+
| 0713 |[Subarray Product Less Than K](src/main/kotlin/g0701_0800/s0713_subarray_product_less_than_k)| Medium | Array, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Udemy_Arrays | 336 | 92.11
1670+
| 0712 |[Minimum ASCII Delete Sum for Two Strings](src/main/kotlin/g0701_0800/s0712_minimum_ascii_delete_sum_for_two_strings)| Medium | String, Dynamic_Programming | 176 | 100.00
1671+
| 0710 |[Random Pick with Blacklist](src/main/kotlin/g0701_0800/s0710_random_pick_with_blacklist)| Hard | Hash_Table, Math, Sorting, Binary_Search, Randomized | 632 | 100.00
1672+
| 0709 |[To Lower Case](src/main/kotlin/g0701_0800/s0709_to_lower_case)| Easy | String, Programming_Skills_I_Day_9_String | 142 | 98.68
1673+
| 0707 |[Design Linked List](src/main/kotlin/g0701_0800/s0707_design_linked_list)| Medium | Design, Linked_List, Data_Structure_II_Day_12_Linked_List, Programming_Skills_II_Day_19, Udemy_Linked_List | 243 | 100.00
1674+
| 0706 |[Design HashMap](src/main/kotlin/g0701_0800/s0706_design_hashmap)| Easy | Array, Hash_Table, Design, Linked_List, Hash_Function, Data_Structure_II_Day_2_Array | 405 | 92.11
1675+
| 0705 |[Design HashSet](src/main/kotlin/g0701_0800/s0705_design_hashset)| Easy | Array, Hash_Table, Design, Linked_List, Hash_Function | 385 | 75.61
1676+
| 0704 |[Binary Search](src/main/kotlin/g0701_0800/s0704_binary_search)| Easy | Array, Binary_Search, Algorithm_I_Day_1_Binary_Search, Binary_Search_I_Day_1, Level_1_Day_7_Binary_Search, Udemy_Binary_Search | 261 | 77.91
1677+
| 0703 |[Kth Largest Element in a Stream](src/main/kotlin/g0701_0800/s0703_kth_largest_element_in_a_stream)| Easy | Tree, Binary_Tree, Design, Heap_Priority_Queue, Binary_Search_Tree, Data_Stream | 286 | 95.45
1678+
| 0701 |[Insert into a Binary Search Tree](src/main/kotlin/g0701_0800/s0701_insert_into_a_binary_search_tree)| Medium | Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_13_Tree, Udemy_Tree_Stack_Queue | 311 | 79.03
1679+
| 0700 |[Search in a Binary Search Tree](src/main/kotlin/g0601_0700/s0700_search_in_a_binary_search_tree)| Easy | Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_13_Tree | 251 | 88.31
1680+
| 0699 |[Falling Squares](src/main/kotlin/g0601_0700/s0699_falling_squares)| Hard | Array, Ordered_Set, Segment_Tree | 293 | 100.00
1681+
| 0698 |[Partition to K Equal Sum Subsets](src/main/kotlin/g0601_0700/s0698_partition_to_k_equal_sum_subsets)| Medium | Array, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask, Memoization | 191 | 100.00
1682+
| 0697 |[Degree of an Array](src/main/kotlin/g0601_0700/s0697_degree_of_an_array)| Easy | Array, Hash_Table, Udemy_Arrays | 289 | 84.62
16501683
| 0696 |[Count Binary Substrings](src/main/kotlin/g0601_0700/s0696_count_binary_substrings)| Easy | String, Two_Pointers | 222 | 100.00
16511684
| 0695 |[Max Area of Island](src/main/kotlin/g0601_0700/s0695_max_area_of_island)| 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
16521685
| 0693 |[Binary Number with Alternating Bits](src/main/kotlin/g0601_0700/s0693_binary_number_with_alternating_bits)| Easy | Bit_Manipulation | 129 | 100.00
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
## 697\. Degree of an Array
5+
6+
Easy
7+
8+
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.
9+
10+
Your task is to find the smallest possible length of a (contiguous) subarray of `nums`, that has the same degree as `nums`.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1,2,2,3,1]
15+
16+
**Output:** 2
17+
18+
**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.
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [1,2,2,3,1,4,2]
23+
24+
**Output:** 6
25+
26+
**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.
27+
28+
**Constraints:**
29+
30+
* `nums.length` will be between 1 and 50,000.
31+
* `nums[i]` will be an integer between 0 and 49,999.
32+
33+
## Solution
34+
35+
```kotlin
36+
class Solution {
37+
private class Value(var count: Int, var start: Int, var end: Int)
38+
39+
fun findShortestSubArray(nums: IntArray): Int {
40+
var max = 1
41+
val map: MutableMap<Int, Value> = HashMap()
42+
for (i in nums.indices) {
43+
val j = nums[i]
44+
if (map.containsKey(j)) {
45+
val v = map[j]
46+
v!!.count++
47+
max = Math.max(max, v.count)
48+
v.end = i
49+
} else {
50+
map[j] = Value(1, i, i)
51+
}
52+
}
53+
var min = Int.MAX_VALUE
54+
for (entry in map.entries.iterator()) {
55+
val v: Value = entry.value
56+
if (v.count == max) {
57+
min = min.coerceAtMost(v.end - v.start)
58+
}
59+
}
60+
return min + 1
61+
}
62+
}
63+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
## 698\. Partition to K Equal Sum Subsets
5+
6+
Medium
7+
8+
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.
9+
10+
**Example 1:**
11+
12+
**Input:** nums = [4,3,2,3,5,2,1], k = 4
13+
14+
**Output:** true
15+
16+
**Explanation:** It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [1,2,3,4], k = 3
21+
22+
**Output:** false
23+
24+
**Constraints:**
25+
26+
* `1 <= k <= nums.length <= 16`
27+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
28+
* The frequency of each element is in the range `[1, 4]`.
29+
30+
## Solution
31+
32+
```kotlin
33+
import java.util.Arrays
34+
35+
class Solution {
36+
fun canPartitionKSubsets(nums: IntArray, k: Int): Boolean {
37+
if (nums.isEmpty()) {
38+
return false
39+
}
40+
val n = nums.size
41+
var sum = 0
42+
for (num in nums) {
43+
sum += num
44+
}
45+
if (sum % k != 0) {
46+
return false
47+
}
48+
// sum of each subset = sum / k
49+
sum /= k
50+
val dp = IntArray(1 shl n)
51+
Arrays.fill(dp, -1)
52+
dp[0] = 0
53+
for (i in 0 until (1 shl n)) {
54+
if (dp[i] == -1) {
55+
continue
56+
}
57+
val rem = sum - dp[i] % sum
58+
for (j in 0 until n) {
59+
// bitmask
60+
val tmp = i or (1 shl j)
61+
// skip if the bit is already taken
62+
if (tmp != i) {
63+
// num too big for current subset
64+
if (nums[j] > rem) {
65+
break
66+
}
67+
// cumulative sum
68+
dp[tmp] = dp[i] + nums[j]
69+
}
70+
}
71+
}
72+
// true if total sum of all nums is the same
73+
return dp[(1 shl n) - 1] == k * sum
74+
}
75+
}
76+
```

0 commit comments

Comments
 (0)