forked from javadev/LeetCode-in-Kotlin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
1,170 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package g2901_3000.s2930_number_of_strings_which_can_be_rearranged_to_contain_substring | ||
|
||
// #Medium #Dynamic_Programming #Math #Combinatorics | ||
// #2024_01_03_Time_132_ms_(100.00%)_Space_33.3_MB_(100.00%) | ||
|
||
@Suppress("NAME_SHADOWING") | ||
class Solution { | ||
private fun pow(x: Long, n: Long, mod: Long): Long { | ||
var n = n | ||
var result: Long = 1 | ||
var p = x % mod | ||
while (n != 0L) { | ||
if ((n and 1L) != 0L) { | ||
result = (result * p) % mod | ||
} | ||
p = (p * p) % mod | ||
n = n shr 1 | ||
} | ||
return result | ||
} | ||
|
||
fun stringCount(n: Int): Int { | ||
val mod = 1e9.toInt() + 7L | ||
return ( | ||
( | ||
( | ||
pow(26, n.toLong(), mod) - | ||
(n + 75) * pow(25, n - 1L, mod) + | ||
(2 * n + 72) * pow(24, n - 1L, mod) - | ||
(n + 23) * pow(23, n - 1L, mod) | ||
) % | ||
mod + | ||
mod | ||
) % | ||
mod | ||
).toInt() | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
2930\. Number of Strings Which Can Be Rearranged to Contain Substring | ||
|
||
Medium | ||
|
||
You are given an integer `n`. | ||
|
||
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**. | ||
|
||
For example: | ||
|
||
* The string `"lteer"` is good because we can rearrange it to form `"leetr"` . | ||
* `"letl"` is not good because we cannot rearrange it to contain `"leet"` as a substring. | ||
|
||
Return _the **total** number of good strings of length_ `n`. | ||
|
||
Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>. | ||
|
||
A **substring** is a contiguous sequence of characters within a string. | ||
|
||
**Example 1:** | ||
|
||
**Input:** n = 4 | ||
|
||
**Output:** 12 | ||
|
||
**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". | ||
|
||
**Example 2:** | ||
|
||
**Input:** n = 10 | ||
|
||
**Output:** 83943898 | ||
|
||
**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. | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= n <= 10<sup>5</sup></code> |
49 changes: 49 additions & 0 deletions
49
src/main/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package g2901_3000.s2931_maximum_spending_after_buying_items | ||
|
||
// #Hard #Array #Sorting #Greedy #Matrix #Heap_Priority_Queue | ||
// #2024_01_03_Time_541_ms_(93.75%)_Space_61.5_MB_(93.75%) | ||
|
||
class Solution { | ||
private class Node { | ||
var `val`: Int = -1 | ||
var next: Node? = null | ||
|
||
constructor(`val`: Int) { | ||
this.`val` = `val` | ||
} | ||
|
||
constructor() | ||
} | ||
|
||
fun maxSpending(values: Array<IntArray>): Long { | ||
val m = values.size | ||
val n = values[0].size | ||
val head = Node() | ||
var node: Node? = head | ||
for (j in n - 1 downTo 0) { | ||
node!!.next = Node(values[0][j]) | ||
node = node.next | ||
} | ||
for (i in 1 until m) { | ||
node = head | ||
for (j in n - 1 downTo 0) { | ||
while (node!!.next != null && node.next!!.`val` <= values[i][j]) { | ||
node = node.next | ||
} | ||
val next = node.next | ||
node.next = Node(values[i][j]) | ||
node = node.next | ||
node!!.next = next | ||
} | ||
} | ||
var res: Long = 0 | ||
var day: Long = 1 | ||
node = head.next | ||
while (node != null) { | ||
res += day * node.`val` | ||
node = node.next | ||
day++ | ||
} | ||
return res | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
2931\. Maximum Spending After Buying Items | ||
|
||
Hard | ||
|
||
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`. | ||
|
||
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: | ||
|
||
* Pick any shop `i`. | ||
* 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`. | ||
|
||
**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. | ||
|
||
Return _the **maximum amount of money that can be spent** on buying all_ `m * n` _products_. | ||
|
||
**Example 1:** | ||
|
||
**Input:** values = [[8,5,2],[6,4,1],[9,7,3]] | ||
|
||
**Output:** 285 | ||
|
||
**Explanation:** On the first day, we buy product 2 from shop 1 for a price of values[1][2] \* 1 = 1. | ||
|
||
On the second day, we buy product 2 from shop 0 for a price of values[0][2] \* 2 = 4. | ||
|
||
On the third day, we buy product 2 from shop 2 for a price of values[2][2] \* 3 = 9. | ||
|
||
On the fourth day, we buy product 1 from shop 1 for a price of values[1][1] \* 4 = 16. | ||
|
||
On the fifth day, we buy product 1 from shop 0 for a price of values[0][1] \* 5 = 25. | ||
|
||
On the sixth day, we buy product 0 from shop 1 for a price of values[1][0] \* 6 = 36. | ||
|
||
On the seventh day, we buy product 1 from shop 2 for a price of values[2][1] \* 7 = 49. | ||
|
||
On the eighth day, we buy product 0 from shop 0 for a price of values[0][0] \* 8 = 64. | ||
|
||
On the ninth day, we buy product 0 from shop 2 for a price of values[2][0] \* 9 = 81. | ||
|
||
Hence, our total spending is equal to 285. | ||
|
||
It can be shown that 285 is the maximum amount of money that can be spent buying all m \* n products. | ||
|
||
**Example 2:** | ||
|
||
**Input:** values = [[10,8,6,4,2],[9,7,5,3,2]] | ||
|
||
**Output:** 386 | ||
|
||
**Explanation:** On the first day, we buy product 4 from shop 0 for a price of values[0][4] \* 1 = 2. | ||
|
||
On the second day, we buy product 4 from shop 1 for a price of values[1][4] \* 2 = 4. | ||
|
||
On the third day, we buy product 3 from shop 1 for a price of values[1][3] \* 3 = 9. | ||
|
||
On the fourth day, we buy product 3 from shop 0 for a price of values[0][3] \* 4 = 16. | ||
|
||
On the fifth day, we buy product 2 from shop 1 for a price of values[1][2] \* 5 = 25. | ||
|
||
On the sixth day, we buy product 2 from shop 0 for a price of values[0][2] \* 6 = 36. | ||
|
||
On the seventh day, we buy product 1 from shop 1 for a price of values[1][1] \* 7 = 49. | ||
|
||
On the eighth day, we buy product 1 from shop 0 for a price of values[0][1] \* 8 = 64 | ||
|
||
On the ninth day, we buy product 0 from shop 1 for a price of values[1][0] \* 9 = 81. | ||
|
||
On the tenth day, we buy product 0 from shop 0 for a price of values[0][0] \* 10 = 100. | ||
|
||
Hence, our total spending is equal to 386. | ||
|
||
It can be shown that 386 is the maximum amount of money that can be spent buying all m \* n products. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= m == values.length <= 10` | ||
* <code>1 <= n == values[i].length <= 10<sup>4</sup></code> | ||
* <code>1 <= values[i][j] <= 10<sup>6</sup></code> | ||
* `values[i]` are sorted in non-increasing order. |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package g2901_3000.s2932_maximum_strong_pair_xor_i | ||
|
||
// #Easy #Array #Hash_Table #Bit_Manipulation #Sliding_Window #Trie | ||
// #2024_01_03_Time_192_ms_(43.08%)_Space_36.5_MB_(90.77%) | ||
|
||
import kotlin.math.abs | ||
import kotlin.math.max | ||
import kotlin.math.min | ||
|
||
class Solution { | ||
fun maximumStrongPairXor(nums: IntArray): Int { | ||
var max = 0 | ||
var pair: Int | ||
for (i in nums.indices) { | ||
for (j in i until nums.size) { | ||
if (abs((nums[i] - nums[j])) <= min(nums[i], nums[j])) { | ||
pair = nums[i] xor nums[j] | ||
max = max(max, pair) | ||
} | ||
} | ||
} | ||
return max | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
2932\. Maximum Strong Pair XOR I | ||
|
||
Easy | ||
|
||
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: | ||
|
||
* `|x - y| <= min(x, y)` | ||
|
||
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. | ||
|
||
Return _the **maximum**_ `XOR` _value out of all possible strong pairs in the array_ `nums`. | ||
|
||
**Note** that you can pick the same integer twice to form a pair. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [1,2,3,4,5] | ||
|
||
**Output:** 7 | ||
|
||
**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. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [10,100] | ||
|
||
**Output:** 0 | ||
|
||
**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. | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [5,6,25,30] | ||
|
||
**Output:** 7 | ||
|
||
**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. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= nums.length <= 50` | ||
* `1 <= nums[i] <= 100` |
70 changes: 70 additions & 0 deletions
70
src/main/kotlin/g2901_3000/s2933_high_access_employees/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package g2901_3000.s2933_high_access_employees | ||
|
||
// #Medium #Array #String #Hash_Table #Sorting | ||
// #2024_01_03_Time_304_ms_(91.67%)_Space_39.6_MB_(95.83%) | ||
|
||
class Solution { | ||
private fun isPossible(a: Int, b: Int): Boolean { | ||
val hb = b / 100 | ||
val ha = a / 100 | ||
var mind = b % 100 | ||
val mina = a % 100 | ||
if (hb == 23 && ha == 0) { | ||
return false | ||
} | ||
if (hb - ha > 1) { | ||
return false | ||
} | ||
if (hb - ha == 1) { | ||
mind += 60 | ||
} | ||
return mind - mina < 60 | ||
} | ||
|
||
private fun isHighAccess(list: List<Int>): Boolean { | ||
if (list.size < 3) { | ||
return false | ||
} | ||
var i = 0 | ||
var j = 1 | ||
var k = 2 | ||
while (k < list.size) { | ||
val a = list[i++] | ||
val b = list[j++] | ||
val c = list[k++] | ||
if (isPossible(a, c) && isPossible(b, c) && isPossible(a, b)) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
private fun stringToInt(str: String): Int { | ||
var i = 1000 | ||
var `val` = 0 | ||
for (ch in str.toCharArray()) { | ||
val n = ch.code - '0'.code | ||
`val` += i * n | ||
i = i / 10 | ||
} | ||
return `val` | ||
} | ||
|
||
fun findHighAccessEmployees(accessTimes: List<List<String>>): List<String> { | ||
val map = HashMap<String, MutableList<Int>>() | ||
for (list in accessTimes) { | ||
val temp = map.getOrDefault(list[0], ArrayList()) | ||
val `val` = stringToInt(list[1]) | ||
temp.add(`val`) | ||
map[list[0]] = temp | ||
} | ||
val ans: MutableList<String> = ArrayList() | ||
for ((key, temp) in map) { | ||
temp.sort() | ||
if (isHighAccess(temp)) { | ||
ans.add(key) | ||
} | ||
} | ||
return ans | ||
} | ||
} |
Oops, something went wrong.