Skip to content

Commit e907340

Browse files
authored
Added tasks 3163-3165
1 parent 4d2e92e commit e907340

File tree

9 files changed

+370
-0
lines changed

9 files changed

+370
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3101_3200.s3163_string_compression_iii
2+
3+
// #Medium #String #2024_06_03_Time_331_ms_(66.13%)_Space_45.8_MB_(41.94%)
4+
5+
class Solution {
6+
fun compressedString(word: String): String {
7+
val builder = StringBuilder()
8+
var last = word[0]
9+
var count = 1
10+
var i = 1
11+
val l = word.length
12+
while (i < l) {
13+
if (word[i] == last) {
14+
count++
15+
if (count == 10) {
16+
builder.append(9).append(last)
17+
count = 1
18+
}
19+
} else {
20+
builder.append(count).append(last)
21+
last = word[i]
22+
count = 1
23+
}
24+
i++
25+
}
26+
builder.append(count).append(last)
27+
return builder.toString()
28+
}
29+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3163\. String Compression III
2+
3+
Medium
4+
5+
Given a string `word`, compress it using the following algorithm:
6+
7+
* Begin with an empty string `comp`. While `word` is **not** empty, use the following operation:
8+
* Remove a maximum length prefix of `word` made of a _single character_ `c` repeating **at most** 9 times.
9+
* Append the length of the prefix followed by `c` to `comp`.
10+
11+
Return the string `comp`.
12+
13+
**Example 1:**
14+
15+
**Input:** word = "abcde"
16+
17+
**Output:** "1a1b1c1d1e"
18+
19+
**Explanation:**
20+
21+
Initially, `comp = ""`. Apply the operation 5 times, choosing `"a"`, `"b"`, `"c"`, `"d"`, and `"e"` as the prefix in each operation.
22+
23+
For each prefix, append `"1"` followed by the character to `comp`.
24+
25+
**Example 2:**
26+
27+
**Input:** word = "aaaaaaaaaaaaaabb"
28+
29+
**Output:** "9a5a2b"
30+
31+
**Explanation:**
32+
33+
Initially, `comp = ""`. Apply the operation 3 times, choosing `"aaaaaaaaa"`, `"aaaaa"`, and `"bb"` as the prefix in each operation.
34+
35+
* For prefix `"aaaaaaaaa"`, append `"9"` followed by `"a"` to `comp`.
36+
* For prefix `"aaaaa"`, append `"5"` followed by `"a"` to `comp`.
37+
* For prefix `"bb"`, append `"2"` followed by `"b"` to `comp`.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= word.length <= 2 * 10<sup>5</sup></code>
42+
* `word` consists only of lowercase English letters.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g3101_3200.s3164_find_the_number_of_good_pairs_ii
2+
3+
// #Medium #Array #Hash_Table #2024_06_03_Time_1175_ms_(90.00%)_Space_75_MB_(65.00%)
4+
5+
class Solution {
6+
fun numberOfPairs(nums1: IntArray, nums2: IntArray, k: Int): Long {
7+
val hm = HashMap<Int, Int>()
8+
var ans: Long = 0
9+
for (`val` in nums2) {
10+
hm[`val` * k] = hm.getOrDefault(`val` * k, 0) + 1
11+
}
12+
for (index in nums1.indices) {
13+
if (nums1[index] % k != 0) {
14+
continue
15+
}
16+
var factor = 1
17+
while (factor * factor <= nums1[index]) {
18+
if (nums1[index] % factor != 0) {
19+
factor++
20+
continue
21+
}
22+
val factor1 = factor
23+
val factor2 = nums1[index] / factor
24+
if (hm.containsKey(factor1)) {
25+
ans += hm[factor1]!!.toLong()
26+
}
27+
if (factor1 != factor2 && hm.containsKey(factor2)) {
28+
ans += hm[factor2]!!.toLong()
29+
}
30+
factor++
31+
}
32+
}
33+
return ans
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
3164\. Find the Number of Good Pairs II
2+
3+
Medium
4+
5+
You are given 2 integer arrays `nums1` and `nums2` of lengths `n` and `m` respectively. You are also given a **positive** integer `k`.
6+
7+
A pair `(i, j)` is called **good** if `nums1[i]` is divisible by `nums2[j] * k` (`0 <= i <= n - 1`, `0 <= j <= m - 1`).
8+
9+
Return the total number of **good** pairs.
10+
11+
**Example 1:**
12+
13+
**Input:** nums1 = [1,3,4], nums2 = [1,3,4], k = 1
14+
15+
**Output:** 5
16+
17+
**Explanation:**
18+
19+
The 5 good pairs are `(0, 0)`, `(1, 0)`, `(1, 1)`, `(2, 0)`, and `(2, 2)`.
20+
21+
**Example 2:**
22+
23+
**Input:** nums1 = [1,2,4,12], nums2 = [2,4], k = 3
24+
25+
**Output:** 2
26+
27+
**Explanation:**
28+
29+
The 2 good pairs are `(3, 0)` and `(3, 1)`.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= n, m <= 10<sup>5</sup></code>
34+
* <code>1 <= nums1[i], nums2[j] <= 10<sup>6</sup></code>
35+
* <code>1 <= k <= 10<sup>3</sup></code>
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package g3101_3200.s3165_maximum_sum_of_subsequence_with_non_adjacent_elements
2+
3+
// #Hard #Array #Dynamic_Programming #Divide_and_Conquer #Segment_Tree
4+
// #2024_06_03_Time_1301_ms_(22.22%)_Space_69.8_MB_(100.00%)
5+
6+
import java.util.stream.Stream
7+
import kotlin.math.max
8+
9+
class Solution {
10+
fun maximumSumSubsequence(nums: IntArray, queries: Array<IntArray>): Int {
11+
var ans = 0
12+
val segTree = SegTree(nums)
13+
for (q in queries) {
14+
val idx = q[0]
15+
val `val` = q[1]
16+
segTree.update(idx, `val`)
17+
ans = (ans + segTree.max!!) % MOD
18+
}
19+
return ans
20+
}
21+
22+
internal class SegTree(private val nums: IntArray) {
23+
private class Record {
24+
var takeFirstTakeLast: Int = 0
25+
var takeFirstSkipLast: Int = 0
26+
var skipFirstSkipLast: Int = 0
27+
var skipFirstTakeLast: Int = 0
28+
29+
val max: Int
30+
get() = Stream.of(
31+
this.takeFirstSkipLast,
32+
this.takeFirstTakeLast,
33+
this.skipFirstSkipLast,
34+
this.skipFirstTakeLast
35+
)
36+
.max { x: Int?, y: Int? -> x!!.compareTo(y!!) }
37+
.orElse(null)
38+
39+
fun skipLast(): Int? {
40+
return Stream.of(this.takeFirstSkipLast, this.skipFirstSkipLast)
41+
.max { x: Int?, y: Int? -> x!!.compareTo(y!!) }
42+
.orElse(null)
43+
}
44+
45+
fun takeLast(): Int? {
46+
return Stream.of(this.skipFirstTakeLast, this.takeFirstTakeLast)
47+
.max { x: Int?, y: Int? -> x!!.compareTo(y!!) }
48+
.orElse(null)
49+
}
50+
}
51+
52+
private val seg = arrayOfNulls<Record>(4 * nums.size)
53+
54+
init {
55+
for (i in 0 until 4 * nums.size) {
56+
seg[i] = Record()
57+
}
58+
build(0, nums.size - 1, 0)
59+
}
60+
61+
private fun build(i: Int, j: Int, k: Int) {
62+
if (i == j) {
63+
seg[k]!!.takeFirstTakeLast = nums[i]
64+
return
65+
}
66+
val mid = (i + j) shr 1
67+
build(i, mid, 2 * k + 1)
68+
build(mid + 1, j, 2 * k + 2)
69+
merge(k)
70+
}
71+
72+
// merge [2*k+1, 2*k+2] into k
73+
private fun merge(k: Int) {
74+
seg[k]!!.takeFirstSkipLast = max(
75+
(seg[2 * k + 1]!!.takeFirstSkipLast + seg[2 * k + 2]!!.skipLast()!!),
76+
(seg[2 * k + 1]!!.takeFirstTakeLast + seg[2 * k + 2]!!.skipFirstSkipLast)
77+
)
78+
79+
seg[k]!!.takeFirstTakeLast = max(
80+
(seg[2 * k + 1]!!.takeFirstSkipLast + seg[2 * k + 2]!!.takeLast()!!),
81+
(seg[2 * k + 1]!!.takeFirstTakeLast + seg[2 * k + 2]!!.skipFirstTakeLast)
82+
)
83+
84+
seg[k]!!.skipFirstTakeLast = max(
85+
(seg[2 * k + 1]!!.skipFirstSkipLast + seg[2 * k + 2]!!.takeLast()!!),
86+
(seg[2 * k + 1]!!.skipFirstTakeLast + seg[2 * k + 2]!!.skipFirstTakeLast)
87+
)
88+
89+
seg[k]!!.skipFirstSkipLast = max(
90+
(seg[2 * k + 1]!!.skipFirstSkipLast + seg[2 * k + 2]!!.skipLast()!!),
91+
(seg[2 * k + 1]!!.skipFirstTakeLast + seg[2 * k + 2]!!.skipFirstSkipLast)
92+
)
93+
}
94+
95+
// child -> parent
96+
fun update(idx: Int, `val`: Int) {
97+
val i = 0
98+
val j = nums.size - 1
99+
val k = 0
100+
update(idx, `val`, k, i, j)
101+
}
102+
103+
private fun update(idx: Int, `val`: Int, k: Int, i: Int, j: Int) {
104+
if (i == j) {
105+
seg[k]!!.takeFirstTakeLast = `val`
106+
return
107+
}
108+
val mid = (i + j) shr 1
109+
if (idx <= mid) {
110+
update(idx, `val`, 2 * k + 1, i, mid)
111+
} else {
112+
update(idx, `val`, 2 * k + 2, mid + 1, j)
113+
}
114+
merge(k)
115+
}
116+
117+
val max: Int?
118+
get() = seg[0]?.max
119+
}
120+
121+
companion object {
122+
private const val MOD = 1000000007
123+
}
124+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3165\. Maximum Sum of Subsequence With Non-adjacent Elements
2+
3+
Hard
4+
5+
You are given an array `nums` consisting of integers. You are also given a 2D array `queries`, where <code>queries[i] = [pos<sub>i</sub>, x<sub>i</sub>]</code>.
6+
7+
For query `i`, we first set <code>nums[pos<sub>i</sub>]</code> equal to <code>x<sub>i</sub></code>, then we calculate the answer to query `i` which is the **maximum** sum of a subsequence of `nums` where **no two adjacent elements are selected**.
8+
9+
Return the _sum_ of the answers to all queries.
10+
11+
Since the final answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
12+
13+
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.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [3,5,9], queries = [[1,-2],[0,-3]]
18+
19+
**Output:** 21
20+
21+
**Explanation:**
22+
After the 1<sup>st</sup> query, `nums = [3,-2,9]` and the maximum sum of a subsequence with non-adjacent elements is `3 + 9 = 12`.
23+
After the 2<sup>nd</sup> query, `nums = [-3,-2,9]` and the maximum sum of a subsequence with non-adjacent elements is 9.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [0,-1], queries = [[0,-5]]
28+
29+
**Output:** 0
30+
31+
**Explanation:**
32+
After the 1<sup>st</sup> query, `nums = [-5,-1]` and the maximum sum of a subsequence with non-adjacent elements is 0 (choosing an empty subsequence).
33+
34+
**Constraints:**
35+
36+
* <code>1 <= nums.length <= 5 * 10<sup>4</sup></code>
37+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
38+
* <code>1 <= queries.length <= 5 * 10<sup>4</sup></code>
39+
* <code>queries[i] == [pos<sub>i</sub>, x<sub>i</sub>]</code>
40+
* <code>0 <= pos<sub>i</sub> <= nums.length - 1</code>
41+
* <code>-10<sup>5</sup> <= x<sub>i</sub> <= 10<sup>5</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3101_3200.s3163_string_compression_iii
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun compressedString() {
10+
assertThat(Solution().compressedString("abcde"), equalTo("1a1b1c1d1e"))
11+
}
12+
13+
@Test
14+
fun compressedString2() {
15+
assertThat(Solution().compressedString("aaaaaaaaaaaaaabb"), equalTo("9a5a2b"))
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3101_3200.s3164_find_the_number_of_good_pairs_ii
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun numberOfPairs() {
10+
assertThat(
11+
Solution().numberOfPairs(intArrayOf(1, 3, 4), intArrayOf(1, 3, 4), 1),
12+
equalTo(5L)
13+
)
14+
}
15+
16+
@Test
17+
fun numberOfPairs2() {
18+
assertThat(
19+
Solution().numberOfPairs(intArrayOf(1, 2, 4, 12), intArrayOf(2, 4), 3),
20+
equalTo(2L)
21+
)
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3101_3200.s3165_maximum_sum_of_subsequence_with_non_adjacent_elements
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun maximumSumSubsequence() {
10+
assertThat(
11+
Solution()
12+
.maximumSumSubsequence(intArrayOf(3, 5, 9), arrayOf(intArrayOf(1, -2), intArrayOf(0, -3))),
13+
equalTo(21)
14+
)
15+
}
16+
17+
@Test
18+
fun maximumSumSubsequence2() {
19+
assertThat(
20+
Solution().maximumSumSubsequence(intArrayOf(0, -1), arrayOf(intArrayOf(0, -5))),
21+
equalTo(0)
22+
)
23+
}
24+
}

0 commit comments

Comments
 (0)