Skip to content

Commit 24f92f0

Browse files
committed
Added tasks 3550-3553
1 parent b2abc8a commit 24f92f0

File tree

6 files changed

+581
-63
lines changed
  • src/main/kotlin
    • g3301_3400/s3337_total_characters_in_string_after_transformations_ii
    • g3501_3600
      • s3550_smallest_index_with_digit_sum_equal_to_index
      • s3551_minimum_swaps_to_sort_by_digit_sum
      • s3552_grid_teleportation_traversal
      • s3553_minimum_weighted_subgraph_with_the_required_paths_ii

6 files changed

+581
-63
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,10 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3553 |[Minimum Weighted Subgraph With the Required Paths II](src/main/kotlin/g3501_3600/s3553_minimum_weighted_subgraph_with_the_required_paths_ii)| Hard | Array, Depth_First_Search, Tree | 142 | 100.00
2092+
| 3552 |[Grid Teleportation Traversal](src/main/kotlin/g3501_3600/s3552_grid_teleportation_traversal)| Medium | Array, Hash_Table, Breadth_First_Search, Matrix | 147 | 100.00
2093+
| 3551 |[Minimum Swaps to Sort by Digit Sum](src/main/kotlin/g3501_3600/s3551_minimum_swaps_to_sort_by_digit_sum)| Medium | Array, Hash_Table, Sorting | 481 | 83.33
2094+
| 3550 |[Smallest Index With Digit Sum Equal to Index](src/main/kotlin/g3501_3600/s3550_smallest_index_with_digit_sum_equal_to_index)| Easy | Array, Math | 1 | 100.00
20912095
| 3548 |[Equal Sum Grid Partition II](src/main/kotlin/g3501_3600/s3548_equal_sum_grid_partition_ii)| Hard | Array, Hash_Table, Matrix, Prefix_Sum, Enumeration | 61 | 100.00
20922096
| 3547 |[Maximum Sum of Edge Values in a Graph](src/main/kotlin/g3501_3600/s3547_maximum_sum_of_edge_values_in_a_graph)| Hard | Sorting, Depth_First_Search, Greedy, Graph | 61 | 100.00
20932097
| 3546 |[Equal Sum Grid Partition I](src/main/kotlin/g3501_3600/s3546_equal_sum_grid_partition_i)| Medium | Array, Matrix, Prefix_Sum, Enumeration | 7 | 82.61
@@ -2265,7 +2269,7 @@
22652269
| 3342 |[Find Minimum Time to Reach Last Room II](src/main/kotlin/g3301_3400/s3342_find_minimum_time_to_reach_last_room_ii)| Medium | Array, Matrix, Heap_Priority_Queue, Graph, Shortest_Path | 122 | 100.00
22662270
| 3341 |[Find Minimum Time to Reach Last Room I](src/main/kotlin/g3301_3400/s3341_find_minimum_time_to_reach_last_room_i)| Medium | Array, Matrix, Heap_Priority_Queue, Graph, Shortest_Path | 257 | 42.10
22672271
| 3340 |[Check Balanced String](src/main/kotlin/g3301_3400/s3340_check_balanced_string)| Easy | String | 1 | 100.00
2268-
| 3337 |[Total Characters in String After Transformations II](src/main/kotlin/g3301_3400/s3337_total_characters_in_string_after_transformations_ii)| Hard | String, Hash_Table, Dynamic_Programming, Math, Counting | 320 | 100.00
2272+
| 3337 |[Total Characters in String After Transformations II](src/main/kotlin/g3301_3400/s3337_total_characters_in_string_after_transformations_ii)| Hard | String, Hash_Table, Dynamic_Programming, Math, Counting | 302 | 100.00
22692273
| 3336 |[Find the Number of Subsequences With Equal GCD](src/main/kotlin/g3301_3400/s3336_find_the_number_of_subsequences_with_equal_gcd)| Hard | Array, Dynamic_Programming, Math, Number_Theory | 324 | 100.00
22702274
| 3335 |[Total Characters in String After Transformations I](src/main/kotlin/g3301_3400/s3335_total_characters_in_string_after_transformations_i)| Medium | String, Hash_Table, Dynamic_Programming, Math, Counting | 58 | 80.00
22712275
| 3334 |[Find the Maximum Factor Score of Array](src/main/kotlin/g3301_3400/s3334_find_the_maximum_factor_score_of_array)| Medium | Array, Math, Number_Theory | 4 | 95.83

src/main/kotlin/g3301_3400/s3337_total_characters_in_string_after_transformations_ii/readme.md

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -74,87 +74,70 @@ Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> +
7474
```kotlin
7575
class Solution {
7676
fun lengthAfterTransformations(s: String, t: Int, nums: List<Int>): Int {
77-
val m = Array<IntArray>(26) { IntArray(26) }
78-
for (i in 0..25) {
79-
for (j in 1..nums[i]) {
80-
m[(i + j) % 26][i] = m[(i + j) % 26][i] + 1
81-
}
82-
}
83-
var v = IntArray(26)
77+
val localT = buildTransformationMatrix(nums)
78+
val tPower = matrixPower(localT, t)
79+
val freq = IntArray(26)
8480
for (c in s.toCharArray()) {
85-
v[c.code - 'a'.code]++
81+
freq[c.code - 'a'.code]++
8682
}
87-
v = pow(m, v, t.toLong())
88-
var ans: Long = 0
89-
for (x in v) {
90-
ans += x.toLong()
83+
var result: Long = 0
84+
for (i in 0..25) {
85+
var sum: Long = 0
86+
for (j in 0..25) {
87+
sum = (sum + freq[j].toLong() * tPower[j][i]) % MOD
88+
}
89+
result = (result + sum) % MOD
9190
}
92-
return (ans % MOD).toInt()
91+
return result.toInt()
9392
}
9493

95-
// A^e*v
96-
private fun pow(a: Array<IntArray>, v: IntArray, e: Long): IntArray {
97-
var v = v
98-
var e = e
99-
for (i in v.indices) {
100-
if (v[i] >= MOD) {
101-
v[i] %= MOD
102-
}
103-
}
104-
var mul = a
105-
while (e > 0) {
106-
if ((e and 1L) == 1L) {
107-
v = mul(mul, v)
94+
private fun buildTransformationMatrix(numsList: List<Int>): Array<IntArray> {
95+
val localT = Array(26) { IntArray(26) }
96+
for (i in 0..25) {
97+
val steps: Int = numsList[i]
98+
for (j in 1..steps) {
99+
localT[i][(i + j) % 26] = localT[i][(i + j) % 26] + 1
108100
}
109-
mul = p2(mul)
110-
e = e ushr 1
111101
}
112-
return v
102+
return localT
113103
}
114104

115-
// int matrix*int vector
116-
private fun mul(a: Array<IntArray>, v: IntArray): IntArray {
117-
val m = a.size
118-
val n = v.size
119-
val w = IntArray(m)
120-
for (i in 0 until m) {
121-
var sum: Long = 0
122-
for (k in 0 until n) {
123-
sum += a[i][k].toLong() * v[k]
124-
if (sum >= BIG) {
125-
sum -= BIG
126-
}
105+
private fun matrixPower(matrix: Array<IntArray>, power: Int): Array<IntArray> {
106+
var matrix = matrix
107+
var power = power
108+
val size = matrix.size
109+
var result = Array(size) { IntArray(size) }
110+
for (i in 0..<size) {
111+
result[i][i] = 1
112+
}
113+
while (power > 0) {
114+
if ((power and 1) == 1) {
115+
result = multiplyMatrices(result, matrix)
127116
}
128-
w[i] = (sum % MOD).toInt()
117+
matrix = multiplyMatrices(matrix, matrix)
118+
power = power shr 1
129119
}
130-
return w
120+
return result
131121
}
132122

133-
// int matrix^2 (be careful about negative value)
134-
private fun p2(a: Array<IntArray>): Array<IntArray> {
135-
val n = a.size
136-
val c = Array<IntArray>(n) { IntArray(n) }
137-
for (i in 0 until n) {
138-
val sum = LongArray(n)
139-
for (k in 0 until n) {
140-
for (j in 0 until n) {
141-
sum[j] += a[i][k].toLong() * a[k][j]
142-
if (sum[j] >= BIG) {
143-
sum[j] -= BIG
144-
}
123+
private fun multiplyMatrices(a: Array<IntArray>, b: Array<IntArray>): Array<IntArray> {
124+
val size = a.size
125+
val result = Array(size) { IntArray(size) }
126+
for (i in 0..<size) {
127+
for (k in 0..<size) {
128+
if (a[i][k] == 0) {
129+
continue
130+
}
131+
for (j in 0..<size) {
132+
result[i][j] = ((result[i][j] + a[i][k].toLong() * b[k][j]) % MOD).toInt()
145133
}
146-
}
147-
for (j in 0 until n) {
148-
c[i][j] = (sum[j] % MOD).toInt()
149134
}
150135
}
151-
return c
136+
return result
152137
}
153138

154139
companion object {
155-
const val MOD: Int = 1000000007
156-
const val M2: Long = MOD.toLong() * MOD
157-
const val BIG: Long = 8L * M2
140+
private const val MOD = 1000000007
158141
}
159142
}
160143
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
## 3550\. Smallest Index With Digit Sum Equal to Index
5+
6+
Easy
7+
8+
You are given an integer array `nums`.
9+
10+
Return the **smallest** index `i` such that the sum of the digits of `nums[i]` is equal to `i`.
11+
12+
If no such index exists, return `-1`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,3,2]
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
* For `nums[2] = 2`, the sum of digits is 2, which is equal to index `i = 2`. Thus, the output is 2.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [1,10,11]
27+
28+
**Output:** 1
29+
30+
**Explanation:**
31+
32+
* For `nums[1] = 10`, the sum of digits is `1 + 0 = 1`, which is equal to index `i = 1`.
33+
* For `nums[2] = 11`, the sum of digits is `1 + 1 = 2`, which is equal to index `i = 2`.
34+
* Since index 1 is the smallest, the output is 1.
35+
36+
**Example 3:**
37+
38+
**Input:** nums = [1,2,3]
39+
40+
**Output:** \-1
41+
42+
**Explanation:**
43+
44+
* Since no index satisfies the condition, the output is -1.
45+
46+
**Constraints:**
47+
48+
* `1 <= nums.length <= 100`
49+
* `0 <= nums[i] <= 1000`
50+
51+
## Solution
52+
53+
```kotlin
54+
class Solution {
55+
private fun sum(num: Int): Int {
56+
var num = num
57+
var s = 0
58+
while (num > 0) {
59+
s += num % 10
60+
num /= 10
61+
}
62+
return s
63+
}
64+
65+
fun smallestIndex(nums: IntArray): Int {
66+
for (i in nums.indices) {
67+
if (i == sum(nums[i])) {
68+
return i
69+
}
70+
}
71+
return -1
72+
}
73+
}
74+
```
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
## 3551\. Minimum Swaps to Sort by Digit Sum
5+
6+
Medium
7+
8+
You are given an array `nums` of **distinct** positive integers. You need to sort the array in **increasing** order based on the sum of the digits of each number. If two numbers have the same digit sum, the **smaller** number appears first in the sorted order.
9+
10+
Return the **minimum** number of swaps required to rearrange `nums` into this sorted order.
11+
12+
A **swap** is defined as exchanging the values at two distinct positions in the array.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [37,100]
17+
18+
**Output:** 1
19+
20+
**Explanation:**
21+
22+
* Compute the digit sum for each integer: `[3 + 7 = 10, 1 + 0 + 0 = 1] → [10, 1]`
23+
* Sort the integers based on digit sum: `[100, 37]`. Swap `37` with `100` to obtain the sorted order.
24+
* Thus, the minimum number of swaps required to rearrange `nums` is 1.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [22,14,33,7]
29+
30+
**Output:** 0
31+
32+
**Explanation:**
33+
34+
* Compute the digit sum for each integer: `[2 + 2 = 4, 1 + 4 = 5, 3 + 3 = 6, 7 = 7] → [4, 5, 6, 7]`
35+
* Sort the integers based on digit sum: `[22, 14, 33, 7]`. The array is already sorted.
36+
* Thus, the minimum number of swaps required to rearrange `nums` is 0.
37+
38+
**Example 3:**
39+
40+
**Input:** nums = [18,43,34,16]
41+
42+
**Output:** 2
43+
44+
**Explanation:**
45+
46+
* Compute the digit sum for each integer: `[1 + 8 = 9, 4 + 3 = 7, 3 + 4 = 7, 1 + 6 = 7] → [9, 7, 7, 7]`
47+
* Sort the integers based on digit sum: `[16, 34, 43, 18]`. Swap `18` with `16`, and swap `43` with `34` to obtain the sorted order.
48+
* Thus, the minimum number of swaps required to rearrange `nums` is 2.
49+
50+
**Constraints:**
51+
52+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
53+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
54+
* `nums` consists of **distinct** positive integers.
55+
56+
## Solution
57+
58+
```kotlin
59+
class Solution {
60+
private class Pair(var sum: Int, var value: Int, var index: Int)
61+
62+
fun minSwaps(arr: IntArray): Int {
63+
val n = arr.size
64+
val pairs = arrayOfNulls<Pair>(n)
65+
for (i in 0..<n) {
66+
var v = arr[i]
67+
var s = 0
68+
while (v > 0) {
69+
s += v % 10
70+
v /= 10
71+
}
72+
pairs[i] = Pair(s, arr[i], i)
73+
}
74+
pairs.sortWith { a, b ->
75+
if (a!!.sum != b!!.sum) {
76+
a.sum - b.sum
77+
} else {
78+
a.value - b.value
79+
}
80+
}
81+
val posMap = IntArray(n)
82+
for (i in 0..<n) {
83+
posMap[i] = pairs[i]!!.index
84+
}
85+
val seen = BooleanArray(n)
86+
var swaps = 0
87+
for (i in 0..<n) {
88+
if (seen[i] || posMap[i] == i) {
89+
continue
90+
}
91+
var cycleSize = 0
92+
var j = i
93+
while (!seen[j]) {
94+
seen[j] = true
95+
j = posMap[j]
96+
cycleSize++
97+
}
98+
swaps += cycleSize - 1
99+
}
100+
return swaps
101+
}
102+
}
103+
```

0 commit comments

Comments
 (0)