Skip to content

Commit a9988c4

Browse files
authored
Added tasks 2301-2312
1 parent b101111 commit a9988c4

File tree

30 files changed

+1082
-0
lines changed

30 files changed

+1082
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g2301_2400.s2301_match_substring_after_replacement
2+
3+
// #Hard #Array #String #Hash_Table #String_Matching
4+
// #2023_06_29_Time_343_ms_(100.00%)_Space_44.5_MB_(100.00%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
private lateinit var c1: CharArray
9+
private lateinit var c2: CharArray
10+
private lateinit var al: Array<MutableSet<Char>?>
11+
fun matchReplacement(s: String, sub: String, mappings: Array<CharArray>): Boolean {
12+
c1 = s.toCharArray()
13+
c2 = sub.toCharArray()
14+
al = arrayOfNulls(75)
15+
for (i in 0..74) {
16+
val temp: MutableSet<Char> = HashSet()
17+
al[i] = temp
18+
}
19+
for (mapping in mappings) {
20+
al[mapping[0].code - '0'.code]!!.add(mapping[1])
21+
}
22+
return ans(c1.size, c2.size) == 1
23+
}
24+
25+
private fun ans(m: Int, n: Int): Int {
26+
var m = m
27+
var n = n
28+
if (m == 0) {
29+
return 0
30+
}
31+
if (ans(m - 1, n) == 1) {
32+
return 1
33+
}
34+
if (m >= n && (c1[m - 1] == c2[n - 1] || al[c2[n - 1].code - '0'.code]!!.contains(c1[m - 1]))) {
35+
while (n >= 1 && (c1[m - 1] == c2[n - 1] || al[c2[n - 1].code - '0'.code]!!.contains(c1[m - 1]))) {
36+
n--
37+
m--
38+
}
39+
if (n == 0) {
40+
return 1
41+
}
42+
}
43+
return 0
44+
}
45+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2301\. Match Substring After Replacement
2+
3+
Hard
4+
5+
You are given two strings `s` and `sub`. You are also given a 2D character array `mappings` where <code>mappings[i] = [old<sub>i</sub>, new<sub>i</sub>]</code> indicates that you may **replace** any number of <code>old<sub>i</sub></code> characters of `sub` with <code>new<sub>i</sub></code>. Each character in `sub` **cannot** be replaced more than once.
6+
7+
Return `true` _if it is possible to make_ `sub` _a substring of_ `s` _by replacing zero or more characters according to_ `mappings`. Otherwise, return `false`.
8+
9+
A **substring** is a contiguous non-empty sequence of characters within a string.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "fool3e7bar", sub = "leet", mappings = [["e","3"],["t","7"],["t","8"]]
14+
15+
**Output:** true
16+
17+
**Explanation:** Replace the first 'e' in sub with '3' and 't' in sub with '7'.
18+
19+
Now sub = "l3e7" is a substring of s, so we return true.
20+
21+
**Example 2:**
22+
23+
**Input:** s = "fooleetbar", sub = "f00l", mappings = [["o","0"]]
24+
25+
**Output:** false
26+
27+
**Explanation:** The string "f00l" is not a substring of s and no replacements can be made.
28+
29+
Note that we cannot replace '0' with 'o'.
30+
31+
**Example 3:**
32+
33+
**Input:** s = "Fool33tbaR", sub = "leetd", mappings = [["e","3"],["t","7"],["t","8"],["d","b"],["p","b"]]
34+
35+
**Output:** true
36+
37+
**Explanation:** Replace the first and second 'e' in sub with '3' and 'd' in sub with 'b'.
38+
39+
Now sub = "l33tb" is a substring of s, so we return true.
40+
41+
**Constraints:**
42+
43+
* `1 <= sub.length <= s.length <= 5000`
44+
* `0 <= mappings.length <= 1000`
45+
* `mappings[i].length == 2`
46+
* <code>old<sub>i</sub> != new<sub>i</sub></code>
47+
* `s` and `sub` consist of uppercase and lowercase English letters and digits.
48+
* <code>old<sub>i</sub></code> and <code>new<sub>i</sub></code> are either uppercase or lowercase English letters or digits.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2301_2400.s2302_count_subarrays_with_score_less_than_k
2+
3+
// #Hard #Array #Binary_Search #Prefix_Sum #Sliding_Window
4+
// #2023_06_29_Time_556_ms_(100.00%)_Space_55.3_MB_(100.00%)
5+
6+
class Solution {
7+
fun countSubarrays(nums: IntArray, k: Long): Long {
8+
var sum: Long = 0
9+
var count: Long = 0
10+
var i = 0
11+
var j = 0
12+
while (i < nums.size) {
13+
sum += nums[i].toLong()
14+
while (sum * (i - j + 1) >= k) {
15+
sum -= nums[j++].toLong()
16+
}
17+
count += (i++ - j + 1).toLong()
18+
}
19+
return count
20+
}
21+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2302\. Count Subarrays With Score Less Than K
2+
3+
Hard
4+
5+
The **score** of an array is defined as the **product** of its sum and its length.
6+
7+
* For example, the score of `[1, 2, 3, 4, 5]` is `(1 + 2 + 3 + 4 + 5) * 5 = 75`.
8+
9+
Given a positive integer array `nums` and an integer `k`, return _the **number of non-empty subarrays** of_ `nums` _whose score is **strictly less** than_ `k`.
10+
11+
A **subarray** is a contiguous sequence of elements within an array.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,1,4,3,5], k = 10
16+
17+
**Output:** 6
18+
19+
**Explanation:**
20+
21+
The 6 subarrays having scores less than 10 are:
22+
23+
- [2] with score 2 \* 1 = 2.
24+
25+
- [1] with score 1 \* 1 = 1.
26+
27+
- [4] with score 4 \* 1 = 4.
28+
29+
- [3] with score 3 \* 1 = 3.
30+
31+
- [5] with score 5 \* 1 = 5.
32+
33+
- [2,1] with score (2 + 1) \* 2 = 6.
34+
35+
Note that subarrays such as [1,4] and [4,3,5] are not considered because their scores are 10 and 36 respectively, while we need scores strictly less than 10.
36+
37+
**Example 2:**
38+
39+
**Input:** nums = [1,1,1], k = 5
40+
41+
**Output:** 5
42+
43+
**Explanation:**
44+
45+
Every subarray except [1,1,1] has a score less than 5.
46+
47+
[1,1,1] has a score (1 + 1 + 1) \* 3 = 9, which is greater than 5.
48+
49+
Thus, there are 5 subarrays having scores less than 5.
50+
51+
**Constraints:**
52+
53+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
54+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
55+
* <code>1 <= k <= 10<sup>15</sup></code>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2301_2400.s2303_calculate_amount_paid_in_taxes
2+
3+
// #Easy #Array #Simulation #2023_06_29_Time_213_ms_(100.00%)_Space_40.4_MB_(100.00%)
4+
5+
class Solution {
6+
fun calculateTax(brackets: Array<IntArray>, income: Int): Double {
7+
// you can remove this line
8+
if (income == 0) {
9+
return 0.0
10+
}
11+
var sum = 0.0
12+
var prev = 0.0
13+
for (bracket in brackets) {
14+
val salary = bracket[0].coerceAtMost(income).toDouble()
15+
val tax = bracket[1].toDouble()
16+
sum += (salary - prev) * tax
17+
prev = salary
18+
}
19+
return sum / 100
20+
}
21+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2303\. Calculate Amount Paid in Taxes
2+
3+
Easy
4+
5+
You are given a **0-indexed** 2D integer array `brackets` where <code>brackets[i] = [upper<sub>i</sub>, percent<sub>i</sub>]</code> means that the <code>i<sup>th</sup></code> tax bracket has an upper bound of <code>upper<sub>i</sub></code> and is taxed at a rate of <code>percent<sub>i</sub></code>. The brackets are **sorted** by upper bound (i.e. <code>upper<sub>i-1</sub> < upper<sub>i</sub></code> for `0 < i < brackets.length`).
6+
7+
Tax is calculated as follows:
8+
9+
* The first <code>upper<sub>0</sub></code> dollars earned are taxed at a rate of <code>percent<sub>0</sub></code>.
10+
* The next <code>upper<sub>1</sub> - upper<sub>0</sub></code> dollars earned are taxed at a rate of <code>percent<sub>1</sub></code>.
11+
* The next <code>upper<sub>2</sub> - upper<sub>1</sub></code> dollars earned are taxed at a rate of <code>percent<sub>2</sub></code>.
12+
* And so on.
13+
14+
You are given an integer `income` representing the amount of money you earned. Return _the amount of money that you have to pay in taxes._ Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.
15+
16+
**Example 1:**
17+
18+
**Input:** brackets = [[3,50],[7,10],[12,25]], income = 10
19+
20+
**Output:** 2.65000
21+
22+
**Explanation:**
23+
24+
The first 3 dollars you earn are taxed at 50%. You have to pay $3 \* 50% = $1.50 dollars in taxes.
25+
26+
The next 7 - 3 = 4 dollars you earn are taxed at 10%. You have to pay $4 \* 10% = $0.40 dollars in taxes.
27+
28+
The final 10 - 7 = 3 dollars you earn are taxed at 25%. You have to pay $3 \* 25% = $0.75 dollars in taxes. You have to pay a total of $1.50 + $0.40 + $0.75 = $2.65 dollars in taxes.
29+
30+
**Example 2:**
31+
32+
**Input:** brackets = [[1,0],[4,25],[5,50]], income = 2
33+
34+
**Output:** 0.25000
35+
36+
**Explanation:**
37+
38+
The first dollar you earn is taxed at 0%. You have to pay $1 \* 0% = $0 dollars in taxes.
39+
40+
The second dollar you earn is taxed at 25%. You have to pay $1 \* 25% = $0.25 dollars in taxes.
41+
42+
You have to pay a total of $0 + $0.25 = $0.25 dollars in taxes.
43+
44+
**Example 3:**
45+
46+
**Input:** brackets = [[2,50]], income = 0
47+
48+
**Output:** 0.00000
49+
50+
**Explanation:** You have no income to tax, so you have to pay a total of $0 dollars in taxes.
51+
52+
**Constraints:**
53+
54+
* `1 <= brackets.length <= 100`
55+
* <code>1 <= upper<sub>i</sub> <= 1000</code>
56+
* <code>0 <= percent<sub>i</sub> <= 100</code>
57+
* `0 <= income <= 1000`
58+
* <code>upper<sub>i</sub></code> is sorted in ascending order.
59+
* All the values of <code>upper<sub>i</sub></code> are **unique**.
60+
* The upper bound of the last tax bracket is greater than or equal to `income`.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g2301_2400.s2304_minimum_path_cost_in_a_grid
2+
3+
// #Medium #Array #Dynamic_Programming #Matrix
4+
// #2023_06_29_Time_1048_ms_(100.00%)_Space_71.7_MB_(100.00%)
5+
6+
class Solution {
7+
fun minPathCost(grid: Array<IntArray>, moveCost: Array<IntArray>): Int {
8+
val m = grid.size
9+
val n = grid[0].size
10+
val dp = Array(m) { IntArray(n) }
11+
System.arraycopy(grid[m - 1], 0, dp[m - 1], 0, n)
12+
for (i in m - 2 downTo 0) {
13+
for (j in 0 until n) {
14+
var min = Int.MAX_VALUE
15+
for (k in 0 until n) {
16+
min = min.coerceAtMost(grid[i][j] + moveCost[grid[i][j]][k] + dp[i + 1][k])
17+
}
18+
dp[i][j] = min
19+
}
20+
}
21+
var min = Int.MAX_VALUE
22+
for (s in dp[0]) {
23+
min = min.coerceAtMost(s)
24+
}
25+
return min
26+
}
27+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2304\. Minimum Path Cost in a Grid
2+
3+
Medium
4+
5+
You are given a **0-indexed** `m x n` integer matrix `grid` consisting of **distinct** integers from `0` to `m * n - 1`. You can move in this matrix from a cell to any other cell in the **next** row. That is, if you are in cell `(x, y)` such that `x < m - 1`, you can move to any of the cells `(x + 1, 0)`, `(x + 1, 1)`, ..., `(x + 1, n - 1)`. **Note** that it is not possible to move from cells in the last row.
6+
7+
Each possible move has a cost given by a **0-indexed** 2D array `moveCost` of size `(m * n) x n`, where `moveCost[i][j]` is the cost of moving from a cell with value `i` to a cell in column `j` of the next row. The cost of moving from cells in the last row of `grid` can be ignored.
8+
9+
The cost of a path in `grid` is the **sum** of all values of cells visited plus the **sum** of costs of all the moves made. Return _the **minimum** cost of a path that starts from any cell in the **first** row and ends at any cell in the **last** row._
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2022/04/28/griddrawio-2.png)
14+
15+
**Input:** grid = [[5,3],[4,0],[2,1]], moveCost = [[9,8],[1,5],[10,12],[18,6],[2,4],[14,3]]
16+
17+
**Output:** 17
18+
19+
**Explanation:** The path with the minimum possible cost is the path 5 -> 0 -> 1.
20+
21+
- The sum of the values of cells visited is 5 + 0 + 1 = 6.
22+
23+
- The cost of moving from 5 to 0 is 3.
24+
25+
- The cost of moving from 0 to 1 is 8.
26+
27+
So the total cost of the path is 6 + 3 + 8 = 17.
28+
29+
**Example 2:**
30+
31+
**Input:** grid = [[5,1,2],[4,0,3]], moveCost = [[12,10,15],[20,23,8],[21,7,1],[8,1,13],[9,10,25],[5,3,2]]
32+
33+
**Output:** 6
34+
35+
**Explanation:** The path with the minimum possible cost is the path 2 -> 3.
36+
37+
- The sum of the values of cells visited is 2 + 3 = 5.
38+
39+
- The cost of moving from 2 to 3 is 1.
40+
41+
So the total cost of this path is 5 + 1 = 6.
42+
43+
**Constraints:**
44+
45+
* `m == grid.length`
46+
* `n == grid[i].length`
47+
* `2 <= m, n <= 50`
48+
* `grid` consists of distinct integers from `0` to `m * n - 1`.
49+
* `moveCost.length == m * n`
50+
* `moveCost[i].length == n`
51+
* `1 <= moveCost[i][j] <= 100`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g2301_2400.s2305_fair_distribution_of_cookies
2+
3+
// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask
4+
// #2023_06_29_Time_255_ms_(100.00%)_Space_33.7_MB_(100.00%)
5+
6+
class Solution {
7+
private var res = Int.MAX_VALUE
8+
fun distributeCookies(c: IntArray, k: Int): Int {
9+
val nums = IntArray(k)
10+
dfs(c, nums, 0)
11+
return res
12+
}
13+
14+
private fun dfs(c: IntArray, nums: IntArray, cur: Int) {
15+
if (cur == c.size) {
16+
var r = 0
17+
for (num in nums) {
18+
r = r.coerceAtLeast(num)
19+
}
20+
res = res.coerceAtMost(r)
21+
return
22+
}
23+
for (i in nums.indices) {
24+
if (nums[i] + c[cur] > res) {
25+
continue
26+
}
27+
nums[i] += c[cur]
28+
dfs(c, nums, cur + 1)
29+
nums[i] -= c[cur]
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)