Skip to content

Commit e0d85ba

Browse files
authored
Added tasks 2930-2940
1 parent c56d48c commit e0d85ba

File tree

30 files changed

+1170
-0
lines changed

30 files changed

+1170
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g2901_3000.s2930_number_of_strings_which_can_be_rearranged_to_contain_substring
2+
3+
// #Medium #Dynamic_Programming #Math #Combinatorics
4+
// #2024_01_03_Time_132_ms_(100.00%)_Space_33.3_MB_(100.00%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
private fun pow(x: Long, n: Long, mod: Long): Long {
9+
var n = n
10+
var result: Long = 1
11+
var p = x % mod
12+
while (n != 0L) {
13+
if ((n and 1L) != 0L) {
14+
result = (result * p) % mod
15+
}
16+
p = (p * p) % mod
17+
n = n shr 1
18+
}
19+
return result
20+
}
21+
22+
fun stringCount(n: Int): Int {
23+
val mod = 1e9.toInt() + 7L
24+
return (
25+
(
26+
(
27+
pow(26, n.toLong(), mod) -
28+
(n + 75) * pow(25, n - 1L, mod) +
29+
(2 * n + 72) * pow(24, n - 1L, mod) -
30+
(n + 23) * pow(23, n - 1L, mod)
31+
) %
32+
mod +
33+
mod
34+
) %
35+
mod
36+
).toInt()
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2930\. Number of Strings Which Can Be Rearranged to Contain Substring
2+
3+
Medium
4+
5+
You are given an integer `n`.
6+
7+
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**.
8+
9+
For example:
10+
11+
* The string `"lteer"` is good because we can rearrange it to form `"leetr"` .
12+
* `"letl"` is not good because we cannot rearrange it to contain `"leet"` as a substring.
13+
14+
Return _the **total** number of good strings of length_ `n`.
15+
16+
Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
17+
18+
A **substring** is a contiguous sequence of characters within a string.
19+
20+
**Example 1:**
21+
22+
**Input:** n = 4
23+
24+
**Output:** 12
25+
26+
**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".
27+
28+
**Example 2:**
29+
30+
**Input:** n = 10
31+
32+
**Output:** 83943898
33+
34+
**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.
35+
36+
**Constraints:**
37+
38+
* <code>1 <= n <= 10<sup>5</sup></code>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g2901_3000.s2931_maximum_spending_after_buying_items
2+
3+
// #Hard #Array #Sorting #Greedy #Matrix #Heap_Priority_Queue
4+
// #2024_01_03_Time_541_ms_(93.75%)_Space_61.5_MB_(93.75%)
5+
6+
class Solution {
7+
private class Node {
8+
var `val`: Int = -1
9+
var next: Node? = null
10+
11+
constructor(`val`: Int) {
12+
this.`val` = `val`
13+
}
14+
15+
constructor()
16+
}
17+
18+
fun maxSpending(values: Array<IntArray>): Long {
19+
val m = values.size
20+
val n = values[0].size
21+
val head = Node()
22+
var node: Node? = head
23+
for (j in n - 1 downTo 0) {
24+
node!!.next = Node(values[0][j])
25+
node = node.next
26+
}
27+
for (i in 1 until m) {
28+
node = head
29+
for (j in n - 1 downTo 0) {
30+
while (node!!.next != null && node.next!!.`val` <= values[i][j]) {
31+
node = node.next
32+
}
33+
val next = node.next
34+
node.next = Node(values[i][j])
35+
node = node.next
36+
node!!.next = next
37+
}
38+
}
39+
var res: Long = 0
40+
var day: Long = 1
41+
node = head.next
42+
while (node != null) {
43+
res += day * node.`val`
44+
node = node.next
45+
day++
46+
}
47+
return res
48+
}
49+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2931\. Maximum Spending After Buying Items
2+
3+
Hard
4+
5+
You are given a **0-indexed** `m * n` integer matrix `values`, representing the values of `m * n` different items in `m` different shops. Each shop has `n` items where the <code>j<sup>th</sup></code> item in the <code>i<sup>th</sup></code> shop has a value of `values[i][j]`. Additionally, the items in the <code>i<sup>th</sup></code> shop are sorted in non-increasing order of value. That is, `values[i][j] >= values[i][j + 1]` for all `0 <= j < n - 1`.
6+
7+
On each day, you would like to buy a single item from one of the shops. Specifically, On the <code>d<sup>th</sup></code> day you can:
8+
9+
* Pick any shop `i`.
10+
* Buy the rightmost available item `j` for the price of `values[i][j] * d`. That is, find the greatest index `j` such that item `j` was never bought before, and buy it for the price of `values[i][j] * d`.
11+
12+
**Note** that all items are pairwise different. For example, if you have bought item `0` from shop `1`, you can still buy item `0` from any other shop.
13+
14+
Return _the **maximum amount of money that can be spent** on buying all_ `m * n` _products_.
15+
16+
**Example 1:**
17+
18+
**Input:** values = [[8,5,2],[6,4,1],[9,7,3]]
19+
20+
**Output:** 285
21+
22+
**Explanation:** On the first day, we buy product 2 from shop 1 for a price of values[1][2] \* 1 = 1.
23+
24+
On the second day, we buy product 2 from shop 0 for a price of values[0][2] \* 2 = 4.
25+
26+
On the third day, we buy product 2 from shop 2 for a price of values[2][2] \* 3 = 9.
27+
28+
On the fourth day, we buy product 1 from shop 1 for a price of values[1][1] \* 4 = 16.
29+
30+
On the fifth day, we buy product 1 from shop 0 for a price of values[0][1] \* 5 = 25.
31+
32+
On the sixth day, we buy product 0 from shop 1 for a price of values[1][0] \* 6 = 36.
33+
34+
On the seventh day, we buy product 1 from shop 2 for a price of values[2][1] \* 7 = 49.
35+
36+
On the eighth day, we buy product 0 from shop 0 for a price of values[0][0] \* 8 = 64.
37+
38+
On the ninth day, we buy product 0 from shop 2 for a price of values[2][0] \* 9 = 81.
39+
40+
Hence, our total spending is equal to 285.
41+
42+
It can be shown that 285 is the maximum amount of money that can be spent buying all m \* n products.
43+
44+
**Example 2:**
45+
46+
**Input:** values = [[10,8,6,4,2],[9,7,5,3,2]]
47+
48+
**Output:** 386
49+
50+
**Explanation:** On the first day, we buy product 4 from shop 0 for a price of values[0][4] \* 1 = 2.
51+
52+
On the second day, we buy product 4 from shop 1 for a price of values[1][4] \* 2 = 4.
53+
54+
On the third day, we buy product 3 from shop 1 for a price of values[1][3] \* 3 = 9.
55+
56+
On the fourth day, we buy product 3 from shop 0 for a price of values[0][3] \* 4 = 16.
57+
58+
On the fifth day, we buy product 2 from shop 1 for a price of values[1][2] \* 5 = 25.
59+
60+
On the sixth day, we buy product 2 from shop 0 for a price of values[0][2] \* 6 = 36.
61+
62+
On the seventh day, we buy product 1 from shop 1 for a price of values[1][1] \* 7 = 49.
63+
64+
On the eighth day, we buy product 1 from shop 0 for a price of values[0][1] \* 8 = 64
65+
66+
On the ninth day, we buy product 0 from shop 1 for a price of values[1][0] \* 9 = 81.
67+
68+
On the tenth day, we buy product 0 from shop 0 for a price of values[0][0] \* 10 = 100.
69+
70+
Hence, our total spending is equal to 386.
71+
72+
It can be shown that 386 is the maximum amount of money that can be spent buying all m \* n products.
73+
74+
**Constraints:**
75+
76+
* `1 <= m == values.length <= 10`
77+
* <code>1 <= n == values[i].length <= 10<sup>4</sup></code>
78+
* <code>1 <= values[i][j] <= 10<sup>6</sup></code>
79+
* `values[i]` are sorted in non-increasing order.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2901_3000.s2932_maximum_strong_pair_xor_i
2+
3+
// #Easy #Array #Hash_Table #Bit_Manipulation #Sliding_Window #Trie
4+
// #2024_01_03_Time_192_ms_(43.08%)_Space_36.5_MB_(90.77%)
5+
6+
import kotlin.math.abs
7+
import kotlin.math.max
8+
import kotlin.math.min
9+
10+
class Solution {
11+
fun maximumStrongPairXor(nums: IntArray): Int {
12+
var max = 0
13+
var pair: Int
14+
for (i in nums.indices) {
15+
for (j in i until nums.size) {
16+
if (abs((nums[i] - nums[j])) <= min(nums[i], nums[j])) {
17+
pair = nums[i] xor nums[j]
18+
max = max(max, pair)
19+
}
20+
}
21+
}
22+
return max
23+
}
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2932\. Maximum Strong Pair XOR I
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums`. A pair of integers `x` and `y` is called a **strong** pair if it satisfies the condition:
6+
7+
* `|x - y| <= min(x, y)`
8+
9+
You need to select two integers from `nums` such that they form a strong pair and their bitwise `XOR` is the **maximum** among all strong pairs in the array.
10+
11+
Return _the **maximum**_ `XOR` _value out of all possible strong pairs in the array_ `nums`.
12+
13+
**Note** that you can pick the same integer twice to form a pair.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1,2,3,4,5]
18+
19+
**Output:** 7
20+
21+
**Explanation:** There are 11 strong pairs in the array `nums`: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5). The maximum XOR possible from these pairs is 3 XOR 4 = 7.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [10,100]
26+
27+
**Output:** 0
28+
29+
**Explanation:** There are 2 strong pairs in the array `nums`: (10, 10) and (100, 100). The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [5,6,25,30]
34+
35+
**Output:** 7
36+
37+
**Explanation:** There are 6 strong pairs in the array `nums`: (5, 5), (5, 6), (6, 6), (25, 25), (25, 30) and (30, 30). The maximum XOR possible from these pairs is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3.
38+
39+
**Constraints:**
40+
41+
* `1 <= nums.length <= 50`
42+
* `1 <= nums[i] <= 100`
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package g2901_3000.s2933_high_access_employees
2+
3+
// #Medium #Array #String #Hash_Table #Sorting
4+
// #2024_01_03_Time_304_ms_(91.67%)_Space_39.6_MB_(95.83%)
5+
6+
class Solution {
7+
private fun isPossible(a: Int, b: Int): Boolean {
8+
val hb = b / 100
9+
val ha = a / 100
10+
var mind = b % 100
11+
val mina = a % 100
12+
if (hb == 23 && ha == 0) {
13+
return false
14+
}
15+
if (hb - ha > 1) {
16+
return false
17+
}
18+
if (hb - ha == 1) {
19+
mind += 60
20+
}
21+
return mind - mina < 60
22+
}
23+
24+
private fun isHighAccess(list: List<Int>): Boolean {
25+
if (list.size < 3) {
26+
return false
27+
}
28+
var i = 0
29+
var j = 1
30+
var k = 2
31+
while (k < list.size) {
32+
val a = list[i++]
33+
val b = list[j++]
34+
val c = list[k++]
35+
if (isPossible(a, c) && isPossible(b, c) && isPossible(a, b)) {
36+
return true
37+
}
38+
}
39+
return false
40+
}
41+
42+
private fun stringToInt(str: String): Int {
43+
var i = 1000
44+
var `val` = 0
45+
for (ch in str.toCharArray()) {
46+
val n = ch.code - '0'.code
47+
`val` += i * n
48+
i = i / 10
49+
}
50+
return `val`
51+
}
52+
53+
fun findHighAccessEmployees(accessTimes: List<List<String>>): List<String> {
54+
val map = HashMap<String, MutableList<Int>>()
55+
for (list in accessTimes) {
56+
val temp = map.getOrDefault(list[0], ArrayList())
57+
val `val` = stringToInt(list[1])
58+
temp.add(`val`)
59+
map[list[0]] = temp
60+
}
61+
val ans: MutableList<String> = ArrayList()
62+
for ((key, temp) in map) {
63+
temp.sort()
64+
if (isHighAccess(temp)) {
65+
ans.add(key)
66+
}
67+
}
68+
return ans
69+
}
70+
}

0 commit comments

Comments
 (0)