Skip to content

Commit fb26039

Browse files
authored
Added tasks 301-330.
1 parent d0ade85 commit fb26039

File tree

21 files changed

+1533
-0
lines changed

21 files changed

+1533
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178

179179
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
180180
|-|-|-|-|-|-
181+
| 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
181182

182183
#### Day 9
183184

@@ -216,6 +217,7 @@
216217

217218
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
218219
|-|-|-|-|-|-
220+
| 0304 |[Range Sum Query 2D - Immutable](src/main/kotlin/g0301_0400/s0304_range_sum_query_2d_immutable)| Medium | Array, Matrix, Design, Prefix_Sum | 1373 | 85.71
219221

220222
#### Day 15
221223

@@ -330,6 +332,7 @@
330332

331333
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
332334
|-|-|-|-|-|-
335+
| 0303 |[Range Sum Query - Immutable](src/main/kotlin/g0301_0400/s0303_range_sum_query_immutable)| Easy | Array, Design, Prefix_Sum | 472 | 63.64
333336

334337
### Programming Skills II
335338

@@ -409,6 +412,7 @@
409412

410413
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
411414
|-|-|-|-|-|-
415+
| 0304 |[Range Sum Query 2D - Immutable](src/main/kotlin/g0301_0400/s0304_range_sum_query_2d_immutable)| Medium | Array, Matrix, Design, Prefix_Sum | 1373 | 85.71
412416

413417
#### Day 14
414418

@@ -703,6 +707,7 @@
703707

704708
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
705709
|-|-|-|-|-|-
710+
| 0328 |[Odd Even Linked List](src/main/kotlin/g0301_0400/s0328_odd_even_linked_list)| Medium | Top_Interview_Questions, Linked_List | 216 | 86.96
706711
| 0148 |[Sort List](src/main/kotlin/g0101_0200/s0148_sort_list)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort | 820 | 61.70
707712

708713
#### Day 5 Greedy
@@ -892,6 +897,7 @@
892897

893898
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
894899
|-|-|-|-|-|-
900+
| 0304 |[Range Sum Query 2D - Immutable](src/main/kotlin/g0301_0400/s0304_range_sum_query_2d_immutable)| Medium | Array, Matrix, Design, Prefix_Sum | 1373 | 85.71
895901
| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix | 290 | 40.17
896902
| 0054 |[Spiral Matrix](src/main/kotlin/g0001_0100/s0054_spiral_matrix)| Medium | Top_Interview_Questions, Array, Matrix, Simulation | 224 | 62.50
897903
| 0048 |[Rotate Image](src/main/kotlin/g0001_0100/s0048_rotate_image)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix | 287 | 46.50
@@ -903,6 +909,7 @@
903909
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
904910
|-|-|-|-|-|-
905911
| 0114 |[Flatten Binary Tree to Linked List](src/main/kotlin/g0101_0200/s0114_flatten_binary_tree_to_linked_list)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List | 191 | 93.10
912+
| 0328 |[Odd Even Linked List](src/main/kotlin/g0301_0400/s0328_odd_even_linked_list)| Medium | Top_Interview_Questions, Linked_List | 216 | 86.96
906913
| 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list)| Medium | Two_Pointers, Linked_List | 193 | 92.16
907914
| 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
908915
| 0142 |[Linked List Cycle II](src/main/kotlin/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List | 192 | 63.39
@@ -1559,7 +1566,27 @@
15591566
| 0378 |[Kth Smallest Element in a Sorted Matrix](src/main/kotlin/g0301_0400/s0378_kth_smallest_element_in_a_sorted_matrix)| Medium | Top_Interview_Questions, Array, Sorting, Binary_Search, Matrix, Heap_Priority_Queue | 522 | 59.78
15601567
| 0347 |[Top K Frequent Elements](src/main/kotlin/g0301_0400/s0347_top_k_frequent_elements)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Data_Structure_II_Day_20_Heap_Priority_Queue | 268 | 99.74
15611568
| 0338 |[Counting Bits](src/main/kotlin/g0301_0400/s0338_counting_bits)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation | 186 | 99.26
1569+
| 0330 |[Patching Array](src/main/kotlin/g0301_0400/s0330_patching_array)| Hard | Array, Greedy | 201 | 100.00
1570+
| 0329 |[Longest Increasing Path in a Matrix](src/main/kotlin/g0301_0400/s0329_longest_increasing_path_in_a_matrix)| Hard | Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Breadth_First_Search, Graph, Memoization, Topological_Sort | 322 | 92.65
1571+
| 0328 |[Odd Even Linked List](src/main/kotlin/g0301_0400/s0328_odd_even_linked_list)| Medium | Top_Interview_Questions, Linked_List, Level_2_Day_4_Linked_List, Udemy_Linked_List | 216 | 86.96
1572+
| 0327 |[Count of Range Sum](src/main/kotlin/g0301_0400/s0327_count_of_range_sum)| Hard | Array, Binary_Search, Ordered_Set, Divide_and_Conquer, Segment_Tree, Binary_Indexed_Tree, Merge_Sort | 638 | 100.00
1573+
| 0326 |[Wiggle Sort II](src/main/kotlin/g0301_0400/s0326_power_of_three)| Easy | Top_Interview_Questions, Math, Recursion | 413 | 76.12
1574+
| 0324 |[Wiggle Sort II](src/main/kotlin/g0301_0400/s0324_wiggle_sort_ii)| Medium | Top_Interview_Questions, Array, Sorting, Divide_and_Conquer, Quickselect | 545 | 57.14
15621575
| 0322 |[Coin Change](src/main/kotlin/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming | 332 | 50.68
1576+
| 0321 |[Create Maximum Number](src/main/kotlin/g0301_0400/s0321_create_maximum_number)| Hard | Greedy, Stack, Monotonic_Stack | 209 | 100.00
1577+
| 0319 |[Bulb Switcher](src/main/kotlin/g0301_0400/s0319_bulb_switcher)| Medium | Math, Brainteaser | 214 | 77.78
1578+
| 0318 |[Maximum Product of Word Lengths](src/main/kotlin/g0301_0400/s0318_maximum_product_of_word_lengths)| Medium | Array, String, Bit_Manipulation | 477 | 100.00
1579+
| 0316 |[Remove Duplicate Letters](src/main/kotlin/g0301_0400/s0316_remove_duplicate_letters)| Medium | String, Greedy, Stack, Monotonic_Stack | 291 | 81.82
1580+
| 0315 |[Count of Smaller Numbers After Self](src/main/kotlin/g0301_0400/s0315_count_of_smaller_numbers_after_self)| Hard | Top_Interview_Questions, Array, Binary_Search, Ordered_Set, Divide_and_Conquer, Segment_Tree, Binary_Indexed_Tree, Merge_Sort | 1282 | 88.46
1581+
| 0313 |[Super Ugly Number](src/main/kotlin/g0301_0400/s0313_super_ugly_number)| Medium | Array, Dynamic_Programming, Math | 330 | 100.00
1582+
| 0312 |[Burst Balloons](src/main/kotlin/g0301_0400/s0312_burst_balloons)| Hard | Array, Dynamic_Programming | 210 | 100.00
1583+
| 0310 |[Minimum Height Trees](src/main/kotlin/g0301_0400/s0310_minimum_height_trees)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 521 | 97.56
1584+
| 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, Dynamic_Programming_I_Day_8 | 272 | 73.33
1585+
| 0307 |[Range Sum Query - Mutable](src/main/kotlin/g0301_0400/s0307_range_sum_query_mutable)| Medium | Array, Design, Segment_Tree, Binary_Indexed_Tree | 1729 | 78.79
1586+
| 0306 |[Additive Number](src/main/kotlin/g0301_0400/s0306_additive_number)| Medium | String, Backtracking | 289 | 22.22
1587+
| 0304 |[Range Sum Query 2D - Immutable](src/main/kotlin/g0301_0400/s0304_range_sum_query_2d_immutable)| Medium | Array, Matrix, Design, Prefix_Sum, Dynamic_Programming_I_Day_14, Programming_Skills_II_Day_13, Udemy_2D_Arrays/Matrix | 1373 | 85.71
1588+
| 0303 |[Range Sum Query - Immutable](src/main/kotlin/g0301_0400/s0303_range_sum_query_immutable)| Easy | Array, Design, Prefix_Sum, Programming_Skills_I_Day_12_Class_and_Object | 472 | 63.64
1589+
| 0301 |[Remove Invalid Parentheses](src/main/kotlin/g0301_0400/s0301_remove_invalid_parentheses)| Hard | String, Breadth_First_Search, Backtracking | 312 | 100.00
15631590
| 0300 |[Longest Increasing Subsequence](src/main/kotlin/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Algorithm_II_Day_16_Dynamic_Programming, Binary_Search_II_Day_3, Dynamic_Programming_I_Day_18, Udemy_Dynamic_Programming | 318 | 82.28
15641591
| 0299 |[Bulls and Cows](src/main/kotlin/g0201_0300/s0299_bulls_and_cows)| Medium | String, Hash_Table, Counting, Level_1_Day_13_Hashmap | 254 | 84.82
15651592
| 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, Data_Structure_II_Day_18_Tree, Udemy_Tree_Stack_Queue | 475 | 78.85
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
## 301\. Remove Invalid Parentheses
5+
6+
Hard
7+
8+
Given a string `s` that contains parentheses and letters, remove the minimum number of invalid parentheses to make the input string valid.
9+
10+
Return _all the possible results_. You may return the answer in **any order**.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "()())()"
15+
16+
**Output:** ["(())()","()()()"]
17+
18+
**Example 2:**
19+
20+
**Input:** s = "(a)())()"
21+
22+
**Output:** ["(a())()","(a)()()"]
23+
24+
**Example 3:**
25+
26+
**Input:** s = ")("
27+
28+
**Output:** [""]
29+
30+
**Constraints:**
31+
32+
* `1 <= s.length <= 25`
33+
* `s` consists of lowercase English letters and parentheses `'('` and `')'`.
34+
* There will be at most `20` parentheses in `s`.
35+
36+
## Solution
37+
38+
```kotlin
39+
@Suppress("NAME_SHADOWING")
40+
class Solution {
41+
fun removeInvalidParentheses(s: String): List<String> {
42+
val res: MutableList<String> = ArrayList()
43+
// reversed+inverted
44+
val ri = false
45+
dfs(s, 0, 0, res, ri)
46+
return res
47+
}
48+
49+
// BASIC IDEA: find prefix w/ extra ")".
50+
// THEN use for loop to delete ")"s inside prefix, making recursive calls on the ENTIRE STRING
51+
// b/c you don't know where the next ")" will be deleted.
52+
private fun dfs(s: String, deletionSearch: Int, stackSearch: Int, res: MutableList<String>, ri: Boolean) {
53+
// functions imilarly to LC20. Valid Parenthesis, -1 for ")" and +1 for "("
54+
var s = s
55+
var deletionSearch = deletionSearch
56+
var stack = 0
57+
// see recursive call for explanation
58+
var p = stackSearch
59+
while (p < s.length && stack >= 0) {
60+
if (s[p] == ')') {
61+
stack--
62+
}
63+
if (s[p] == '(') {
64+
stack++
65+
}
66+
p++
67+
}
68+
if (stack < 0) {
69+
// p already goes beyond the prefix by +1
70+
val prefix = s.substring(0, p)
71+
// remove extra ")" from prefix
72+
for (i in deletionSearch until prefix.length) {
73+
// find last ")" in ")))...)" to avoid duplicates
74+
if (s[i] == ')' && (i == prefix.length - 1 || s[i + 1] != ')')) {
75+
// remove s.charAt(i) and recurse
76+
// NOTE: p-1 b/c after you make a deletion, you know that the prefix is valid,
77+
// so there's no point in recounting ")"
78+
// NOTE: p-1 is the start index for COUNTING ")" in the recursive call, not for
79+
// DELETIONS.
80+
// Think of the DELETION index as SEPARATE from the COUNTING/STACK index.
81+
dfs(s.substring(0, i) + s.substring(i + 1), deletionSearch, p - 1, res, ri)
82+
// for next iteration, can only search BEYOND i in recursive calls for the ")"
83+
// to delete
84+
deletionSearch = i + 1
85+
}
86+
}
87+
} else {
88+
// no extra ")" found
89+
// repeat for "("
90+
if (!ri) {
91+
// reverse + invert
92+
s = reverseInvert(s)
93+
// call again
94+
dfs(s, 0, 0, res, true)
95+
} else {
96+
// done with both ")" and "("
97+
// revert to original arr
98+
s = reverseInvert(s)
99+
res.add(s)
100+
}
101+
}
102+
}
103+
104+
// reverses and inverts to accomplish r->l scan
105+
private fun reverseInvert(s: String): String {
106+
val sb = StringBuilder()
107+
// invert
108+
for (c in s.toCharArray()) {
109+
if (c == '(') {
110+
sb.append(')')
111+
} else if (c == ')') {
112+
sb.append('(')
113+
} else {
114+
sb.append(c)
115+
}
116+
}
117+
// reverse
118+
return sb.reverse().toString()
119+
}
120+
}
121+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
## 303\. Range Sum Query - Immutable
5+
6+
Easy
7+
8+
Given an integer array `nums`, handle multiple queries of the following type:
9+
10+
1. Calculate the **sum** of the elements of `nums` between indices `left` and `right` **inclusive** where `left <= right`.
11+
12+
Implement the `NumArray` class:
13+
14+
* `NumArray(int[] nums)` Initializes the object with the integer array `nums`.
15+
* `int sumRange(int left, int right)` Returns the **sum** of the elements of `nums` between indices `left` and `right` **inclusive** (i.e. `nums[left] + nums[left + 1] + ... + nums[right]`).
16+
17+
**Example 1:**
18+
19+
**Input** ["NumArray", "sumRange", "sumRange", "sumRange"] [[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
20+
21+
**Output:** [null, 1, -1, -3]
22+
23+
**Explanation:** NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]); numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1 numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1 numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3
24+
25+
**Constraints:**
26+
27+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
28+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
29+
* `0 <= left <= right < nums.length`
30+
* At most <code>10<sup>4</sup></code> calls will be made to `sumRange`.
31+
32+
## Solution
33+
34+
```kotlin
35+
class NumArray(nums: IntArray) {
36+
private val sums: IntArray
37+
38+
init {
39+
sums = IntArray(nums.size)
40+
for (i in nums.indices) {
41+
if (i == 0) {
42+
sums[i] = nums[i]
43+
} else {
44+
sums[i] = sums[i - 1] + nums[i]
45+
}
46+
}
47+
}
48+
49+
fun sumRange(i: Int, j: Int): Int {
50+
return if (i == 0) {
51+
sums[j]
52+
} else sums[j] - sums[i - 1]
53+
}
54+
}
55+
56+
/*
57+
* Your NumArray object will be instantiated and called as such:
58+
* var obj = NumArray(nums)
59+
* var param_1 = obj.sumRange(left,right)
60+
*/
61+
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
## 304\. Range Sum Query 2D - Immutable
5+
6+
Medium
7+
8+
Given a 2D matrix `matrix`, handle multiple queries of the following type:
9+
10+
* Calculate the **sum** of the elements of `matrix` inside the rectangle defined by its **upper left corner** `(row1, col1)` and **lower right corner** `(row2, col2)`.
11+
12+
Implement the NumMatrix class:
13+
14+
* `NumMatrix(int[][] matrix)` Initializes the object with the integer matrix `matrix`.
15+
* `int sumRegion(int row1, int col1, int row2, int col2)` Returns the **sum** of the elements of `matrix` inside the rectangle defined by its **upper left corner** `(row1, col1)` and **lower right corner** `(row2, col2)`.
16+
17+
**Example 1:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/03/14/sum-grid.jpg)
20+
21+
**Input**
22+
23+
["NumMatrix", "sumRegion", "sumRegion", "sumRegion"]
24+
[[[[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]], [2, 1, 4, 3], [1, 1, 2, 2], [1, 2, 2, 4]]
25+
26+
**Output:** [null, 8, 11, 12]
27+
28+
**Explanation:**
29+
30+
NumMatrix numMatrix = new NumMatrix([[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]);
31+
numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle)
32+
numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle)
33+
numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)
34+
35+
**Constraints:**
36+
37+
* `m == matrix.length`
38+
* `n == matrix[i].length`
39+
* `1 <= m, n <= 200`
40+
* <code>-10<sup>5</sup> <= matrix[i][j] <= 10<sup>5</sup></code>
41+
* `0 <= row1 <= row2 < m`
42+
* `0 <= col1 <= col2 < n`
43+
* At most <code>10<sup>4</sup></code> calls will be made to `sumRegion`.
44+
45+
## Solution
46+
47+
```kotlin
48+
class NumMatrix(matrix: Array<IntArray>) {
49+
50+
private val M = matrix.size
51+
private val N = if (M > 0) matrix[0].size else 0
52+
53+
var array = Array<IntArray> (M + 1) { IntArray(N + 1) }
54+
55+
init {
56+
for (i in 1..M) {
57+
for (j in 1..N) {
58+
array[i][j] = matrix[i - 1][j - 1] + array[i][j - 1] + array[i - 1][j] - array[i - 1][j - 1]
59+
}
60+
}
61+
}
62+
63+
fun sumRegion(row1: Int, col1: Int, row2: Int, col2: Int): Int {
64+
return array[row2 + 1][col2 + 1] - array[row2 + 1][col1] - array[row1][col2 + 1] + array[row1][col1]
65+
}
66+
}
67+
68+
/*
69+
* Your NumMatrix object will be instantiated and called as such:
70+
* var obj = NumMatrix(matrix)
71+
* var param_1 = obj.sumRegion(row1,col1,row2,col2)
72+
*/
73+
```

0 commit comments

Comments
 (0)