Skip to content

Commit 511450c

Browse files
authored
Added tasks 2053-2063
1 parent 91035de commit 511450c

File tree

30 files changed

+1279
-0
lines changed

30 files changed

+1279
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2001_2100.s2053_kth_distinct_string_in_an_array
2+
3+
// #Easy #Array #String #Hash_Table #Counting
4+
// #2023_06_25_Time_181_ms_(90.00%)_Space_36.9_MB_(90.00%)
5+
6+
class Solution {
7+
fun kthDistinct(arr: Array<String>, k: Int): String {
8+
val m: MutableMap<String, Int> = HashMap()
9+
for (value in arr) {
10+
m[value] = m.getOrDefault(value, 0) + 1
11+
}
12+
var c = 0
13+
for (s in arr) {
14+
if (m[s] == 1) {
15+
++c
16+
if (c == k) {
17+
return s
18+
}
19+
}
20+
}
21+
return ""
22+
}
23+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2053\. Kth Distinct String in an Array
2+
3+
Easy
4+
5+
A **distinct string** is a string that is present only **once** in an array.
6+
7+
Given an array of strings `arr`, and an integer `k`, return _the_ <code>k<sup>th</sup></code> _**distinct string** present in_ `arr`. If there are **fewer** than `k` distinct strings, return _an **empty string**_ `""`.
8+
9+
Note that the strings are considered in the **order in which they appear** in the array.
10+
11+
**Example 1:**
12+
13+
**Input:** arr = ["d","b","c","b","c","a"], k = 2
14+
15+
**Output:** "a"
16+
17+
**Explanation:**
18+
19+
The only distinct strings in arr are "d" and "a".
20+
21+
"d" appears 1<sup>st</sup>, so it is the 1<sup>st</sup> distinct string.
22+
23+
"a" appears 2<sup>nd</sup>, so it is the 2<sup>nd</sup> distinct string.
24+
25+
Since k == 2, "a" is returned.
26+
27+
**Example 2:**
28+
29+
**Input:** arr = ["aaa","aa","a"], k = 1
30+
31+
**Output:** "aaa"
32+
33+
**Explanation:**
34+
35+
All strings in arr are distinct, so the 1<sup>st</sup> string "aaa" is returned.
36+
37+
**Example 3:**
38+
39+
**Input:** arr = ["a","b","a"], k = 3
40+
41+
**Output:** ""
42+
43+
**Explanation:** The only distinct string is "b". Since there are fewer than 3 distinct strings, we return an empty string "".
44+
45+
**Constraints:**
46+
47+
* `1 <= k <= arr.length <= 1000`
48+
* `1 <= arr[i].length <= 5`
49+
* `arr[i]` consists of lowercase English letters.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g2001_2100.s2054_two_best_non_overlapping_events
2+
3+
// #Medium #Array #Dynamic_Programming #Sorting #Binary_Search #Heap_Priority_Queue
4+
// #2023_06_25_Time_851_ms_(100.00%)_Space_108.7_MB_(50.00%)
5+
6+
import java.util.Arrays
7+
8+
class Solution {
9+
fun maxTwoEvents(events: Array<IntArray>): Int {
10+
Arrays.sort(events) { a: IntArray, b: IntArray -> a[0] - b[0] }
11+
val max = IntArray(events.size)
12+
for (i in events.indices.reversed()) {
13+
if (i == events.size - 1) {
14+
max[i] = events[i][2]
15+
} else {
16+
max[i] = events[i][2].coerceAtLeast(max[i + 1])
17+
}
18+
}
19+
var ans = 0
20+
for (i in events.indices) {
21+
val end = events[i][1]
22+
var left = i + 1
23+
var right = events.size
24+
while (left < right) {
25+
val mid = left + (right - left) / 2
26+
if (events[mid][0] <= end) {
27+
left = mid + 1
28+
} else {
29+
right = mid
30+
}
31+
}
32+
var value = events[i][2]
33+
if (right < events.size) {
34+
value += max[right]
35+
}
36+
ans = ans.coerceAtLeast(value)
37+
}
38+
return ans
39+
}
40+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2054\. Two Best Non-Overlapping Events
2+
3+
Medium
4+
5+
You are given a **0-indexed** 2D integer array of `events` where <code>events[i] = [startTime<sub>i</sub>, endTime<sub>i</sub>, value<sub>i</sub>]</code>. The <code>i<sup>th</sup></code> event starts at <code>startTime<sub>i</sub></code> and ends at <code>endTime<sub>i</sub></code>, and if you attend this event, you will receive a value of <code>value<sub>i</sub></code>. You can choose **at most** **two** **non-overlapping** events to attend such that the sum of their values is **maximized**.
6+
7+
Return _this **maximum** sum._
8+
9+
Note that the start time and end time is **inclusive**: that is, you cannot attend two events where one of them starts and the other ends at the same time. More specifically, if you attend an event with end time `t`, the next event must start at or after `t + 1`.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/09/21/picture5.png)
14+
15+
**Input:** events = [[1,3,2],[4,5,2],[2,4,3]]
16+
17+
**Output:** 4
18+
19+
**Explanation:** Choose the green events, 0 and 1 for a sum of 2 + 2 = 4.
20+
21+
**Example 2:**
22+
23+
![Example 1 Diagram](https://assets.leetcode.com/uploads/2021/09/21/picture1.png)
24+
25+
**Input:** events = [[1,3,2],[4,5,2],[1,5,5]]
26+
27+
**Output:** 5
28+
29+
**Explanation:** Choose event 2 for a sum of 5.
30+
31+
**Example 3:**
32+
33+
![](https://assets.leetcode.com/uploads/2021/09/21/picture3.png)
34+
35+
**Input:** events = [[1,5,3],[1,5,1],[6,6,5]]
36+
37+
**Output:** 8
38+
39+
**Explanation:** Choose events 0 and 2 for a sum of 3 + 5 = 8.
40+
41+
**Constraints:**
42+
43+
* <code>2 <= events.length <= 10<sup>5</sup></code>
44+
* `events[i].length == 3`
45+
* <code>1 <= startTime<sub>i</sub> <= endTime<sub>i</sub> <= 10<sup>9</sup></code>
46+
* <code>1 <= value<sub>i</sub> <= 10<sup>6</sup></code>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g2001_2100.s2055_plates_between_candles
2+
3+
// #Medium #Array #String #Binary_Search #Prefix_Sum
4+
// #2023_06_25_Time_831_ms_(100.00%)_Space_96.3_MB_(100.00%)
5+
6+
class Solution {
7+
fun platesBetweenCandles(s: String, queries: Array<IntArray>): IntArray {
8+
val sa = s.toCharArray()
9+
val n = sa.size
10+
val nextCandle = IntArray(n + 1)
11+
nextCandle[n] = -1
12+
for (i in n - 1 downTo 0) {
13+
nextCandle[i] = if (sa[i] == '|') i else nextCandle[i + 1]
14+
}
15+
val prevCandle = IntArray(n)
16+
val prevPlates = IntArray(n)
17+
var countPlate = 0
18+
if (sa[0] == '*') {
19+
countPlate = 1
20+
prevCandle[0] = -1
21+
}
22+
for (i in 1 until n) {
23+
if (sa[i] == '|') {
24+
prevCandle[i] = i
25+
prevPlates[i] = countPlate
26+
} else {
27+
prevCandle[i] = prevCandle[i - 1]
28+
countPlate++
29+
}
30+
}
31+
val len = queries.size
32+
val res = IntArray(len)
33+
for (i in 0 until len) {
34+
val query = queries[i]
35+
val start = query[0]
36+
val end = query[1]
37+
var curPlates = 0
38+
val endCandle = prevCandle[end]
39+
if (endCandle >= start) {
40+
val startCandle = nextCandle[start]
41+
curPlates = prevPlates[endCandle] - prevPlates[startCandle]
42+
}
43+
res[i] = curPlates
44+
}
45+
return res
46+
}
47+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2055\. Plates Between Candles
2+
3+
Medium
4+
5+
There is a long table with a line of plates and candles arranged on top of it. You are given a **0-indexed** string `s` consisting of characters `'*'` and `'|'` only, where a `'*'` represents a **plate** and a `'|'` represents a **candle**.
6+
7+
You are also given a **0-indexed** 2D integer array `queries` where <code>queries[i] = [left<sub>i</sub>, right<sub>i</sub>]</code> denotes the **substring** <code>s[left<sub>i</sub>...right<sub>i</sub>]</code> (**inclusive**). For each query, you need to find the **number** of plates **between candles** that are **in the substring**. A plate is considered **between candles** if there is at least one candle to its left **and** at least one candle to its right **in the substring**.
8+
9+
* For example, `s = "||**||**|*"`, and a query `[3, 8]` denotes the substring `"*||******|"`. The number of plates between candles in this substring is `2`, as each of the two plates has at least one candle **in the substring** to its left **and** right.
10+
11+
Return _an integer array_ `answer` _where_ `answer[i]` _is the answer to the_ <code>i<sup>th</sup></code> _query_.
12+
13+
**Example 1:**
14+
15+
![ex-1](https://assets.leetcode.com/uploads/2021/10/04/ex-1.png)
16+
17+
**Input:** s = "\*\*|\*\*|\*\*\*|", queries = [[2,5],[5,9]]
18+
19+
**Output:** [2,3]
20+
21+
**Explanation:** - queries[0] has two plates between candles. - queries[1] has three plates between candles.
22+
23+
**Example 2:**
24+
25+
![ex-2](https://assets.leetcode.com/uploads/2021/10/04/ex-2.png)
26+
27+
**Input:** s = "\*\*\*|\*\*|\*\*\*\*\*|\*\*||\*\*|\*", queries = [[1,17],[4,5],[14,17],[5,11],[15,16]]
28+
29+
**Output:** [9,0,0,0,0]
30+
31+
**Explanation:** - queries[0] has nine plates between candles. - The other queries have zero plates between candles.
32+
33+
**Constraints:**
34+
35+
* <code>3 <= s.length <= 10<sup>5</sup></code>
36+
* `s` consists of `'*'` and `'|'` characters.
37+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
38+
* `queries[i].length == 2`
39+
* <code>0 <= left<sub>i</sub> <= right<sub>i</sub> < s.length</code>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package g2001_2100.s2056_number_of_valid_move_combinations_on_chessboard
2+
3+
// #Hard #Array #String #Simulation #Backtracking
4+
// #2023_06_25_Time_600_ms_(100.00%)_Space_44.1_MB_(100.00%)
5+
6+
class Solution {
7+
// 0: rook, queen, bishop
8+
private val dirs = arrayOf(
9+
arrayOf(intArrayOf(-1, 0), intArrayOf(1, 0), intArrayOf(0, -1), intArrayOf(0, 1)),
10+
arrayOf(
11+
intArrayOf(-1, 0),
12+
intArrayOf(1, 0),
13+
intArrayOf(0, -1),
14+
intArrayOf(0, 1),
15+
intArrayOf(1, 1),
16+
intArrayOf(-1, -1),
17+
intArrayOf(-1, 1),
18+
intArrayOf(1, -1)
19+
),
20+
arrayOf(intArrayOf(1, 1), intArrayOf(-1, -1), intArrayOf(-1, 1), intArrayOf(1, -1))
21+
)
22+
23+
fun countCombinations(pieces: Array<String?>, positions: Array<IntArray>): Int {
24+
val endPosition: Array<ArrayList<IntArray>?> = arrayOfNulls(pieces.size)
25+
for (i in pieces.indices) {
26+
endPosition[i] = ArrayList()
27+
}
28+
for (i in pieces.indices) {
29+
positions[i][0]--
30+
positions[i][1]--
31+
endPosition[i]!!.add(positions[i])
32+
var dirIndex = 0
33+
when (pieces[i]) {
34+
"rook" -> dirIndex = 0
35+
"queen" -> dirIndex = 1
36+
"bishop" -> dirIndex = 2
37+
}
38+
for (d in dirs[dirIndex]) {
39+
var r = positions[i][0]
40+
var c = positions[i][1]
41+
while (true) {
42+
r += d[0]
43+
c += d[1]
44+
if (r < 0 || r >= 8 || c < 0 || c >= 8) {
45+
break
46+
}
47+
endPosition[i]!!.add(intArrayOf(r, c))
48+
}
49+
}
50+
}
51+
return dfs(positions, endPosition, IntArray(pieces.size), 0)
52+
}
53+
54+
private fun dfs(positions: Array<IntArray>, stop: Array<ArrayList<IntArray>?>, stopIndex: IntArray, cur: Int): Int {
55+
if (cur == stopIndex.size) {
56+
val p = Array(positions.size) { IntArray(2) }
57+
for (i in p.indices) {
58+
p[i] = intArrayOf(positions[i][0], positions[i][1])
59+
}
60+
return check(p, stop, stopIndex)
61+
}
62+
var res = 0
63+
for (i in stop[cur]!!.indices) {
64+
stopIndex[cur] = i
65+
res += dfs(positions, stop, stopIndex, cur + 1)
66+
}
67+
return res
68+
}
69+
70+
private fun check(positions: Array<IntArray>, stop: Array<ArrayList<IntArray>?>, stopIndex: IntArray): Int {
71+
var keepGoing = true
72+
while (keepGoing) {
73+
keepGoing = false
74+
for (i in positions.indices) {
75+
var diff = stop[i]!![stopIndex[i]][0] - positions[i][0]
76+
if (diff > 0) {
77+
keepGoing = true
78+
positions[i][0]++
79+
} else if (diff < 0) {
80+
keepGoing = true
81+
positions[i][0]--
82+
}
83+
diff = stop[i]!![stopIndex[i]][1] - positions[i][1]
84+
if (diff > 0) {
85+
keepGoing = true
86+
positions[i][1]++
87+
} else if (diff < 0) {
88+
keepGoing = true
89+
positions[i][1]--
90+
}
91+
}
92+
val seen: MutableSet<Int> = HashSet()
93+
for (position in positions) {
94+
val key = position[0] * 100 + position[1]
95+
if (seen.contains(key)) {
96+
return 0
97+
}
98+
seen.add(key)
99+
}
100+
}
101+
return 1
102+
}
103+
}

0 commit comments

Comments
 (0)