forked from javadev/LeetCode-in-Kotlin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
1,987 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
...in/g2901_3000/s2914_minimum_number_of_changes_to_make_binary_string_beautiful/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package g2901_3000.s2914_minimum_number_of_changes_to_make_binary_string_beautiful | ||
|
||
// #Medium #String #2023_12_31_Time_180_ms_(100.00%)_Space_38.2_MB_(80.00%) | ||
|
||
class Solution { | ||
fun minChanges(s: String): Int { | ||
var ans = 0 | ||
var i = 0 | ||
while (i < s.length) { | ||
if (s[i] != s[i + 1]) { | ||
ans++ | ||
} | ||
i += 2 | ||
} | ||
return ans | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
..._3000/s2914_minimum_number_of_changes_to_make_binary_string_beautiful/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
2914\. Minimum Number of Changes to Make Binary String Beautiful | ||
|
||
Medium | ||
|
||
You are given a **0-indexed** binary string `s` having an even length. | ||
|
||
A string is **beautiful** if it's possible to partition it into one or more substrings such that: | ||
|
||
* Each substring has an **even length**. | ||
* Each substring contains **only** `1`'s or **only** `0`'s. | ||
|
||
You can change any character in `s` to `0` or `1`. | ||
|
||
Return _the **minimum** number of changes required to make the string_ `s` _beautiful_. | ||
|
||
**Example 1:** | ||
|
||
**Input:** s = "1001" | ||
|
||
**Output:** 2 | ||
|
||
**Explanation:** We change s[1] to 1 and s[3] to 0 to get string "1100". It can be seen that the string "1100" is beautiful because we can partition it into "11|00". It can be proven that 2 is the minimum number of changes needed to make the string beautiful. | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = "10" | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** We change s[1] to 1 to get string "11". It can be seen that the string "11" is beautiful because we can partition it into "11". It can be proven that 1 is the minimum number of changes needed to make the string beautiful. | ||
|
||
**Example 3:** | ||
|
||
**Input:** s = "0000" | ||
|
||
**Output:** 0 | ||
|
||
**Explanation:** We don't need to make any changes as the string "0000" is beautiful already. | ||
|
||
**Constraints:** | ||
|
||
* <code>2 <= s.length <= 10<sup>5</sup></code> | ||
* `s` has an even length. | ||
* `s[i]` is either `'0'` or `'1'`. |
26 changes: 26 additions & 0 deletions
26
...kotlin/g2901_3000/s2915_length_of_the_longest_subsequence_that_sums_to_target/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package g2901_3000.s2915_length_of_the_longest_subsequence_that_sums_to_target | ||
|
||
// #Medium #Array #Dynamic_Programming #2023_12_31_Time_552_ms_(66.67%)_Space_39.4_MB_(83.33%) | ||
|
||
import kotlin.math.max | ||
|
||
class Solution { | ||
fun lengthOfLongestSubsequence(nums: List<Int>, target: Int): Int { | ||
val dp = IntArray(target + 1) | ||
for (i in 1..target) { | ||
dp[i] = -1 | ||
} | ||
dp[0] = 0 | ||
for (num in nums) { | ||
for (j in target downTo num) { | ||
if (dp[j - num] != -1) { | ||
dp[j] = max(dp[j], dp[j - num] + 1) | ||
} | ||
} | ||
} | ||
if (dp[target] == -1) { | ||
return -1 | ||
} | ||
return dp[target] | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...2901_3000/s2915_length_of_the_longest_subsequence_that_sums_to_target/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
2915\. Length of the Longest Subsequence That Sums to Target | ||
|
||
Medium | ||
|
||
You are given a **0-indexed** array of integers `nums`, and an integer `target`. | ||
|
||
Return _the **length of the longest subsequence** of_ `nums` _that sums up to_ `target`. _If no such subsequence exists, return_ `-1`. | ||
|
||
A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [1,2,3,4,5], target = 9 | ||
|
||
**Output:** 3 | ||
|
||
**Explanation:** There are 3 subsequences with a sum equal to 9: [4,5], [1,3,5], and [2,3,4]. The longest subsequences are [1,3,5], and [2,3,4]. Hence, the answer is 3. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [4,1,3,2,1,5], target = 7 | ||
|
||
**Output:** 4 | ||
|
||
**Explanation:** There are 5 subsequences with a sum equal to 7: [4,3], [4,1,2], [4,2,1], [1,1,5], and [1,3,2,1]. The longest subsequence is [1,3,2,1]. Hence, the answer is 4. | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [1,1,5,4,5], target = 3 | ||
|
||
**Output:** -1 | ||
|
||
**Explanation:** It can be shown that nums has no subsequence that sums up to 3. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= nums.length <= 1000` | ||
* `1 <= nums[i] <= 1000` | ||
* `1 <= target <= 1000` |
65 changes: 65 additions & 0 deletions
65
src/main/kotlin/g2901_3000/s2916_subarrays_distinct_element_sum_of_squares_ii/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package g2901_3000.s2916_subarrays_distinct_element_sum_of_squares_ii | ||
|
||
// #Hard #Array #Dynamic_Programming #Segment_Tree #Binary_Indexed_Tree | ||
// #2023_12_31_Time_467_ms_(100.00%)_Space_58_MB_(100.00%) | ||
|
||
@Suppress("NAME_SHADOWING") | ||
class Solution { | ||
private var n = 0 | ||
private lateinit var tree1: LongArray | ||
private lateinit var tree2: LongArray | ||
|
||
fun sumCounts(nums: IntArray): Int { | ||
n = nums.size | ||
tree1 = LongArray(n + 1) | ||
tree2 = LongArray(n + 1) | ||
var max = 0 | ||
for (x in nums) { | ||
if (x > max) { | ||
max = x | ||
} | ||
} | ||
val last = IntArray(max + 1) | ||
var ans: Long = 0 | ||
var cur: Long = 0 | ||
for (i in 1..n) { | ||
val x = nums[i - 1] | ||
val j = last[x] | ||
cur += 2 * (query(i) - query(j)) + (i - j) | ||
ans += cur | ||
update(j + 1, 1) | ||
update(i + 1, -1) | ||
last[x] = i | ||
} | ||
return (ans % MOD).toInt() | ||
} | ||
|
||
private fun lowbit(index: Int): Int { | ||
return index and (-index) | ||
} | ||
|
||
private fun update(index: Int, x: Int) { | ||
var index = index | ||
val v = index * x | ||
while (index <= n) { | ||
tree1[index] += x.toLong() | ||
tree2[index] += v.toLong() | ||
index += lowbit(index) | ||
} | ||
} | ||
|
||
private fun query(index: Int): Long { | ||
var index = index | ||
var res: Long = 0 | ||
val p = index + 1 | ||
while (index > 0) { | ||
res += p * tree1[index] - tree2[index] | ||
index -= lowbit(index) | ||
} | ||
return res | ||
} | ||
|
||
companion object { | ||
private const val MOD = 1e9.toInt() + 7 | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
.../kotlin/g2901_3000/s2916_subarrays_distinct_element_sum_of_squares_ii/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
2916\. Subarrays Distinct Element Sum of Squares II | ||
|
||
Hard | ||
|
||
You are given a **0-indexed** integer array `nums`. | ||
|
||
The **distinct count** of a subarray of `nums` is defined as: | ||
|
||
* Let `nums[i..j]` be a subarray of `nums` consisting of all the indices from `i` to `j` such that `0 <= i <= j < nums.length`. Then the number of distinct values in `nums[i..j]` is called the distinct count of `nums[i..j]`. | ||
|
||
Return _the sum of the **squares** of **distinct counts** of all subarrays of_ `nums`. | ||
|
||
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>. | ||
|
||
A subarray is a contiguous **non-empty** sequence of elements within an array. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [1,2,1] | ||
|
||
**Output:** 15 | ||
|
||
**Explanation:** Six possible subarrays are: | ||
|
||
[1]: 1 distinct value | ||
|
||
[2]: 1 distinct value | ||
|
||
[1]: 1 distinct value | ||
|
||
[1,2]: 2 distinct values | ||
|
||
[2,1]: 2 distinct values | ||
|
||
[1,2,1]: 2 distinct values | ||
|
||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> = 15. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [2,2] | ||
|
||
**Output:** 3 | ||
|
||
**Explanation:** Three possible subarrays are: | ||
|
||
[2]: 1 distinct value | ||
|
||
[2]: 1 distinct value | ||
|
||
[2,2]: 1 distinct value | ||
|
||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3. | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= nums.length <= 10<sup>5</sup></code> | ||
* <code>1 <= nums[i] <= 10<sup>5</sup></code> |
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/g2901_3000/s2917_find_the_k_or_of_an_array/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package g2901_3000.s2917_find_the_k_or_of_an_array | ||
|
||
// #Easy #Array #Bit_Manipulation #2023_12_31_Time_191_ms_(76.47%)_Space_36.8_MB_(100.00%) | ||
|
||
class Solution { | ||
fun findKOr(nums: IntArray, k: Int): Int { | ||
val dp = IntArray(31) | ||
for (num in nums) { | ||
var i = 0 | ||
var localNum = num | ||
while (localNum > 0) { | ||
if ((localNum and 1) == 1) { | ||
dp[i] += 1 | ||
} | ||
i += 1 | ||
localNum = localNum shr 1 | ||
} | ||
} | ||
var ans = 0 | ||
for (i in 0..30) { | ||
if (dp[i] >= k) { | ||
ans += (1 shl i) | ||
} | ||
} | ||
return ans | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/g2901_3000/s2917_find_the_k_or_of_an_array/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
2917\. Find the K-or of an Array | ||
|
||
Easy | ||
|
||
You are given a **0-indexed** integer array `nums`, and an integer `k`. | ||
|
||
The **K-or** of `nums` is a non-negative integer that satisfies the following: | ||
|
||
* The <code>i<sup>th</sup></code> bit is set in the K-or **if and only if** there are at least `k` elements of nums in which bit `i` is set. | ||
|
||
Return _the **K-or** of_ `nums`. | ||
|
||
**Note** that a bit `i` is set in `x` if <code>(2<sup>i</sup> AND x) == 2<sup>i</sup></code>, where `AND` is the bitwise `AND` operator. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [7,12,9,8,9,15], k = 4 | ||
|
||
**Output:** 9 | ||
|
||
**Explanation:** | ||
|
||
Bit 0 is set at nums[0], nums[2], nums[4], and nums[5]. | ||
|
||
Bit 1 is set at nums[0], and nums[5]. | ||
|
||
Bit 2 is set at nums[0], nums[1], and nums[5]. | ||
|
||
Bit 3 is set at nums[1], nums[2], nums[3], nums[4], and nums[5]. | ||
|
||
Only bits 0 and 3 are set in at least k elements of the array, and bits i >= 4 are not set in any of the array's elements. Hence, the answer is 2^0 + 2^3 = 9. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [2,12,1,11,4,5], k = 6 | ||
|
||
**Output:** 0 | ||
|
||
**Explanation:** Since k == 6 == nums.length, the 6-or of the array is equal to the bitwise AND of all its elements. Hence, the answer is 2 AND 12 AND 1 AND 11 AND 4 AND 5 = 0. | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [10,8,5,9,11,6,8], k = 1 | ||
|
||
**Output:** 15 | ||
|
||
**Explanation:** Since k == 1, the 1-or of the array is equal to the bitwise OR of all its elements. Hence, the answer is 10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= nums.length <= 50` | ||
* <code>0 <= nums[i] < 2<sup>31</sup></code> | ||
* `1 <= k <= nums.length` |
21 changes: 21 additions & 0 deletions
21
...kotlin/g2901_3000/s2918_minimum_equal_sum_of_two_arrays_after_replacing_zeros/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package g2901_3000.s2918_minimum_equal_sum_of_two_arrays_after_replacing_zeros | ||
|
||
// #Medium #Array #Greedy #2023_12_31_Time_1410_ms_(7.69%)_Space_63.2_MB_(38.46%) | ||
|
||
class Solution { | ||
fun minSum(nums1: IntArray, nums2: IntArray): Long { | ||
val sum1 = nums1.fold(0L) { sum, element -> sum + element } | ||
val zeroCount1 = nums1.count { it == 0 } | ||
|
||
val sum2 = nums2.fold(0L) { sum, element -> sum + element } | ||
val zeroCount2 = nums2.count { it == 0 } | ||
|
||
if ( | ||
(zeroCount1 == 0 && sum1 < sum2 + zeroCount2) || | ||
(zeroCount2 == 0 && sum2 < sum1 + zeroCount1) | ||
) { | ||
return -1 | ||
} | ||
return Math.max(sum1 + zeroCount1, sum2 + zeroCount2) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...2901_3000/s2918_minimum_equal_sum_of_two_arrays_after_replacing_zeros/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
2918\. Minimum Equal Sum of Two Arrays After Replacing Zeros | ||
|
||
Medium | ||
|
||
You are given two arrays `nums1` and `nums2` consisting of positive integers. | ||
|
||
You have to replace **all** the `0`'s in both arrays with **strictly** positive integers such that the sum of elements of both arrays becomes **equal**. | ||
|
||
Return _the **minimum** equal sum you can obtain, or_ `-1` _if it is impossible_. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums1 = [3,2,0,1,0], nums2 = [6,5,0] | ||
|
||
**Output:** 12 | ||
|
||
**Explanation:** We can replace 0's in the following way: | ||
- Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4]. | ||
- Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1]. Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums1 = [2,0,2,0], nums2 = [1,4] | ||
|
||
**Output:** -1 | ||
|
||
**Explanation:** It is impossible to make the sum of both arrays equal. | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code> | ||
* <code>0 <= nums1[i], nums2[i] <= 10<sup>6</sup></code> |
Oops, something went wrong.