Skip to content

Commit 9a68863

Browse files
authored
Added tasks 712, 713, 714, 715
1 parent 357f009 commit 9a68863

File tree

13 files changed

+359
-0
lines changed

13 files changed

+359
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
126126
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
127127
|-|-|-|-|-|-
128128
| 0438 |[Find All Anagrams in a String](src.save/main/kotlin/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window | 561 | 54.68
129+
| 0713 |[Subarray Product Less Than K](src/main/kotlin/g0701_0800/s0713_subarray_product_less_than_k/Solution.kt)| Medium | Array, Sliding_Window | 336 | 92.11
129130

130131
#### Day 13
131132

@@ -625,6 +626,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
625626
| 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
626627
| 0697 |[Degree of an Array](src/main/kotlin/g0601_0700/s0697_degree_of_an_array/Solution.kt)| Easy | Array, Hash_Table | 289 | 84.62
627628
| 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
629+
| 0713 |[Subarray Product Less Than K](src/main/kotlin/g0701_0800/s0713_subarray_product_less_than_k/Solution.kt)| Medium | Array, Sliding_Window | 336 | 92.11
628630
| 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
629631
| 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
630632

@@ -1157,6 +1159,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
11571159
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11581160
|-|-|-|-|-|-
11591161
| 0438 |[Find All Anagrams in a String](src.save/main/kotlin/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window | 561 | 54.68
1162+
| 0713 |[Subarray Product Less Than K](src/main/kotlin/g0701_0800/s0713_subarray_product_less_than_k/Solution.kt)| Medium | Array, Sliding_Window | 336 | 92.11
11601163
| 0209 |[Minimum Size Subarray Sum](src.save/main/kotlin/g0201_0300/s0209_minimum_size_subarray_sum/Solution.kt)| Medium | Array, Binary_Search, Prefix_Sum, Sliding_Window | 315 | 96.73
11611164

11621165
#### Day 6 Breadth First Search Depth First Search
@@ -1515,6 +1518,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
15151518
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15161519
|-|-|-|-|-|-
15171520
| 0309 |[Best Time to Buy and Sell Stock with Cooldown](src.save/main/kotlin/g0301_0400/s0309_best_time_to_buy_and_sell_stock_with_cooldown/Solution.kt)| Medium | Array, Dynamic_Programming | 272 | 73.33
1521+
| 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/Solution.kt)| Medium | Array, Dynamic_Programming, Greedy | 417 | 90.91
15181522

15191523
#### Day 9
15201524

@@ -1692,6 +1696,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16921696
| 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
16931697
| 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
16941698
| 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
1699+
| 0715 |[Range Module](src/main/kotlin/g0701_0800/s0715_range_module/RangeModule.kt)| Hard | Design, Ordered_Set, Segment_Tree | 638 | 58.33
1700+
| 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/Solution.kt)| Medium | Array, Dynamic_Programming, Greedy, Dynamic_Programming_I_Day_8 | 417 | 90.91
1701+
| 0713 |[Subarray Product Less Than K](src/main/kotlin/g0701_0800/s0713_subarray_product_less_than_k/Solution.kt)| Medium | Array, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Udemy_Arrays | 336 | 92.11
1702+
| 0712 |[Minimum ASCII Delete Sum for Two Strings](src/main/kotlin/g0701_0800/s0712_minimum_ascii_delete_sum_for_two_strings/Solution.kt)| Medium | String, Dynamic_Programming | 176 | 100.00
16951703
| 0710 |[Random Pick with Blacklist](src/main/kotlin/g0701_0800/s0710_random_pick_with_blacklist/Solution.kt)| Hard | Hash_Table, Math, Sorting, Binary_Search, Randomized | 632 | 100.00
16961704
| 0709 |[To Lower Case](src/main/kotlin/g0701_0800/s0709_to_lower_case/Solution.kt)| Easy | String, Programming_Skills_I_Day_9_String | 142 | 98.68
16971705
| 0707 |[Design Linked List](src/main/kotlin/g0701_0800/s0707_design_linked_list/MyLinkedList.kt)| Medium | Design, Linked_List, Data_Structure_II_Day_12_Linked_List, Programming_Skills_II_Day_19, Udemy_Linked_List | 243 | 100.00
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0701_0800.s0712_minimum_ascii_delete_sum_for_two_strings
2+
3+
// #Medium #String #Dynamic_Programming #2023_02_25_Time_176_ms_(100.00%)_Space_36.9_MB_(58.33%)
4+
5+
class Solution {
6+
fun minimumDeleteSum(s1: String, s2: String): Int {
7+
val len1 = s1.length
8+
val len2 = s2.length
9+
val dp = Array(len1 + 1) { IntArray(len2 + 1) }
10+
var c1: Char
11+
var c2: Char
12+
for (i in 1 until len1 + 1) {
13+
dp[i][0] = dp[i - 1][0] + s1[i - 1].code
14+
}
15+
for (j in 1 until len2 + 1) {
16+
dp[0][j] = dp[0][j - 1] + s2[j - 1].code
17+
}
18+
for (i in 1 until len1 + 1) {
19+
c1 = s1[i - 1]
20+
for (j in 1 until len2 + 1) {
21+
c2 = s2[j - 1]
22+
if (c1 == c2) {
23+
dp[i][j] = dp[i - 1][j - 1]
24+
} else {
25+
dp[i][j] = (dp[i - 1][j] + c1.code).coerceAtMost(dp[i][j - 1] + c2.code)
26+
}
27+
}
28+
}
29+
return dp[len1][len2]
30+
}
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
712\. Minimum ASCII Delete Sum for Two Strings
2+
3+
Medium
4+
5+
Given two strings `s1` and `s2`, return _the lowest **ASCII** sum of deleted characters to make two strings equal_.
6+
7+
**Example 1:**
8+
9+
**Input:** s1 = "sea", s2 = "eat"
10+
11+
**Output:** 231
12+
13+
**Explanation:** Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum. Deleting "t" from "eat" adds 116 to the sum. At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.
14+
15+
**Example 2:**
16+
17+
**Input:** s1 = "delete", s2 = "leet"
18+
19+
**Output:** 403
20+
21+
**Explanation:** Deleting "dee" from "delete" to turn the string into "let", adds 100[d] + 101[e] + 101[e] to the sum. Deleting "e" from "leet" adds 101[e] to the sum. At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403. If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.
22+
23+
**Constraints:**
24+
25+
* `1 <= s1.length, s2.length <= 1000`
26+
* `s1` and `s2` consist of lowercase English letters.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0701_0800.s0713_subarray_product_less_than_k
2+
3+
// #Medium #Array #Sliding_Window #Algorithm_II_Day_5_Sliding_Window #Programming_Skills_II_Day_12
4+
// #Udemy_Arrays #2023_02_25_Time_336_ms_(92.11%)_Space_42_MB_(94.74%)
5+
6+
class Solution {
7+
fun numSubarrayProductLessThanK(nums: IntArray, k: Int): Int {
8+
var p = 1
9+
var j = 0
10+
var ans = 0
11+
for (i in nums.indices) {
12+
p *= nums[i]
13+
while (p >= k && j < i) {
14+
p /= nums[j]
15+
j++
16+
}
17+
ans += if (p < k) i - j + 1 else 0
18+
}
19+
return ans
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
713\. Subarray Product Less Than K
2+
3+
Medium
4+
5+
Given an array of integers `nums` and an integer `k`, return _the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than_ `k`.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [10,5,2,6], k = 100
10+
11+
**Output:** 8
12+
13+
**Explanation:** The 8 subarrays that have product less than 100 are: [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6] Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [1,2,3], k = 0
18+
19+
**Output:** 0
20+
21+
**Constraints:**
22+
23+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
24+
* `1 <= nums[i] <= 1000`
25+
* <code>0 <= k <= 10<sup>6</sup></code>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g0701_0800.s0714_best_time_to_buy_and_sell_stock_with_transaction_fee
2+
3+
// #Medium #Array #Dynamic_Programming #Greedy #Dynamic_Programming_I_Day_8
4+
// #2023_02_25_Time_417_ms_(90.91%)_Space_46.1_MB_(75.00%)
5+
6+
class Solution {
7+
fun maxProfit(prices: IntArray, fee: Int): Int {
8+
var cash = 0
9+
var hold = -prices[0]
10+
for (i in 1 until prices.size) {
11+
cash = cash.coerceAtLeast(hold + prices[i] - fee)
12+
hold = hold.coerceAtLeast(cash - prices[i])
13+
}
14+
return cash
15+
}
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
714\. Best Time to Buy and Sell Stock with Transaction Fee
2+
3+
Medium
4+
5+
You are given an array `prices` where `prices[i]` is the price of a given stock on the <code>i<sup>th</sup></code> day, and an integer `fee` representing a transaction fee.
6+
7+
Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.
8+
9+
**Note:** You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
10+
11+
**Example 1:**
12+
13+
**Input:** prices = [1,3,2,8,4,9], fee = 2
14+
15+
**Output:** 8
16+
17+
**Explanation:** The maximum profit can be achieved by:
18+
19+
- Buying at prices[0] = 1
20+
21+
- Selling at prices[3] = 8
22+
23+
- Buying at prices[4] = 4
24+
25+
- Selling at prices[5] = 9
26+
27+
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
28+
29+
**Example 2:**
30+
31+
**Input:** prices = [1,3,7,5,10,3], fee = 3
32+
33+
**Output:** 6
34+
35+
**Constraints:**
36+
37+
* <code>1 <= prices.length <= 5 * 10<sup>4</sup></code>
38+
* <code>1 <= prices[i] < 5 * 10<sup>4</sup></code>
39+
* <code>0 <= fee < 5 * 10<sup>4</sup></code>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package g0701_0800.s0715_range_module
2+
3+
// #Hard #Design #Ordered_Set #Segment_Tree #2023_02_25_Time_638_ms_(58.33%)_Space_60.4_MB_(66.67%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class RangeModule {
7+
private val head: Interval = Interval(0, 0)
8+
9+
fun addRange(left: Int, right: Int) {
10+
var left = left
11+
var right = right
12+
var pos: Interval? = head
13+
while (pos!!.next != null && pos.next!!.end < left) {
14+
pos = pos.next
15+
}
16+
while (pos.next != null && pos.next!!.start <= right) {
17+
left = pos.next!!.start.coerceAtMost(left)
18+
right = pos.next!!.end.coerceAtLeast(right)
19+
removeNext(pos)
20+
}
21+
insert(pos, Interval(left, right))
22+
}
23+
24+
fun queryRange(left: Int, right: Int): Boolean {
25+
var pos: Interval? = head
26+
while (pos != null) {
27+
if (left >= pos.start && right <= pos.end) {
28+
return true
29+
}
30+
pos = pos.next
31+
}
32+
return false
33+
}
34+
35+
fun removeRange(left: Int, right: Int) {
36+
var pos: Interval? = head
37+
while (pos!!.next != null && pos.next!!.end <= left) {
38+
pos = pos.next
39+
}
40+
var prev = pos
41+
var curr = pos.next
42+
while (curr != null && curr.start < right) {
43+
if (curr.start < left) {
44+
insert(prev, Interval(curr.start, left))
45+
curr.start = left
46+
prev = prev!!.next
47+
curr = prev!!.next
48+
continue
49+
}
50+
if (right >= curr.end) {
51+
removeNext(prev)
52+
curr = prev!!.next
53+
} else {
54+
curr.start = right
55+
curr = curr.next
56+
}
57+
}
58+
}
59+
60+
private fun insert(curr: Interval?, next: Interval) {
61+
next.next = curr!!.next
62+
curr.next = next
63+
}
64+
65+
private fun removeNext(curr: Interval?) {
66+
val del = curr!!.next
67+
if (del != null) {
68+
curr.next = del.next
69+
del.next = null
70+
}
71+
}
72+
73+
internal class Interval(var start: Int, var end: Int) {
74+
var next: Interval? = null
75+
}
76+
}
77+
78+
/*
79+
* Your RangeModule object will be instantiated and called as such:
80+
* var obj = RangeModule()
81+
* obj.addRange(left,right)
82+
* var param_2 = obj.queryRange(left,right)
83+
* obj.removeRange(left,right)
84+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
715\. Range Module
2+
3+
Hard
4+
5+
A Range Module is a module that tracks ranges of numbers. Design a data structure to track the ranges represented as **half-open intervals** and query about them.
6+
7+
A **half-open interval** `[left, right)` denotes all the real numbers `x` where `left <= x < right`.
8+
9+
Implement the `RangeModule` class:
10+
11+
* `RangeModule()` Initializes the object of the data structure.
12+
* `void addRange(int left, int right)` Adds the **half-open interval** `[left, right)`, tracking every real number in that interval. Adding an interval that partially overlaps with currently tracked numbers should add any numbers in the interval `[left, right)` that are not already tracked.
13+
* `boolean queryRange(int left, int right)` Returns `true` if every real number in the interval `[left, right)` is currently being tracked, and `false` otherwise.
14+
* `void removeRange(int left, int right)` Stops tracking every real number currently being tracked in the **half-open interval** `[left, right)`.
15+
16+
**Example 1:**
17+
18+
**Input**
19+
20+
["RangeModule", "addRange", "removeRange", "queryRange", "queryRange", "queryRange"]
21+
22+
[[], [10, 20], [14, 16], [10, 14], [13, 15], [16, 17]]
23+
24+
**Output:** [null, null, null, true, false, true]
25+
26+
**Explanation:**
27+
28+
RangeModule rangeModule = new RangeModule();
29+
rangeModule.addRange(10, 20);
30+
rangeModule.removeRange(14, 16);
31+
rangeModule.queryRange(10, 14); // return True,(Every number in [10, 14) is being tracked)
32+
rangeModule.queryRange(13, 15); // return False,(Numbers like 14, 14.03, 14.17 in [13, 15) are not being tracked)
33+
rangeModule.queryRange(16, 17); // return True, (The number 16 in [16, 17) is still being tracked, despite the remove operation)
34+
35+
**Constraints:**
36+
37+
* <code>1 <= left < right <= 10<sup>9</sup></code>
38+
* At most <code>10<sup>4</sup></code> calls will be made to `addRange`, `queryRange`, and `removeRange`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0701_0800.s0712_minimum_ascii_delete_sum_for_two_strings
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 minimumDeleteSum() {
10+
assertThat(Solution().minimumDeleteSum("sea", "eat"), equalTo(231))
11+
}
12+
13+
@Test
14+
fun minimumDeleteSum2() {
15+
assertThat(Solution().minimumDeleteSum("delete", "leet"), equalTo(403))
16+
}
17+
}

0 commit comments

Comments
 (0)