From e0d85ba158bfef14f9bc17c674cc9725ce0d52f4 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 3 Jan 2024 14:41:27 +0200 Subject: [PATCH] Added tasks 2930-2940 --- .../Solution.kt | 38 +++++++++ .../readme.md | 38 +++++++++ .../Solution.kt | 49 ++++++++++++ .../readme.md | 79 +++++++++++++++++++ .../Solution.kt | 24 ++++++ .../s2932_maximum_strong_pair_xor_i/readme.md | 42 ++++++++++ .../s2933_high_access_employees/Solution.kt | 70 ++++++++++++++++ .../s2933_high_access_employees/readme.md | 49 ++++++++++++ .../Solution.kt | 30 +++++++ .../readme.md | 70 ++++++++++++++++ .../Solution.kt | 48 +++++++++++ .../readme.md | 42 ++++++++++ .../Solution.kt | 23 ++++++ .../s2937_make_three_strings_equal/readme.md | 30 +++++++ .../Solution.kt | 25 ++++++ .../readme.md | 47 +++++++++++ .../s2939_maximum_xor_product/Solution.kt | 28 +++++++ .../s2939_maximum_xor_product/readme.md | 38 +++++++++ .../Solution.kt | 54 +++++++++++++ .../readme.md | 59 ++++++++++++++ .../SolutionTest.kt | 17 ++++ .../SolutionTest.kt | 34 ++++++++ .../SolutionTest.kt | 22 ++++++ .../SolutionTest.kt | 62 +++++++++++++++ .../SolutionTest.kt | 30 +++++++ .../SolutionTest.kt | 25 ++++++ .../SolutionTest.kt | 17 ++++ .../SolutionTest.kt | 22 ++++++ .../s2939_maximum_xor_product/SolutionTest.kt | 22 ++++++ .../SolutionTest.kt | 36 +++++++++ 30 files changed, 1170 insertions(+) create mode 100644 src/main/kotlin/g2901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/Solution.kt create mode 100644 src/main/kotlin/g2901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/readme.md create mode 100644 src/main/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/Solution.kt create mode 100644 src/main/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/readme.md create mode 100644 src/main/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/Solution.kt create mode 100644 src/main/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/readme.md create mode 100644 src/main/kotlin/g2901_3000/s2933_high_access_employees/Solution.kt create mode 100644 src/main/kotlin/g2901_3000/s2933_high_access_employees/readme.md create mode 100644 src/main/kotlin/g2901_3000/s2934_minimum_operations_to_maximize_last_elements_in_arrays/Solution.kt create mode 100644 src/main/kotlin/g2901_3000/s2934_minimum_operations_to_maximize_last_elements_in_arrays/readme.md create mode 100644 src/main/kotlin/g2901_3000/s2935_maximum_strong_pair_xor_ii/Solution.kt create mode 100644 src/main/kotlin/g2901_3000/s2935_maximum_strong_pair_xor_ii/readme.md create mode 100644 src/main/kotlin/g2901_3000/s2937_make_three_strings_equal/Solution.kt create mode 100644 src/main/kotlin/g2901_3000/s2937_make_three_strings_equal/readme.md create mode 100644 src/main/kotlin/g2901_3000/s2938_separate_black_and_white_balls/Solution.kt create mode 100644 src/main/kotlin/g2901_3000/s2938_separate_black_and_white_balls/readme.md create mode 100644 src/main/kotlin/g2901_3000/s2939_maximum_xor_product/Solution.kt create mode 100644 src/main/kotlin/g2901_3000/s2939_maximum_xor_product/readme.md create mode 100644 src/main/kotlin/g2901_3000/s2940_find_building_where_alice_and_bob_can_meet/Solution.kt create mode 100644 src/main/kotlin/g2901_3000/s2940_find_building_where_alice_and_bob_can_meet/readme.md create mode 100644 src/test/kotlin/g2901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/SolutionTest.kt create mode 100644 src/test/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/SolutionTest.kt create mode 100644 src/test/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/SolutionTest.kt create mode 100644 src/test/kotlin/g2901_3000/s2933_high_access_employees/SolutionTest.kt create mode 100644 src/test/kotlin/g2901_3000/s2934_minimum_operations_to_maximize_last_elements_in_arrays/SolutionTest.kt create mode 100644 src/test/kotlin/g2901_3000/s2935_maximum_strong_pair_xor_ii/SolutionTest.kt create mode 100644 src/test/kotlin/g2901_3000/s2937_make_three_strings_equal/SolutionTest.kt create mode 100644 src/test/kotlin/g2901_3000/s2938_separate_black_and_white_balls/SolutionTest.kt create mode 100644 src/test/kotlin/g2901_3000/s2939_maximum_xor_product/SolutionTest.kt create mode 100644 src/test/kotlin/g2901_3000/s2940_find_building_where_alice_and_bob_can_meet/SolutionTest.kt diff --git a/src/main/kotlin/g2901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/Solution.kt b/src/main/kotlin/g2901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/Solution.kt new file mode 100644 index 00000000..bd09e1e6 --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/Solution.kt @@ -0,0 +1,38 @@ +package g2901_3000.s2930_number_of_strings_which_can_be_rearranged_to_contain_substring + +// #Medium #Dynamic_Programming #Math #Combinatorics +// #2024_01_03_Time_132_ms_(100.00%)_Space_33.3_MB_(100.00%) + +@Suppress("NAME_SHADOWING") +class Solution { + private fun pow(x: Long, n: Long, mod: Long): Long { + var n = n + var result: Long = 1 + var p = x % mod + while (n != 0L) { + if ((n and 1L) != 0L) { + result = (result * p) % mod + } + p = (p * p) % mod + n = n shr 1 + } + return result + } + + fun stringCount(n: Int): Int { + val mod = 1e9.toInt() + 7L + return ( + ( + ( + pow(26, n.toLong(), mod) - + (n + 75) * pow(25, n - 1L, mod) + + (2 * n + 72) * pow(24, n - 1L, mod) - + (n + 23) * pow(23, n - 1L, mod) + ) % + mod + + mod + ) % + mod + ).toInt() + } +} diff --git a/src/main/kotlin/g2901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/readme.md b/src/main/kotlin/g2901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/readme.md new file mode 100644 index 00000000..8aa8ffba --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/readme.md @@ -0,0 +1,38 @@ +2930\. Number of Strings Which Can Be Rearranged to Contain Substring + +Medium + +You are given an integer `n`. + +A string `s` is called **good** if it contains only lowercase English characters **and** it is possible to rearrange the characters of `s` such that the new string contains `"leet"` as a **substring**. + +For example: + +* The string `"lteer"` is good because we can rearrange it to form `"leetr"` . +* `"letl"` is not good because we cannot rearrange it to contain `"leet"` as a substring. + +Return _the **total** number of good strings of length_ `n`. + +Since the answer may be large, return it **modulo** 109 + 7. + +A **substring** is a contiguous sequence of characters within a string. + +**Example 1:** + +**Input:** n = 4 + +**Output:** 12 + +**Explanation:** The 12 strings which can be rearranged to have "leet" as a substring are: "eelt", "eetl", "elet", "elte", "etel", "etle", "leet", "lete", "ltee", "teel", "tele", and "tlee". + +**Example 2:** + +**Input:** n = 10 + +**Output:** 83943898 + +**Explanation:** The number of strings with length 10 which can be rearranged to have "leet" as a substring is 526083947580. Hence the answer is 526083947580 % (109 + 7) = 83943898. + +**Constraints:** + +* 1 <= n <= 105 \ No newline at end of file diff --git a/src/main/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/Solution.kt b/src/main/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/Solution.kt new file mode 100644 index 00000000..49bc7103 --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/Solution.kt @@ -0,0 +1,49 @@ +package g2901_3000.s2931_maximum_spending_after_buying_items + +// #Hard #Array #Sorting #Greedy #Matrix #Heap_Priority_Queue +// #2024_01_03_Time_541_ms_(93.75%)_Space_61.5_MB_(93.75%) + +class Solution { + private class Node { + var `val`: Int = -1 + var next: Node? = null + + constructor(`val`: Int) { + this.`val` = `val` + } + + constructor() + } + + fun maxSpending(values: Array): Long { + val m = values.size + val n = values[0].size + val head = Node() + var node: Node? = head + for (j in n - 1 downTo 0) { + node!!.next = Node(values[0][j]) + node = node.next + } + for (i in 1 until m) { + node = head + for (j in n - 1 downTo 0) { + while (node!!.next != null && node.next!!.`val` <= values[i][j]) { + node = node.next + } + val next = node.next + node.next = Node(values[i][j]) + node = node.next + node!!.next = next + } + } + var res: Long = 0 + var day: Long = 1 + node = head.next + while (node != null) { + res += day * node.`val` + node = node.next + day++ + } + return res + } +} diff --git a/src/main/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/readme.md b/src/main/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/readme.md new file mode 100644 index 00000000..36499707 --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/readme.md @@ -0,0 +1,79 @@ +2931\. Maximum Spending After Buying Items + +Hard + +You are given a **0-indexed** `m * n` integer matrix `values`, representing the values of `m * n` different items in `m` different shops. Each shop has `n` items where the jth item in the ith shop has a value of `values[i][j]`. Additionally, the items in the ith shop are sorted in non-increasing order of value. That is, `values[i][j] >= values[i][j + 1]` for all `0 <= j < n - 1`. + +On each day, you would like to buy a single item from one of the shops. Specifically, On the dth day you can: + +* Pick any shop `i`. +* Buy the rightmost available item `j` for the price of `values[i][j] * d`. That is, find the greatest index `j` such that item `j` was never bought before, and buy it for the price of `values[i][j] * d`. + +**Note** that all items are pairwise different. For example, if you have bought item `0` from shop `1`, you can still buy item `0` from any other shop. + +Return _the **maximum amount of money that can be spent** on buying all_ `m * n` _products_. + +**Example 1:** + +**Input:** values = [[8,5,2],[6,4,1],[9,7,3]] + +**Output:** 285 + +**Explanation:** On the first day, we buy product 2 from shop 1 for a price of values[1][2] \* 1 = 1. + +On the second day, we buy product 2 from shop 0 for a price of values[0][2] \* 2 = 4. + +On the third day, we buy product 2 from shop 2 for a price of values[2][2] \* 3 = 9. + +On the fourth day, we buy product 1 from shop 1 for a price of values[1][1] \* 4 = 16. + +On the fifth day, we buy product 1 from shop 0 for a price of values[0][1] \* 5 = 25. + +On the sixth day, we buy product 0 from shop 1 for a price of values[1][0] \* 6 = 36. + +On the seventh day, we buy product 1 from shop 2 for a price of values[2][1] \* 7 = 49. + +On the eighth day, we buy product 0 from shop 0 for a price of values[0][0] \* 8 = 64. + +On the ninth day, we buy product 0 from shop 2 for a price of values[2][0] \* 9 = 81. + +Hence, our total spending is equal to 285. + +It can be shown that 285 is the maximum amount of money that can be spent buying all m \* n products. + +**Example 2:** + +**Input:** values = [[10,8,6,4,2],[9,7,5,3,2]] + +**Output:** 386 + +**Explanation:** On the first day, we buy product 4 from shop 0 for a price of values[0][4] \* 1 = 2. + +On the second day, we buy product 4 from shop 1 for a price of values[1][4] \* 2 = 4. + +On the third day, we buy product 3 from shop 1 for a price of values[1][3] \* 3 = 9. + +On the fourth day, we buy product 3 from shop 0 for a price of values[0][3] \* 4 = 16. + +On the fifth day, we buy product 2 from shop 1 for a price of values[1][2] \* 5 = 25. + +On the sixth day, we buy product 2 from shop 0 for a price of values[0][2] \* 6 = 36. + +On the seventh day, we buy product 1 from shop 1 for a price of values[1][1] \* 7 = 49. + +On the eighth day, we buy product 1 from shop 0 for a price of values[0][1] \* 8 = 64 + +On the ninth day, we buy product 0 from shop 1 for a price of values[1][0] \* 9 = 81. + +On the tenth day, we buy product 0 from shop 0 for a price of values[0][0] \* 10 = 100. + +Hence, our total spending is equal to 386. + +It can be shown that 386 is the maximum amount of money that can be spent buying all m \* n products. + +**Constraints:** + +* `1 <= m == values.length <= 10` +* 1 <= n == values[i].length <= 104 +* 1 <= values[i][j] <= 106 +* `values[i]` are sorted in non-increasing order. \ No newline at end of file diff --git a/src/main/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/Solution.kt b/src/main/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/Solution.kt new file mode 100644 index 00000000..488755fe --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/Solution.kt @@ -0,0 +1,24 @@ +package g2901_3000.s2932_maximum_strong_pair_xor_i + +// #Easy #Array #Hash_Table #Bit_Manipulation #Sliding_Window #Trie +// #2024_01_03_Time_192_ms_(43.08%)_Space_36.5_MB_(90.77%) + +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun maximumStrongPairXor(nums: IntArray): Int { + var max = 0 + var pair: Int + for (i in nums.indices) { + for (j in i until nums.size) { + if (abs((nums[i] - nums[j])) <= min(nums[i], nums[j])) { + pair = nums[i] xor nums[j] + max = max(max, pair) + } + } + } + return max + } +} diff --git a/src/main/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/readme.md b/src/main/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/readme.md new file mode 100644 index 00000000..91b9b78a --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/readme.md @@ -0,0 +1,42 @@ +2932\. Maximum Strong Pair XOR I + +Easy + +You are given a **0-indexed** integer array `nums`. A pair of integers `x` and `y` is called a **strong** pair if it satisfies the condition: + +* `|x - y| <= min(x, y)` + +You need to select two integers from `nums` such that they form a strong pair and their bitwise `XOR` is the **maximum** among all strong pairs in the array. + +Return _the **maximum**_ `XOR` _value out of all possible strong pairs in the array_ `nums`. + +**Note** that you can pick the same integer twice to form a pair. + +**Example 1:** + +**Input:** nums = [1,2,3,4,5] + +**Output:** 7 + +**Explanation:** There are 11 strong pairs in the array `nums`: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5). The maximum XOR possible from these pairs is 3 XOR 4 = 7. + +**Example 2:** + +**Input:** nums = [10,100] + +**Output:** 0 + +**Explanation:** There are 2 strong pairs in the array `nums`: (10, 10) and (100, 100). The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0. + +**Example 3:** + +**Input:** nums = [5,6,25,30] + +**Output:** 7 + +**Explanation:** There are 6 strong pairs in the array `nums`: (5, 5), (5, 6), (6, 6), (25, 25), (25, 30) and (30, 30). The maximum XOR possible from these pairs is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3. + +**Constraints:** + +* `1 <= nums.length <= 50` +* `1 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/kotlin/g2901_3000/s2933_high_access_employees/Solution.kt b/src/main/kotlin/g2901_3000/s2933_high_access_employees/Solution.kt new file mode 100644 index 00000000..78ddb212 --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2933_high_access_employees/Solution.kt @@ -0,0 +1,70 @@ +package g2901_3000.s2933_high_access_employees + +// #Medium #Array #String #Hash_Table #Sorting +// #2024_01_03_Time_304_ms_(91.67%)_Space_39.6_MB_(95.83%) + +class Solution { + private fun isPossible(a: Int, b: Int): Boolean { + val hb = b / 100 + val ha = a / 100 + var mind = b % 100 + val mina = a % 100 + if (hb == 23 && ha == 0) { + return false + } + if (hb - ha > 1) { + return false + } + if (hb - ha == 1) { + mind += 60 + } + return mind - mina < 60 + } + + private fun isHighAccess(list: List): Boolean { + if (list.size < 3) { + return false + } + var i = 0 + var j = 1 + var k = 2 + while (k < list.size) { + val a = list[i++] + val b = list[j++] + val c = list[k++] + if (isPossible(a, c) && isPossible(b, c) && isPossible(a, b)) { + return true + } + } + return false + } + + private fun stringToInt(str: String): Int { + var i = 1000 + var `val` = 0 + for (ch in str.toCharArray()) { + val n = ch.code - '0'.code + `val` += i * n + i = i / 10 + } + return `val` + } + + fun findHighAccessEmployees(accessTimes: List>): List { + val map = HashMap>() + for (list in accessTimes) { + val temp = map.getOrDefault(list[0], ArrayList()) + val `val` = stringToInt(list[1]) + temp.add(`val`) + map[list[0]] = temp + } + val ans: MutableList = ArrayList() + for ((key, temp) in map) { + temp.sort() + if (isHighAccess(temp)) { + ans.add(key) + } + } + return ans + } +} diff --git a/src/main/kotlin/g2901_3000/s2933_high_access_employees/readme.md b/src/main/kotlin/g2901_3000/s2933_high_access_employees/readme.md new file mode 100644 index 00000000..9f501fb3 --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2933_high_access_employees/readme.md @@ -0,0 +1,49 @@ +2933\. High-Access Employees + +Medium + +You are given a 2D **0-indexed** array of strings, `access_times`, with size `n`. For each `i` where `0 <= i <= n - 1`, `access_times[i][0]` represents the name of an employee, and `access_times[i][1]` represents the access time of that employee. All entries in `access_times` are within the same day. + +The access time is represented as **four digits** using a **24-hour** time format, for example, `"0800"` or `"2250"`. + +An employee is said to be **high-access** if he has accessed the system **three or more** times within a **one-hour period**. + +Times with exactly one hour of difference are **not** considered part of the same one-hour period. For example, `"0815"` and `"0915"` are not part of the same one-hour period. + +Access times at the start and end of the day are **not** counted within the same one-hour period. For example, `"0005"` and `"2350"` are not part of the same one-hour period. + +Return _a list that contains the names of **high-access** employees with any order you want._ + +**Example 1:** + +**Input:** access\_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]] + +**Output:** ["a"] + +**Explanation:** "a" has three access times in the one-hour period of [05:32, 06:31] which are 05:32, 05:49, and 06:21. But "b" does not have more than two access times at all. So the answer is ["a"]. + +**Example 2:** + +**Input:** access\_times = [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]] + +**Output:** ["c","d"] + +**Explanation:** "c" has three access times in the one-hour period of [08:08, 09:07] which are 08:08, 08:09, and 08:29. "d" has also three access times in the one-hour period of [14:10, 15:09] which are 14:10, 14:44, and 15:08. However, "e" has just one access time, so it can not be in the answer and the final answer is ["c","d"]. + +**Example 3:** + +**Input:** access\_times = [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]] + +**Output:** ["ab","cd"] + +**Explanation:** "ab" has three access times in the one-hour period of [10:25, 11:24] which are 10:25, 11:20, and 11:24. "cd" has also three access times in the one-hour period of [10:25, 11:24] which are 10:25, 10:46, and 10:55. So the answer is ["ab","cd"]. + +**Constraints:** + +* `1 <= access_times.length <= 100` +* `access_times[i].length == 2` +* `1 <= access_times[i][0].length <= 10` +* `access_times[i][0]` consists only of English small letters. +* `access_times[i][1].length == 4` +* `access_times[i][1]` is in 24-hour time format. +* `access_times[i][1]` consists only of `'0'` to `'9'`. \ No newline at end of file diff --git a/src/main/kotlin/g2901_3000/s2934_minimum_operations_to_maximize_last_elements_in_arrays/Solution.kt b/src/main/kotlin/g2901_3000/s2934_minimum_operations_to_maximize_last_elements_in_arrays/Solution.kt new file mode 100644 index 00000000..837386b9 --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2934_minimum_operations_to_maximize_last_elements_in_arrays/Solution.kt @@ -0,0 +1,30 @@ +package g2901_3000.s2934_minimum_operations_to_maximize_last_elements_in_arrays + +// #Medium #Array #Greedy #2024_01_03_Time_243_ms_(81.25%)_Space_38.7_MB_(93.75%) + +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun minOperations(nums1: IntArray, nums2: IntArray): Int { + val n = nums1.size + var count1 = 0 + var count2 = 0 + val max1 = min(nums1[n - 1], nums2[n - 1]) + val max2 = max(nums1[n - 1], nums2[n - 1]) + for (i in 0 until n) { + val min = min(nums1[i], nums2[i]) + val max = max(nums1[i], nums2[i]) + if (max > max2 || min > max1) { + return -1 + } + if (max == nums1[i] && max != min && max > max1) { + count1++ + } + if (max == nums2[i] && max != min && max > max1) { + count2++ + } + } + return min(count1, count2) + } +} diff --git a/src/main/kotlin/g2901_3000/s2934_minimum_operations_to_maximize_last_elements_in_arrays/readme.md b/src/main/kotlin/g2901_3000/s2934_minimum_operations_to_maximize_last_elements_in_arrays/readme.md new file mode 100644 index 00000000..ee7ff858 --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2934_minimum_operations_to_maximize_last_elements_in_arrays/readme.md @@ -0,0 +1,70 @@ +2934\. Minimum Operations to Maximize Last Elements in Arrays + +Medium + +You are given two **0-indexed** integer arrays, `nums1` and `nums2`, both having length `n`. + +You are allowed to perform a series of **operations** (**possibly none**). + +In an operation, you select an index `i` in the range `[0, n - 1]` and **swap** the values of `nums1[i]` and `nums2[i]`. + +Your task is to find the **minimum** number of operations required to satisfy the following conditions: + +* `nums1[n - 1]` is equal to the **maximum value** among all elements of `nums1`, i.e., `nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1])`. +* `nums2[n - 1]` is equal to the **maximum** **value** among all elements of `nums2`, i.e., `nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1])`. + +Return _an integer denoting the **minimum** number of operations needed to meet **both** conditions_, _or_ `-1` _if it is **impossible** to satisfy both conditions._ + +**Example 1:** + +**Input:** nums1 = [1,2,7], nums2 = [4,5,3] + +**Output:** 1 + +**Explanation:** In this example, an operation can be performed using index i = 2. + +When nums1[2] and nums2[2] are swapped, nums1 becomes [1,2,3] and nums2 becomes [4,5,7]. + +Both conditions are now satisfied. + +It can be shown that the minimum number of operations needed to be performed is 1. + +So, the answer is 1. + +**Example 2:** + +**Input:** nums1 = [2,3,4,5,9], nums2 = [8,8,4,4,4] + +**Output:** 2 + +**Explanation:** In this example, the following operations can be performed: + +First operation using index i = 4. + +When nums1[4] and nums2[4] are swapped, nums1 becomes [2,3,4,5,4], and nums2 becomes [8,8,4,4,9]. + +Another operation using index i = 3. + +When nums1[3] and nums2[3] are swapped, nums1 becomes [2,3,4,4,4], and nums2 becomes [8,8,4,5,9]. + +Both conditions are now satisfied. + +It can be shown that the minimum number of operations needed to be performed is 2. + +So, the answer is 2. + +**Example 3:** + +**Input:** nums1 = [1,5,4], nums2 = [2,5,3] + +**Output:** -1 + +**Explanation:** In this example, it is not possible to satisfy both conditions. + +So, the answer is -1. + +**Constraints:** + +* `1 <= n == nums1.length == nums2.length <= 1000` +* 1 <= nums1[i] <= 109 +* 1 <= nums2[i] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g2901_3000/s2935_maximum_strong_pair_xor_ii/Solution.kt b/src/main/kotlin/g2901_3000/s2935_maximum_strong_pair_xor_ii/Solution.kt new file mode 100644 index 00000000..8450c7e7 --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2935_maximum_strong_pair_xor_ii/Solution.kt @@ -0,0 +1,48 @@ +package g2901_3000.s2935_maximum_strong_pair_xor_ii + +// #Hard #Array #Hash_Table #Bit_Manipulation #Sliding_Window #Trie +// #2024_01_03_Time_748_ms_(84.38%)_Space_63.6_MB_(59.38%) + +import java.util.BitSet + +class Solution { + private val map = IntArray(1 shl 20) + + fun maximumStrongPairXor(nums: IntArray): Int { + nums.sort() + val n = nums.size + val max = nums[n - 1] + var ans = 0 + var mask: Int + var masks = 0 + var highBit = 20 + while (--highBit >= 0) { + if (((max shr highBit) and 1) == 1) { + break + } + } + val m = 1 shl highBit + 1 + var seen = BitSet(m) + for (i in highBit downTo 0) { + mask = 1 shl i + masks = masks or mask + if (check(nums, masks, ans or mask, seen)) { + ans = ans or mask + } + seen = BitSet(m) + } + return ans + } + + private fun check(nums: IntArray, masks: Int, ans: Int, seen: BitSet): Boolean { + for (x in nums) { + val mask = x and masks + if (seen[mask xor ans] && x <= 2 * map[mask xor ans]) { + return true + } + seen.set(mask) + map[mask] = x + } + return false + } +} diff --git a/src/main/kotlin/g2901_3000/s2935_maximum_strong_pair_xor_ii/readme.md b/src/main/kotlin/g2901_3000/s2935_maximum_strong_pair_xor_ii/readme.md new file mode 100644 index 00000000..728d73f2 --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2935_maximum_strong_pair_xor_ii/readme.md @@ -0,0 +1,42 @@ +2935\. Maximum Strong Pair XOR II + +Hard + +You are given a **0-indexed** integer array `nums`. A pair of integers `x` and `y` is called a **strong** pair if it satisfies the condition: + +* `|x - y| <= min(x, y)` + +You need to select two integers from `nums` such that they form a strong pair and their bitwise `XOR` is the **maximum** among all strong pairs in the array. + +Return _the **maximum**_ `XOR` _value out of all possible strong pairs in the array_ `nums`. + +**Note** that you can pick the same integer twice to form a pair. + +**Example 1:** + +**Input:** nums = [1,2,3,4,5] + +**Output:** 7 + +**Explanation:** There are 11 strong pairs in the array `nums`: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5). The maximum XOR possible from these pairs is 3 XOR 4 = 7. + +**Example 2:** + +**Input:** nums = [10,100] + +**Output:** 0 + +**Explanation:** There are 2 strong pairs in the array nums: (10, 10) and (100, 100). The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0. + +**Example 3:** + +**Input:** nums = [500,520,2500,3000] + +**Output:** 1020 + +**Explanation:** There are 6 strong pairs in the array nums: (500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) and (3000, 3000). The maximum XOR possible from these pairs is 500 XOR 520 = 1020 since the only other non-zero XOR value is 2500 XOR 3000 = 636. + +**Constraints:** + +* 1 <= nums.length <= 5 * 104 +* 1 <= nums[i] <= 220 - 1 \ No newline at end of file diff --git a/src/main/kotlin/g2901_3000/s2937_make_three_strings_equal/Solution.kt b/src/main/kotlin/g2901_3000/s2937_make_three_strings_equal/Solution.kt new file mode 100644 index 00000000..82754c3e --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2937_make_three_strings_equal/Solution.kt @@ -0,0 +1,23 @@ +package g2901_3000.s2937_make_three_strings_equal + +// #Easy #String #2024_01_03_Time_186_ms_(83.78%)_Space_36.9_MB_(93.24%) + +import kotlin.math.min + +class Solution { + fun findMinimumOperations(s1: String, s2: String, s3: String): Int { + var pos = true + val n = min(s1.length, min(s2.length, s3.length)) + var ans = 0 + var i = 0 + while (i < n && pos) { + if (s1[i] == s2[i] && s1[i] == s3[i]) { + ans++ + } else { + pos = false + } + i++ + } + return if (ans == 0) -1 else s1.length + s2.length + s3.length - (3 * ans) + } +} diff --git a/src/main/kotlin/g2901_3000/s2937_make_three_strings_equal/readme.md b/src/main/kotlin/g2901_3000/s2937_make_three_strings_equal/readme.md new file mode 100644 index 00000000..803236c4 --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2937_make_three_strings_equal/readme.md @@ -0,0 +1,30 @@ +2937\. Make Three Strings Equal + +Easy + +You are given three strings `s1`, `s2`, and `s3`. You have to perform the following operation on these three strings **as many times** as you want. + +In one operation you can choose one of these three strings such that its length is at least `2` and delete the **rightmost** character of it. + +Return _the **minimum** number of operations you need to perform to make the three strings equal if there is a way to make them equal, otherwise, return_ `-1`_._ + +**Example 1:** + +**Input:** s1 = "abc", s2 = "abb", s3 = "ab" + +**Output:** 2 + +**Explanation:** Performing operations on s1 and s2 once will lead to three equal strings. It can be shown that there is no way to make them equal with less than two operations. + +**Example 2:** + +**Input:** s1 = "dac", s2 = "bac", s3 = "cac" + +**Output:** -1 + +**Explanation:** Because the leftmost letters of s1 and s2 are not equal, they could not be equal after any number of operations. So the answer is -1. + +**Constraints:** + +* `1 <= s1.length, s2.length, s3.length <= 100` +* `s1`, `s2` and `s3` consist only of lowercase English letters. \ No newline at end of file diff --git a/src/main/kotlin/g2901_3000/s2938_separate_black_and_white_balls/Solution.kt b/src/main/kotlin/g2901_3000/s2938_separate_black_and_white_balls/Solution.kt new file mode 100644 index 00000000..5e4d5f97 --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2938_separate_black_and_white_balls/Solution.kt @@ -0,0 +1,25 @@ +package g2901_3000.s2938_separate_black_and_white_balls + +// #Medium #String #Greedy #Two_Pointers #2024_01_03_Time_199_ms_(98.21%)_Space_38.7_MB_(58.93%) + +class Solution { + fun minimumSteps(s: String): Long { + var left = 0 + var right = s.length - 1 + var total: Long = 0 + while (left < right) { + while (left < right && s[left] == '0') { + left++ + } + while (left < right && s[right] == '1') { + right-- + } + if (left < right) { + total += (right - left).toLong() + left++ + right-- + } + } + return total + } +} diff --git a/src/main/kotlin/g2901_3000/s2938_separate_black_and_white_balls/readme.md b/src/main/kotlin/g2901_3000/s2938_separate_black_and_white_balls/readme.md new file mode 100644 index 00000000..443df069 --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2938_separate_black_and_white_balls/readme.md @@ -0,0 +1,47 @@ +2938\. Separate Black and White Balls + +Medium + +There are `n` balls on a table, each ball has a color black or white. + +You are given a **0-indexed** binary string `s` of length `n`, where `1` and `0` represent black and white balls, respectively. + +In each step, you can choose two adjacent balls and swap them. + +Return _the **minimum** number of steps to group all the black balls to the right and all the white balls to the left_. + +**Example 1:** + +**Input:** s = "101" + +**Output:** 1 + +**Explanation:** We can group all the black balls to the right in the following way: +- Swap s[0] and s[1], s = "011". + +Initially, 1s are not grouped together, requiring at least 1 step to group them to the right. + +**Example 2:** + +**Input:** s = "100" + +**Output:** 2 + +**Explanation:** We can group all the black balls to the right in the following way: +- Swap s[0] and s[1], s = "010". +- Swap s[1] and s[2], s = "001". + +It can be proven that the minimum number of steps needed is 2. + +**Example 3:** + +**Input:** s = "0111" + +**Output:** 0 + +**Explanation:** All the black balls are already grouped to the right. + +**Constraints:** + +* 1 <= n == s.length <= 105 +* `s[i]` is either `'0'` or `'1'`. \ No newline at end of file diff --git a/src/main/kotlin/g2901_3000/s2939_maximum_xor_product/Solution.kt b/src/main/kotlin/g2901_3000/s2939_maximum_xor_product/Solution.kt new file mode 100644 index 00000000..876a09a9 --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2939_maximum_xor_product/Solution.kt @@ -0,0 +1,28 @@ +package g2901_3000.s2939_maximum_xor_product + +// #Medium #Math #Greedy #Bit_Manipulation #2024_01_03_Time_151_ms_(76.19%)_Space_34.1_MB_(80.95%) + +class Solution { + fun maximumXorProduct(a: Long, b: Long, n: Int): Int { + var tempa = a + var tempb = b + val mask = ((1L shl n) - 1) + tempa = (tempa and mask.inv()) + tempb = (tempb and mask.inv()) + for (i in n - 1 downTo 0) { + if (((a shr i) and 1L) == ((b shr i) and 1L)) { + tempa = ((tempa) or (1L shl i)) + tempb = ((tempb) or (1L shl i)) + } else { + if (tempa > tempb) { + tempb = ((tempb) or (1L shl i)) + } else { + tempa = ((tempa) or (1L shl i)) + } + } + } + val mod = 1000000007 + val finalans = ((tempa % mod) * (tempb % mod)) % mod + return finalans.toInt() + } +} diff --git a/src/main/kotlin/g2901_3000/s2939_maximum_xor_product/readme.md b/src/main/kotlin/g2901_3000/s2939_maximum_xor_product/readme.md new file mode 100644 index 00000000..def4fa5f --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2939_maximum_xor_product/readme.md @@ -0,0 +1,38 @@ +2939\. Maximum Xor Product + +Medium + +Given three integers `a`, `b`, and `n`, return _the **maximum value** of_ `(a XOR x) * (b XOR x)` _where_ 0 <= x < 2n. + +Since the answer may be too large, return it **modulo** 109 + 7. + +**Note** that `XOR` is the bitwise XOR operation. + +**Example 1:** + +**Input:** a = 12, b = 5, n = 4 + +**Output:** 98 + +**Explanation:** For x = 2, (a XOR x) = 14 and (b XOR x) = 7. Hence, (a XOR x) \* (b XOR x) = 98. It can be shown that 98 is the maximum value of (a XOR x) \* (b XOR x) for all 0 <= x < 2n. + +**Example 2:** + +**Input:** a = 6, b = 7 , n = 5 + +**Output:** 930 + +**Explanation:** For x = 25, (a XOR x) = 31 and (b XOR x) = 30. Hence, (a XOR x) \* (b XOR x) = 930. It can be shown that 930 is the maximum value of (a XOR x) \* (b XOR x) for all 0 <= x < 2n. + +**Example 3:** + +**Input:** a = 1, b = 6, n = 3 + +**Output:** 12 + +**Explanation:** For x = 5, (a XOR x) = 4 and (b XOR x) = 3. Hence, (a XOR x) \* (b XOR x) = 12. It can be shown that 12 is the maximum value of (a XOR x) \* (b XOR x) for all 0 <= x < 2n. + +**Constraints:** + +* 0 <= a, b < 250 +* `0 <= n <= 50` \ No newline at end of file diff --git a/src/main/kotlin/g2901_3000/s2940_find_building_where_alice_and_bob_can_meet/Solution.kt b/src/main/kotlin/g2901_3000/s2940_find_building_where_alice_and_bob_can_meet/Solution.kt new file mode 100644 index 00000000..5153cfad --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2940_find_building_where_alice_and_bob_can_meet/Solution.kt @@ -0,0 +1,54 @@ +package g2901_3000.s2940_find_building_where_alice_and_bob_can_meet + +// #Hard #Array #Binary_Search #Stack #Heap_Priority_Queue #Monotonic_Stack #Segment_Tree +// #Binary_Indexed_Tree #2024_01_03_Time_928_ms_(90.00%)_Space_84.9_MB_(65.00%) + +import java.util.LinkedList +import kotlin.math.max + +class Solution { + fun leftmostBuildingQueries(heights: IntArray, queries: Array): IntArray { + val n = heights.size + val gr = IntArray(n) + val l = LinkedList() + l.offer(n - 1) + gr[n - 1] = -1 + for (i in n - 2 downTo 0) { + while (l.isNotEmpty() && heights[i] > heights[l.peek()]) { + l.pop() + } + if (l.isNotEmpty()) { + gr[i] = l.peek() + } else { + gr[i] = -1 + } + l.push(i) + } + val ans = IntArray(queries.size) + var i = 0 + for (a in queries) { + val x = gr[a[0]] + val y = gr[a[1]] + if (a[0] == a[1]) { + ans[i++] = a[0] + } else if (a[0] < a[1] && heights[a[0]] < heights[a[1]]) { + ans[i++] = a[1] + } else if (a[1] < a[0] && heights[a[1]] < heights[a[0]]) { + ans[i++] = a[0] + } else if (x == -1 || y == -1) { + ans[i++] = -1 + } else { + var m = max(a[0], a[1]) + while (m < heights.size && m != -1 && (heights[m] <= heights[a[0]] || heights[m] <= heights[a[1]])) { + m = gr[m] + } + if (m >= heights.size || m == -1) { + ans[i++] = -1 + } else { + ans[i++] = m + } + } + } + return ans + } +} diff --git a/src/main/kotlin/g2901_3000/s2940_find_building_where_alice_and_bob_can_meet/readme.md b/src/main/kotlin/g2901_3000/s2940_find_building_where_alice_and_bob_can_meet/readme.md new file mode 100644 index 00000000..92d4369c --- /dev/null +++ b/src/main/kotlin/g2901_3000/s2940_find_building_where_alice_and_bob_can_meet/readme.md @@ -0,0 +1,59 @@ +2940\. Find Building Where Alice and Bob Can Meet + +Hard + +You are given a **0-indexed** array `heights` of positive integers, where `heights[i]` represents the height of the ith building. + +If a person is in building `i`, they can move to any other building `j` if and only if `i < j` and `heights[i] < heights[j]`. + +You are also given another array `queries` where queries[i] = [ai, bi]. On the ith query, Alice is in building ai while Bob is in building bi. + +Return _an array_ `ans` _where_ `ans[i]` _is **the index of the leftmost building** where Alice and Bob can meet on the_ ith _query_. _If Alice and Bob cannot move to a common building on query_ `i`, _set_ `ans[i]` _to_ `-1`. + +**Example 1:** + +**Input:** heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]] + +**Output:** [2,5,-1,5,2] + +**Explanation:** In the first query, Alice and Bob can move to building 2 since heights[0] < heights[2] and heights[1] < heights[2]. + +In the second query, Alice and Bob can move to building 5 since heights[0] < heights[5] and heights[3] < heights[5]. + +In the third query, Alice cannot meet Bob since Alice cannot move to any other building. + +In the fourth query, Alice and Bob can move to building 5 since heights[3] < heights[5] and heights[4] < heights[5]. + +In the fifth query, Alice and Bob are already in the same building. + +For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. + +For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet. + +**Example 2:** + +**Input:** heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]] + +**Output:** [7,6,-1,4,6] + +**Explanation:** In the first query, Alice can directly move to Bob's building since heights[0] < heights[7]. + +In the second query, Alice and Bob can move to building 6 since heights[3] < heights[6] and heights[5] < heights[6]. + +In the third query, Alice cannot meet Bob since Bob cannot move to any other building. + +In the fourth query, Alice and Bob can move to building 4 since heights[3] < heights[4] and heights[0] < heights[4]. + +In the fifth query, Alice can directly move to Bob's building since heights[1] < heights[6]. + +For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. + +For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet. + +**Constraints:** + +* 1 <= heights.length <= 5 * 104 +* 1 <= heights[i] <= 109 +* 1 <= queries.length <= 5 * 104 +* queries[i] = [ai, bi] +* 0 <= ai, bi <= heights.length - 1 \ No newline at end of file diff --git a/src/test/kotlin/g2901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/SolutionTest.kt b/src/test/kotlin/g2901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/SolutionTest.kt new file mode 100644 index 00000000..8f7b1c08 --- /dev/null +++ b/src/test/kotlin/g2901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring/SolutionTest.kt @@ -0,0 +1,17 @@ +package g2901_3000.s2930_number_of_strings_which_can_be_rearranged_to_contain_substring + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun stringCount() { + assertThat(Solution().stringCount(4), equalTo(12)) + } + + @Test + fun stringCount2() { + assertThat(Solution().stringCount(10), equalTo(83943898)) + } +} diff --git a/src/test/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/SolutionTest.kt b/src/test/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/SolutionTest.kt new file mode 100644 index 00000000..8db883dc --- /dev/null +++ b/src/test/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items/SolutionTest.kt @@ -0,0 +1,34 @@ +package g2901_3000.s2931_maximum_spending_after_buying_items + +import com_github_leetcode.CommonUtils +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxSpending() { + assertThat( + Solution() + .maxSpending( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[8,5,2],[6,4,1],[9,7,3]" + ) + ), + equalTo(285L) + ) + } + + @Test + fun maxSpending2() { + assertThat( + Solution() + .maxSpending( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[10,8,6,4,2],[9,7,5,3,2]" + ) + ), + equalTo(386L) + ) + } +} diff --git a/src/test/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/SolutionTest.kt b/src/test/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/SolutionTest.kt new file mode 100644 index 00000000..82e8cd1f --- /dev/null +++ b/src/test/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i/SolutionTest.kt @@ -0,0 +1,22 @@ +package g2901_3000.s2932_maximum_strong_pair_xor_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumStrongPairXor() { + assertThat(Solution().maximumStrongPairXor(intArrayOf(1, 2, 3, 4, 5)), equalTo(7)) + } + + @Test + fun maximumStrongPairXor2() { + assertThat(Solution().maximumStrongPairXor(intArrayOf(10, 100)), equalTo(0)) + } + + @Test + fun maximumStrongPairXor3() { + assertThat(Solution().maximumStrongPairXor(intArrayOf(5, 6, 25, 30)), equalTo(7)) + } +} diff --git a/src/test/kotlin/g2901_3000/s2933_high_access_employees/SolutionTest.kt b/src/test/kotlin/g2901_3000/s2933_high_access_employees/SolutionTest.kt new file mode 100644 index 00000000..2eb25cb0 --- /dev/null +++ b/src/test/kotlin/g2901_3000/s2933_high_access_employees/SolutionTest.kt @@ -0,0 +1,62 @@ +package g2901_3000.s2933_high_access_employees + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun findHighAccessEmployees() { + assertThat( + Solution() + .findHighAccessEmployees( + listOf>( + mutableListOf("a", "0549"), + mutableListOf("b", "0457"), + mutableListOf("a", "0532"), + mutableListOf("a", "0621"), + mutableListOf("b", "0540") + ) + ), + equalTo(listOf("a")) + ) + } + + @Test + fun findHighAccessEmployees2() { + assertThat( + Solution() + .findHighAccessEmployees( + listOf>( + mutableListOf("d", "0002"), + mutableListOf("c", "0808"), + mutableListOf("c", "0829"), + mutableListOf("e", "0215"), + mutableListOf("d", "1508"), + mutableListOf("d", "1444"), + mutableListOf("d", "1410"), + mutableListOf("c", "0809") + ) + ), + equalTo(listOf("c", "d")) + ) + } + + @Test + fun findHighAccessEmployees3() { + assertThat( + Solution() + .findHighAccessEmployees( + listOf>( + mutableListOf("cd", "1025"), + mutableListOf("ab", "1025"), + mutableListOf("cd", "1046"), + mutableListOf("cd", "1055"), + mutableListOf("ab", "1124"), + mutableListOf("ab", "1120") + ) + ), + equalTo(listOf("cd", "ab")) + ) + } +} diff --git a/src/test/kotlin/g2901_3000/s2934_minimum_operations_to_maximize_last_elements_in_arrays/SolutionTest.kt b/src/test/kotlin/g2901_3000/s2934_minimum_operations_to_maximize_last_elements_in_arrays/SolutionTest.kt new file mode 100644 index 00000000..b16d0ed4 --- /dev/null +++ b/src/test/kotlin/g2901_3000/s2934_minimum_operations_to_maximize_last_elements_in_arrays/SolutionTest.kt @@ -0,0 +1,30 @@ +package g2901_3000.s2934_minimum_operations_to_maximize_last_elements_in_arrays + +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(intArrayOf(1, 2, 7), intArrayOf(4, 5, 3)), equalTo(1) + ) + } + + @Test + fun minOperations2() { + assertThat( + Solution().minOperations(intArrayOf(2, 3, 4, 5, 9), intArrayOf(8, 8, 4, 4, 4)), + equalTo(2) + ) + } + + @Test + fun minOperations3() { + assertThat( + Solution().minOperations(intArrayOf(1, 5, 4), intArrayOf(2, 5, 3)), + equalTo(-1) + ) + } +} diff --git a/src/test/kotlin/g2901_3000/s2935_maximum_strong_pair_xor_ii/SolutionTest.kt b/src/test/kotlin/g2901_3000/s2935_maximum_strong_pair_xor_ii/SolutionTest.kt new file mode 100644 index 00000000..7dadc09d --- /dev/null +++ b/src/test/kotlin/g2901_3000/s2935_maximum_strong_pair_xor_ii/SolutionTest.kt @@ -0,0 +1,25 @@ +package g2901_3000.s2935_maximum_strong_pair_xor_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumStrongPairXor() { + assertThat(Solution().maximumStrongPairXor(intArrayOf(1, 2, 3, 4, 5)), equalTo(7)) + } + + @Test + fun maximumStrongPairXor2() { + assertThat(Solution().maximumStrongPairXor(intArrayOf(10, 100)), equalTo(0)) + } + + @Test + fun maximumStrongPairXor3() { + assertThat( + Solution().maximumStrongPairXor(intArrayOf(500, 520, 2500, 3000)), + equalTo(1020) + ) + } +} diff --git a/src/test/kotlin/g2901_3000/s2937_make_three_strings_equal/SolutionTest.kt b/src/test/kotlin/g2901_3000/s2937_make_three_strings_equal/SolutionTest.kt new file mode 100644 index 00000000..fbe07633 --- /dev/null +++ b/src/test/kotlin/g2901_3000/s2937_make_three_strings_equal/SolutionTest.kt @@ -0,0 +1,17 @@ +package g2901_3000.s2937_make_three_strings_equal + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun findMinimumOperations() { + assertThat(Solution().findMinimumOperations("abc", "abb", "ab"), equalTo(2)) + } + + @Test + fun findMinimumOperations2() { + assertThat(Solution().findMinimumOperations("dac", "bac", "cac"), equalTo(-1)) + } +} diff --git a/src/test/kotlin/g2901_3000/s2938_separate_black_and_white_balls/SolutionTest.kt b/src/test/kotlin/g2901_3000/s2938_separate_black_and_white_balls/SolutionTest.kt new file mode 100644 index 00000000..aa3413ac --- /dev/null +++ b/src/test/kotlin/g2901_3000/s2938_separate_black_and_white_balls/SolutionTest.kt @@ -0,0 +1,22 @@ +package g2901_3000.s2938_separate_black_and_white_balls + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumSteps() { + assertThat(Solution().minimumSteps("101"), equalTo(1L)) + } + + @Test + fun minimumSteps2() { + assertThat(Solution().minimumSteps("100"), equalTo(2L)) + } + + @Test + fun minimumSteps3() { + assertThat(Solution().minimumSteps("0111"), equalTo(0L)) + } +} diff --git a/src/test/kotlin/g2901_3000/s2939_maximum_xor_product/SolutionTest.kt b/src/test/kotlin/g2901_3000/s2939_maximum_xor_product/SolutionTest.kt new file mode 100644 index 00000000..3d5923eb --- /dev/null +++ b/src/test/kotlin/g2901_3000/s2939_maximum_xor_product/SolutionTest.kt @@ -0,0 +1,22 @@ +package g2901_3000.s2939_maximum_xor_product + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumXorProduct() { + assertThat(Solution().maximumXorProduct(12, 5, 4), equalTo(98)) + } + + @Test + fun maximumXorProduct2() { + assertThat(Solution().maximumXorProduct(6, 7, 5), equalTo(930)) + } + + @Test + fun maximumXorProduct3() { + assertThat(Solution().maximumXorProduct(1, 6, 3), equalTo(12)) + } +} diff --git a/src/test/kotlin/g2901_3000/s2940_find_building_where_alice_and_bob_can_meet/SolutionTest.kt b/src/test/kotlin/g2901_3000/s2940_find_building_where_alice_and_bob_can_meet/SolutionTest.kt new file mode 100644 index 00000000..d48ba403 --- /dev/null +++ b/src/test/kotlin/g2901_3000/s2940_find_building_where_alice_and_bob_can_meet/SolutionTest.kt @@ -0,0 +1,36 @@ +package g2901_3000.s2940_find_building_where_alice_and_bob_can_meet + +import com_github_leetcode.CommonUtils +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun leftmostBuildingQueries() { + assertThat( + Solution() + .leftmostBuildingQueries( + intArrayOf(6, 4, 8, 5, 2, 7), + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,1],[0,3],[2,4],[3,4],[2,2]" + ) + ), + equalTo(intArrayOf(2, 5, -1, 5, 2)) + ) + } + + @Test + fun leftmostBuildingQueries2() { + assertThat( + Solution() + .leftmostBuildingQueries( + intArrayOf(5, 3, 8, 2, 6, 1, 4, 6), + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,7],[3,5],[5,2],[3,0],[1,6]" + ) + ), + equalTo(intArrayOf(7, 6, -1, 4, 6)) + ) + } +}