Skip to content

Commit 21c5906

Browse files
authored
Added tasks 2930-2944
1 parent 703fd7c commit 21c5906

File tree

18 files changed

+1381
-163
lines changed
  • src/main/kotlin
    • g0201_0300/s0207_course_schedule
    • g2901_3000
      • s2902_count_of_sub_multisets_with_bounded_sum
      • s2920_maximum_points_after_collecting_coins_from_all_nodes
      • s2930_number_of_strings_which_can_be_rearranged_to_contain_substring
      • s2931_maximum_spending_after_buying_items
      • s2932_maximum_strong_pair_xor_i
      • s2933_high_access_employees
      • s2934_minimum_operations_to_maximize_last_elements_in_arrays
      • s2935_maximum_strong_pair_xor_ii
      • s2937_make_three_strings_equal
      • s2938_separate_black_and_white_balls
      • s2939_maximum_xor_product
      • s2940_find_building_where_alice_and_bob_can_meet
      • s2942_find_words_containing_character
      • s2943_maximize_area_of_square_hole_in_grid
      • s2944_minimum_number_of_coins_for_fruits
      • s2948_make_lexicographically_smallest_array_by_swapping_elements

18 files changed

+1381
-163
lines changed

README.md

Lines changed: 102 additions & 89 deletions
Large diffs are not rendered by default.

src/main/kotlin/g0201_0300/s0207_course_schedule/readme.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,22 @@ Return `true` if you can finish all courses. Otherwise, return `false`.
4040
```kotlin
4141
class Solution {
4242
fun canFinish(numCourses: Int, prerequisites: Array<IntArray>): Boolean {
43-
val adj: Array<ArrayList<Int>?> = arrayOfNulls<ArrayList<Int>>(numCourses)
44-
for (i in 0 until numCourses) {
45-
adj[i] = ArrayList()
46-
}
43+
val adj: Array<ArrayList<Int>> = Array(numCourses) { ArrayList() }
4744
for (pre in prerequisites) {
48-
adj[pre[1]]?.add(pre[0])
45+
adj[pre[1]].add(pre[0])
4946
}
5047
val colors = IntArray(numCourses)
5148
for (i in 0 until numCourses) {
52-
if (colors[i] == WHITE && !adj[i]?.isEmpty()!! && hasCycle(adj, i, colors)) {
49+
if (colors[i] == WHITE && adj[i].isNotEmpty() && hasCycle(adj, i, colors)) {
5350
return false
5451
}
5552
}
5653
return true
5754
}
5855

59-
private fun hasCycle(adj: Array<ArrayList<Int>?>, node: Int, colors: IntArray): Boolean {
56+
private fun hasCycle(adj: Array<ArrayList<Int>>, node: Int, colors: IntArray): Boolean {
6057
colors[node] = GRAY
61-
for (nei in adj[node]!!) {
58+
for (nei in adj[node]) {
6259
if (colors[nei] == GRAY) {
6360
return true
6461
}

src/main/kotlin/g2901_3000/s2902_count_of_sub_multisets_with_bounded_sum/readme.md

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -52,66 +52,77 @@ A **sub-multiset** is an **unordered** collection of elements of the array in wh
5252
## Solution
5353

5454
```kotlin
55+
import kotlin.math.min
56+
57+
@Suppress("NAME_SHADOWING")
5558
class Solution {
56-
private var map: HashMap<Int, Int>? = null
57-
private lateinit var dp: Array<IntArray>
59+
private val mod = 1000000007
60+
private val intMap = IntMap()
5861

59-
private fun solve(al: List<Int>, l: Int, r: Int, index: Int, sum: Int): Int {
60-
if (sum > r) {
62+
fun countSubMultisets(nums: List<Int>, l: Int, r: Int): Int {
63+
intMap.clear()
64+
intMap.add(0)
65+
var total = 0
66+
for (num in nums) {
67+
intMap.add(num)
68+
total += num
69+
}
70+
if (total < l) {
6171
return 0
6272
}
63-
var ans: Long = 0
64-
if (index >= al.size) {
65-
return ans.toInt()
73+
val r = min(r, total)
74+
val cnt = IntArray(r + 1)
75+
cnt[0] = intMap.map[0]
76+
var sum = 0
77+
for (i in 1 until intMap.size) {
78+
val value = intMap.vals[i]
79+
val count = intMap.map[value]
80+
if (count > 0) {
81+
sum = min(r, sum + value * count)
82+
update(cnt, value, count, sum)
83+
}
6684
}
67-
if (dp[index][sum] != -1) {
68-
return dp[index][sum]
85+
var res = 0
86+
for (i in l..r) {
87+
res = (res + cnt[i]) % mod
6988
}
70-
val cur = al[index]
71-
val count = map!![cur]!!
72-
for (i in 0..count) {
73-
val curSum = sum + cur * i
74-
if (curSum > r) {
75-
break
89+
return res
90+
}
91+
92+
private fun update(cnt: IntArray, n: Int, count: Int, sum: Int) {
93+
if (count == 1) {
94+
for (i in sum downTo n) {
95+
cnt[i] = (cnt[i] + cnt[i - n]) % mod
96+
}
97+
} else {
98+
for (i in n..sum) {
99+
cnt[i] = (cnt[i] + cnt[i - n]) % mod
76100
}
77-
ans += solve(al, l, r, index + 1, curSum)
78-
if (i != 0 && curSum >= l) {
79-
ans += 1
101+
val max = (count + 1) * n
102+
for (i in sum downTo max) {
103+
cnt[i] = (cnt[i] - cnt[i - max] + mod) % mod
80104
}
81-
ans %= MOD
82105
}
83-
dp[index][sum] = ans.toInt()
84-
return ans.toInt()
85106
}
86107

87-
fun countSubMultisets(nums: List<Int>, l: Int, r: Int): Int {
88-
map = HashMap()
89-
val al: MutableList<Int> = ArrayList()
90-
for (cur in nums) {
91-
val count = map!!.getOrDefault(cur, 0) + 1
92-
map!![cur] = count
93-
if (count == 1) {
94-
al.add(cur)
108+
private class IntMap {
109+
private val max = 20001
110+
val map = IntArray(max)
111+
val vals = IntArray(max)
112+
var size = 0
113+
114+
fun add(v: Int) {
115+
if (map[v]++ == 0) {
116+
vals[size++] = v
95117
}
96118
}
97-
val n = al.size
98-
dp = Array(n) { IntArray(r + 1) }
99-
for (i in dp.indices) {
100-
for (j in dp[0].indices) {
101-
dp[i][j] = -1
119+
120+
fun clear() {
121+
for (i in 0 until size) {
122+
map[vals[i]] = 0
102123
}
124+
size = 0
103125
}
104-
al.sort()
105-
var ans = solve(al, l, r, 0, 0)
106-
if (l == 0) {
107-
ans += 1
108-
}
109-
ans %= MOD
110-
return ans
111-
}
112-
113-
companion object {
114-
private const val MOD = 1e9.toInt() + 7
115126
}
116127
}
117128
```

src/main/kotlin/g2901_3000/s2920_maximum_points_after_collecting_coins_from_all_nodes/readme.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,19 @@ It can be shown that the maximum points we can get after collecting coins from a
6161
import kotlin.math.max
6262

6363
class Solution {
64-
private lateinit var adjList: Array<MutableList<Int>?>
64+
private lateinit var adjList: Array<MutableList<Int>>
6565
private lateinit var coins: IntArray
6666
private var k = 0
6767
private lateinit var dp: Array<IntArray>
6868

6969
private fun init(edges: Array<IntArray>, coins: IntArray, k: Int) {
7070
val n = coins.size
71-
adjList = arrayOfNulls(n)
72-
for (v in 0 until n) {
73-
adjList[v] = ArrayList()
74-
}
71+
adjList = Array(n) { ArrayList() }
7572
for (edge in edges) {
7673
val u = edge[0]
7774
val v = edge[1]
78-
adjList[u]?.add(v)
79-
adjList[v]?.add(u)
75+
adjList[u].add(v)
76+
adjList[v].add(u)
8077
}
8178
this.coins = coins
8279
this.k = k
@@ -96,7 +93,7 @@ class Solution {
9693
val coinsV = coins[v] / (1 shl numOfWay2Parents)
9794
var s0 = coinsV - k
9895
var s1 = coinsV / 2
99-
for (child in adjList[v]!!) {
96+
for (child in adjList[v]) {
10097
if (child != p) {
10198
s0 += rec(child, v, numOfWay2Parents)
10299
s1 += rec(child, v, numOfWay2Parents + 1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
## 2930\. Number of Strings Which Can Be Rearranged to Contain Substring
5+
6+
Medium
7+
8+
You are given an integer `n`.
9+
10+
A string `s` is called **good** if it contains only lowercase English characters **and** it is possible to rearrange the characters of `s` such that the new string contains `"leet"` as a **substring**.
11+
12+
For example:
13+
14+
* The string `"lteer"` is good because we can rearrange it to form `"leetr"` .
15+
* `"letl"` is not good because we cannot rearrange it to contain `"leet"` as a substring.
16+
17+
Return _the **total** number of good strings of length_ `n`.
18+
19+
Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
20+
21+
A **substring** is a contiguous sequence of characters within a string.
22+
23+
**Example 1:**
24+
25+
**Input:** n = 4
26+
27+
**Output:** 12
28+
29+
**Explanation:** The 12 strings which can be rearranged to have "leet" as a substring are: "eelt", "eetl", "elet", "elte", "etel", "etle", "leet", "lete", "ltee", "teel", "tele", and "tlee".
30+
31+
**Example 2:**
32+
33+
**Input:** n = 10
34+
35+
**Output:** 83943898
36+
37+
**Explanation:** The number of strings with length 10 which can be rearranged to have "leet" as a substring is 526083947580. Hence the answer is 526083947580 % (10<sup>9</sup> + 7) = 83943898.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= n <= 10<sup>5</sup></code>
42+
43+
## Solution
44+
45+
```kotlin
46+
@Suppress("NAME_SHADOWING")
47+
class Solution {
48+
private fun pow(x: Long, n: Long, mod: Long): Long {
49+
var n = n
50+
var result: Long = 1
51+
var p = x % mod
52+
while (n != 0L) {
53+
if ((n and 1L) != 0L) {
54+
result = (result * p) % mod
55+
}
56+
p = (p * p) % mod
57+
n = n shr 1
58+
}
59+
return result
60+
}
61+
62+
fun stringCount(n: Int): Int {
63+
val mod = 1e9.toInt() + 7L
64+
return (
65+
(
66+
(
67+
pow(26, n.toLong(), mod) -
68+
(n + 75) * pow(25, n - 1L, mod) +
69+
(2 * n + 72) * pow(24, n - 1L, mod) -
70+
(n + 23) * pow(23, n - 1L, mod)
71+
) %
72+
mod +
73+
mod
74+
) %
75+
mod
76+
).toInt()
77+
}
78+
}
79+
```

0 commit comments

Comments
 (0)