Skip to content

Commit a1cd7e3

Browse files
authored
Added tasks 2745-2749
1 parent 34cdced commit a1cd7e3

File tree

15 files changed

+498
-0
lines changed

15 files changed

+498
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g2701_2800.s2745_construct_the_longest_new_string
2+
3+
// #Medium #Math #Greedy #Brainteaser #2023_08_08_Time_146_ms_(97.37%)_Space_33.6_MB_(94.74%)
4+
5+
class Solution {
6+
fun longestString(x: Int, y: Int, z: Int): Int {
7+
var first = x.coerceAtMost(y)
8+
var second = if (x == y) first else first + 1
9+
first = first shl 1
10+
second = second shl 1
11+
return first + second + (z shl 1)
12+
}
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2745\. Construct the Longest New String
2+
3+
Medium
4+
5+
You are given three integers `x`, `y`, and `z`.
6+
7+
You have `x` strings equal to `"AA"`, `y` strings equal to `"BB"`, and `z` strings equal to `"AB"`. You want to choose some (possibly all or none) of these strings and concactenate them in some order to form a new string. This new string must not contain `"AAA"` or `"BBB"` as a substring.
8+
9+
Return _the maximum possible length of the new string_.
10+
11+
A **substring** is a contiguous **non-empty** sequence of characters within a string.
12+
13+
**Example 1:**
14+
15+
**Input:** x = 2, y = 5, z = 1
16+
17+
**Output:** 12
18+
19+
**Explanation:** We can concactenate the strings "BB", "AA", "BB", "AA", "BB", and "AB" in that order. Then, our new string is "BBAABBAABBAB". That string has length 12, and we can show that it is impossible to construct a string of longer length.
20+
21+
**Example 2:**
22+
23+
**Input:** x = 3, y = 2, z = 2
24+
25+
**Output:** 14
26+
27+
**Explanation:** We can concactenate the strings "AB", "AB", "AA", "BB", "AA", "BB", and "AA" in that order. Then, our new string is "ABABAABBAABBAA". That string has length 14, and we can show that it is impossible to construct a string of longer length.
28+
29+
**Constraints:**
30+
31+
* `1 <= x, y, z <= 50`
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g2701_2800.s2746_decremental_string_concatenation
2+
3+
// #Medium #Array #String #Dynamic_Programming
4+
// #2023_08_08_Time_264_ms_(100.00%)_Space_44.7_MB_(59.38%)
5+
6+
class Solution {
7+
private val inf = 1e9.toInt()
8+
private lateinit var dp: Array<Array<Array<Int?>>>
9+
10+
fun minimizeConcatenatedLength(words: Array<String>): Int {
11+
val n = words.size
12+
dp = Array(n) {
13+
Array(26) {
14+
arrayOfNulls(
15+
26
16+
)
17+
}
18+
}
19+
val curWord = words[0]
20+
val curLen = curWord.length
21+
val curFirst = curWord[0]
22+
val curLast = curWord[curLen - 1]
23+
return curLen + solve(1, curFirst, curLast, n, words)
24+
}
25+
26+
private fun solve(idx: Int, prevFirst: Char, prevLast: Char, n: Int, words: Array<String>): Int {
27+
if (idx == n) return 0
28+
if (dp[idx][prevFirst.code - 'a'.code][prevLast.code - 'a'.code] != null) {
29+
return dp[idx][prevFirst.code - 'a'.code][prevLast.code - 'a'.code]!!
30+
}
31+
val curWord = words[idx]
32+
val curLen = curWord.length
33+
val curFirst = curWord[0]
34+
val curLast = curWord[curLen - 1]
35+
var ans = inf
36+
ans = if (prevFirst == curLast) {
37+
ans.coerceAtMost(curLen - 1 + solve(idx + 1, curFirst, prevLast, n, words))
38+
} else {
39+
ans.coerceAtMost(curLen + solve(idx + 1, curFirst, prevLast, n, words))
40+
}
41+
ans = if (prevLast == curFirst) {
42+
ans.coerceAtMost(curLen - 1 + solve(idx + 1, prevFirst, curLast, n, words))
43+
} else {
44+
ans.coerceAtMost(curLen + solve(idx + 1, prevFirst, curLast, n, words))
45+
}
46+
return ans.also { dp[idx][prevFirst.code - 'a'.code][prevLast.code - 'a'.code] = it }
47+
}
48+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2746\. Decremental String Concatenation
2+
3+
Medium
4+
5+
You are given a **0-indexed** array `words` containing `n` strings.
6+
7+
Let's define a **join** operation `join(x, y)` between two strings `x` and `y` as concatenating them into `xy`. However, if the last character of `x` is equal to the first character of `y`, one of them is **deleted**.
8+
9+
For example `join("ab", "ba") = "aba"` and `join("ab", "cde") = "abcde"`.
10+
11+
You are to perform `n - 1` **join** operations. Let <code>str<sub>0</sub> = words[0]</code>. Starting from `i = 1` up to `i = n - 1`, for the <code>i<sup>th</sup></code> operation, you can do one of the following:
12+
13+
* Make <code>str<sub>i</sub> = join(str<sub>i - 1</sub>, words[i])</code>
14+
* Make <code>str<sub>i</sub> = join(words[i], str<sub>i - 1</sub>)</code>
15+
16+
Your task is to **minimize** the length of <code>str<sub>n - 1</sub></code>.
17+
18+
Return _an integer denoting the minimum possible length of_ <code>str<sub>n - 1</sub></code>.
19+
20+
**Example 1:**
21+
22+
**Input:** words = ["aa","ab","bc"]
23+
24+
**Output:** 4
25+
26+
**Explanation:** In this example, we can perform join operations in the following order to minimize the length of str<sub>2</sub>:
27+
28+
str<sub>0</sub> = "aa"
29+
30+
str<sub>1</sub> = join(str<sub>0</sub>, "ab") = "aab"
31+
32+
str<sub>2</sub> = join(str<sub>1</sub>, "bc") = "aabc"
33+
34+
It can be shown that the minimum possible length of str<sub>2</sub> is 4.
35+
36+
**Example 2:**
37+
38+
**Input:** words = ["ab","b"]
39+
40+
**Output:** 2
41+
42+
**Explanation:** In this example, str<sub>0</sub> = "ab", there are two ways to get str<sub>1</sub>: join(str<sub>0</sub>, "b") = "ab" or join("b", str<sub>0</sub>) = "bab". The first string, "ab", has the minimum length. Hence, the answer is 2.
43+
44+
**Example 3:**
45+
46+
**Input:** words = ["aaa","c","aba"]
47+
48+
**Output:** 6
49+
50+
**Explanation:** In this example, we can perform join operations in the following order to minimize the length of str<sub>2</sub>:
51+
52+
str<sub>0</sub> = "aaa"
53+
54+
str<sub>1</sub> = join(str<sub>0</sub>, "c") = "aaac"
55+
56+
str<sub>2</sub> = join("aba", str<sub>1</sub>) = "abaaac"
57+
58+
It can be shown that the minimum possible length of str<sub>2</sub> is 6.
59+
60+
**Constraints:**
61+
62+
* `1 <= words.length <= 1000`
63+
* `1 <= words[i].length <= 50`
64+
* Each character in `words[i]` is an English lowercase letter
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g2701_2800.s2747_count_zero_request_servers
2+
3+
// #Medium #Array #Hash_Table #Sorting #Sliding_Window
4+
// #2023_08_08_Time_991_ms_(100.00%)_Space_109.2_MB_(90.00%)
5+
6+
import java.util.Arrays
7+
8+
class Solution {
9+
fun countServers(n: Int, logs: Array<IntArray>, x: Int, qs: IntArray): IntArray {
10+
val m = qs.size
11+
val valIdx = Array(m) { IntArray(2) }
12+
for (i in 0 until m) valIdx[i] = intArrayOf(qs[i], i)
13+
Arrays.sort(valIdx) { a: IntArray, b: IntArray ->
14+
a[0] - b[0]
15+
}
16+
Arrays.sort(logs) { a: IntArray, b: IntArray ->
17+
a[1] - b[1]
18+
}
19+
var l = 0
20+
var r = 0
21+
val res = IntArray(m)
22+
val servCount: HashMap<Int, Int> = HashMap<Int, Int>()
23+
for (q in valIdx) {
24+
val rVal = q[0]
25+
val lVal = q[0] - x
26+
val i = q[1]
27+
while (r < logs.size && logs[r][1] <= rVal) servCount.merge(logs[r++][0], 1) { a: Int, b: Int ->
28+
Integer.sum(
29+
a,
30+
b
31+
)
32+
}
33+
while (l < r && logs[l][1] < lVal) {
34+
servCount.compute(logs[l][0]) { _, v -> v!! - 1 }
35+
servCount.remove(logs[l][0], 0)
36+
l++
37+
}
38+
res[i] = n - servCount.size
39+
}
40+
return res
41+
}
42+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2747\. Count Zero Request Servers
2+
3+
Medium
4+
5+
You are given an integer `n` denoting the total number of servers and a **2D** **0-indexed** integer array `logs`, where `logs[i] = [server_id, time]` denotes that the server with id `server_id` received a request at time `time`.
6+
7+
You are also given an integer `x` and a **0-indexed** integer array `queries`.
8+
9+
Return _a **0-indexed** integer array_ `arr` _of length_ `queries.length` _where_ `arr[i]` _represents the number of servers that **did not receive** any requests during the time interval_ `[queries[i] - x, queries[i]]`.
10+
11+
Note that the time intervals are inclusive.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 3, logs = [[1,3],[2,6],[1,5]], x = 5, queries = [10,11]
16+
17+
**Output:** [1,2]
18+
19+
**Explanation:**
20+
21+
For queries[0]: The servers with ids 1 and 2 get requests in the duration of [5, 10].
22+
23+
Hence, only server 3 gets zero requests.
24+
25+
For queries[1]: Only the server with id 2 gets a request in duration of [6,11]. Hence, the servers with ids 1 and 3 are the only servers that do not receive any requests during that time period.
26+
27+
**Example 2:**
28+
29+
**Input:** n = 3, logs = [[2,4],[2,1],[1,2],[3,1]], x = 2, queries = [3,4]
30+
31+
**Output:** [0,1]
32+
33+
**Explanation:**
34+
35+
For queries[0]: All servers get at least one request in the duration of [1, 3].
36+
37+
For queries[1]: Only server with id 3 gets no request in the duration [2,4].
38+
39+
**Constraints:**
40+
41+
* <code>1 <= n <= 10<sup>5</sup></code>
42+
* <code>1 <= logs.length <= 10<sup>5</sup></code>
43+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
44+
* `logs[i].length == 2`
45+
* `1 <= logs[i][0] <= n`
46+
* <code>1 <= logs[i][1] <= 10<sup>6</sup></code>
47+
* <code>1 <= x <= 10<sup>5</sup></code>
48+
* <code>x < queries[i] <= 10<sup>6</sup></code>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g2701_2800.s2748_number_of_beautiful_pairs
2+
3+
// #Easy #Array #Math #Number_Theory #2023_08_08_Time_227_ms_(100.00%)_Space_38.5_MB_(100.00%)
4+
5+
class Solution {
6+
fun countBeautifulPairs(nums: IntArray): Int {
7+
var beautifulPairs = 0
8+
var i = 0
9+
var j = 1
10+
while (i < nums.size - 1) {
11+
val firstDigit = getFirstDigit(nums[i])
12+
while (j < nums.size) {
13+
val lastDigit = nums[j] % 10
14+
val botDigitsAreEqualAndNot1 = firstDigit == lastDigit && firstDigit > 1
15+
val botDigitsAreDivisibleBy2 = firstDigit % 2 == 0 && lastDigit % 2 == 0
16+
val botDigitsAreDivisibleBy3 = firstDigit % 3 == 0 && lastDigit % 3 == 0
17+
18+
if (!botDigitsAreEqualAndNot1 && !botDigitsAreDivisibleBy2 && !botDigitsAreDivisibleBy3) {
19+
beautifulPairs++
20+
}
21+
j++
22+
}
23+
i++
24+
j = i + 1
25+
}
26+
return beautifulPairs
27+
}
28+
29+
private fun getFirstDigit(num: Int): Int {
30+
var n = num
31+
var digit = 0
32+
while (n > 0) {
33+
digit = n % 10
34+
n /= 10
35+
}
36+
return digit
37+
}
38+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2748\. Number of Beautiful Pairs
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums`. A pair of indices `i`, `j` where `0 <= i < j < nums.length` is called beautiful if the **first digit** of `nums[i]` and the **last digit** of `nums[j]` are **coprime**.
6+
7+
Return _the total number of beautiful pairs in_ `nums`.
8+
9+
Two integers `x` and `y` are **coprime** if there is no integer greater than 1 that divides both of them. In other words, `x` and `y` are coprime if `gcd(x, y) == 1`, where `gcd(x, y)` is the **greatest common divisor** of `x` and `y`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,5,1,4]
14+
15+
**Output:** 5
16+
17+
**Explanation:** There are 5 beautiful pairs in nums:
18+
19+
When i = 0 and j = 1: the first digit of nums[0] is 2, and the last digit of nums[1] is 5. We can confirm that 2 and 5 are coprime, since gcd(2,5) == 1.
20+
21+
When i = 0 and j = 2: the first digit of nums[0] is 2, and the last digit of nums[2] is 1. Indeed, gcd(2,1) == 1.
22+
23+
When i = 1 and j = 2: the first digit of nums[1] is 5, and the last digit of nums[2] is 1. Indeed, gcd(5,1) == 1.
24+
25+
When i = 1 and j = 3: the first digit of nums[1] is 5, and the last digit of nums[3] is 4. Indeed, gcd(5,4) == 1.
26+
27+
When i = 2 and j = 3: the first digit of nums[2] is 1, and the last digit of nums[3] is 4. Indeed, gcd(1,4) == 1.
28+
29+
Thus, we return 5.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [11,21,12]
34+
35+
**Output:** 2
36+
37+
**Explanation:** There are 2 beautiful pairs:
38+
39+
When i = 0 and j = 1: the first digit of nums[0] is 1, and the last digit of nums[1] is 1. Indeed, gcd(1,1) == 1.
40+
41+
When i = 0 and j = 2: the first digit of nums[0] is 1, and the last digit of nums[2] is 2. Indeed, gcd(1,2) == 1.
42+
43+
Thus, we return 2.
44+
45+
**Constraints:**
46+
47+
* `2 <= nums.length <= 100`
48+
* `1 <= nums[i] <= 9999`
49+
* `nums[i] % 10 != 0`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2701_2800.s2749_minimum_operations_to_make_the_integer_zero
2+
3+
// #Medium #Bit_Manipulation #Brainteaser #2023_08_08_Time_132_ms_(91.67%)_Space_33.2_MB_(62.50%)
4+
5+
class Solution {
6+
fun makeTheIntegerZero(num1: Int, num2: Int): Int {
7+
val n1 = num1.toLong()
8+
val n2 = num2.toLong()
9+
for (i in 0..60) {
10+
val target = n1 - n2 * i
11+
val noOfBits = java.lang.Long.bitCount(target)
12+
if (i.toLong() in noOfBits..target) {
13+
return i
14+
}
15+
}
16+
return -1
17+
}
18+
}

0 commit comments

Comments
 (0)