Skip to content

Commit ffd66ba

Browse files
authored
Added tasks 3158-3162
1 parent b791f94 commit ffd66ba

File tree

15 files changed

+539
-0
lines changed

15 files changed

+539
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3101_3200.s3158_find_the_xor_of_numbers_which_appear_twice;
2+
3+
// #Easy #Array #Hash_Table #Bit_Manipulation #2024_05_30_Time_1_ms_(99.87%)_Space_42.3_MB_(99.40%)
4+
5+
public class Solution {
6+
public int duplicateNumbersXOR(int[] nums) {
7+
boolean[] appeared = new boolean[51];
8+
int res = 0;
9+
for (int num : nums) {
10+
if (appeared[num]) {
11+
res ^= num;
12+
}
13+
appeared[num] = true;
14+
}
15+
return res;
16+
}
17+
}
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,22 @@
1+
package g3101_3200.s3159_find_occurrences_of_an_element_in_an_array;
2+
3+
// #Medium #Array #Hash_Table #2024_05_30_Time_4_ms_(96.74%)_Space_64_MB_(69.94%)
4+
5+
import java.util.ArrayList;
6+
7+
public class Solution {
8+
public int[] occurrencesOfElement(int[] nums, int[] queries, int x) {
9+
ArrayList<Integer> a = new ArrayList<>();
10+
for (int i = 0, l = nums.length; i < l; i++) {
11+
if (nums[i] == x) {
12+
a.add(i);
13+
}
14+
}
15+
int l = queries.length;
16+
int[] r = new int[l];
17+
for (int i = 0; i < l; i++) {
18+
r[i] = queries[i] > a.size() ? -1 : a.get(queries[i] - 1);
19+
}
20+
return r;
21+
}
22+
}
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,32 @@
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_36_ms_(98.82%)_Space_79.6_MB_(93.03%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
@SuppressWarnings("java:S1172")
9+
public class Solution {
10+
public int[] queryResults(int ignoredLimit, int[][] queries) {
11+
Map<Integer, Integer> ballToColor = new HashMap<>();
12+
Map<Integer, Integer> colorToCnt = new HashMap<>();
13+
int[] ret = new int[queries.length];
14+
for (int i = 0; i < queries.length; i += 1) {
15+
final int ball = queries[i][0];
16+
final int color = queries[i][1];
17+
if (ballToColor.containsKey(ball)) {
18+
int oldColor = ballToColor.get(ball);
19+
int oldColorCnt = colorToCnt.get(oldColor);
20+
if (oldColorCnt >= 2) {
21+
colorToCnt.put(oldColor, oldColorCnt - 1);
22+
} else {
23+
colorToCnt.remove(oldColor);
24+
}
25+
}
26+
ballToColor.put(ball, color);
27+
colorToCnt.put(color, colorToCnt.getOrDefault(color, 0) + 1);
28+
ret[i] = colorToCnt.size();
29+
}
30+
return ret;
31+
}
32+
}
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,107 @@
1+
package g3101_3200.s3161_block_placement_queries;
2+
3+
// #Hard #Array #Binary_Search #Segment_Tree #Binary_Indexed_Tree
4+
// #2024_05_30_Time_137_ms_(99.38%)_Space_143.7_MB_(54.52%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Solution {
10+
private static class Seg {
11+
private final int start;
12+
private final int end;
13+
private int min;
14+
private int max;
15+
private int len;
16+
private boolean obstacle;
17+
private Seg left;
18+
private Seg right;
19+
20+
public static Seg init(int n) {
21+
return new Seg(0, n);
22+
}
23+
24+
private Seg(int start, int end) {
25+
this.start = start;
26+
this.end = end;
27+
if (start >= end) {
28+
return;
29+
}
30+
int mid = start + ((end - start) >> 1);
31+
left = new Seg(start, mid);
32+
right = new Seg(mid + 1, end);
33+
refresh();
34+
}
35+
36+
public void set(int i) {
37+
if (i < start || i > end) {
38+
return;
39+
} else if (i == start && i == end) {
40+
obstacle = true;
41+
min = max = start;
42+
return;
43+
}
44+
left.set(i);
45+
right.set(i);
46+
refresh();
47+
}
48+
49+
private void refresh() {
50+
if (left.obstacle) {
51+
min = left.min;
52+
if (right.obstacle) {
53+
max = right.max;
54+
len = Math.max(right.min - left.max, Math.max(left.len, right.len));
55+
} else {
56+
max = left.max;
57+
len = Math.max(left.len, right.end - left.max);
58+
}
59+
obstacle = true;
60+
} else if (right.obstacle) {
61+
min = right.min;
62+
max = right.max;
63+
len = Math.max(right.len, right.min - left.start);
64+
obstacle = true;
65+
} else {
66+
len = end - start;
67+
}
68+
}
69+
70+
public void max(int n, int[] t) {
71+
if (end <= n) {
72+
t[0] = Math.max(t[0], len);
73+
if (obstacle) {
74+
t[1] = max;
75+
}
76+
return;
77+
}
78+
left.max(n, t);
79+
if (!right.obstacle || right.min >= n) {
80+
return;
81+
}
82+
t[0] = Math.max(t[0], right.min - t[1]);
83+
right.max(n, t);
84+
}
85+
}
86+
87+
public List<Boolean> getResults(int[][] queries) {
88+
int max = 0;
89+
for (int[] i : queries) {
90+
max = Math.max(max, i[1]);
91+
}
92+
Seg root = Seg.init(max);
93+
root.set(0);
94+
95+
List<Boolean> res = new ArrayList<>(queries.length);
96+
for (int[] i : queries) {
97+
if (i[0] == 1) {
98+
root.set(i[1]);
99+
} else {
100+
int[] t = new int[2];
101+
root.max(i[1], t);
102+
res.add(Math.max(t[0], i[1] - t[1]) >= i[2]);
103+
}
104+
}
105+
return res;
106+
}
107+
}
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_1_ms_(99.96%)_Space_42.1_MB_(99.36%)
4+
5+
public class Solution {
6+
public int numberOfPairs(int[] nums1, int[] nums2, int k) {
7+
int c = 0;
8+
for (int j : nums1) {
9+
for (int value : nums2) {
10+
if (j % (value * k) == 0) {
11+
c++;
12+
}
13+
}
14+
}
15+
return c;
16+
}
17+
}

0 commit comments

Comments
 (0)