Skip to content

Commit dac0f07

Browse files
authored
Added tasks 3264-3277
1 parent 3fbf09b commit dac0f07

File tree

13 files changed

+1343
-148
lines changed
  • src/main/kotlin/g3201_3300
    • s3264_final_array_state_after_k_multiplication_operations_i
    • s3265_count_almost_equal_pairs_i
    • s3266_final_array_state_after_k_multiplication_operations_ii
    • s3267_count_almost_equal_pairs_ii
    • s3270_find_the_key_of_the_numbers
    • s3271_hash_divided_string
    • s3272_find_the_count_of_good_integers
    • s3273_minimum_amount_of_damage_dealt_to_bob
    • s3274_check_if_two_chessboard_squares_have_the_same_color
    • s3275_k_th_nearest_obstacle_queries
    • s3276_select_cells_in_grid_with_maximum_score
    • s3277_maximum_xor_score_subarray_queries

13 files changed

+1343
-148
lines changed

README.md

Lines changed: 160 additions & 148 deletions
Large diffs are not rendered by default.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
## 3264\. Final Array State After K Multiplication Operations I
5+
6+
Easy
7+
8+
You are given an integer array `nums`, an integer `k`, and an integer `multiplier`.
9+
10+
You need to perform `k` operations on `nums`. In each operation:
11+
12+
* Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**.
13+
* Replace the selected minimum value `x` with `x * multiplier`.
14+
15+
Return an integer array denoting the _final state_ of `nums` after performing all `k` operations.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [2,1,3,5,6], k = 5, multiplier = 2
20+
21+
**Output:** [8,4,6,5,6]
22+
23+
**Explanation:**
24+
25+
| Operation | Result |
26+
|---------------------|------------------|
27+
| After operation 1 | [2, 2, 3, 5, 6] |
28+
| After operation 2 | [4, 2, 3, 5, 6] |
29+
| After operation 3 | [4, 4, 3, 5, 6] |
30+
| After operation 4 | [4, 4, 6, 5, 6] |
31+
| After operation 5 | [8, 4, 6, 5, 6] |
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [1,2], k = 3, multiplier = 4
36+
37+
**Output:** [16,8]
38+
39+
**Explanation:**
40+
41+
| Operation | Result |
42+
|---------------------|-------------|
43+
| After operation 1 | [4, 2] |
44+
| After operation 2 | [4, 8] |
45+
| After operation 3 | [16, 8] |
46+
47+
**Constraints:**
48+
49+
* `1 <= nums.length <= 100`
50+
* `1 <= nums[i] <= 100`
51+
* `1 <= k <= 10`
52+
* `1 <= multiplier <= 5`
53+
54+
## Solution
55+
56+
```kotlin
57+
@Suppress("NAME_SHADOWING")
58+
class Solution {
59+
fun getFinalState(nums: IntArray, k: Int, multiplier: Int): IntArray {
60+
var k = k
61+
while (k-- > 0) {
62+
var min = nums[0]
63+
var index = 0
64+
for (i in nums.indices) {
65+
if (min > nums[i]) {
66+
min = nums[i]
67+
index = i
68+
}
69+
}
70+
nums[index] = nums[index] * multiplier
71+
}
72+
return nums
73+
}
74+
}
75+
```
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
## 3265\. Count Almost Equal Pairs I
5+
6+
Medium
7+
8+
You are given an array `nums` consisting of positive integers.
9+
10+
We call two integers `x` and `y` in this problem **almost equal** if both integers can become equal after performing the following operation **at most once**:
11+
12+
* Choose **either** `x` or `y` and swap any two digits within the chosen number.
13+
14+
Return the number of indices `i` and `j` in `nums` where `i < j` such that `nums[i]` and `nums[j]` are **almost equal**.
15+
16+
**Note** that it is allowed for an integer to have leading zeros after performing an operation.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [3,12,30,17,21]
21+
22+
**Output:** 2
23+
24+
**Explanation:**
25+
26+
The almost equal pairs of elements are:
27+
28+
* 3 and 30. By swapping 3 and 0 in 30, you get 3.
29+
* 12 and 21. By swapping 1 and 2 in 12, you get 21.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [1,1,1,1,1]
34+
35+
**Output:** 10
36+
37+
**Explanation:**
38+
39+
Every two elements in the array are almost equal.
40+
41+
**Example 3:**
42+
43+
**Input:** nums = [123,231]
44+
45+
**Output:** 0
46+
47+
**Explanation:**
48+
49+
We cannot swap any two digits of 123 or 231 to reach the other.
50+
51+
**Constraints:**
52+
53+
* `2 <= nums.length <= 100`
54+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
55+
56+
## Solution
57+
58+
```kotlin
59+
@Suppress("NAME_SHADOWING")
60+
class Solution {
61+
fun countPairs(nums: IntArray): Int {
62+
var ans = 0
63+
for (i in 0 until nums.size - 1) {
64+
for (j in i + 1 until nums.size) {
65+
if (nums[i] == nums[j] ||
66+
((nums[j] - nums[i]) % 9 == 0 && check(nums[i], nums[j]))
67+
) {
68+
ans++
69+
}
70+
}
71+
}
72+
return ans
73+
}
74+
75+
private fun check(a: Int, b: Int): Boolean {
76+
var a = a
77+
var b = b
78+
val ca = IntArray(10)
79+
val cb = IntArray(10)
80+
var d = 0
81+
while (a > 0 || b > 0) {
82+
if (a % 10 != b % 10) {
83+
d++
84+
if (d > 2) {
85+
return false
86+
}
87+
}
88+
ca[a % 10]++
89+
cb[b % 10]++
90+
a /= 10
91+
b /= 10
92+
}
93+
return d == 2 && areEqual(ca, cb)
94+
}
95+
96+
private fun areEqual(a: IntArray, b: IntArray): Boolean {
97+
for (i in 0..9) {
98+
if (a[i] != b[i]) {
99+
return false
100+
}
101+
}
102+
return true
103+
}
104+
}
105+
```
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
## 3266\. Final Array State After K Multiplication Operations II
5+
6+
Hard
7+
8+
You are given an integer array `nums`, an integer `k`, and an integer `multiplier`.
9+
10+
You need to perform `k` operations on `nums`. In each operation:
11+
12+
* Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**.
13+
* Replace the selected minimum value `x` with `x * multiplier`.
14+
15+
After the `k` operations, apply **modulo** <code>10<sup>9</sup> + 7</code> to every value in `nums`.
16+
17+
Return an integer array denoting the _final state_ of `nums` after performing all `k` operations and then applying the modulo.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [2,1,3,5,6], k = 5, multiplier = 2
22+
23+
**Output:** [8,4,6,5,6]
24+
25+
**Explanation:**
26+
27+
| Operation | Result |
28+
|-------------------------|------------------|
29+
| After operation 1 | [2, 2, 3, 5, 6] |
30+
| After operation 2 | [4, 2, 3, 5, 6] |
31+
| After operation 3 | [4, 4, 3, 5, 6] |
32+
| After operation 4 | [4, 4, 6, 5, 6] |
33+
| After operation 5 | [8, 4, 6, 5, 6] |
34+
| After applying modulo | [8, 4, 6, 5, 6] |
35+
36+
**Example 2:**
37+
38+
**Input:** nums = [100000,2000], k = 2, multiplier = 1000000
39+
40+
**Output:** [999999307,999999993]
41+
42+
**Explanation:**
43+
44+
| Operation | Result |
45+
|-------------------------|----------------------|
46+
| After operation 1 | [100000, 2000000000] |
47+
| After operation 2 | [100000000000, 2000000000] |
48+
| After applying modulo | [999999307, 999999993] |
49+
50+
**Constraints:**
51+
52+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
53+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
54+
* <code>1 <= k <= 10<sup>9</sup></code>
55+
* <code>1 <= multiplier <= 10<sup>6</sup></code>
56+
57+
## Solution
58+
59+
```kotlin
60+
import java.util.PriorityQueue
61+
import kotlin.math.max
62+
63+
@Suppress("NAME_SHADOWING")
64+
class Solution {
65+
fun getFinalState(nums: IntArray, k: Int, multiplier: Int): IntArray {
66+
var k = k
67+
if (multiplier == 1) {
68+
return nums
69+
}
70+
val n = nums.size
71+
var mx = 0
72+
for (x in nums) {
73+
mx = max(mx, x)
74+
}
75+
val a = LongArray(n)
76+
var left = k
77+
var shouldExit = false
78+
run {
79+
var i = 0
80+
while (i < n && !shouldExit) {
81+
var x = nums[i].toLong()
82+
while (x < mx) {
83+
x *= multiplier.toLong()
84+
if (--left < 0) {
85+
shouldExit = true
86+
break
87+
}
88+
}
89+
a[i] = x
90+
i++
91+
}
92+
}
93+
if (left < 0) {
94+
val pq =
95+
PriorityQueue { p: LongArray, q: LongArray ->
96+
if (p[0] != q[0]
97+
) java.lang.Long.compare(p[0], q[0])
98+
else java.lang.Long.compare(p[1], q[1])
99+
}
100+
for (i in 0 until n) {
101+
pq.offer(longArrayOf(nums[i].toLong(), i.toLong()))
102+
}
103+
while (k-- > 0) {
104+
val p = pq.poll()
105+
p[0] *= multiplier.toLong()
106+
pq.offer(p)
107+
}
108+
while (pq.isNotEmpty()) {
109+
val p = pq.poll()
110+
nums[p[1].toInt()] = (p[0] % MOD).toInt()
111+
}
112+
return nums
113+
}
114+
115+
val ids: Array<Int> = Array(n) { i: Int -> i }
116+
ids.sortWith { i: Int?, j: Int? -> java.lang.Long.compare(a[i!!], a[j!!]) }
117+
k = left
118+
val pow1 = pow(multiplier.toLong(), k / n)
119+
val pow2 = pow1 * multiplier % MOD
120+
for (i in 0 until n) {
121+
val j = ids[i]
122+
nums[j] = (a[j] % MOD * (if (i < k % n) pow2 else pow1) % MOD).toInt()
123+
}
124+
return nums
125+
}
126+
127+
private fun pow(x: Long, n: Int): Long {
128+
var x = x
129+
var n = n
130+
var res: Long = 1
131+
while (n > 0) {
132+
if (n % 2 > 0) {
133+
res = res * x % MOD
134+
}
135+
x = x * x % MOD
136+
n /= 2
137+
}
138+
return res
139+
}
140+
141+
companion object {
142+
private const val MOD = 1000000007
143+
}
144+
}
145+
```

0 commit comments

Comments
 (0)