Skip to content

Commit eb62317

Browse files
authored
Added tasks 1937, 1938, 1941, 1942, 1943.
1 parent 736d186 commit eb62317

File tree

15 files changed

+603
-0
lines changed

15 files changed

+603
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g1901_2000.s1937_maximum_number_of_points_with_cost;
2+
3+
// #Medium #Array #Dynamic_Programming #2022_05_16_Time_18_ms_(44.04%)_Space_128_MB_(22.97%)
4+
5+
public class Solution {
6+
public long maxPoints(int[][] points) {
7+
int m = points[0].length;
8+
9+
long[] pre = new long[m];
10+
11+
for (int[] point : points) {
12+
long[] current = new long[m];
13+
14+
long max = Long.MIN_VALUE;
15+
for (int j = 0; j < m; j++) {
16+
max = Math.max(max, pre[j] + j);
17+
current[j] = Math.max(current[j], point[j] - j + max);
18+
}
19+
20+
max = Long.MIN_VALUE;
21+
for (int j = m - 1; j >= 0; j--) {
22+
max = Math.max(max, pre[j] - j);
23+
current[j] = Math.max(current[j], point[j] + j + max);
24+
}
25+
pre = current;
26+
}
27+
28+
long max = Long.MIN_VALUE;
29+
for (long val : pre) {
30+
max = Math.max(max, val);
31+
}
32+
return max;
33+
}
34+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
1937\. Maximum Number of Points with Cost
2+
3+
Medium
4+
5+
You are given an `m x n` integer matrix `points` (**0-indexed**). Starting with `0` points, you want to **maximize** the number of points you can get from the matrix.
6+
7+
To gain points, you must pick one cell in **each row**. Picking the cell at coordinates `(r, c)` will **add** `points[r][c]` to your score.
8+
9+
However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows `r` and `r + 1` (where `0 <= r < m - 1`), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will **subtract** <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.
10+
11+
Return _the **maximum** number of points you can achieve_.
12+
13+
`abs(x)` is defined as:
14+
15+
* `x` for `x >= 0`.
16+
* `-x` for `x < 0`.
17+
18+
**Example 1:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png)
21+
22+
**Input:** points = [[1,2,3],[1,5,1],[3,1,1]]
23+
24+
**Output:** 9
25+
26+
**Explanation:**
27+
28+
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
29+
30+
You add 3 + 5 + 3 = 11 to your score.
31+
32+
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
33+
34+
Your final score is 11 - 2 = 9.
35+
36+
**Example 2:**
37+
38+
![](https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png)
39+
40+
**Input:** points = [[1,5],[2,3],[4,2]]
41+
42+
**Output:** 11
43+
44+
**Explanation:**
45+
46+
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
47+
48+
You add 5 + 3 + 4 = 12 to your score.
49+
50+
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
51+
52+
Your final score is 12 - 1 = 11.
53+
54+
**Constraints:**
55+
56+
* `m == points.length`
57+
* `n == points[r].length`
58+
* <code>1 <= m, n <= 10<sup>5</sup></code>
59+
* <code>1 <= m * n <= 10<sup>5</sup></code>
60+
* <code>0 <= points[r][c] <= 10<sup>5</sup></code>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g1901_2000.s1938_maximum_genetic_difference_query;
2+
3+
// #Hard #Array #Bit_Manipulation #Trie #2022_05_16_Time_174_ms_(100.00%)_Space_134.4_MB_(85.00%)
4+
5+
public class Solution {
6+
public int[] maxGeneticDifference(int[] parents, int[][] queries) {
7+
int n = parents.length;
8+
int[][] fd = new int[n][];
9+
for (int i = 0; i < n; i++) {
10+
fill(parents, n, fd, i);
11+
}
12+
int[] ret = new int[queries.length];
13+
for (int q = 0; q < queries.length; q++) {
14+
int cur = queries[q][0];
15+
int value = queries[q][1];
16+
for (int p = 30; p >= 0; p--) {
17+
int msk = 1 << p;
18+
if ((value & msk) != (cur & msk)) {
19+
ret[q] |= msk;
20+
} else if (fd[cur][p] >= 0) {
21+
ret[q] |= msk;
22+
cur = fd[cur][p];
23+
}
24+
}
25+
}
26+
return ret;
27+
}
28+
29+
private void fill(int[] parents, int n, int[][] fd, int i) {
30+
if (fd[i] == null) {
31+
fd[i] = new int[31];
32+
int a = parents[i];
33+
if (a >= 0) {
34+
fill(parents, n, fd, a);
35+
}
36+
for (int p = 30; p >= 0; p--) {
37+
if (a == -1) {
38+
fd[i][p] = -1;
39+
} else {
40+
if ((i & (1 << p)) == (a & (1 << p))) {
41+
fd[i][p] = fd[a][p];
42+
} else {
43+
fd[i][p] = a;
44+
a = fd[a][p];
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
1938\. Maximum Genetic Difference Query
2+
3+
Hard
4+
5+
There is a rooted tree consisting of `n` nodes numbered `0` to `n - 1`. Each node's number denotes its **unique genetic value** (i.e. the genetic value of node `x` is `x`). The **genetic difference** between two genetic values is defined as the **bitwise-****XOR** of their values. You are given the integer array `parents`, where `parents[i]` is the parent for node `i`. If node `x` is the **root** of the tree, then `parents[x] == -1`.
6+
7+
You are also given the array `queries` where <code>queries[i] = [node<sub>i</sub>, val<sub>i</sub>]</code>. For each query `i`, find the **maximum genetic difference** between <code>val<sub>i</sub></code> and <code>p<sub>i</sub></code>, where <code>p<sub>i</sub></code> is the genetic value of any node that is on the path between <code>node<sub>i</sub></code> and the root (including <code>node<sub>i</sub></code> and the root). More formally, you want to maximize <code>val<sub>i</sub> XOR p<sub>i</sub></code>.
8+
9+
Return _an array_ `ans` _where_ `ans[i]` _is the answer to the_ <code>i<sup>th</sup></code> _query_.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/06/29/c1.png)
14+
15+
**Input:** parents = [-1,0,1,1], queries = [[0,2],[3,2],[2,5]]
16+
17+
**Output:** [2,3,7]
18+
19+
**Explanation:** The queries are processed as follows:
20+
21+
- [0,2]: The node with the maximum genetic difference is 0, with a difference of 2 XOR 0 = 2.
22+
23+
- [3,2]: The node with the maximum genetic difference is 1, with a difference of 2 XOR 1 = 3.
24+
25+
- [2,5]: The node with the maximum genetic difference is 2, with a difference of 5 XOR 2 = 7.
26+
27+
**Example 2:**
28+
29+
![](https://assets.leetcode.com/uploads/2021/06/29/c2.png)
30+
31+
**Input:** parents = [3,7,-1,2,0,7,0,2], queries = [[4,6],[1,15],[0,5]]
32+
33+
**Output:** [6,14,7]
34+
35+
**Explanation:** The queries are processed as follows:
36+
37+
- [4,6]: The node with the maximum genetic difference is 0, with a difference of 6 XOR 0 = 6.
38+
39+
- [1,15]: The node with the maximum genetic difference is 1, with a difference of 15 XOR 1 = 14.
40+
41+
- [0,5]: The node with the maximum genetic difference is 2, with a difference of 5 XOR 2 = 7.
42+
43+
**Constraints:**
44+
45+
* <code>2 <= parents.length <= 10<sup>5</sup></code>
46+
* `0 <= parents[i] <= parents.length - 1` for every node `i` that is **not** the root.
47+
* `parents[root] == -1`
48+
* <code>1 <= queries.length <= 3 * 10<sup>4</sup></code>
49+
* <code>0 <= node<sub>i</sub> <= parents.length - 1</code>
50+
* <code>0 <= val<sub>i</sub> <= 2 * 10<sup>5</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g1901_2000.s1941_check_if_all_characters_have_equal_number_of_occurrences;
2+
3+
// #Easy #String #Hash_Table #Counting #2022_05_16_Time_11_ms_(23.73%)_Space_42.6_MB_(36.57%)
4+
5+
import java.util.Arrays;
6+
import java.util.stream.Collectors;
7+
8+
public class Solution {
9+
public boolean areOccurrencesEqual(String s) {
10+
int[] counts = new int[26];
11+
char[] charArray = s.toCharArray();
12+
for (char c : charArray) {
13+
counts[c - 'a']++;
14+
}
15+
return Arrays.stream(counts).filter(i -> i != 0).boxed().collect(Collectors.toSet()).size()
16+
== 1;
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
1941\. Check if All Characters Have Equal Number of Occurrences
2+
3+
Easy
4+
5+
Given a string `s`, return `true` _if_ `s` _is a **good** string, or_ `false` _otherwise_.
6+
7+
A string `s` is **good** if **all** the characters that appear in `s` have the **same** number of occurrences (i.e., the same frequency).
8+
9+
**Example 1:**
10+
11+
**Input:** s = "abacbc"
12+
13+
**Output:** true
14+
15+
**Explanation:** The characters that appear in s are 'a', 'b', and 'c'. All characters occur 2 times in s.
16+
17+
**Example 2:**
18+
19+
**Input:** s = "aaabb"
20+
21+
**Output:** false
22+
23+
**Explanation:** The characters that appear in s are 'a' and 'b'. 'a' occurs 3 times while 'b' occurs 2 times, which is not the same number of times.
24+
25+
**Constraints:**
26+
27+
* `1 <= s.length <= 1000`
28+
* `s` consists of lowercase English letters.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g1901_2000.s1942_the_number_of_the_smallest_unoccupied_chair;
2+
3+
// #Medium #Array #Heap_Priority_Queue #Ordered_Set
4+
// #2022_05_16_Time_73_ms_(49.69%)_Space_73.1_MB_(38.04%)
5+
6+
import java.util.Arrays;
7+
import java.util.PriorityQueue;
8+
9+
public class Solution {
10+
public int smallestChair(int[][] times, int targetFriend) {
11+
PriorityQueue<Integer> minheap = new PriorityQueue<>();
12+
minheap.offer(0);
13+
Person[] all = new Person[times.length * 2];
14+
for (int i = 0; i < times.length; i++) {
15+
all[2 * i] = new Person(i, times[i][0], false, true);
16+
all[2 * i + 1] = new Person(i, times[i][1], true, false);
17+
}
18+
Arrays.sort(
19+
all,
20+
(a, b) -> {
21+
int i = a.leave ? -1 : 1;
22+
int j = b.leave ? -1 : 1;
23+
return a.time == b.time ? i - j : a.time - b.time;
24+
});
25+
26+
int[] seat = new int[times.length];
27+
for (int i = 0; true; i++) {
28+
if (all[i].arrive) {
29+
if (targetFriend == all[i].idx) {
30+
return minheap.peek();
31+
}
32+
seat[all[i].idx] = minheap.poll();
33+
if (minheap.isEmpty()) {
34+
minheap.offer(seat[all[i].idx] + 1);
35+
}
36+
} else {
37+
minheap.offer(seat[all[i].idx]);
38+
}
39+
}
40+
}
41+
42+
private static class Person {
43+
boolean leave;
44+
boolean arrive;
45+
int time;
46+
int idx;
47+
48+
Person(int idx, int time, boolean leave, boolean arrive) {
49+
this.time = time;
50+
this.leave = leave;
51+
this.arrive = arrive;
52+
this.idx = idx;
53+
}
54+
}
55+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
1942\. The Number of the Smallest Unoccupied Chair
2+
3+
Medium
4+
5+
There is a party where `n` friends numbered from `0` to `n - 1` are attending. There is an **infinite** number of chairs in this party that are numbered from `0` to `infinity`. When a friend arrives at the party, they sit on the unoccupied chair with the **smallest number**.
6+
7+
* For example, if chairs `0`, `1`, and `5` are occupied when a friend comes, they will sit on chair number `2`.
8+
9+
When a friend leaves the party, their chair becomes unoccupied at the moment they leave. If another friend arrives at that same moment, they can sit in that chair.
10+
11+
You are given a **0-indexed** 2D integer array `times` where <code>times[i] = [arrival<sub>i</sub>, leaving<sub>i</sub>]</code>, indicating the arrival and leaving times of the <code>i<sup>th</sup></code> friend respectively, and an integer `targetFriend`. All arrival times are **distinct**.
12+
13+
Return _the **chair number** that the friend numbered_ `targetFriend` _will sit on_.
14+
15+
**Example 1:**
16+
17+
**Input:** times = [[1,4],[2,3],[4,6]], targetFriend = 1
18+
19+
**Output:** 1
20+
21+
**Explanation:**
22+
23+
- Friend 0 arrives at time 1 and sits on chair 0.
24+
25+
- Friend 1 arrives at time 2 and sits on chair 1.
26+
27+
- Friend 1 leaves at time 3 and chair 1 becomes empty.
28+
29+
- Friend 0 leaves at time 4 and chair 0 becomes empty.
30+
31+
- Friend 2 arrives at time 4 and sits on chair 0.
32+
33+
Since friend 1 sat on chair 1, we return 1.
34+
35+
**Example 2:**
36+
37+
**Input:** times = [[3,10],[1,5],[2,6]], targetFriend = 0
38+
39+
**Output:** 2
40+
41+
**Explanation:**
42+
43+
- Friend 1 arrives at time 1 and sits on chair 0.
44+
45+
- Friend 2 arrives at time 2 and sits on chair 1.
46+
47+
- Friend 0 arrives at time 3 and sits on chair 2.
48+
49+
- Friend 1 leaves at time 5 and chair 0 becomes empty.
50+
51+
- Friend 2 leaves at time 6 and chair 1 becomes empty.
52+
53+
- Friend 0 leaves at time 10 and chair 2 becomes empty.
54+
55+
Since friend 0 sat on chair 2, we return 2.
56+
57+
**Constraints:**
58+
59+
* `n == times.length`
60+
* <code>2 <= n <= 10<sup>4</sup></code>
61+
* `times[i].length == 2`
62+
* <code>1 <= arrival<sub>i</sub> < leaving<sub>i</sub> <= 10<sup>5</sup></code>
63+
* `0 <= targetFriend <= n - 1`
64+
* Each <code>arrival<sub>i</sub></code> time is **distinct**.

0 commit comments

Comments
 (0)