Skip to content

Commit f64a80a

Browse files
authored
Added tasks 638, 639, 640, 641
1 parent a860c08 commit f64a80a

File tree

13 files changed

+562
-0
lines changed

13 files changed

+562
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16741674
| 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
16751675
| 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
16761676
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
1677+
| 0641 |[Design Circular Deque](src/main/kotlin/g0601_0700/s0641_design_circular_deque/MyCircularDeque.kt)| Medium | Array, Design, Linked_List, Queue | 232 | 100.00
1678+
| 0640 |[Solve the Equation](src/main/kotlin/g0601_0700/s0640_solve_the_equation/Solution.kt)| Medium | String, Math, Simulation | 170 | 66.67
1679+
| 0639 |[Decode Ways II](src/main/kotlin/g0601_0700/s0639_decode_ways_ii/Solution.kt)| Hard | String, Dynamic_Programming | 259 | 100.00
1680+
| 0638 |[Shopping Offers](src/main/kotlin/g0601_0700/s0638_shopping_offers/Solution.kt)| Medium | Array, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask, Memoization | 195 | 100.00
16771681
| 0637 |[Average of Levels in Binary Tree](src/main/kotlin/g0601_0700/s0637_average_of_levels_in_binary_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 249 | 100.00
16781682
| 0636 |[Exclusive Time of Functions](src/main/kotlin/g0601_0700/s0636_exclusive_time_of_functions/Solution.kt)| Medium | Array, Stack | 270 | 80.00
16791683
| 0633 |[Sum of Square Numbers](src/main/kotlin/g0601_0700/s0633_sum_of_square_numbers/Solution.kt)| Medium | Math, Binary_Search, Two_Pointers, Binary_Search_I_Day_10 | 126 | 100.00
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package g0601_0700.s0638_shopping_offers
2+
3+
// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask #Memoization
4+
// #2023_02_10_Time_195_ms_(100.00%)_Space_35.1_MB_(100.00%)
5+
6+
class Solution() {
7+
fun shoppingOffers(
8+
price: List<Int>,
9+
special: List<List<Int>>,
10+
needs: List<Int>
11+
): Int {
12+
val map: MutableMap<List<Int>, Int> = HashMap()
13+
shoppingOffersUtil(price, special, needs, map)
14+
return map[needs]!!
15+
}
16+
17+
private fun shoppingOffersUtil(
18+
price: List<Int>,
19+
special: List<List<Int>>,
20+
needs: List<Int>,
21+
map: MutableMap<List<Int>, Int>
22+
): Int {
23+
if (map.containsKey(needs)) {
24+
return map[needs]!!
25+
}
26+
var ans = computePrice(price, needs)
27+
for (i in special.indices) {
28+
if (verify(special[i], needs)) {
29+
ans = Math.min(
30+
special[i][needs.size] +
31+
shoppingOffersUtil(
32+
price,
33+
special,
34+
updatedNeeds(needs, special[i]),
35+
map
36+
),
37+
ans
38+
)
39+
}
40+
}
41+
map[needs] = ans
42+
return (map[needs])!!
43+
}
44+
45+
private fun updatedNeeds(needs: List<Int>, special: List<Int>): List<Int> {
46+
val updatedNeeds: MutableList<Int> = ArrayList(needs)
47+
for (i in needs.indices) {
48+
updatedNeeds[i] = updatedNeeds[i] - special[i]
49+
}
50+
return updatedNeeds
51+
}
52+
53+
private fun verify(special: List<Int>, needs: List<Int>): Boolean {
54+
for (i in needs.indices) {
55+
if (special[i] > needs[i]) {
56+
return false
57+
}
58+
}
59+
return true
60+
}
61+
62+
private fun computePrice(price: List<Int>, needs: List<Int>): Int {
63+
var ans = 0
64+
for (i in needs.indices) {
65+
ans += (needs[i] * price[i])
66+
}
67+
return ans
68+
}
69+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
638\. Shopping Offers
2+
3+
Medium
4+
5+
In LeetCode Store, there are `n` items to sell. Each item has a price. However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.
6+
7+
You are given an integer array `price` where `price[i]` is the price of the <code>i<sup>th</sup></code> item, and an integer array `needs` where `needs[i]` is the number of pieces of the <code>i<sup>th</sup></code> item you want to buy.
8+
9+
You are also given an array `special` where `special[i]` is of size `n + 1` where `special[i][j]` is the number of pieces of the <code>j<sup>th</sup></code> item in the <code>i<sup>th</sup></code> offer and `special[i][n]` (i.e., the last integer in the array) is the price of the <code>i<sup>th</sup></code> offer.
10+
11+
Return _the lowest price you have to pay for exactly certain items as given, where you could make optimal use of the special offers_. You are not allowed to buy more items than you want, even if that would lower the overall price. You could use any of the special offers as many times as you want.
12+
13+
**Example 1:**
14+
15+
**Input:** price = [2,5], special = [[3,0,5],[1,2,10]], needs = [3,2]
16+
17+
**Output:** 14
18+
19+
**Explanation:** There are two kinds of items, A and B. Their prices are $2 and $5 respectively.
20+
21+
In special offer 1, you can pay $5 for 3A and 0B In special offer 2, you can pay $10 for 1A and 2B.
22+
23+
You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A.
24+
25+
**Example 2:**
26+
27+
**Input:** price = [2,3,4], special = [[1,1,0,4],[2,2,1,9]], needs = [1,2,1]
28+
29+
**Output:** 11
30+
31+
**Explanation:** The price of A is $2, and $3 for B, $4 for C.
32+
33+
You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C.
34+
35+
You need to buy 1A ,2B and 1C, so you may pay $4 for 1A and 1B (special offer #1), and $3 for 1B, $4 for 1C.
36+
37+
You cannot add more items, though only $9 for 2A ,2B and 1C.
38+
39+
**Constraints:**
40+
41+
* `n == price.length == needs.length`
42+
* `1 <= n <= 6`
43+
* `0 <= price[i], needs[i] <= 10`
44+
* `1 <= special.length <= 100`
45+
* `special[i].length == n + 1`
46+
* `0 <= special[i][j] <= 50`
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g0601_0700.s0639_decode_ways_ii
2+
3+
// #Hard #String #Dynamic_Programming #2023_02_10_Time_259_ms_(100.00%)_Space_38.4_MB_(100.00%)
4+
5+
class Solution {
6+
fun numDecodings(s: String): Int {
7+
if (s[0] == '0') {
8+
return 0
9+
}
10+
val dp = LongArray(s.length + 1)
11+
dp[0] = 1
12+
dp[1] = (if (s[0] == '*') 9 else 1).toLong()
13+
val ch = s.toCharArray()
14+
for (i in 2..ch.size) {
15+
if (ch[i - 1] == '0') {
16+
if (ch[i - 2] == '1' || ch[i - 2] == '2') {
17+
dp[i] = dp[i - 2]
18+
} else if (ch[i - 2] == '*') {
19+
dp[i] = 2 * dp[i - 2]
20+
} else {
21+
return 0
22+
}
23+
} else if (ch[i - 1] >= '1' && ch[i - 1] <= '6') {
24+
dp[i] = dp[i - 1]
25+
if (ch[i - 2] == '1' || ch[i - 2] == '2') {
26+
dp[i] += dp[i - 2]
27+
} else if (ch[i - 2] == '*') {
28+
dp[i] += 2 * dp[i - 2]
29+
}
30+
} else if (ch[i - 1] >= '7' && ch[i - 1] <= '9') {
31+
dp[i] = dp[i - 1]
32+
if (ch[i - 2] == '1' || ch[i - 2] == '*') {
33+
dp[i] += dp[i - 2]
34+
}
35+
} else if (ch[i - 1] == '*') {
36+
dp[i] = 9 * dp[i - 1]
37+
if (ch[i - 2] == '1') {
38+
dp[i] += 9 * dp[i - 2]
39+
} else if (ch[i - 2] == '2') {
40+
dp[i] += 6 * dp[i - 2]
41+
} else if (ch[i - 2] == '*') {
42+
dp[i] += 15 * dp[i - 2]
43+
}
44+
}
45+
dp[i] = dp[i] % 1000000007
46+
}
47+
return dp[s.length].toInt()
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
639\. Decode Ways II
2+
3+
Hard
4+
5+
A message containing letters from `A-Z` can be **encoded** into numbers using the following mapping:
6+
7+
'A' -> "1" 'B' -> "2" ... 'Z' -> "26"
8+
9+
To **decode** an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, `"11106"` can be mapped into:
10+
11+
* `"AAJF"` with the grouping `(1 1 10 6)`
12+
* `"KJF"` with the grouping `(11 10 6)`
13+
14+
Note that the grouping `(1 11 06)` is invalid because `"06"` cannot be mapped into `'F'` since `"6"` is different from `"06"`.
15+
16+
**In addition** to the mapping above, an encoded message may contain the `'*'` character, which can represent any digit from `'1'` to `'9'` (`'0'` is excluded). For example, the encoded message `"1*"` may represent any of the encoded messages `"11"`, `"12"`, `"13"`, `"14"`, `"15"`, `"16"`, `"17"`, `"18"`, or `"19"`. Decoding `"1*"` is equivalent to decoding **any** of the encoded messages it can represent.
17+
18+
Given a string `s` consisting of digits and `'*'` characters, return _the **number** of ways to **decode** it_.
19+
20+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
21+
22+
**Example 1:**
23+
24+
**Input:** s = "\*"
25+
26+
**Output:** 9
27+
28+
**Explanation:** The encoded message can represent any of the encoded messages "1", "2", "3", "4", "5", "6", "7", "8", or "9". Each of these can be decoded to the strings "A", "B", "C", "D", "E", "F", "G", "H", and "I" respectively. Hence, there are a total of 9 ways to decode "\*".
29+
30+
**Example 2:**
31+
32+
**Input:** s = "1\*"
33+
34+
**Output:** 18
35+
36+
**Explanation:** The encoded message can represent any of the encoded messages "11", "12", "13", "14", "15", "16", "17", "18", or "19". Each of these encoded messages have 2 ways to be decoded (e.g. "11" can be decoded to "AA" or "K"). Hence, there are a total of 9 \* 2 = 18 ways to decode "1\*".
37+
38+
**Example 3:**
39+
40+
**Input:** s = "2\*"
41+
42+
**Output:** 15
43+
44+
**Explanation:** The encoded message can represent any of the encoded messages "21", "22", "23", "24", "25", "26", "27", "28", or "29". "21", "22", "23", "24", "25", and "26" have 2 ways of being decoded, but "27", "28", and "29" only have 1 way. Hence, there are a total of (6 \* 2) + (3 \* 1) = 12 + 3 = 15 ways to decode "2\*".
45+
46+
**Constraints:**
47+
48+
* <code>1 <= s.length <= 10<sup>5</sup></code>
49+
* `s[i]` is a digit or `'*'`.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package g0601_0700.s0640_solve_the_equation
2+
3+
// #Medium #String #Math #Simulation #2023_02_10_Time_170_ms_(66.67%)_Space_35.3_MB_(66.67%)
4+
5+
class Solution {
6+
fun solveEquation(equation: String): String {
7+
val eqs = equation.split("=".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
8+
val arr1 = evaluate(eqs[0])
9+
val arr2 = evaluate(eqs[1])
10+
return if (arr1[0] == arr2[0] && arr1[1] == arr2[1]) {
11+
"Infinite solutions"
12+
} else if (arr1[0] == arr2[0]) {
13+
"No solution"
14+
} else {
15+
"x=" + (arr2[1] - arr1[1]) / (arr1[0] - arr2[0])
16+
}
17+
}
18+
19+
private fun evaluate(eq: String): IntArray {
20+
val arr = eq.toCharArray()
21+
var f = false
22+
var a = 0
23+
var b = 0
24+
var i = 0
25+
if (arr[0] == '-') {
26+
f = true
27+
i++
28+
}
29+
while (i < arr.size) {
30+
if (arr[i] == '-') {
31+
f = true
32+
i++
33+
} else if (arr[i] == '+') {
34+
i++
35+
}
36+
val sb = StringBuilder()
37+
while (i < arr.size && Character.isDigit(arr[i])) {
38+
sb.append(arr[i])
39+
i++
40+
}
41+
val n = sb.toString()
42+
if (i < arr.size && arr[i] == 'x') {
43+
var number: Int
44+
number = if (n == "") {
45+
1
46+
} else {
47+
n.toInt()
48+
}
49+
if (f) {
50+
number = -number
51+
}
52+
a += number
53+
i++
54+
} else {
55+
var number = n.toInt()
56+
if (f) {
57+
number = -number
58+
}
59+
b += number
60+
}
61+
f = false
62+
}
63+
val op = IntArray(2)
64+
op[0] = a
65+
op[1] = b
66+
return op
67+
}
68+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
640\. Solve the Equation
2+
3+
Medium
4+
5+
Solve a given equation and return the value of `'x'` in the form of a string `"x=#value"`. The equation contains only `'+'`, `'-'` operation, the variable `'x'` and its coefficient. You should return `"No solution"` if there is no solution for the equation, or `"Infinite solutions"` if there are infinite solutions for the equation.
6+
7+
If there is exactly one solution for the equation, we ensure that the value of `'x'` is an integer.
8+
9+
**Example 1:**
10+
11+
**Input:** equation = "x+5-3+x=6+x-2"
12+
13+
**Output:** "x=2"
14+
15+
**Example 2:**
16+
17+
**Input:** equation = "x=x"
18+
19+
**Output:** "Infinite solutions"
20+
21+
**Example 3:**
22+
23+
**Input:** equation = "2x=x"
24+
25+
**Output:** "x=0"
26+
27+
**Constraints:**
28+
29+
* `3 <= equation.length <= 1000`
30+
* `equation` has exactly one `'='`.
31+
* `equation` consists of integers with an absolute value in the range `[0, 100]` without any leading zeros, and the variable `'x'`.

0 commit comments

Comments
 (0)