Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a9988c4

Browse files
authoredJun 29, 2023
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+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2305\. Fair Distribution of Cookies
2+
3+
Medium
4+
5+
You are given an integer array `cookies`, where `cookies[i]` denotes the number of cookies in the <code>i<sup>th</sup></code> bag. You are also given an integer `k` that denotes the number of children to distribute **all** the bags of cookies to. All the cookies in the same bag must go to the same child and cannot be split up.
6+
7+
The **unfairness** of a distribution is defined as the **maximum** **total** cookies obtained by a single child in the distribution.
8+
9+
Return _the **minimum** unfairness of all distributions_.
10+
11+
**Example 1:**
12+
13+
**Input:** cookies = [8,15,10,20,8], k = 2
14+
15+
**Output:** 31
16+
17+
**Explanation:** One optimal distribution is [8,15,8] and [10,20]
18+
19+
- The 1<sup>st</sup> child receives [8,15,8] which has a total of 8 + 15 + 8 = 31 cookies.
20+
21+
- The 2<sup>nd</sup> child receives [10,20] which has a total of 10 + 20 = 30 cookies.
22+
23+
The unfairness of the distribution is max(31,30) = 31.
24+
25+
It can be shown that there is no distribution with an unfairness less than 31.
26+
27+
**Example 2:**
28+
29+
**Input:** cookies = [6,1,3,2,2,4,1,2], k = 3
30+
31+
**Output:** 7
32+
33+
**Explanation:** One optimal distribution is [6,1], [3,2,2], and [4,1,2]
34+
35+
- The 1<sup>st</sup> child receives [6,1] which has a total of 6 + 1 = 7 cookies.
36+
37+
- The 2<sup>nd</sup> child receives [3,2,2] which has a total of 3 + 2 + 2 = 7 cookies.
38+
39+
- The 3<sup>rd</sup> child receives [4,1,2] which has a total of 4 + 1 + 2 = 7 cookies.
40+
41+
The unfairness of the distribution is max(7,7,7) = 7.
42+
43+
It can be shown that there is no distribution with an unfairness less than 7.
44+
45+
**Constraints:**
46+
47+
* `2 <= cookies.length <= 8`
48+
* <code>1 <= cookies[i] <= 10<sup>5</sup></code>
49+
* `2 <= k <= cookies.length`
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g2301_2400.s2306_naming_a_company
2+
3+
// #Hard #Array #String #Hash_Table #Bit_Manipulation #Enumeration
4+
// #2023_06_29_Time_590_ms_(100.00%)_Space_51.1_MB_(100.00%)
5+
6+
class Solution {
7+
fun distinctNames(a: Array<String>): Long {
8+
val m = Array<MutableSet<String>>(26) { mutableSetOf() }
9+
for (s in a) {
10+
val i = s[0].code - 97
11+
m[i].add(s.substring(1))
12+
}
13+
14+
var res = 0L
15+
for (i in m.indices) {
16+
val b1 = m[i]
17+
if (b1.isEmpty()) {
18+
continue
19+
}
20+
for (y in i + 1 until m.size) {
21+
val b2 = m[y]
22+
if (b2.isEmpty()) {
23+
continue
24+
}
25+
res += compare(b1, b2)
26+
}
27+
}
28+
29+
return res
30+
}
31+
32+
fun compare(b1: MutableSet<String>, b2: MutableSet<String>): Long {
33+
val set1 = if (b1.size > b2.size) b1 else b2
34+
val set2 = if (b1.size > b2.size) b2 else b1
35+
var n1 = set1.size
36+
var n2 = set2.size
37+
for (s in set1) {
38+
if (set2.contains(s)) {
39+
n1--
40+
n2--
41+
}
42+
}
43+
44+
return (n1 * n2) * 2L
45+
}
46+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2306\. Naming a Company
2+
3+
Hard
4+
5+
You are given an array of strings `ideas` that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:
6+
7+
1. Choose 2 **distinct** names from `ideas`, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.
8+
2. Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.
9+
3. If **both** of the new names are not found in the original `ideas`, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the **concatenation** of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.
10+
4. Otherwise, it is not a valid name.
11+
12+
Return _the number of **distinct** valid names for the company_.
13+
14+
**Example 1:**
15+
16+
**Input:** ideas = ["coffee","donuts","time","toffee"]
17+
18+
**Output:** 6
19+
20+
**Explanation:** The following selections are valid:
21+
22+
- ("coffee", "donuts"): The company name created is "doffee conuts".
23+
24+
- ("donuts", "coffee"): The company name created is "conuts doffee".
25+
26+
- ("donuts", "time"): The company name created is "tonuts dime".
27+
28+
- ("donuts", "toffee"): The company name created is "tonuts doffee".
29+
30+
- ("time", "donuts"): The company name created is "dime tonuts".
31+
32+
- ("toffee", "donuts"): The company name created is "doffee tonuts".
33+
34+
Therefore, there are a total of 6 distinct company names.
35+
36+
37+
The following are some examples of invalid selections:
38+
39+
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
40+
41+
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
42+
43+
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
44+
45+
**Example 2:**
46+
47+
**Input:** ideas = ["lack","back"]
48+
49+
**Output:** 0
50+
51+
**Explanation:** There are no valid selections. Therefore, 0 is returned.
52+
53+
**Constraints:**
54+
55+
* <code>2 <= ideas.length <= 5 * 10<sup>4</sup></code>
56+
* `1 <= ideas[i].length <= 10`
57+
* `ideas[i]` consists of lowercase English letters.
58+
* All the strings in `ideas` are **unique**.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2301_2400.s2309_greatest_english_letter_in_upper_and_lower_case
2+
3+
// #Easy #Array #2023_06_29_Time_154_ms_(87.50%)_Space_35.5_MB_(62.50%)
4+
5+
class Solution {
6+
fun greatestLetter(s: String): String {
7+
var gt = ' '
8+
val sA = BooleanArray(26)
9+
val uA = BooleanArray(26)
10+
for (ch in s.toCharArray()) {
11+
var i: Int
12+
if (ch in 'A'..'Z') {
13+
i = ch.code - 'A'.code
14+
uA[i] = true
15+
} else {
16+
i = ch.code - 'a'.code
17+
sA[i] = true
18+
}
19+
if (uA[i] == sA[i] && gt.code < 'A'.code + i) {
20+
gt = ('A'.code + i).toChar()
21+
}
22+
}
23+
return if (gt == ' ') "" else gt.toString() + ""
24+
}
25+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2309\. Greatest English Letter in Upper and Lower Case
2+
3+
Easy
4+
5+
Given a string of English letters `s`, return _the **greatest** English letter which occurs as **both** a lowercase and uppercase letter in_ `s`. The returned letter should be in **uppercase**. If no such letter exists, return _an empty string_.
6+
7+
An English letter `b` is **greater** than another letter `a` if `b` appears **after** `a` in the English alphabet.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "l**Ee**TcOd**E**"
12+
13+
**Output:** "E"
14+
15+
**Explanation:**
16+
17+
The letter 'E' is the only letter to appear in both lower and upper case.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "a**rR**AzFif"
22+
23+
**Output:** "R"
24+
25+
**Explanation:**
26+
27+
The letter 'R' is the greatest letter to appear in both lower and upper case.
28+
29+
Note that 'A' and 'F' also appear in both lower and upper case, but 'R' is greater than 'F' or 'A'.
30+
31+
**Example 3:**
32+
33+
**Input:** s = "AbCdEfGhIjK"
34+
35+
**Output:** ""
36+
37+
**Explanation:** There is no letter that appears in both lower and upper case.
38+
39+
**Constraints:**
40+
41+
* `1 <= s.length <= 1000`
42+
* `s` consists of lowercase and uppercase English letters.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2301_2400.s2310_sum_of_numbers_with_units_digit_k
2+
3+
// #Medium #Math #2023_06_29_Time_133_ms_(100.00%)_Space_32.8_MB_(100.00%)
4+
5+
class Solution {
6+
fun minimumNumbers(nums: Int, k: Int): Int {
7+
// Base Case Check
8+
if (nums == 0) {
9+
return 0
10+
}
11+
val x = nums % 10
12+
for (i in 1..10) {
13+
// check if the unit digits are equal for any case and if n>k*i
14+
if (k * i % 10 == x && nums >= k * i) {
15+
return i
16+
}
17+
}
18+
// in case nothing matches
19+
return -1
20+
}
21+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2310\. Sum of Numbers With Units Digit K
2+
3+
Medium
4+
5+
Given two integers `num` and `k`, consider a set of positive integers with the following properties:
6+
7+
* The units digit of each integer is `k`.
8+
* The sum of the integers is `num`.
9+
10+
Return _the **minimum** possible size of such a set, or_ `-1` _if no such set exists._
11+
12+
Note:
13+
14+
* The set can contain multiple instances of the same integer, and the sum of an empty set is considered `0`.
15+
* The **units digit** of a number is the rightmost digit of the number.
16+
17+
**Example 1:**
18+
19+
**Input:** num = 58, k = 9
20+
21+
**Output:** 2
22+
23+
**Explanation:**
24+
25+
One valid set is [9,49], as the sum is 58 and each integer has a units digit of 9.
26+
27+
Another valid set is [19,39].
28+
29+
It can be shown that 2 is the minimum possible size of a valid set.
30+
31+
**Example 2:**
32+
33+
**Input:** num = 37, k = 2
34+
35+
**Output:** -1
36+
37+
**Explanation:** It is not possible to obtain a sum of 37 using only integers that have a units digit of 2.
38+
39+
**Example 3:**
40+
41+
**Input:** num = 0, k = 7
42+
43+
**Output:** 0
44+
45+
**Explanation:** The sum of an empty set is considered 0.
46+
47+
**Constraints:**
48+
49+
* `0 <= num <= 3000`
50+
* `0 <= k <= 9`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2301_2400.s2311_longest_binary_subsequence_less_than_or_equal_to_k
2+
3+
// #Medium #String #Dynamic_Programming #Greedy #Memoization
4+
// #2023_06_29_Time_140_ms_(100.00%)_Space_34.1_MB_(100.00%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
fun longestSubsequence(s: String, k: Int): Int {
9+
var k = k
10+
var res = 0
11+
var cost = 1
12+
val n = s.length
13+
for (i in n - 1 downTo 0) {
14+
if (s[i] == '0' || cost <= k) {
15+
k -= cost * (s[i].code - '0'.code)
16+
++res
17+
}
18+
if (cost <= k) {
19+
cost *= 2
20+
}
21+
}
22+
return res
23+
}
24+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2311\. Longest Binary Subsequence Less Than or Equal to K
2+
3+
Medium
4+
5+
You are given a binary string `s` and a positive integer `k`.
6+
7+
Return _the length of the **longest** subsequence of_ `s` _that makes up a **binary** number less than or equal to_ `k`.
8+
9+
Note:
10+
11+
* The subsequence can contain **leading zeroes**.
12+
* The empty string is considered to be equal to `0`.
13+
* A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "1001010", k = 5
18+
19+
**Output:** 5
20+
21+
**Explanation:** The longest subsequence of s that makes up a binary number less than or equal to 5 is "00010", as this number is equal to 2 in decimal.
22+
23+
Note that "00100" and "00101" are also possible, which are equal to 4 and 5 in decimal, respectively.
24+
25+
The length of this subsequence is 5, so 5 is returned.
26+
27+
**Example 2:**
28+
29+
**Input:** s = "00101001", k = 1
30+
31+
**Output:** 6
32+
33+
**Explanation:** "000001" is the longest subsequence of s that makes up a binary number less than or equal to 1, as this number is equal to 1 in decimal.
34+
35+
The length of this subsequence is 6, so 6 is returned.
36+
37+
**Constraints:**
38+
39+
* `1 <= s.length <= 1000`
40+
* `s[i]` is either `'0'` or `'1'`.
41+
* <code>1 <= k <= 10<sup>9</sup></code>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g2301_2400.s2312_selling_pieces_of_wood
2+
3+
// #Hard #Backtracking #2023_06_29_Time_396_ms_(100.00%)_Space_55.6_MB_(100.00%)
4+
5+
class Solution {
6+
fun sellingWood(m: Int, n: Int, prices: Array<IntArray>): Long {
7+
// dp[i][j] = Maximum profit selling wood of size i*j
8+
val dp = Array(m) { LongArray(n) }
9+
for (price in prices) {
10+
dp[price[0] - 1][price[1] - 1] = dp[price[0] - 1][price[1] - 1].coerceAtLeast(price[2].toLong())
11+
}
12+
for (i in 0 until m) {
13+
for (j in 0 until n) {
14+
// Cut Vertically
15+
for (k in 0 until j) {
16+
dp[i][j] = dp[i][j].coerceAtLeast(dp[i][k] + dp[i][j - k - 1])
17+
}
18+
// Cut Horizontally
19+
for (k in 0 until i) {
20+
dp[i][j] = dp[i][j].coerceAtLeast(dp[k][j] + dp[i - k - 1][j])
21+
}
22+
}
23+
}
24+
return dp[m - 1][n - 1]
25+
}
26+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2312\. Selling Pieces of Wood
2+
3+
Hard
4+
5+
You are given two integers `m` and `n` that represent the height and width of a rectangular piece of wood. You are also given a 2D integer array `prices`, where <code>prices[i] = [h<sub>i</sub>, w<sub>i</sub>, price<sub>i</sub>]</code> indicates you can sell a rectangular piece of wood of height <code>h<sub>i</sub></code> and width <code>w<sub>i</sub></code> for <code>price<sub>i</sub></code> dollars.
6+
7+
To cut a piece of wood, you must make a vertical or horizontal cut across the **entire** height or width of the piece to split it into two smaller pieces. After cutting a piece of wood into some number of smaller pieces, you can sell pieces according to `prices`. You may sell multiple pieces of the same shape, and you do not have to sell all the shapes. The grain of the wood makes a difference, so you **cannot** rotate a piece to swap its height and width.
8+
9+
Return _the **maximum** money you can earn after cutting an_ `m x n` _piece of wood_.
10+
11+
Note that you can cut the piece of wood as many times as you want.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2022/04/27/ex1.png)
16+
17+
**Input:** m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
18+
19+
**Output:** 19
20+
21+
**Explanation:** The diagram above shows a possible scenario. It consists of:
22+
23+
- 2 pieces of wood shaped 2 x 2, selling for a price of 2 \* 7 = 14.
24+
25+
- 1 piece of wood shaped 2 x 1, selling for a price of 1 \* 3 = 3.
26+
27+
- 1 piece of wood shaped 1 x 4, selling for a price of 1 \* 2 = 2.
28+
29+
This obtains a total of 14 + 3 + 2 = 19 money earned.
30+
31+
It can be shown that 19 is the maximum amount of money that can be earned.
32+
33+
**Example 2:**
34+
35+
![](https://assets.leetcode.com/uploads/2022/04/27/ex2new.png)
36+
37+
**Input:** m = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]]
38+
39+
**Output:** 32
40+
41+
**Explanation:** The diagram above shows a possible scenario. It consists of:
42+
43+
- 3 pieces of wood shaped 3 x 2, selling for a price of 3 \* 10 = 30.
44+
45+
- 1 piece of wood shaped 1 x 4, selling for a price of 1 \* 2 = 2.
46+
47+
This obtains a total of 30 + 2 = 32 money earned.
48+
49+
It can be shown that 32 is the maximum amount of money that can be earned.
50+
51+
Notice that we cannot rotate the 1 x 4 piece of wood to obtain a 4 x 1 piece of wood.
52+
53+
**Constraints:**
54+
55+
* `1 <= m, n <= 200`
56+
* <code>1 <= prices.length <= 2 * 10<sup>4</sup></code>
57+
* `prices[i].length == 3`
58+
* <code>1 <= h<sub>i</sub> <= m</code>
59+
* <code>1 <= w<sub>i</sub> <= n</code>
60+
* <code>1 <= price<sub>i</sub> <= 10<sup>6</sup></code>
61+
* All the shapes of wood <code>(h<sub>i</sub>, w<sub>i</sub>)</code> are pairwise **distinct**.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g2301_2400.s2301_match_substring_after_replacement
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 matchReplacement() {
10+
assertThat(
11+
Solution()
12+
.matchReplacement(
13+
"fool3e7bar",
14+
"leet", arrayOf(charArrayOf('e', '3'), charArrayOf('t', '7'), charArrayOf('t', '8'))
15+
),
16+
equalTo(true)
17+
)
18+
}
19+
20+
@Test
21+
fun matchReplacement2() {
22+
assertThat(
23+
Solution().matchReplacement("fooleetbar", "f00l", arrayOf(charArrayOf('o', '0'))),
24+
equalTo(false)
25+
)
26+
}
27+
28+
@Test
29+
fun matchReplacement3() {
30+
assertThat(
31+
Solution()
32+
.matchReplacement(
33+
"Fool33tbaR",
34+
"leetd",
35+
arrayOf(
36+
charArrayOf('e', '3'),
37+
charArrayOf('t', '7'),
38+
charArrayOf('t', '8'),
39+
charArrayOf('d', 'b'),
40+
charArrayOf('p', 'b')
41+
)
42+
),
43+
equalTo(true)
44+
)
45+
}
46+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2301_2400.s2302_count_subarrays_with_score_less_than_k
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 countSubarrays() {
10+
assertThat(Solution().countSubarrays(intArrayOf(2, 1, 4, 3, 5), 10), equalTo(6L))
11+
}
12+
13+
@Test
14+
fun countSubarrays2() {
15+
assertThat(Solution().countSubarrays(intArrayOf(1, 1, 1), 5), equalTo(5L))
16+
}
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g2301_2400.s2303_calculate_amount_paid_in_taxes
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 calculateTax() {
10+
assertThat(
11+
Solution().calculateTax(arrayOf(intArrayOf(3, 50), intArrayOf(7, 10), intArrayOf(12, 25)), 10),
12+
equalTo(2.65000)
13+
)
14+
}
15+
16+
@Test
17+
fun calculateTax2() {
18+
assertThat(
19+
Solution().calculateTax(arrayOf(intArrayOf(1, 0), intArrayOf(4, 25), intArrayOf(5, 50)), 2),
20+
equalTo(0.25000)
21+
)
22+
}
23+
24+
@Test
25+
fun calculateTax3() {
26+
assertThat(
27+
Solution().calculateTax(arrayOf(intArrayOf(1, 0), intArrayOf(4, 25), intArrayOf(5, 50)), 0),
28+
equalTo(0.0)
29+
)
30+
}
31+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g2301_2400.s2304_minimum_path_cost_in_a_grid
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 minPathCost() {
10+
assertThat(
11+
Solution()
12+
.minPathCost(
13+
arrayOf(intArrayOf(5, 3), intArrayOf(4, 0), intArrayOf(2, 1)),
14+
arrayOf(
15+
intArrayOf(9, 8),
16+
intArrayOf(1, 5),
17+
intArrayOf(10, 12),
18+
intArrayOf(18, 6),
19+
intArrayOf(2, 4),
20+
intArrayOf(14, 3)
21+
)
22+
),
23+
equalTo(17)
24+
)
25+
}
26+
27+
@Test
28+
fun minPathCost2() {
29+
assertThat(
30+
Solution()
31+
.minPathCost(
32+
arrayOf(intArrayOf(5, 1, 2), intArrayOf(4, 0, 3)),
33+
arrayOf(
34+
intArrayOf(12, 10, 15),
35+
intArrayOf(20, 23, 8),
36+
intArrayOf(21, 7, 1),
37+
intArrayOf(8, 1, 13),
38+
intArrayOf(9, 10, 25),
39+
intArrayOf(5, 3, 2)
40+
)
41+
),
42+
equalTo(6)
43+
)
44+
}
45+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2301_2400.s2305_fair_distribution_of_cookies
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 distributeCookies() {
10+
assertThat(
11+
Solution().distributeCookies(intArrayOf(8, 15, 10, 20, 8), 2),
12+
equalTo(31)
13+
)
14+
}
15+
16+
@Test
17+
fun distributeCookies2() {
18+
assertThat(
19+
Solution().distributeCookies(intArrayOf(6, 1, 3, 2, 2, 4, 1, 2), 3),
20+
equalTo(7)
21+
)
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2301_2400.s2306_naming_a_company
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 distinctNames() {
10+
assertThat(
11+
Solution().distinctNames(arrayOf("coffee", "donuts", "time", "toffee")),
12+
equalTo(6L)
13+
)
14+
}
15+
16+
@Test
17+
fun distinctNames2() {
18+
assertThat(Solution().distinctNames(arrayOf("lack", "back")), equalTo(0L))
19+
}
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2301_2400.s2309_greatest_english_letter_in_upper_and_lower_case
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Assertions
6+
import org.junit.jupiter.api.Test
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun greatestLetter() {
11+
assertThat(Solution().greatestLetter("lEeTcOdE"), equalTo("E"))
12+
}
13+
14+
@Test
15+
fun greatestLetter2() {
16+
assertThat(Solution().greatestLetter("arRAzFif"), equalTo("R"))
17+
}
18+
19+
@Test
20+
fun greatestLetter3() {
21+
assertThat(Solution().greatestLetter(""), equalTo(""))
22+
}
23+
24+
@Test
25+
fun greatestLetter4() {
26+
Assertions.assertThrows(
27+
ArrayIndexOutOfBoundsException::class.java
28+
) { Solution().greatestLetter("0|") }
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g2301_2400.s2310_sum_of_numbers_with_units_digit_k
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 minimumNumbers() {
10+
assertThat(Solution().minimumNumbers(58, 9), equalTo(2))
11+
}
12+
13+
@Test
14+
fun minimumNumbers2() {
15+
assertThat(Solution().minimumNumbers(37, 2), equalTo(-1))
16+
}
17+
18+
@Test
19+
fun minimumNumbers3() {
20+
assertThat(Solution().minimumNumbers(0, 7), equalTo(0))
21+
}
22+
23+
@Test
24+
fun minimumNumbers4() {
25+
assertThat(Solution().minimumNumbers(2, 8), equalTo(-1))
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2301_2400.s2311_longest_binary_subsequence_less_than_or_equal_to_k
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 longestSubsequence() {
10+
assertThat(Solution().longestSubsequence("1001010", 5), equalTo(5))
11+
}
12+
13+
@Test
14+
fun longestSubsequence2() {
15+
assertThat(Solution().longestSubsequence("00101001", 1), equalTo(6))
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2301_2400.s2312_selling_pieces_of_wood
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 sellingWood() {
10+
assertThat(
11+
Solution().sellingWood(3, 5, arrayOf(intArrayOf(1, 4, 2), intArrayOf(2, 2, 7), intArrayOf(2, 1, 3))),
12+
equalTo(19L)
13+
)
14+
}
15+
16+
@Test
17+
fun sellingWood2() {
18+
assertThat(
19+
Solution().sellingWood(4, 6, arrayOf(intArrayOf(3, 2, 10), intArrayOf(1, 4, 2), intArrayOf(4, 1, 3))),
20+
equalTo(32L)
21+
)
22+
}
23+
}

0 commit comments

Comments
 (0)
Please sign in to comment.