From e4172034218ee9f28e056075a44a45281421b566 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: 2025年8月31日 20:18:45 +0300 Subject: [PATCH 1/5] Added tasks 3663-3671 --- .../Solution.kt | 28 +++++ .../readme.md | 33 +++++ .../s3664_two_letter_card_game/Solution.kt | 57 +++++++++ .../s3664_two_letter_card_game/readme.md | 58 +++++++++ .../Solution.kt | 46 +++++++ .../s3665_twisted_mirror_path_count/readme.md | 75 ++++++++++++ .../Solution.kt | 66 ++++++++++ .../readme.md | 51 ++++++++ .../s3668_restore_finishing_order/Solution.kt | 20 +++ .../s3668_restore_finishing_order/readme.md | 38 ++++++ .../Solution.kt | 75 ++++++++++++ .../readme.md | 38 ++++++ .../Solution.kt | 64 ++++++++++ .../readme.md | 44 +++++++ .../Solution.kt | 114 ++++++++++++++++++ .../readme.md | 72 +++++++++++ .../SolutionTest.kt | 17 +++ .../SolutionTest.kt | 31 +++++ .../SolutionTest.kt | 36 ++++++ .../SolutionTest.kt | 22 ++++ .../SolutionTest.kt | 23 ++++ .../SolutionTest.kt | 23 ++++ .../SolutionTest.kt | 28 +++++ .../SolutionTest.kt | 17 +++ 24 files changed, 1076 insertions(+) create mode 100644 src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt create mode 100644 src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/readme.md create mode 100644 src/main/kotlin/g3601_3700/s3664_two_letter_card_game/Solution.kt create mode 100644 src/main/kotlin/g3601_3700/s3664_two_letter_card_game/readme.md create mode 100644 src/main/kotlin/g3601_3700/s3665_twisted_mirror_path_count/Solution.kt create mode 100644 src/main/kotlin/g3601_3700/s3665_twisted_mirror_path_count/readme.md create mode 100644 src/main/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/Solution.kt create mode 100644 src/main/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/readme.md create mode 100644 src/main/kotlin/g3601_3700/s3668_restore_finishing_order/Solution.kt create mode 100644 src/main/kotlin/g3601_3700/s3668_restore_finishing_order/readme.md create mode 100644 src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/Solution.kt create mode 100644 src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/readme.md create mode 100644 src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/Solution.kt create mode 100644 src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/readme.md create mode 100644 src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/Solution.kt create mode 100644 src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/readme.md create mode 100644 src/test/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/SolutionTest.kt create mode 100644 src/test/kotlin/g3601_3700/s3664_two_letter_card_game/SolutionTest.kt create mode 100644 src/test/kotlin/g3601_3700/s3665_twisted_mirror_path_count/SolutionTest.kt create mode 100644 src/test/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/SolutionTest.kt create mode 100644 src/test/kotlin/g3601_3700/s3668_restore_finishing_order/SolutionTest.kt create mode 100644 src/test/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/SolutionTest.kt create mode 100644 src/test/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/SolutionTest.kt create mode 100644 src/test/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/SolutionTest.kt diff --git a/src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt b/src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt new file mode 100644 index 00000000..e8e6b8e1 --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt @@ -0,0 +1,28 @@ +package g3601_3700.s3663_find_the_least_frequent_digit + +// #Easy #Biweekly_Contest_164 #2025_08_31_Time_3_ms_(100.00%)_Space_41.13_MB_(100.00%) + +import kotlin.math.min + +class Solution { + fun getLeastFrequentDigit(n: Int): Int { + val s = n.toString() + val k = s.length + val freq: MutableMap = HashMap() + for (i in 0..1 <= n <= 231 - 1 \ No newline at end of file diff --git a/src/main/kotlin/g3601_3700/s3664_two_letter_card_game/Solution.kt b/src/main/kotlin/g3601_3700/s3664_two_letter_card_game/Solution.kt new file mode 100644 index 00000000..87943595 --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3664_two_letter_card_game/Solution.kt @@ -0,0 +1,57 @@ +package g3601_3700.s3664_two_letter_card_game + +// #Medium #Biweekly_Contest_164 #2025_08_31_Time_11_ms_(100.00%)_Space_82.34_MB_(_%) + +import kotlin.math.min + +class Solution { + fun score(cards: Array, x: Char): Int { + // store input midway as required + // counts for "x?" group by second char and "?x" group by first char + val left = IntArray(10) + val right = IntArray(10) + var xx = 0 + for (c in cards) { + val a = c[0] + val b = c[1] + if (a == x && b == x) { + xx++ + } else if (a == x) { + left[b.code - 'a'.code]++ + } else if (b == x) { + right[a.code - 'a'.code]++ + } + } + // max pairs inside a group where pairs must come from different buckets: + // pairs = min(total/2, total - maxBucket) + var l = 0 + var maxL = 0 + for (v in left) { + l += v + if (v> maxL) { + maxL = v + } + } + var r = 0 + var maxR = 0 + for (v in right) { + r += v + if (v> maxR) { + maxR = v + } + } + val pairsLeft = min(l / 2, l - maxL) + val pairsRight = min(r / 2, r - maxR) + // leftovers after internal pairing + val leftoverL = l - 2 * pairsLeft + val leftoverR = r - 2 * pairsRight + val leftovers = leftoverL + leftoverR + // First, use "xx" to pair with any leftovers + val useWithXX = min(xx, leftovers) + val xxLeft = xx - useWithXX + // If "xx" still remain, we can break existing internal pairs: + // breaking 1 internal pair frees 2 cards, which can pair with 2 "xx" to gain +1 net point + val extraByBreaking = min(xxLeft / 2, pairsLeft + pairsRight) + return pairsLeft + pairsRight + useWithXX + extraByBreaking + } +} diff --git a/src/main/kotlin/g3601_3700/s3664_two_letter_card_game/readme.md b/src/main/kotlin/g3601_3700/s3664_two_letter_card_game/readme.md new file mode 100644 index 00000000..fda7d853 --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3664_two_letter_card_game/readme.md @@ -0,0 +1,58 @@ +3664\. Two-Letter Card Game + +Medium + +You are given a deck of cards represented by a string array `cards`, and each card displays two lowercase letters. + +You are also given a letter `x`. You play a game with the following rules: + +* Start with 0 points. +* On each turn, you must find two **compatible** cards from the deck that both contain the letter `x` in any position. +* Remove the pair of cards and earn **1 point**. +* The game ends when you can no longer find a pair of compatible cards. + +Return the **maximum** number of points you can gain with optimal play. + +Two cards are **compatible** if the strings differ in **exactly** 1 position. + +**Example 1:** + +**Input:** cards = ["aa","ab","ba","ac"], x = "a" + +**Output:** 2 + +**Explanation:** + +* On the first turn, select and remove cards `"ab"` and `"ac"`, which are compatible because they differ at only index 1. +* On the second turn, select and remove cards `"aa"` and `"ba"`, which are compatible because they differ at only index 0. + +Because there are no more compatible pairs, the total score is 2. + +**Example 2:** + +**Input:** cards = ["aa","ab","ba"], x = "a" + +**Output:** 1 + +**Explanation:** + +* On the first turn, select and remove cards `"aa"` and `"ba"`. + +Because there are no more compatible pairs, the total score is 1. + +**Example 3:** + +**Input:** cards = ["aa","ab","ba","ac"], x = "b" + +**Output:** 0 + +**Explanation:** + +The only cards that contain the character `'b'` are `"ab"` and `"ba"`. However, they differ in both indices, so they are not compatible. Thus, the output is 0. + +**Constraints:** + +* 2 <= cards.length <= 105 +* `cards[i].length == 2` +* Each `cards[i]` is composed of only lowercase English letters between `'a'` and `'j'`. +* `x` is a lowercase English letter between `'a'` and `'j'`. \ No newline at end of file diff --git a/src/main/kotlin/g3601_3700/s3665_twisted_mirror_path_count/Solution.kt b/src/main/kotlin/g3601_3700/s3665_twisted_mirror_path_count/Solution.kt new file mode 100644 index 00000000..d9a431e6 --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3665_twisted_mirror_path_count/Solution.kt @@ -0,0 +1,46 @@ +package g3601_3700.s3665_twisted_mirror_path_count + +// #Medium #Biweekly_Contest_164 #2025_08_31_Time_114_ms_(100.00%)_Space_120.02_MB_(100.00%) + +class Solution { + fun uniquePaths(grid: Array): Int { + val n = grid.size + val m = grid[0].size + val dp = Array>(n) { Array(m) { IntArray(2) } } + for (i in 0.., n: Int, m: Int, dp: Array>): Int { + if (i == n - 1 && j == m - 1) { + return 1 + } + if (i>= n || j>= m) { + return 0 + } + if (dp[i][j][dir] != -1) { + return dp[i][j][dir] + } + var ways: Long = 0 + if (grid[i][j] == 1) { + ways = if (dir == 0) { + f(i + 1, j, 1, grid, n, m, dp).toLong() + } else { + f(i, j + 1, 0, grid, n, m, dp).toLong() + } + } else { + ways += f(i + 1, j, 1, grid, n, m, dp).toLong() + ways += f(i, j + 1, 0, grid, n, m, dp).toLong() + } + dp[i][j][dir] = ways.toInt() % MOD + return dp[i][j][dir] + } + + companion object { + private const val MOD = 1000000007 + } +} diff --git a/src/main/kotlin/g3601_3700/s3665_twisted_mirror_path_count/readme.md b/src/main/kotlin/g3601_3700/s3665_twisted_mirror_path_count/readme.md new file mode 100644 index 00000000..62bec19a --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3665_twisted_mirror_path_count/readme.md @@ -0,0 +1,75 @@ +3665\. Twisted Mirror Path Count + +Medium + +Given an `m x n` binary grid `grid` where: + +* `grid[i][j] == 0` represents an empty cell, and +* `grid[i][j] == 1` represents a mirror. + +A robot starts at the top-left corner of the grid `(0, 0)` and wants to reach the bottom-right corner `(m - 1, n - 1)`. It can move only **right** or **down**. If the robot attempts to move into a mirror cell, it is **reflected** before entering that cell: + +* If it tries to move **right** into a mirror, it is turned **down** and moved into the cell directly below the mirror. +* If it tries to move **down** into a mirror, it is turned **right** and moved into the cell directly to the right of the mirror. + +If this reflection would cause the robot to move outside the `grid` boundaries, the path is considered invalid and should not be counted. + +Return the number of unique valid paths from `(0, 0)` to `(m - 1, n - 1)`. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Note**: If a reflection moves the robot into a mirror cell, the robot is immediately reflected again based on the direction it used to enter that mirror: if it entered while moving right, it will be turned down; if it entered while moving down, it will be turned right. This process will continue until either the last cell is reached, the robot moves out of bounds or the robot moves to a non-mirror cell. + +**Example 1:** + +**Input:** grid = [[0,1,0],[0,0,1],[1,0,0]] + +**Output:** 5 + +**Explanation:** + +| Number | Full Path | +|--------|---------------------------------------------------------------------| +| 1 | (0, 0) → (0, 1) [M] → (1, 1) → (1, 2) [M] → (2, 2) | +| 2 | (0, 0) → (0, 1) [M] → (1, 1) → (2, 1) → (2, 2) | +| 3 | (0, 0) → (1, 0) → (1, 1) → (1, 2) [M] → (2, 2) | +| 4 | (0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) | +| 5 | (0, 0) → (1, 0) → (2, 0) [M] → (2, 1) → (2, 2) | + +* `[M]` indicates the robot attempted to enter a mirror cell and instead reflected. + + +**Example 2:** + +**Input:** grid = [[0,0],[0,0]] + +**Output:** 2 + +**Explanation:** + +| Number | Full Path | +|--------|-----------------------------| +| 1 | (0, 0) → (0, 1) → (1, 1) | +| 2 | (0, 0) → (1, 0) → (1, 1) | + +**Example 3:** + +**Input:** grid = [[0,1,1],[1,1,0]] + +**Output:** 1 + +**Explanation:** + +| Number | Full Path | +|--------|-------------------------------------------| +| 1 | (0, 0) → (0, 1) [M] → (1, 1) [M] → (1, 2) | + +`(0, 0) → (1, 0) [M] → (1, 1) [M] → (2, 1)` goes out of bounds, so it is invalid. + +**Constraints:** + +* `m == grid.length` +* `n == grid[i].length` +* `2 <= m, n <= 500` +* `grid[i][j]` is either `0` or `1`. +* `grid[0][0] == grid[m - 1][n - 1] == 0` \ No newline at end of file diff --git a/src/main/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/Solution.kt b/src/main/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/Solution.kt new file mode 100644 index 00000000..6cacb5f7 --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/Solution.kt @@ -0,0 +1,66 @@ +package g3601_3700.s3666_minimum_operations_to_equalize_binary_string + +// #Hard #Biweekly_Contest_164 #2025_08_31_Time_29_ms_(100.00%)_Space_48.47_MB_(100.00%) + +import java.util.ArrayDeque +import java.util.Queue +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun minOperations(s: String, k: Int): Int { + val zeros = s.chars().map { x: Int -> if (x == '0'.code) 1 else 0 }.sum() + if ((zeros % k) == 0) { + return zeros / k + } + val n = s.length + val q: Queue = ArrayDeque() + q.add(zeros) + var res = 1 + // use bounds for optimization + val bounds = Array(2) { IntArray(2) } + bounds[zeros and 1][1] = zeros + bounds[zeros and 1][0] = bounds[zeros and 1][1] + bounds[1 - (zeros and 1)][0] = Int.Companion.MAX_VALUE + bounds[1 - (zeros and 1)][1] = Int.Companion.MIN_VALUE + while (!q.isEmpty()) { + // find min number of zeros and max number of zeros in this round + var minv = Int.Companion.MAX_VALUE + var maxv = Int.Companion.MIN_VALUE + for (len in q.size downTo 1) { + val h: Int = q.poll() + val t = n - h + var x = min(h, k) + if (t>= k - x) { + val fst = h - x + (k - x) + minv = min(minv, fst) + maxv = max(maxv, fst) + } + x = min(t, k) + if (h>= k - x) { + val snd = h - (k - x) + x + minv = min(minv, snd) + maxv = max(maxv, snd) + } + } + // possible children are sequence of equal difference 2 + val ind = minv and 1 + var temp = minv + while (temp <= maxv) { + if ((temp % k) == 0) { + return res + temp / k + } + if (temp < bounds[ind][0] || temp> bounds[ind][1]) { + q.add(temp) + temp += 2 + } else { + temp = bounds[ind][1] + 2 + } + } + bounds[ind][0] = min(bounds[ind][0], minv) + bounds[ind][1] = max(bounds[ind][1], maxv) + res++ + } + return -1 + } +} diff --git a/src/main/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/readme.md b/src/main/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/readme.md new file mode 100644 index 00000000..cc5ab4bc --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/readme.md @@ -0,0 +1,51 @@ +3666\. Minimum Operations to Equalize Binary String + +Hard + +You are given a binary string `s`, and an integer `k`. + +In one operation, you must choose **exactly** `k` **different** indices and **flip** each `'0'` to `'1'` and each `'1'` to `'0'`. + +Return the **minimum** number of operations required to make all characters in the string equal to `'1'`. If it is not possible, return -1. + +**Example 1:** + +**Input:** s = "110", k = 1 + +**Output:** 1 + +**Explanation:** + +* There is one `'0'` in `s`. +* Since `k = 1`, we can flip it directly in one operation. + +**Example 2:** + +**Input:** s = "0101", k = 3 + +**Output:** 2 + +**Explanation:** + +One optimal set of operations choosing `k = 3` indices in each operation is: + +* **Operation 1**: Flip indices `[0, 1, 3]`. `s` changes from `"0101"` to `"1000"`. +* **Operation 2**: Flip indices `[1, 2, 3]`. `s` changes from `"1000"` to `"1111"`. + +Thus, the minimum number of operations is 2. + +**Example 3:** + +**Input:** s = "101", k = 2 + +**Output:** \-1 + +**Explanation:** + +Since `k = 2` and `s` has only one `'0'`, it is impossible to flip exactly `k` indices to make all `'1'`. Hence, the answer is -1. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s[i]` is either `'0'` or `'1'`. +* `1 <= k <= s.length` \ No newline at end of file diff --git a/src/main/kotlin/g3601_3700/s3668_restore_finishing_order/Solution.kt b/src/main/kotlin/g3601_3700/s3668_restore_finishing_order/Solution.kt new file mode 100644 index 00000000..83462c0f --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3668_restore_finishing_order/Solution.kt @@ -0,0 +1,20 @@ +package g3601_3700.s3668_restore_finishing_order + +// #Easy #Weekly_Contest_465 #2025_08_31_Time_2_ms_(100.00%)_Space_49.00_MB_(93.75%) + +class Solution { + fun recoverOrder(order: IntArray, friends: IntArray): IntArray { + val rs = IntArray(friends.size) + var index = 0 + for (k in order) { + for (friend in friends) { + if (k == friend) { + rs[index] = k + index++ + break + } + } + } + return rs + } +} diff --git a/src/main/kotlin/g3601_3700/s3668_restore_finishing_order/readme.md b/src/main/kotlin/g3601_3700/s3668_restore_finishing_order/readme.md new file mode 100644 index 00000000..04a121df --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3668_restore_finishing_order/readme.md @@ -0,0 +1,38 @@ +3668\. Restore Finishing Order + +Easy + +You are given an integer array `order` of length `n` and an integer array `friends`. + +* `order` contains every integer from 1 to `n` **exactly once**, representing the IDs of the participants of a race in their **finishing** order. +* `friends` contains the IDs of your friends in the race **sorted** in strictly increasing order. Each ID in friends is guaranteed to appear in the `order` array. + +Return an array containing your friends' IDs in their **finishing** order. + +**Example 1:** + +**Input:** order = [3,1,2,5,4], friends = [1,3,4] + +**Output:** [3,1,4] + +**Explanation:** + +The finishing order is [(追記) **3** (追記ここまで), (追記) **1** (追記ここまで), 2, 5, (追記) **4** (追記ここまで)]. Therefore, the finishing order of your friends is `[3, 1, 4]`. + +**Example 2:** + +**Input:** order = [1,4,5,3,2], friends = [2,5] + +**Output:** [5,2] + +**Explanation:** + +The finishing order is [1, 4, (追記) **5** (追記ここまで), 3, (追記) **2** (追記ここまで)]. Therefore, the finishing order of your friends is `[5, 2]`. + +**Constraints:** + +* `1 <= n == order.length <= 100` +* `order` contains every integer from 1 to `n` exactly once +* `1 <= friends.length <= min(8, n)` +* `1 <= friends[i] <= n` +* `friends` is strictly increasing \ No newline at end of file diff --git a/src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/Solution.kt b/src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/Solution.kt new file mode 100644 index 00000000..e0233a3f --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/Solution.kt @@ -0,0 +1,75 @@ +package g3601_3700.s3669_balanced_k_factor_decomposition + +// #Medium #Weekly_Contest_465 #2025_08_31_Time_33_ms_(100.00%)_Space_58.00_MB_(40.00%) + +import kotlin.math.max +import kotlin.math.min + +class Solution { + private var kGlobal = 0 + private var bestDiff = Int.Companion.MAX_VALUE + private var bestList: MutableList = ArrayList() + private val current: MutableList = ArrayList() + + fun minDifference(n: Int, k: Int): IntArray { + kGlobal = k + dfs(n, 1, 0) + val ans = IntArray(bestList.size) + for (i in bestList.indices) { + ans[i] = bestList[i] + } + return ans + } + + private fun dfs(rem: Int, start: Int, depth: Int) { + if (depth == kGlobal - 1) { + if (rem>= start) { + current.add(rem) + evaluate() + current.removeAt(current.size - 1) + } + return + } + val divs = getDivisors(rem) + for (d in divs) { + if (d < start) { + continue + } + current.add(d) + dfs(rem / d, d, depth + 1) + current.removeAt(current.size - 1) + } + } + + private fun evaluate() { + var mn = Int.Companion.MAX_VALUE + var mx = Int.Companion.MIN_VALUE + for (v in current) { + mn = min(mn, v) + mx = max(mx, v) + } + val diff = mx - mn + if (diff < bestDiff) { + bestDiff = diff + bestList = ArrayList(current) + } + } + + private fun getDivisors(x: Int): MutableList { + val small: MutableList = ArrayList() + val large: MutableList = ArrayList() + var i = 1 + while (i * i.toLong() <= x) { + if (x % i == 0) { + small.add(i) + if (i != x / i) { + large.add(x / i) + } + } + i++ + } + large.reverse() + small.addAll(large) + return small + } +} diff --git a/src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/readme.md b/src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/readme.md new file mode 100644 index 00000000..8123c6df --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/readme.md @@ -0,0 +1,38 @@ +3669\. Balanced K-Factor Decomposition + +Medium + +Given two integers `n` and `k`, split the number `n` into exactly `k` positive integers such that the **product** of these integers is equal to `n`. + +Return _any_ _one_ split in which the **maximum** difference between any two numbers is **minimized**. You may return the result in _any order_. + +**Example 1:** + +**Input:** n = 100, k = 2 + +**Output:** [10,10] + +**Explanation:** + +The split `[10, 10]` yields `10 * 10 = 100` and a max-min difference of 0, which is minimal. + +**Example 2:** + +**Input:** n = 44, k = 3 + +**Output:** [2,2,11] + +**Explanation:** + +* Split `[1, 1, 44]` yields a difference of 43 +* Split `[1, 2, 22]` yields a difference of 21 +* Split `[1, 4, 11]` yields a difference of 10 +* Split `[2, 2, 11]` yields a difference of 9 + +Therefore, `[2, 2, 11]` is the optimal split with the smallest difference 9. + +**Constraints:** + +* 4 <= n <= 105 +* `2 <= k <= 5` +* `k` is strictly less than the total number of positive divisors of `n`. \ No newline at end of file diff --git a/src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/Solution.kt b/src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/Solution.kt new file mode 100644 index 00000000..c8cd655b --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/Solution.kt @@ -0,0 +1,64 @@ +package g3601_3700.s3670_maximum_product_of_two_integers_with_no_common_bits + +// #Medium #Weekly_Contest_465 #2025_08_31_Time_103_ms_(100.00%)_Space_82.76_MB_(33.33%) + +class Solution { + fun maxProduct(nums: IntArray): Long { + // Find highest value to limit DP size + var maxVal = 0 + for (v in nums) { + if (v> maxVal) { + maxVal = v + } + } + // If all numbers are>=1, maxVal> 0; compute needed bit-width + // in [1..20] + val maxBits = 32 - Integer.numberOfLeadingZeros(maxVal) + val size = 1 shl maxBits + // ---- store input midway, as required ---- + // dp[mask] = largest number present whose bitmask == mask (later becomes: max over all + // submasks) + val dp = IntArray(size) + for (x in nums) { + // numbers themselves are their masks + if (dp[x] < x) { + dp[x] = x + } + } + // SOS DP: for each bit b, propagate lower-half block maxima to upper-half block + // (branch-light) + for (b in 0.. 0) { + val prod = x.toLong() * y + if (prod> ans) { + ans = prod + } + } + } + // 0 if no valid pair + return ans + } +} diff --git a/src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/readme.md b/src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/readme.md new file mode 100644 index 00000000..4caebe98 --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/readme.md @@ -0,0 +1,44 @@ +3670\. Maximum Product of Two Integers With No Common Bits + +Medium + +You are given an integer array `nums`. + +Your task is to find two **distinct** indices `i` and `j` such that the product `nums[i] * nums[j]` is **maximized,** and the binary representations of `nums[i]` and `nums[j]` do not share any common set bits. + +Return the **maximum** possible product of such a pair. If no such pair exists, return 0. + +**Example 1:** + +**Input:** nums = [1,2,3,4,5,6,7] + +**Output:** 12 + +**Explanation:** + +The best pair is 3 (011) and 4 (100). They share no set bits and `3 * 4 = 12`. + +**Example 2:** + +**Input:** nums = [5,6,4] + +**Output:** 0 + +**Explanation:** + +Every pair of numbers has at least one common set bit. Hence, the answer is 0. + +**Example 3:** + +**Input:** nums = [64,8,32] + +**Output:** 2048 + +**Explanation:** + +No pair of numbers share a common bit, so the answer is the product of the two maximum elements, 64 and 32 (`64 * 32 = 2048`). + +**Constraints:** + +* 2 <= nums.length <= 105 +* 1 <= nums[i] <= 106 \ No newline at end of file diff --git a/src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/Solution.kt b/src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/Solution.kt new file mode 100644 index 00000000..f5e63aef --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/Solution.kt @@ -0,0 +1,114 @@ +package g3601_3700.s3671_sum_of_beautiful_subsequences + +// #Hard #Weekly_Contest_465 #2025_08_31_Time_270_ms_(100.00%)_Space_79.84_MB_(100.00%) + +import kotlin.math.sqrt + +class Solution { + fun totalBeauty(nums: IntArray): Int { + var maxV = 0 + for (v in nums) { + if (v> maxV) { + maxV = v + } + } + // index by g + val fenwicks = arrayOfNulls(maxV + 1) + // FDiv[g] = # inc subseq with all elements multiple of g + val fDiv = LongArray(maxV + 1) + // temp buffer for divisors (max divisors of any number <= ~128 for this constraint) + val divisors = IntArray(256) + // Left-to-right DP restricted to multiples of each divisor g + for (x in nums) { + var cnt = 0 + val r = sqrt(x.toDouble()).toInt() + for (d in 1..r) { + if (x % d == 0) { + divisors[cnt++] = d + val d2 = x / d + if (d2 != d) { + divisors[cnt++] = d2 + } + } + } + for (i in 0..= max index (maxV/g). Use +2 for safety and 1-based + // indexing. + fw = Fenwick(maxV / g + 2) + fenwicks[g] = fw + } + var dp = 1 + fw.query(idxQ - 1) + if (dp>= MOD) { + dp -= MOD.toLong() + } + fw.add(idxQ, dp) + fDiv[g] += dp + if (fDiv[g]>= MOD) { + fDiv[g] -= MOD.toLong() + } + } + } + // Inclusion–exclusion to get exact gcd counts + val exact = LongArray(maxV + 1) + for (g in maxV downTo 1) { + var s = fDiv[g] + var m = g + g + while (m <= maxV) { + s -= exact[m] + if (s < 0) { + s += MOD.toLong() + } + m += g + } + exact[g] = s + } + var ans: Long = 0 + for (g in 1..maxV) { + if (exact[g] != 0L) { + ans += exact[g] * g % MOD + if (ans>= MOD) { + ans -= MOD.toLong() + } + } + } + return ans.toInt() + } + + private class Fenwick(size: Int) { + private val tree: LongArray = LongArray(size) + + fun add(indexOneBased: Int, delta: Long) { + var i = indexOneBased + while (i < tree.size) { + var v = tree[i] + delta + if (v>= MOD) { + v -= MOD.toLong() + } + tree[i] = v + i += i and -i + } + } + + fun query(indexOneBased: Int): Long { + var sum: Long = 0 + var i = indexOneBased + while (i> 0) { + sum += tree[i] + if (sum>= MOD) { + sum -= MOD.toLong() + } + i -= i and -i + } + return sum + } + } + + companion object { + private const val MOD = 1000000007 + } +} diff --git a/src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/readme.md b/src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/readme.md new file mode 100644 index 00000000..8655b9d7 --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/readme.md @@ -0,0 +1,72 @@ +3671\. Sum of Beautiful Subsequences + +Hard + +You are given an integer array `nums` of length `n`. + +For every **positive** integer `g`, we define the **beauty** of `g` as the **product** of `g` and the number of **strictly increasing** **subsequences** of `nums` whose greatest common divisor (GCD) is exactly `g`. + +Return the **sum** of **beauty** values for all positive integers `g`. + +Since the answer could be very large, return it modulo 109 + 7. + +**Example 1:** + +**Input:** nums = [1,2,3] + +**Output:** 10 + +**Explanation:** + +All strictly increasing subsequences and their GCDs are: + +| Subsequence | GCD | +|-------------|-----| +| [1] | 1 | +| [2] | 2 | +| [3] | 3 | +| [1,2] | 1 | +| [1,3] | 1 | +| [2,3] | 1 | +| [1,2,3] | 1 | + +Calculating beauty for each GCD: + +| GCD | Count of subsequences | Beauty (GCD ×ばつ Count) | +|-----|------------------------|----------------------| +| 1 | 5 | 1 ×ばつ 5 = 5 | +| 2 | 1 | 2 ×ばつ 1 = 2 | +| 3 | 1 | 3 ×ばつ 1 = 3 | + +Total beauty is `5 + 2 + 3 = 10`. + +**Example 2:** + +**Input:** nums = [4,6] + +**Output:** 12 + +**Explanation:** + +All strictly increasing subsequences and their GCDs are: + +| Subsequence | GCD | +|-------------|-----| +| [4] | 4 | +| [6] | 6 | +| [4,6] | 2 | + +Calculating beauty for each GCD: + +| GCD | Count of subsequences | Beauty (GCD ×ばつ Count) | +|-----|------------------------|----------------------| +| 2 | 1 | 2 ×ばつ 1 = 2 | +| 4 | 1 | 4 ×ばつ 1 = 4 | +| 6 | 1 | 6 ×ばつ 1 = 6 | + +Total beauty is `2 + 4 + 6 = 12`. + +**Constraints:** + +* 1 <= n == nums.length <= 104 +* 1 <= nums[i] <= 7 * 104 \ No newline at end of file diff --git a/src/test/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/SolutionTest.kt b/src/test/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/SolutionTest.kt new file mode 100644 index 00000000..aa071f7f --- /dev/null +++ b/src/test/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3601_3700.s3663_find_the_least_frequent_digit + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun getLeastFrequentDigit() { + assertThat(Solution().getLeastFrequentDigit(1553322), equalTo(1)) + } + + @Test + fun getLeastFrequentDigit2() { + assertThat(Solution().getLeastFrequentDigit(723344511), equalTo(2)) + } +} diff --git a/src/test/kotlin/g3601_3700/s3664_two_letter_card_game/SolutionTest.kt b/src/test/kotlin/g3601_3700/s3664_two_letter_card_game/SolutionTest.kt new file mode 100644 index 00000000..da84639f --- /dev/null +++ b/src/test/kotlin/g3601_3700/s3664_two_letter_card_game/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3601_3700.s3664_two_letter_card_game + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun score() { + assertThat( + Solution().score(arrayOf("aa", "ab", "ba", "ac"), 'a'), + equalTo(2), + ) + } + + @Test + fun score2() { + assertThat( + Solution().score(arrayOf("aa", "ab", "ba"), 'a'), + equalTo(1), + ) + } + + @Test + fun score3() { + assertThat( + Solution().score(arrayOf("aa", "ab", "ba", "ac"), 'b'), + equalTo(0), + ) + } +} diff --git a/src/test/kotlin/g3601_3700/s3665_twisted_mirror_path_count/SolutionTest.kt b/src/test/kotlin/g3601_3700/s3665_twisted_mirror_path_count/SolutionTest.kt new file mode 100644 index 00000000..257b0e24 --- /dev/null +++ b/src/test/kotlin/g3601_3700/s3665_twisted_mirror_path_count/SolutionTest.kt @@ -0,0 +1,36 @@ +package g3601_3700.s3665_twisted_mirror_path_count + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun uniquePaths() { + assertThat( + Solution().uniquePaths(arrayOf(intArrayOf(0, 1, 0), intArrayOf(0, 0, 1), intArrayOf(1, 0, 0))), + equalTo(5), + ) + } + + @Test + fun uniquePaths2() { + assertThat( + Solution().uniquePaths(arrayOf(intArrayOf(0, 0), intArrayOf(0, 0))), + equalTo(2), + ) + } + + @Test + fun uniquePaths3() { + assertThat( + Solution().uniquePaths( + arrayOf( + intArrayOf(0, 1, 1), + intArrayOf(1, 1, 0), + ), + ), + equalTo(1), + ) + } +} diff --git a/src/test/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/SolutionTest.kt b/src/test/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/SolutionTest.kt new file mode 100644 index 00000000..8551a2de --- /dev/null +++ b/src/test/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3601_3700.s3666_minimum_operations_to_equalize_binary_string + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minOperations() { + assertThat(Solution().minOperations("110", 1), equalTo(1)) + } + + @Test + fun minOperations2() { + assertThat(Solution().minOperations("0101", 3), equalTo(2)) + } + + @Test + fun minOperations3() { + assertThat(Solution().minOperations("101", 2), equalTo(-1)) + } +} diff --git a/src/test/kotlin/g3601_3700/s3668_restore_finishing_order/SolutionTest.kt b/src/test/kotlin/g3601_3700/s3668_restore_finishing_order/SolutionTest.kt new file mode 100644 index 00000000..ce556f25 --- /dev/null +++ b/src/test/kotlin/g3601_3700/s3668_restore_finishing_order/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3601_3700.s3668_restore_finishing_order + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun recoverOrder() { + assertThat( + Solution().recoverOrder(intArrayOf(3, 1, 2, 5, 4), intArrayOf(1, 3, 4)), + equalTo(intArrayOf(3, 1, 4)), + ) + } + + @Test + fun recoverOrder2() { + assertThat( + Solution().recoverOrder(intArrayOf(1, 4, 5, 3, 2), intArrayOf(2, 5)), + equalTo(intArrayOf(5, 2)), + ) + } +} diff --git a/src/test/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/SolutionTest.kt b/src/test/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/SolutionTest.kt new file mode 100644 index 00000000..6d1f4ae3 --- /dev/null +++ b/src/test/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3601_3700.s3669_balanced_k_factor_decomposition + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minDifference() { + assertThat( + Solution().minDifference(100, 2), + equalTo(intArrayOf(10, 10)), + ) + } + + @Test + fun minDifference2() { + assertThat( + Solution().minDifference(44, 3), + equalTo(intArrayOf(2, 2, 11)), + ) + } +} diff --git a/src/test/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/SolutionTest.kt b/src/test/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/SolutionTest.kt new file mode 100644 index 00000000..fc874e5d --- /dev/null +++ b/src/test/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/SolutionTest.kt @@ -0,0 +1,28 @@ +package g3601_3700.s3670_maximum_product_of_two_integers_with_no_common_bits + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxProduct() { + assertThat( + Solution().maxProduct(intArrayOf(1, 2, 3, 4, 5, 6, 7)), + equalTo(12L), + ) + } + + @Test + fun maxProduct2() { + assertThat(Solution().maxProduct(intArrayOf(4, 5, 6)), equalTo(0L)) + } + + @Test + fun maxProduct3() { + assertThat( + Solution().maxProduct(intArrayOf(64, 8, 32)), + equalTo(2048L), + ) + } +} diff --git a/src/test/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/SolutionTest.kt b/src/test/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/SolutionTest.kt new file mode 100644 index 00000000..abdfd544 --- /dev/null +++ b/src/test/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3601_3700.s3671_sum_of_beautiful_subsequences + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun totalBeauty() { + assertThat(Solution().totalBeauty(intArrayOf(1, 2, 3)), equalTo(10)) + } + + @Test + fun totalBeauty2() { + assertThat(Solution().totalBeauty(intArrayOf(4, 6)), equalTo(12)) + } +} From 6ebf2ee678b69327f74b46974e915e68fe5e980e Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: 2025年8月31日 20:33:13 +0300 Subject: [PATCH 2/5] Fixed sonar --- .../g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt | 2 +- .../Solution.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt b/src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt index e8e6b8e1..41da493d 100644 --- a/src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt +++ b/src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt @@ -10,7 +10,7 @@ class Solution { val k = s.length val freq: MutableMap = HashMap() for (i in 0.. Date: Sun, 7 Sep 2025 08:41:15 +0300 Subject: [PATCH 3/5] Improved tasks --- .../Solution.kt | 33 ++++--- .../s3664_two_letter_card_game/Solution.kt | 6 +- .../Solution.kt | 57 ++++++------ .../Solution.kt | 86 +++++++------------ .../s3668_restore_finishing_order/Solution.kt | 2 +- .../Solution.kt | 2 +- .../Solution.kt | 2 +- .../Solution.kt | 2 +- .../SolutionTest.kt | 60 +++++++++++++ 9 files changed, 140 insertions(+), 110 deletions(-) diff --git a/src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt b/src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt index 41da493d..e68dfec0 100644 --- a/src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt +++ b/src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt @@ -1,26 +1,25 @@ package g3601_3700.s3663_find_the_least_frequent_digit -// #Easy #Biweekly_Contest_164 #2025_08_31_Time_3_ms_(100.00%)_Space_41.13_MB_(100.00%) - -import kotlin.math.min +// #Easy #Biweekly_Contest_164 #2025_09_07_Time_1_ms_(96.30%)_Space_40.60_MB_(100.00%) class Solution { fun getLeastFrequentDigit(n: Int): Int { - val s = n.toString() - val k = s.length - val freq: MutableMap = HashMap() - for (i in 0..): Int { + // 0 right, 1 down val n = grid.size val m = grid[0].size - val dp = Array>(n) { Array(m) { IntArray(2) } } - for (i in 0.., n: Int, m: Int, dp: Array>): Int { - if (i == n - 1 && j == m - 1) { - return 1 - } - if (i>= n || j>= m) { - return 0 - } - if (dp[i][j][dir] != -1) { - return dp[i][j][dir] - } - var ways: Long = 0 - if (grid[i][j] == 1) { - ways = if (dir == 0) { - f(i + 1, j, 1, grid, n, m, dp).toLong() - } else { - f(i, j + 1, 0, grid, n, m, dp).toLong() + for (i in 1.. if (x == '0'.code) 1 else 0 }.sum() - if ((zeros % k) == 0) { - return zeros / k - } val n = s.length - val q: Queue = ArrayDeque() - q.add(zeros) - var res = 1 - // use bounds for optimization - val bounds = Array(2) { IntArray(2) } - bounds[zeros and 1][1] = zeros - bounds[zeros and 1][0] = bounds[zeros and 1][1] - bounds[1 - (zeros and 1)][0] = Int.Companion.MAX_VALUE - bounds[1 - (zeros and 1)][1] = Int.Companion.MIN_VALUE - while (q.isNotEmpty()) { - // find min number of zeros and max number of zeros in this round - var minv = Int.Companion.MAX_VALUE - var maxv = Int.Companion.MIN_VALUE - for (len in q.size downTo 1) { - val h: Int = q.poll() - val t = n - h - var x = min(h, k) - if (t>= k - x) { - val fst = h - x + (k - x) - minv = min(minv, fst) - maxv = max(maxv, fst) - } - x = min(t, k) - if (h>= k - x) { - val snd = h - (k - x) + x - minv = min(minv, snd) - maxv = max(maxv, snd) - } + var cnt0 = 0 + for (c in s.toCharArray()) { + if (c == '0') { + cnt0++ + } + } + if (cnt0 == 0) { + return 0 + } + if (k == n) { + return if (cnt0 == n) 1 else -1 + } + val kP = k and 1 + val needP = cnt0 and 1 + var best = Long.Companion.MAX_VALUE + for (p in 0..1) { + if ((p * kP) % 2 != needP) { + continue + } + val mismatch = (if (p == 0) cnt0 else (n - cnt0)).toLong() + val b1 = (cnt0 + k - 1L) / k + val b2: Long + b2 = (mismatch + (n - k) - 1L) / (n - k) + var lb = max(b1, b2) + if (lb < 1) { + lb = 1 + } + if ((lb and 1L) != p.toLong()) { + lb++ } - // possible children are sequence of equal difference 2 - val ind = minv and 1 - var temp = minv - while (temp <= maxv) { - if ((temp % k) == 0) { - return res + temp / k - } - if (temp < bounds[ind][0] || temp> bounds[ind][1]) { - q.add(temp) - temp += 2 - } else { - temp = bounds[ind][1] + 2 - } + if (lb < best) { + best = lb } - bounds[ind][0] = min(bounds[ind][0], minv) - bounds[ind][1] = max(bounds[ind][1], maxv) - res++ } - return -1 + return if (best == Long.Companion.MAX_VALUE) -1 else best.toInt() } } diff --git a/src/main/kotlin/g3601_3700/s3668_restore_finishing_order/Solution.kt b/src/main/kotlin/g3601_3700/s3668_restore_finishing_order/Solution.kt index 83462c0f..7c416f79 100644 --- a/src/main/kotlin/g3601_3700/s3668_restore_finishing_order/Solution.kt +++ b/src/main/kotlin/g3601_3700/s3668_restore_finishing_order/Solution.kt @@ -1,6 +1,6 @@ package g3601_3700.s3668_restore_finishing_order -// #Easy #Weekly_Contest_465 #2025_08_31_Time_2_ms_(100.00%)_Space_49.00_MB_(93.75%) +// #Easy #Weekly_Contest_465 #2025_09_07_Time_2_ms_(94.29%)_Space_49.08_MB_(72.86%) class Solution { fun recoverOrder(order: IntArray, friends: IntArray): IntArray { diff --git a/src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/Solution.kt b/src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/Solution.kt index e0233a3f..638d2dad 100644 --- a/src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/Solution.kt +++ b/src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/Solution.kt @@ -1,6 +1,6 @@ package g3601_3700.s3669_balanced_k_factor_decomposition -// #Medium #Weekly_Contest_465 #2025_08_31_Time_33_ms_(100.00%)_Space_58.00_MB_(40.00%) +// #Medium #Weekly_Contest_465 #2025_09_07_Time_30_ms_(85.71%)_Space_56.41_MB_(28.57%) import kotlin.math.max import kotlin.math.min diff --git a/src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/Solution.kt b/src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/Solution.kt index c8cd655b..3a612f1f 100644 --- a/src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/Solution.kt +++ b/src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/Solution.kt @@ -1,6 +1,6 @@ package g3601_3700.s3670_maximum_product_of_two_integers_with_no_common_bits -// #Medium #Weekly_Contest_465 #2025_08_31_Time_103_ms_(100.00%)_Space_82.76_MB_(33.33%) +// #Medium #Weekly_Contest_465 #2025_09_07_Time_113_ms_(88.89%)_Space_78.00_MB_(100.00%) class Solution { fun maxProduct(nums: IntArray): Long { diff --git a/src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/Solution.kt b/src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/Solution.kt index f5e63aef..04d794b6 100644 --- a/src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/Solution.kt +++ b/src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/Solution.kt @@ -1,6 +1,6 @@ package g3601_3700.s3671_sum_of_beautiful_subsequences -// #Hard #Weekly_Contest_465 #2025_08_31_Time_270_ms_(100.00%)_Space_79.84_MB_(100.00%) +// #Hard #Weekly_Contest_465 #2025_09_07_Time_225_ms_(100.00%)_Space_75.96_MB_(100.00%) import kotlin.math.sqrt diff --git a/src/test/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/SolutionTest.kt b/src/test/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/SolutionTest.kt index 8551a2de..2024f760 100644 --- a/src/test/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/SolutionTest.kt +++ b/src/test/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/SolutionTest.kt @@ -19,4 +19,64 @@ internal class SolutionTest { fun minOperations3() { assertThat(Solution().minOperations("101", 2), equalTo(-1)) } + + @Test + fun minOperations4() { + val k = 3 + assertThat(Solution().minOperations("111111", k), equalTo(0)) + } + + @Test + fun minOperations5() { + val k = 6 + assertThat(Solution().minOperations("000000", k), equalTo(1)) + } + + @Test + fun minOperations6() { + val k = 6 + assertThat(Solution().minOperations("000111", k), equalTo(-1)) + } + + @Test + fun minOperations7() { + val k = 3 + assertThat(Solution().minOperations("0011", k), equalTo(2)) + } + + @Test + fun minOperations8() { + val k = 4 + assertThat(Solution().minOperations("000011", k), equalTo(1)) + } + + @Test + fun minOperations9() { + val k = 2 + assertThat(Solution().minOperations("000111", k), equalTo(-1)) + } + + @Test + fun minOperations10() { + val k = 4 + assertThat(Solution().minOperations("001100", k), equalTo(1)) + } + + @Test + fun minOperations11() { + val k = 3 + assertThat(Solution().minOperations("000100", k), equalTo(3)) + } + + @Test + fun minOperations12() { + val k = 4 + assertThat(Solution().minOperations("111111", k), equalTo(0)) + } + + @Test + fun minOperations13() { + val k = 4 + assertThat(Solution().minOperations("001001", k), equalTo(1)) + } } From 6b4b3e31fcb9fd9136c372856f1ce00f45357245 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 7 Sep 2025 08:42:32 +0300 Subject: [PATCH 4/5] Updated grage --- gradle.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/gradle.properties b/gradle.properties index 732fad51..c420fffb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,3 @@ sonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml org.gradle.jvmargs=-Xms512m -Xmx2048m +org.gradle.configuration-cache=true From 4c60f1528a899512345f6c5dc367aa49a67af84a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 7 Sep 2025 08:48:58 +0300 Subject: [PATCH 5/5] Fixed sonar --- .../kotlin/g3601_3700/s3664_two_letter_card_game/Solution.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/g3601_3700/s3664_two_letter_card_game/Solution.kt b/src/main/kotlin/g3601_3700/s3664_two_letter_card_game/Solution.kt index 0501065f..6b54721d 100644 --- a/src/main/kotlin/g3601_3700/s3664_two_letter_card_game/Solution.kt +++ b/src/main/kotlin/g3601_3700/s3664_two_letter_card_game/Solution.kt @@ -12,8 +12,8 @@ class Solution { val right = IntArray(10) var xx = 0 for (c in cards) { - val a = c.get(0) - val b = c.get(1) + val a = c[0] + val b = c[1] if (a == x && b == x) { xx++ } else if (a == x) {

AltStyle によって変換されたページ (->オリジナル) /