Skip to content

Commit 594a15c

Browse files
authored
Added tasks 3264-3267
1 parent 784b306 commit 594a15c

File tree

12 files changed

+503
-0
lines changed

12 files changed

+503
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3201_3300.s3264_final_array_state_after_k_multiplication_operations_i
2+
3+
// #Easy #2024_08_28_Time_226_ms_(68.00%)_Space_38.5_MB_(66.00%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun getFinalState(nums: IntArray, k: Int, multiplier: Int): IntArray {
8+
var k = k
9+
while (k-- > 0) {
10+
var min = nums[0]
11+
var index = 0
12+
for (i in nums.indices) {
13+
if (min > nums[i]) {
14+
min = nums[i]
15+
index = i
16+
}
17+
}
18+
nums[index] = nums[index] * multiplier
19+
}
20+
return nums
21+
}
22+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
3264\. Final Array State After K Multiplication Operations I
2+
3+
Easy
4+
5+
You are given an integer array `nums`, an integer `k`, and an integer `multiplier`.
6+
7+
You need to perform `k` operations on `nums`. In each operation:
8+
9+
* Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**.
10+
* Replace the selected minimum value `x` with `x * multiplier`.
11+
12+
Return an integer array denoting the _final state_ of `nums` after performing all `k` operations.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,1,3,5,6], k = 5, multiplier = 2
17+
18+
**Output:** [8,4,6,5,6]
19+
20+
**Explanation:**
21+
22+
| Operation | Result |
23+
|---------------------|------------------|
24+
| After operation 1 | [2, 2, 3, 5, 6] |
25+
| After operation 2 | [4, 2, 3, 5, 6] |
26+
| After operation 3 | [4, 4, 3, 5, 6] |
27+
| After operation 4 | [4, 4, 6, 5, 6] |
28+
| After operation 5 | [8, 4, 6, 5, 6] |
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [1,2], k = 3, multiplier = 4
33+
34+
**Output:** [16,8]
35+
36+
**Explanation:**
37+
38+
| Operation | Result |
39+
|---------------------|-------------|
40+
| After operation 1 | [4, 2] |
41+
| After operation 2 | [4, 8] |
42+
| After operation 3 | [16, 8] |
43+
44+
**Constraints:**
45+
46+
* `1 <= nums.length <= 100`
47+
* `1 <= nums[i] <= 100`
48+
* `1 <= k <= 10`
49+
* `1 <= multiplier <= 5`
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g3201_3300.s3265_count_almost_equal_pairs_i
2+
3+
// #Medium #2024_08_28_Time_188_ms_(100.00%)_Space_37.5_MB_(100.00%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun countPairs(nums: IntArray): Int {
8+
var ans = 0
9+
for (i in 0 until nums.size - 1) {
10+
for (j in i + 1 until nums.size) {
11+
if (nums[i] == nums[j] ||
12+
((nums[j] - nums[i]) % 9 == 0 && check(nums[i], nums[j]))
13+
) {
14+
ans++
15+
}
16+
}
17+
}
18+
return ans
19+
}
20+
21+
private fun check(a: Int, b: Int): Boolean {
22+
var a = a
23+
var b = b
24+
val ca = IntArray(10)
25+
val cb = IntArray(10)
26+
var d = 0
27+
while (a > 0 || b > 0) {
28+
if (a % 10 != b % 10) {
29+
d++
30+
if (d > 2) {
31+
return false
32+
}
33+
}
34+
ca[a % 10]++
35+
cb[b % 10]++
36+
a /= 10
37+
b /= 10
38+
}
39+
return d == 2 && areEqual(ca, cb)
40+
}
41+
42+
private fun areEqual(a: IntArray, b: IntArray): Boolean {
43+
for (i in 0..9) {
44+
if (a[i] != b[i]) {
45+
return false
46+
}
47+
}
48+
return true
49+
}
50+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3265\. Count Almost Equal Pairs I
2+
3+
Medium
4+
5+
You are given an array `nums` consisting of positive integers.
6+
7+
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**:
8+
9+
* Choose **either** `x` or `y` and swap any two digits within the chosen number.
10+
11+
Return the number of indices `i` and `j` in `nums` where `i < j` such that `nums[i]` and `nums[j]` are **almost equal**.
12+
13+
**Note** that it is allowed for an integer to have leading zeros after performing an operation.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [3,12,30,17,21]
18+
19+
**Output:** 2
20+
21+
**Explanation:**
22+
23+
The almost equal pairs of elements are:
24+
25+
* 3 and 30. By swapping 3 and 0 in 30, you get 3.
26+
* 12 and 21. By swapping 1 and 2 in 12, you get 21.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [1,1,1,1,1]
31+
32+
**Output:** 10
33+
34+
**Explanation:**
35+
36+
Every two elements in the array are almost equal.
37+
38+
**Example 3:**
39+
40+
**Input:** nums = [123,231]
41+
42+
**Output:** 0
43+
44+
**Explanation:**
45+
46+
We cannot swap any two digits of 123 or 231 to reach the other.
47+
48+
**Constraints:**
49+
50+
* `2 <= nums.length <= 100`
51+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package g3201_3300.s3266_final_array_state_after_k_multiplication_operations_ii
2+
3+
// #Hard #2024_08_28_Time_546_ms_(100.00%)_Space_60.8_MB_(66.67%)
4+
5+
import java.util.PriorityQueue
6+
import kotlin.math.max
7+
8+
@Suppress("NAME_SHADOWING")
9+
class Solution {
10+
fun getFinalState(nums: IntArray, k: Int, multiplier: Int): IntArray {
11+
var k = k
12+
if (multiplier == 1) {
13+
return nums
14+
}
15+
val n = nums.size
16+
var mx = 0
17+
for (x in nums) {
18+
mx = max(mx, x)
19+
}
20+
val a = LongArray(n)
21+
var left = k
22+
var shouldExit = false
23+
run {
24+
var i = 0
25+
while (i < n && !shouldExit) {
26+
var x = nums[i].toLong()
27+
while (x < mx) {
28+
x *= multiplier.toLong()
29+
if (--left < 0) {
30+
shouldExit = true
31+
break
32+
}
33+
}
34+
a[i] = x
35+
i++
36+
}
37+
}
38+
if (left < 0) {
39+
val pq =
40+
PriorityQueue { p: LongArray, q: LongArray ->
41+
if (p[0] != q[0]
42+
) java.lang.Long.compare(p[0], q[0])
43+
else java.lang.Long.compare(p[1], q[1])
44+
}
45+
for (i in 0 until n) {
46+
pq.offer(longArrayOf(nums[i].toLong(), i.toLong()))
47+
}
48+
while (k-- > 0) {
49+
val p = pq.poll()
50+
p[0] *= multiplier.toLong()
51+
pq.offer(p)
52+
}
53+
while (pq.isNotEmpty()) {
54+
val p = pq.poll()
55+
nums[p[1].toInt()] = (p[0] % MOD).toInt()
56+
}
57+
return nums
58+
}
59+
60+
val ids: Array<Int> = Array(n) { i: Int -> i }
61+
ids.sortWith { i: Int?, j: Int? -> java.lang.Long.compare(a[i!!], a[j!!]) }
62+
k = left
63+
val pow1 = pow(multiplier.toLong(), k / n)
64+
val pow2 = pow1 * multiplier % MOD
65+
for (i in 0 until n) {
66+
val j = ids[i]
67+
nums[j] = (a[j] % MOD * (if (i < k % n) pow2 else pow1) % MOD).toInt()
68+
}
69+
return nums
70+
}
71+
72+
private fun pow(x: Long, n: Int): Long {
73+
var x = x
74+
var n = n
75+
var res: Long = 1
76+
while (n > 0) {
77+
if (n % 2 > 0) {
78+
res = res * x % MOD
79+
}
80+
x = x * x % MOD
81+
n /= 2
82+
}
83+
return res
84+
}
85+
86+
companion object {
87+
private const val MOD = 1000000007
88+
}
89+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
3266\. Final Array State After K Multiplication Operations II
2+
3+
Hard
4+
5+
You are given an integer array `nums`, an integer `k`, and an integer `multiplier`.
6+
7+
You need to perform `k` operations on `nums`. In each operation:
8+
9+
* Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**.
10+
* Replace the selected minimum value `x` with `x * multiplier`.
11+
12+
After the `k` operations, apply **modulo** <code>10<sup>9</sup> + 7</code> to every value in `nums`.
13+
14+
Return an integer array denoting the _final state_ of `nums` after performing all `k` operations and then applying the modulo.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,1,3,5,6], k = 5, multiplier = 2
19+
20+
**Output:** [8,4,6,5,6]
21+
22+
**Explanation:**
23+
24+
| Operation | Result |
25+
|-------------------------|------------------|
26+
| After operation 1 | [2, 2, 3, 5, 6] |
27+
| After operation 2 | [4, 2, 3, 5, 6] |
28+
| After operation 3 | [4, 4, 3, 5, 6] |
29+
| After operation 4 | [4, 4, 6, 5, 6] |
30+
| After operation 5 | [8, 4, 6, 5, 6] |
31+
| After applying modulo | [8, 4, 6, 5, 6] |
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [100000,2000], k = 2, multiplier = 1000000
36+
37+
**Output:** [999999307,999999993]
38+
39+
**Explanation:**
40+
41+
| Operation | Result |
42+
|-------------------------|----------------------|
43+
| After operation 1 | [100000, 2000000000] |
44+
| After operation 2 | [100000000000, 2000000000] |
45+
| After applying modulo | [999999307, 999999993] |
46+
47+
**Constraints:**
48+
49+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
50+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
51+
* <code>1 <= k <= 10<sup>9</sup></code>
52+
* <code>1 <= multiplier <= 10<sup>6</sup></code>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package g3201_3300.s3267_count_almost_equal_pairs_ii
2+
3+
// #Hard #2024_08_28_Time_791_ms_(100.00%)_Space_50.7_MB_(57.14%)
4+
5+
class Solution {
6+
fun countPairs(nums: IntArray): Int {
7+
var pairs = 0
8+
val counts: MutableMap<Int, Int> = HashMap()
9+
nums.sort()
10+
for (num in nums) {
11+
val newNums: MutableSet<Int> = HashSet()
12+
newNums.add(num)
13+
var unit1 = 1
14+
var remain1 = num
15+
while (remain1 > 0) {
16+
val digit1 = num / unit1 % 10
17+
var unit2 = unit1 * 10
18+
var remain2 = remain1 / 10
19+
while (remain2 > 0
20+
) {
21+
val digit2 = num / unit2 % 10
22+
val newNum1 =
23+
num - digit1 * unit1 - digit2 * unit2 + digit2 * unit1 + digit1 * unit2
24+
newNums.add(newNum1)
25+
var unit3 = unit1 * 10
26+
var remain3 = remain1 / 10
27+
while (remain3 > 0
28+
) {
29+
val digit3 = newNum1 / unit3 % 10
30+
var unit4 = unit3 * 10
31+
var remain4 = remain3 / 10
32+
while (remain4 > 0
33+
) {
34+
val digit4 = newNum1 / unit4 % 10
35+
val newNum2 =
36+
newNum1 - digit3 * unit3 - digit4 * unit4 + digit4 * unit3 + digit3 * unit4
37+
newNums.add(newNum2)
38+
unit4 *= 10
39+
remain4 /= 10
40+
}
41+
unit3 *= 10
42+
remain3 /= 10
43+
}
44+
unit2 *= 10
45+
remain2 /= 10
46+
}
47+
unit1 *= 10
48+
remain1 /= 10
49+
}
50+
for (newNum in newNums) {
51+
pairs += counts.getOrDefault(newNum, 0)
52+
}
53+
counts[num] = counts.getOrDefault(num, 0) + 1
54+
}
55+
return pairs
56+
}
57+
}

0 commit comments

Comments
 (0)