Skip to content

Added tasks 3264-3267 #684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g3201_3300.s3264_final_array_state_after_k_multiplication_operations_i

// #Easy #2024_08_28_Time_226_ms_(68.00%)_Space_38.5_MB_(66.00%)

@Suppress("NAME_SHADOWING")
class Solution {
fun getFinalState(nums: IntArray, k: Int, multiplier: Int): IntArray {
var k = k
while (k-- > 0) {
var min = nums[0]
var index = 0
for (i in nums.indices) {
if (min > nums[i]) {
min = nums[i]
index = i
}
}
nums[index] = nums[index] * multiplier
}
return nums
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
3264\. Final Array State After K Multiplication Operations I

Easy

You are given an integer array `nums`, an integer `k`, and an integer `multiplier`.

You need to perform `k` operations on `nums`. In each operation:

* Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**.
* Replace the selected minimum value `x` with `x * multiplier`.

Return an integer array denoting the _final state_ of `nums` after performing all `k` operations.

**Example 1:**

**Input:** nums = [2,1,3,5,6], k = 5, multiplier = 2

**Output:** [8,4,6,5,6]

**Explanation:**

| Operation | Result |
|---------------------|------------------|
| After operation 1 | [2, 2, 3, 5, 6] |
| After operation 2 | [4, 2, 3, 5, 6] |
| After operation 3 | [4, 4, 3, 5, 6] |
| After operation 4 | [4, 4, 6, 5, 6] |
| After operation 5 | [8, 4, 6, 5, 6] |

**Example 2:**

**Input:** nums = [1,2], k = 3, multiplier = 4

**Output:** [16,8]

**Explanation:**

| Operation | Result |
|---------------------|-------------|
| After operation 1 | [4, 2] |
| After operation 2 | [4, 8] |
| After operation 3 | [16, 8] |

**Constraints:**

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
* `1 <= k <= 10`
* `1 <= multiplier <= 5`
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package g3201_3300.s3265_count_almost_equal_pairs_i

// #Medium #2024_08_28_Time_188_ms_(100.00%)_Space_37.5_MB_(100.00%)

@Suppress("NAME_SHADOWING")
class Solution {
fun countPairs(nums: IntArray): Int {
var ans = 0
for (i in 0 until nums.size - 1) {
for (j in i + 1 until nums.size) {
if (nums[i] == nums[j] ||
((nums[j] - nums[i]) % 9 == 0 && check(nums[i], nums[j]))
) {
ans++
}
}
}
return ans
}

private fun check(a: Int, b: Int): Boolean {
var a = a
var b = b
val ca = IntArray(10)
val cb = IntArray(10)
var d = 0
while (a > 0 || b > 0) {
if (a % 10 != b % 10) {
d++
if (d > 2) {
return false
}
}
ca[a % 10]++
cb[b % 10]++
a /= 10
b /= 10
}
return d == 2 && areEqual(ca, cb)
}

private fun areEqual(a: IntArray, b: IntArray): Boolean {
for (i in 0..9) {
if (a[i] != b[i]) {
return false
}
}
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
3265\. Count Almost Equal Pairs I

Medium

You are given an array `nums` consisting of positive integers.

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**:

* Choose **either** `x` or `y` and swap any two digits within the chosen number.

Return the number of indices `i` and `j` in `nums` where `i < j` such that `nums[i]` and `nums[j]` are **almost equal**.

**Note** that it is allowed for an integer to have leading zeros after performing an operation.

**Example 1:**

**Input:** nums = [3,12,30,17,21]

**Output:** 2

**Explanation:**

The almost equal pairs of elements are:

* 3 and 30. By swapping 3 and 0 in 30, you get 3.
* 12 and 21. By swapping 1 and 2 in 12, you get 21.

**Example 2:**

**Input:** nums = [1,1,1,1,1]

**Output:** 10

**Explanation:**

Every two elements in the array are almost equal.

**Example 3:**

**Input:** nums = [123,231]

**Output:** 0

**Explanation:**

We cannot swap any two digits of 123 or 231 to reach the other.

**Constraints:**

* `2 <= nums.length <= 100`
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package g3201_3300.s3266_final_array_state_after_k_multiplication_operations_ii

// #Hard #2024_08_28_Time_546_ms_(100.00%)_Space_60.8_MB_(66.67%)

import java.util.PriorityQueue
import kotlin.math.max

@Suppress("NAME_SHADOWING")
class Solution {
fun getFinalState(nums: IntArray, k: Int, multiplier: Int): IntArray {
var k = k
if (multiplier == 1) {
return nums
}
val n = nums.size
var mx = 0
for (x in nums) {
mx = max(mx, x)
}
val a = LongArray(n)
var left = k
var shouldExit = false
run {
var i = 0
while (i < n && !shouldExit) {
var x = nums[i].toLong()
while (x < mx) {
x *= multiplier.toLong()
if (--left < 0) {
shouldExit = true
break
}
}
a[i] = x
i++
}
}
if (left < 0) {
val pq =
PriorityQueue { p: LongArray, q: LongArray ->
if (p[0] != q[0]
) java.lang.Long.compare(p[0], q[0])
else java.lang.Long.compare(p[1], q[1])
}
for (i in 0 until n) {
pq.offer(longArrayOf(nums[i].toLong(), i.toLong()))
}
while (k-- > 0) {
val p = pq.poll()
p[0] *= multiplier.toLong()
pq.offer(p)
}
while (pq.isNotEmpty()) {
val p = pq.poll()
nums[p[1].toInt()] = (p[0] % MOD).toInt()
}
return nums
}

val ids: Array<Int> = Array(n) { i: Int -> i }
ids.sortWith { i: Int?, j: Int? -> java.lang.Long.compare(a[i!!], a[j!!]) }
k = left
val pow1 = pow(multiplier.toLong(), k / n)
val pow2 = pow1 * multiplier % MOD
for (i in 0 until n) {
val j = ids[i]
nums[j] = (a[j] % MOD * (if (i < k % n) pow2 else pow1) % MOD).toInt()
}
return nums
}

private fun pow(x: Long, n: Int): Long {
var x = x
var n = n
var res: Long = 1
while (n > 0) {
if (n % 2 > 0) {
res = res * x % MOD
}
x = x * x % MOD
n /= 2
}
return res
}

companion object {
private const val MOD = 1000000007
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
3266\. Final Array State After K Multiplication Operations II

Hard

You are given an integer array `nums`, an integer `k`, and an integer `multiplier`.

You need to perform `k` operations on `nums`. In each operation:

* Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**.
* Replace the selected minimum value `x` with `x * multiplier`.

After the `k` operations, apply **modulo** <code>10<sup>9</sup> + 7</code> to every value in `nums`.

Return an integer array denoting the _final state_ of `nums` after performing all `k` operations and then applying the modulo.

**Example 1:**

**Input:** nums = [2,1,3,5,6], k = 5, multiplier = 2

**Output:** [8,4,6,5,6]

**Explanation:**

| Operation | Result |
|-------------------------|------------------|
| After operation 1 | [2, 2, 3, 5, 6] |
| After operation 2 | [4, 2, 3, 5, 6] |
| After operation 3 | [4, 4, 3, 5, 6] |
| After operation 4 | [4, 4, 6, 5, 6] |
| After operation 5 | [8, 4, 6, 5, 6] |
| After applying modulo | [8, 4, 6, 5, 6] |

**Example 2:**

**Input:** nums = [100000,2000], k = 2, multiplier = 1000000

**Output:** [999999307,999999993]

**Explanation:**

| Operation | Result |
|-------------------------|----------------------|
| After operation 1 | [100000, 2000000000] |
| After operation 2 | [100000000000, 2000000000] |
| After applying modulo | [999999307, 999999993] |

**Constraints:**

* <code>1 <= nums.length <= 10<sup>4</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* <code>1 <= k <= 10<sup>9</sup></code>
* <code>1 <= multiplier <= 10<sup>6</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package g3201_3300.s3267_count_almost_equal_pairs_ii

// #Hard #2024_08_28_Time_791_ms_(100.00%)_Space_50.7_MB_(57.14%)

class Solution {
fun countPairs(nums: IntArray): Int {
var pairs = 0
val counts: MutableMap<Int, Int> = HashMap()
nums.sort()
for (num in nums) {
val newNums: MutableSet<Int> = HashSet()
newNums.add(num)
var unit1 = 1
var remain1 = num
while (remain1 > 0) {
val digit1 = num / unit1 % 10
var unit2 = unit1 * 10
var remain2 = remain1 / 10
while (remain2 > 0
) {
val digit2 = num / unit2 % 10
val newNum1 =
num - digit1 * unit1 - digit2 * unit2 + digit2 * unit1 + digit1 * unit2
newNums.add(newNum1)
var unit3 = unit1 * 10
var remain3 = remain1 / 10
while (remain3 > 0
) {
val digit3 = newNum1 / unit3 % 10
var unit4 = unit3 * 10
var remain4 = remain3 / 10
while (remain4 > 0
) {
val digit4 = newNum1 / unit4 % 10
val newNum2 =
newNum1 - digit3 * unit3 - digit4 * unit4 + digit4 * unit3 + digit3 * unit4
newNums.add(newNum2)
unit4 *= 10
remain4 /= 10
}
unit3 *= 10
remain3 /= 10
}
unit2 *= 10
remain2 /= 10
}
unit1 *= 10
remain1 /= 10
}
for (newNum in newNums) {
pairs += counts.getOrDefault(newNum, 0)
}
counts[num] = counts.getOrDefault(num, 0) + 1
}
return pairs
}
}
Loading