Skip to content

Commit 4d2e92e

Browse files
authored
Added tasks 3158-3162
1 parent 5db62c0 commit 4d2e92e

File tree

15 files changed

+570
-0
lines changed

15 files changed

+570
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3101_3200.s3158_find_the_xor_of_numbers_which_appear_twice
2+
3+
// #Easy #Array #Hash_Table #Bit_Manipulation
4+
// #2024_05_30_Time_166_ms_(92.21%)_Space_36.5_MB_(76.62%)
5+
6+
class Solution {
7+
fun duplicateNumbersXOR(nums: IntArray): Int {
8+
val appeared = BooleanArray(51)
9+
var res = 0
10+
for (num in nums) {
11+
if (appeared[num]) {
12+
res = res xor num
13+
}
14+
appeared[num] = true
15+
}
16+
return res
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3158\. Find the XOR of Numbers Which Appear Twice
2+
3+
Easy
4+
5+
You are given an array `nums`, where each number in the array appears **either** once or twice.
6+
7+
Return the bitwise `XOR` of all the numbers that appear twice in the array, or 0 if no number appears twice.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,1,3]
12+
13+
**Output:** 1
14+
15+
**Explanation:**
16+
17+
The only number that appears twice in `nums` is 1.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,2,3]
22+
23+
**Output:** 0
24+
25+
**Explanation:**
26+
27+
No number appears twice in `nums`.
28+
29+
**Example 3:**
30+
31+
**Input:** nums = [1,2,2,1]
32+
33+
**Output:** 3
34+
35+
**Explanation:**
36+
37+
Numbers 1 and 2 appeared twice. `1 XOR 2 == 3`.
38+
39+
**Constraints:**
40+
41+
* `1 <= nums.length <= 50`
42+
* `1 <= nums[i] <= 50`
43+
* Each number in `nums` appears either once or twice.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3101_3200.s3159_find_occurrences_of_an_element_in_an_array
2+
3+
// #Medium #Array #Hash_Table #2024_05_30_Time_810_ms_(98.28%)_Space_66.3_MB_(81.03%)
4+
5+
class Solution {
6+
fun occurrencesOfElement(nums: IntArray, queries: IntArray, x: Int): IntArray {
7+
val a = ArrayList<Int>()
8+
run {
9+
var i = 0
10+
val l = nums.size
11+
while (i < l) {
12+
if (nums[i] == x) {
13+
a.add(i)
14+
}
15+
i++
16+
}
17+
}
18+
val l = queries.size
19+
val r = IntArray(l)
20+
for (i in 0 until l) {
21+
r[i] = if (queries[i] > a.size) -1 else a[queries[i] - 1]
22+
}
23+
return r
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3159\. Find Occurrences of an Element in an Array
2+
3+
Medium
4+
5+
You are given an integer array `nums`, an integer array `queries`, and an integer `x`.
6+
7+
For each `queries[i]`, you need to find the index of the <code>queries[i]<sup>th</sup></code> occurrence of `x` in the `nums` array. If there are fewer than `queries[i]` occurrences of `x`, the answer should be -1 for that query.
8+
9+
Return an integer array `answer` containing the answers to all queries.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,3,1,7], queries = [1,3,2,4], x = 1
14+
15+
**Output:** [0,-1,2,-1]
16+
17+
**Explanation:**
18+
19+
* For the 1<sup>st</sup> query, the first occurrence of 1 is at index 0.
20+
* For the 2<sup>nd</sup> query, there are only two occurrences of 1 in `nums`, so the answer is -1.
21+
* For the 3<sup>rd</sup> query, the second occurrence of 1 is at index 2.
22+
* For the 4<sup>th</sup> query, there are only two occurrences of 1 in `nums`, so the answer is -1.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [1,2,3], queries = [10], x = 5
27+
28+
**Output:** [-1]
29+
30+
**Explanation:**
31+
32+
* For the 1<sup>st</sup> query, 5 doesn't exist in `nums`, so the answer is -1.
33+
34+
**Constraints:**
35+
36+
* <code>1 <= nums.length, queries.length <= 10<sup>5</sup></code>
37+
* <code>1 <= queries[i] <= 10<sup>5</sup></code>
38+
* <code>1 <= nums[i], x <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g3101_3200.s3160_find_the_number_of_distinct_colors_among_the_balls
2+
3+
// #Medium #Array #Hash_Table #Simulation #2024_05_30_Time_1055_ms_(58.82%)_Space_101.1_MB_(86.27%)
4+
5+
class Solution {
6+
fun queryResults(ignoredLimit: Int, queries: Array<IntArray>): IntArray {
7+
val ballToColor: MutableMap<Int, Int> = HashMap()
8+
val colorToCnt: MutableMap<Int, Int> = HashMap()
9+
val ret = IntArray(queries.size)
10+
var i = 0
11+
while (i < queries.size) {
12+
val ball = queries[i][0]
13+
val color = queries[i][1]
14+
if (ballToColor.containsKey(ball)) {
15+
val oldColor = ballToColor[ball]!!
16+
val oldColorCnt = colorToCnt[oldColor]!!
17+
if (oldColorCnt >= 2) {
18+
colorToCnt[oldColor] = oldColorCnt - 1
19+
} else {
20+
colorToCnt.remove(oldColor)
21+
}
22+
}
23+
ballToColor[ball] = color
24+
colorToCnt[color] = colorToCnt.getOrDefault(color, 0) + 1
25+
ret[i] = colorToCnt.size
26+
i += 1
27+
}
28+
return ret
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3160\. Find the Number of Distinct Colors Among the Balls
2+
3+
Medium
4+
5+
You are given an integer `limit` and a 2D array `queries` of size `n x 2`.
6+
7+
There are `limit + 1` balls with **distinct** labels in the range `[0, limit]`. Initially, all balls are uncolored. For every query in `queries` that is of the form `[x, y]`, you mark ball `x` with the color `y`. After each query, you need to find the number of **distinct** colors among the balls.
8+
9+
Return an array `result` of length `n`, where `result[i]` denotes the number of distinct colors _after_ <code>i<sup>th</sup></code> query.
10+
11+
**Note** that when answering a query, lack of a color _will not_ be considered as a color.
12+
13+
**Example 1:**
14+
15+
**Input:** limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]]
16+
17+
**Output:** [1,2,2,3]
18+
19+
**Explanation:**
20+
21+
![](https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop.gif)
22+
23+
* After query 0, ball 1 has color 4.
24+
* After query 1, ball 1 has color 4, and ball 2 has color 5.
25+
* After query 2, ball 1 has color 3, and ball 2 has color 5.
26+
* After query 3, ball 1 has color 3, ball 2 has color 5, and ball 3 has color 4.
27+
28+
**Example 2:**
29+
30+
**Input:** limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]]
31+
32+
**Output:** [1,2,2,3,4]
33+
34+
**Explanation:**
35+
36+
**![](https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop2.gif)**
37+
38+
* After query 0, ball 0 has color 1.
39+
* After query 1, ball 0 has color 1, and ball 1 has color 2.
40+
* After query 2, ball 0 has color 1, and balls 1 and 2 have color 2.
41+
* After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4.
42+
* After query 4, ball 0 has color 1, balls 1 and 2 have color 2, ball 3 has color 4, and ball 4 has color 5.
43+
44+
**Constraints:**
45+
46+
* <code>1 <= limit <= 10<sup>9</sup></code>
47+
* <code>1 <= n == queries.length <= 10<sup>5</sup></code>
48+
* `queries[i].length == 2`
49+
* `0 <= queries[i][0] <= limit`
50+
* <code>1 <= queries[i][1] <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package g3101_3200.s3161_block_placement_queries
2+
3+
// #Hard #Array #Binary_Search #Segment_Tree #Binary_Indexed_Tree
4+
// #2024_05_30_Time_1701_ms_(100.00%)_Space_174.7_MB_(33.33%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
private class Seg private constructor(private val start: Int, private val end: Int) {
10+
private var min = 0
11+
private var max = 0
12+
private var len = 0
13+
private var obstacle = false
14+
private lateinit var left: Seg
15+
private lateinit var right: Seg
16+
17+
init {
18+
if (start < end) {
19+
val mid = start + ((end - start) shr 1)
20+
left = Seg(start, mid)
21+
right = Seg(mid + 1, end)
22+
refresh()
23+
}
24+
}
25+
26+
fun set(i: Int) {
27+
if (i < start || i > end) {
28+
return
29+
} else if (i == start && i == end) {
30+
obstacle = true
31+
max = start
32+
min = max
33+
return
34+
}
35+
left.set(i)
36+
right.set(i)
37+
refresh()
38+
}
39+
40+
private fun refresh() {
41+
if (left.obstacle) {
42+
min = left.min
43+
if (right.obstacle) {
44+
max = right.max
45+
len = max((right.min - left.max), max(left.len, right.len))
46+
} else {
47+
max = left.max
48+
len = max(left.len, (right.end - left.max))
49+
}
50+
obstacle = true
51+
} else if (right.obstacle) {
52+
min = right.min
53+
max = right.max
54+
len = max(right.len, (right.min - left.start))
55+
obstacle = true
56+
} else {
57+
len = end - start
58+
}
59+
}
60+
61+
fun max(n: Int, t: IntArray) {
62+
if (end <= n) {
63+
t[0] = max(t[0], len)
64+
if (obstacle) {
65+
t[1] = max
66+
}
67+
return
68+
}
69+
left.max(n, t)
70+
if (!right.obstacle || right.min >= n) {
71+
return
72+
}
73+
t[0] = max(t[0], (right.min - t[1]))
74+
right.max(n, t)
75+
}
76+
77+
companion object {
78+
fun init(n: Int): Seg {
79+
return Seg(0, n)
80+
}
81+
}
82+
}
83+
84+
fun getResults(queries: Array<IntArray>): List<Boolean> {
85+
var max = 0
86+
for (i in queries) {
87+
max = max(max, i[1])
88+
}
89+
val root = Seg.init(max)
90+
root.set(0)
91+
92+
val res: MutableList<Boolean> = ArrayList(queries.size)
93+
for (i in queries) {
94+
if (i[0] == 1) {
95+
root.set(i[1])
96+
} else {
97+
val t = IntArray(2)
98+
root.max(i[1], t)
99+
res.add(max(t[0], (i[1] - t[1])) >= i[2])
100+
}
101+
}
102+
return res
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
3161\. Block Placement Queries
2+
3+
Hard
4+
5+
There exists an infinite number line, with its origin at 0 and extending towards the **positive** x-axis.
6+
7+
You are given a 2D array `queries`, which contains two types of queries:
8+
9+
1. For a query of type 1, `queries[i] = [1, x]`. Build an obstacle at distance `x` from the origin. It is guaranteed that there is **no** obstacle at distance `x` when the query is asked.
10+
2. For a query of type 2, `queries[i] = [2, x, sz]`. Check if it is possible to place a block of size `sz` _anywhere_ in the range `[0, x]` on the line, such that the block **entirely** lies in the range `[0, x]`. A block **cannot** be placed if it intersects with any obstacle, but it may touch it. Note that you do **not** actually place the block. Queries are separate.
11+
12+
Return a boolean array `results`, where `results[i]` is `true` if you can place the block specified in the <code>i<sup>th</sup></code> query of type 2, and `false` otherwise.
13+
14+
**Example 1:**
15+
16+
**Input:** queries = [[1,2],[2,3,3],[2,3,1],[2,2,2]]
17+
18+
**Output:** [false,true,true]
19+
20+
**Explanation:**
21+
22+
**![](https://assets.leetcode.com/uploads/2024/04/22/example0block.png)**
23+
24+
For query 0, place an obstacle at `x = 2`. A block of size at most 2 can be placed before `x = 3`.
25+
26+
**Example 2:**
27+
28+
**Input:** queries = [[1,7],[2,7,6],[1,2],[2,7,5],[2,7,6]]
29+
30+
**Output:** [true,true,false]
31+
32+
**Explanation:**
33+
34+
**![](https://assets.leetcode.com/uploads/2024/04/22/example1block.png)**
35+
36+
* Place an obstacle at `x = 7` for query 0. A block of size at most 7 can be placed before `x = 7`.
37+
* Place an obstacle at `x = 2` for query 2. Now, a block of size at most 5 can be placed before `x = 7`, and a block of size at most 2 before `x = 2`.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= queries.length <= 15 * 10<sup>4</sup></code>
42+
* `2 <= queries[i].length <= 3`
43+
* `1 <= queries[i][0] <= 2`
44+
* <code>1 <= x, sz <= min(5 * 10<sup>4</sup>, 3 * queries.length)</code>
45+
* The input is generated such that for queries of type 1, no obstacle exists at distance `x` when the query is asked.
46+
* The input is generated such that there is at least one query of type 2.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3101_3200.s3162_find_the_number_of_good_pairs_i
2+
3+
// #Easy #Array #Hash_Table #2024_05_30_Time_182_ms_(54.41%)_Space_36.1_MB_(94.12%)
4+
5+
class Solution {
6+
fun numberOfPairs(nums1: IntArray, nums2: IntArray, k: Int): Int {
7+
var count = 0
8+
for (j in nums1) {
9+
for (value in nums2) {
10+
if (j % (value * k) == 0) {
11+
count++
12+
}
13+
}
14+
}
15+
return count
16+
}
17+
}

0 commit comments

Comments
 (0)