Skip to content

Commit 16ae309

Browse files
authored
Added tasks 2914-2929, 2945-2949
1 parent f5c573a commit 16ae309

File tree

54 files changed

+1987
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1987
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2901_3000.s2914_minimum_number_of_changes_to_make_binary_string_beautiful
2+
3+
// #Medium #String #2023_12_31_Time_180_ms_(100.00%)_Space_38.2_MB_(80.00%)
4+
5+
class Solution {
6+
fun minChanges(s: String): Int {
7+
var ans = 0
8+
var i = 0
9+
while (i < s.length) {
10+
if (s[i] != s[i + 1]) {
11+
ans++
12+
}
13+
i += 2
14+
}
15+
return ans
16+
}
17+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2914\. Minimum Number of Changes to Make Binary String Beautiful
2+
3+
Medium
4+
5+
You are given a **0-indexed** binary string `s` having an even length.
6+
7+
A string is **beautiful** if it's possible to partition it into one or more substrings such that:
8+
9+
* Each substring has an **even length**.
10+
* Each substring contains **only** `1`'s or **only** `0`'s.
11+
12+
You can change any character in `s` to `0` or `1`.
13+
14+
Return _the **minimum** number of changes required to make the string_ `s` _beautiful_.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "1001"
19+
20+
**Output:** 2
21+
22+
**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.
23+
24+
**Example 2:**
25+
26+
**Input:** s = "10"
27+
28+
**Output:** 1
29+
30+
**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.
31+
32+
**Example 3:**
33+
34+
**Input:** s = "0000"
35+
36+
**Output:** 0
37+
38+
**Explanation:** We don't need to make any changes as the string "0000" is beautiful already.
39+
40+
**Constraints:**
41+
42+
* <code>2 <= s.length <= 10<sup>5</sup></code>
43+
* `s` has an even length.
44+
* `s[i]` is either `'0'` or `'1'`.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g2901_3000.s2915_length_of_the_longest_subsequence_that_sums_to_target
2+
3+
// #Medium #Array #Dynamic_Programming #2023_12_31_Time_552_ms_(66.67%)_Space_39.4_MB_(83.33%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun lengthOfLongestSubsequence(nums: List<Int>, target: Int): Int {
9+
val dp = IntArray(target + 1)
10+
for (i in 1..target) {
11+
dp[i] = -1
12+
}
13+
dp[0] = 0
14+
for (num in nums) {
15+
for (j in target downTo num) {
16+
if (dp[j - num] != -1) {
17+
dp[j] = max(dp[j], dp[j - num] + 1)
18+
}
19+
}
20+
}
21+
if (dp[target] == -1) {
22+
return -1
23+
}
24+
return dp[target]
25+
}
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2915\. Length of the Longest Subsequence That Sums to Target
2+
3+
Medium
4+
5+
You are given a **0-indexed** array of integers `nums`, and an integer `target`.
6+
7+
Return _the **length of the longest subsequence** of_ `nums` _that sums up to_ `target`. _If no such subsequence exists, return_ `-1`.
8+
9+
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.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,3,4,5], target = 9
14+
15+
**Output:** 3
16+
17+
**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.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [4,1,3,2,1,5], target = 7
22+
23+
**Output:** 4
24+
25+
**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.
26+
27+
**Example 3:**
28+
29+
**Input:** nums = [1,1,5,4,5], target = 3
30+
31+
**Output:** -1
32+
33+
**Explanation:** It can be shown that nums has no subsequence that sums up to 3.
34+
35+
**Constraints:**
36+
37+
* `1 <= nums.length <= 1000`
38+
* `1 <= nums[i] <= 1000`
39+
* `1 <= target <= 1000`
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package g2901_3000.s2916_subarrays_distinct_element_sum_of_squares_ii
2+
3+
// #Hard #Array #Dynamic_Programming #Segment_Tree #Binary_Indexed_Tree
4+
// #2023_12_31_Time_467_ms_(100.00%)_Space_58_MB_(100.00%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
private var n = 0
9+
private lateinit var tree1: LongArray
10+
private lateinit var tree2: LongArray
11+
12+
fun sumCounts(nums: IntArray): Int {
13+
n = nums.size
14+
tree1 = LongArray(n + 1)
15+
tree2 = LongArray(n + 1)
16+
var max = 0
17+
for (x in nums) {
18+
if (x > max) {
19+
max = x
20+
}
21+
}
22+
val last = IntArray(max + 1)
23+
var ans: Long = 0
24+
var cur: Long = 0
25+
for (i in 1..n) {
26+
val x = nums[i - 1]
27+
val j = last[x]
28+
cur += 2 * (query(i) - query(j)) + (i - j)
29+
ans += cur
30+
update(j + 1, 1)
31+
update(i + 1, -1)
32+
last[x] = i
33+
}
34+
return (ans % MOD).toInt()
35+
}
36+
37+
private fun lowbit(index: Int): Int {
38+
return index and (-index)
39+
}
40+
41+
private fun update(index: Int, x: Int) {
42+
var index = index
43+
val v = index * x
44+
while (index <= n) {
45+
tree1[index] += x.toLong()
46+
tree2[index] += v.toLong()
47+
index += lowbit(index)
48+
}
49+
}
50+
51+
private fun query(index: Int): Long {
52+
var index = index
53+
var res: Long = 0
54+
val p = index + 1
55+
while (index > 0) {
56+
res += p * tree1[index] - tree2[index]
57+
index -= lowbit(index)
58+
}
59+
return res
60+
}
61+
62+
companion object {
63+
private const val MOD = 1e9.toInt() + 7
64+
}
65+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2916\. Subarrays Distinct Element Sum of Squares II
2+
3+
Hard
4+
5+
You are given a **0-indexed** integer array `nums`.
6+
7+
The **distinct count** of a subarray of `nums` is defined as:
8+
9+
* 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]`.
10+
11+
Return _the sum of the **squares** of **distinct counts** of all subarrays of_ `nums`.
12+
13+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
14+
15+
A subarray is a contiguous **non-empty** sequence of elements within an array.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [1,2,1]
20+
21+
**Output:** 15
22+
23+
**Explanation:** Six possible subarrays are:
24+
25+
[1]: 1 distinct value
26+
27+
[2]: 1 distinct value
28+
29+
[1]: 1 distinct value
30+
31+
[1,2]: 2 distinct values
32+
33+
[2,1]: 2 distinct values
34+
35+
[1,2,1]: 2 distinct values
36+
37+
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.
38+
39+
**Example 2:**
40+
41+
**Input:** nums = [2,2]
42+
43+
**Output:** 3
44+
45+
**Explanation:** Three possible subarrays are:
46+
47+
[2]: 1 distinct value
48+
49+
[2]: 1 distinct value
50+
51+
[2,2]: 1 distinct value
52+
53+
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.
54+
55+
**Constraints:**
56+
57+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
58+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g2901_3000.s2917_find_the_k_or_of_an_array
2+
3+
// #Easy #Array #Bit_Manipulation #2023_12_31_Time_191_ms_(76.47%)_Space_36.8_MB_(100.00%)
4+
5+
class Solution {
6+
fun findKOr(nums: IntArray, k: Int): Int {
7+
val dp = IntArray(31)
8+
for (num in nums) {
9+
var i = 0
10+
var localNum = num
11+
while (localNum > 0) {
12+
if ((localNum and 1) == 1) {
13+
dp[i] += 1
14+
}
15+
i += 1
16+
localNum = localNum shr 1
17+
}
18+
}
19+
var ans = 0
20+
for (i in 0..30) {
21+
if (dp[i] >= k) {
22+
ans += (1 shl i)
23+
}
24+
}
25+
return ans
26+
}
27+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2917\. Find the K-or of an Array
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums`, and an integer `k`.
6+
7+
The **K-or** of `nums` is a non-negative integer that satisfies the following:
8+
9+
* 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.
10+
11+
Return _the **K-or** of_ `nums`.
12+
13+
**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.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [7,12,9,8,9,15], k = 4
18+
19+
**Output:** 9
20+
21+
**Explanation:**
22+
23+
Bit 0 is set at nums[0], nums[2], nums[4], and nums[5].
24+
25+
Bit 1 is set at nums[0], and nums[5].
26+
27+
Bit 2 is set at nums[0], nums[1], and nums[5].
28+
29+
Bit 3 is set at nums[1], nums[2], nums[3], nums[4], and nums[5].
30+
31+
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.
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [2,12,1,11,4,5], k = 6
36+
37+
**Output:** 0
38+
39+
**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.
40+
41+
**Example 3:**
42+
43+
**Input:** nums = [10,8,5,9,11,6,8], k = 1
44+
45+
**Output:** 15
46+
47+
**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.
48+
49+
**Constraints:**
50+
51+
* `1 <= nums.length <= 50`
52+
* <code>0 <= nums[i] < 2<sup>31</sup></code>
53+
* `1 <= k <= nums.length`
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2901_3000.s2918_minimum_equal_sum_of_two_arrays_after_replacing_zeros
2+
3+
// #Medium #Array #Greedy #2023_12_31_Time_1410_ms_(7.69%)_Space_63.2_MB_(38.46%)
4+
5+
class Solution {
6+
fun minSum(nums1: IntArray, nums2: IntArray): Long {
7+
val sum1 = nums1.fold(0L) { sum, element -> sum + element }
8+
val zeroCount1 = nums1.count { it == 0 }
9+
10+
val sum2 = nums2.fold(0L) { sum, element -> sum + element }
11+
val zeroCount2 = nums2.count { it == 0 }
12+
13+
if (
14+
(zeroCount1 == 0 && sum1 < sum2 + zeroCount2) ||
15+
(zeroCount2 == 0 && sum2 < sum1 + zeroCount1)
16+
) {
17+
return -1
18+
}
19+
return Math.max(sum1 + zeroCount1, sum2 + zeroCount2)
20+
}
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2918\. Minimum Equal Sum of Two Arrays After Replacing Zeros
2+
3+
Medium
4+
5+
You are given two arrays `nums1` and `nums2` consisting of positive integers.
6+
7+
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**.
8+
9+
Return _the **minimum** equal sum you can obtain, or_ `-1` _if it is impossible_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums1 = [3,2,0,1,0], nums2 = [6,5,0]
14+
15+
**Output:** 12
16+
17+
**Explanation:** We can replace 0's in the following way:
18+
- Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].
19+
- 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.
20+
21+
**Example 2:**
22+
23+
**Input:** nums1 = [2,0,2,0], nums2 = [1,4]
24+
25+
**Output:** -1
26+
27+
**Explanation:** It is impossible to make the sum of both arrays equal.
28+
29+
**Constraints:**
30+
31+
* <code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code>
32+
* <code>0 <= nums1[i], nums2[i] <= 10<sup>6</sup></code>

0 commit comments

Comments
 (0)