Skip to content

Commit 01a12f0

Browse files
authored
Added tasks 1801, 1802, 1803, 1805, 1806.
1 parent f34e944 commit 01a12f0

File tree

15 files changed

+569
-0
lines changed

15 files changed

+569
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package g1801_1900.s1801_number_of_orders_in_the_backlog;
2+
3+
// #Medium #Array #Heap_Priority_Queue #Simulation
4+
// #2022_05_02_Time_39_ms_(78.46%)_Space_95.1_MB_(47.69%)
5+
6+
import java.util.Comparator;
7+
import java.util.PriorityQueue;
8+
9+
public class Solution {
10+
private static class Order {
11+
int price;
12+
int qty;
13+
14+
Order(int price, int qty) {
15+
this.price = price;
16+
this.qty = qty;
17+
}
18+
}
19+
20+
public int getNumberOfBacklogOrders(int[][] orders) {
21+
PriorityQueue<Order> sell = new PriorityQueue<>(Comparator.comparingInt(a -> a.price));
22+
PriorityQueue<Order> buy = new PriorityQueue<>((a, b) -> b.price - a.price);
23+
for (int[] order : orders) {
24+
int price = order[0];
25+
int amount = order[1];
26+
int type = order[2];
27+
if (type == 0) {
28+
while (!sell.isEmpty() && sell.peek().price <= price && amount > 0) {
29+
Order ord = sell.peek();
30+
int toRemove = Math.min(amount, ord.qty);
31+
ord.qty -= toRemove;
32+
amount -= toRemove;
33+
if (ord.qty == 0) {
34+
sell.poll();
35+
}
36+
}
37+
if (amount > 0) {
38+
buy.add(new Order(price, amount));
39+
}
40+
} else {
41+
while (!buy.isEmpty() && buy.peek().price >= price && amount > 0) {
42+
Order ord = buy.peek();
43+
int toRemove = Math.min(amount, ord.qty);
44+
ord.qty -= toRemove;
45+
amount -= toRemove;
46+
if (ord.qty == 0) {
47+
buy.poll();
48+
}
49+
}
50+
if (amount > 0) {
51+
sell.add(new Order(price, amount));
52+
}
53+
}
54+
}
55+
long sellCount = 0;
56+
for (Order ord : sell) {
57+
sellCount += ord.qty;
58+
}
59+
long buyCount = 0;
60+
for (Order ord : buy) {
61+
buyCount += ord.qty;
62+
}
63+
long total = sellCount + buyCount;
64+
return (int) (total % 1000000007L);
65+
}
66+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
1801\. Number of Orders in the Backlog
2+
3+
Medium
4+
5+
You are given a 2D integer array `orders`, where each <code>orders[i] = [price<sub>i</sub>, amount<sub>i</sub>, orderType<sub>i</sub>]</code> denotes that <code>amount<sub>i</sub></code> orders have been placed of type <code>orderType<sub>i</sub></code> at the price <code>price<sub>i</sub></code>. The <code>orderType<sub>i</sub></code> is:
6+
7+
* `0` if it is a batch of `buy` orders, or
8+
* `1` if it is a batch of `sell` orders.
9+
10+
Note that `orders[i]` represents a batch of <code>amount<sub>i</sub></code> independent orders with the same price and order type. All orders represented by `orders[i]` will be placed before all orders represented by `orders[i+1]` for all valid `i`.
11+
12+
There is a **backlog** that consists of orders that have not been executed. The backlog is initially empty. When an order is placed, the following happens:
13+
14+
* If the order is a `buy` order, you look at the `sell` order with the **smallest** price in the backlog. If that `sell` order's price is **smaller than or equal to** the current `buy` order's price, they will match and be executed, and that `sell` order will be removed from the backlog. Else, the `buy` order is added to the backlog.
15+
* Vice versa, if the order is a `sell` order, you look at the `buy` order with the **largest** price in the backlog. If that `buy` order's price is **larger than or equal to** the current `sell` order's price, they will match and be executed, and that `buy` order will be removed from the backlog. Else, the `sell` order is added to the backlog.
16+
17+
Return _the total **amount** of orders in the backlog after placing all the orders from the input_. Since this number can be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
18+
19+
**Example 1:**
20+
21+
![](https://assets.leetcode.com/uploads/2021/03/11/ex1.png)
22+
23+
**Input:** orders = [[10,5,0],[15,2,1],[25,1,1],[30,4,0]]
24+
25+
**Output:** 6
26+
27+
**Explanation:** Here is what happens with the orders:
28+
29+
- 5 orders of type buy with price 10 are placed. There are no sell orders, so the 5 orders are added to the backlog.
30+
31+
- 2 orders of type sell with price 15 are placed. There are no buy orders with prices larger than or equal to 15, so the 2 orders are added to the backlog.
32+
33+
- 1 order of type sell with price 25 is placed. There are no buy orders with prices larger than or equal to 25 in the backlog, so this order is added to the backlog.
34+
35+
- 4 orders of type buy with price 30 are placed. The first 2 orders are matched with the 2 sell orders of the least price, which is 15 and these 2 sell orders are removed from the backlog. The 3<sup>rd</sup> order is matched with the sell order of the least price, which is 25 and this sell order is removed from the backlog. Then, there are no more sell orders in the backlog, so the 4<sup>th</sup> order is added to the backlog. Finally, the backlog has 5 buy orders with price 10, and 1 buy order with price 30. So the total number of orders in the backlog is 6.
36+
37+
**Example 2:**
38+
39+
![](https://assets.leetcode.com/uploads/2021/03/11/ex2.png)
40+
41+
**Input:** orders = [[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]]
42+
43+
**Output:** 999999984
44+
45+
**Explanation:** Here is what happens with the orders:
46+
47+
- 10<sup>9</sup> orders of type sell with price 7 are placed. There are no buy orders, so the 10<sup>9</sup> orders are added to the backlog.
48+
49+
- 3 orders of type buy with price 15 are placed. They are matched with the 3 sell orders with the least price which is 7, and those 3 sell orders are removed from the backlog. - 999999995 orders of type buy with price 5 are placed. The least price of a sell order is 7, so the 999999995 orders are added to the backlog.
50+
51+
- 1 order of type sell with price 5 is placed. It is matched with the buy order of the highest price, which is 5, and that buy order is removed from the backlog. Finally, the backlog has (1000000000-3) sell orders with price 7, and (999999995-1) buy orders with price 5. So the total number of orders = 1999999991, which is equal to 999999984 % (10<sup>9</sup> + 7).
52+
53+
**Constraints:**
54+
55+
* <code>1 <= orders.length <= 10<sup>5</sup></code>
56+
* `orders[i].length == 3`
57+
* <code>1 <= price<sub>i</sub>, amount<sub>i</sub> <= 10<sup>9</sup></code>
58+
* <code>orderType<sub>i</sub></code> is either `0` or `1`.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g1801_1900.s1802_maximum_value_at_a_given_index_in_a_bounded_array;
2+
3+
// #Medium #Greedy #Binary_Search #Binary_Search_II_Day_17
4+
// #2022_05_02_Time_2_ms_(58.44%)_Space_40.9_MB_(65.43%)
5+
6+
public class Solution {
7+
private boolean isPossible(int n, int index, int maxSum, int value) {
8+
int leftValue = Math.max(value - index, 0);
9+
int rightValue = Math.max(value - ((n - 1) - index), 0);
10+
11+
long sumBefore = (long) (value + leftValue) * (value - leftValue + 1) / 2;
12+
long sumAfter = (long) (value + rightValue) * (value - rightValue + 1) / 2;
13+
14+
return sumBefore + sumAfter - value <= maxSum;
15+
}
16+
17+
public int maxValue(int n, int index, int maxSum) {
18+
int left = 0;
19+
int right = maxSum - n;
20+
21+
while (left < right) {
22+
int middle = (left + right + 1) / 2;
23+
if (isPossible(n, index, maxSum - n, middle)) {
24+
left = middle;
25+
} else {
26+
right = middle - 1;
27+
}
28+
}
29+
return left + 1;
30+
}
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
1802\. Maximum Value at a Given Index in a Bounded Array
2+
3+
Medium
4+
5+
You are given three positive integers: `n`, `index`, and `maxSum`. You want to construct an array `nums` (**0-indexed**) that satisfies the following conditions:
6+
7+
* `nums.length == n`
8+
* `nums[i]` is a **positive** integer where `0 <= i < n`.
9+
* `abs(nums[i] - nums[i+1]) <= 1` where `0 <= i < n-1`.
10+
* The sum of all the elements of `nums` does not exceed `maxSum`.
11+
* `nums[index]` is **maximized**.
12+
13+
Return `nums[index]` _of the constructed array_.
14+
15+
Note that `abs(x)` equals `x` if `x >= 0`, and `-x` otherwise.
16+
17+
**Example 1:**
18+
19+
**Input:** n = 4, index = 2, maxSum = 6
20+
21+
**Output:** 2
22+
23+
**Explanation:** nums = [1,2,**2**,1] is one array that satisfies all the conditions. There are no arrays that satisfy all the conditions and have nums[2] == 3, so 2 is the maximum nums[2].
24+
25+
**Example 2:**
26+
27+
**Input:** n = 6, index = 1, maxSum = 10
28+
29+
**Output:** 3
30+
31+
**Constraints:**
32+
33+
* <code>1 <= n <= maxSum <= 10<sup>9</sup></code>
34+
* `0 <= index < n`
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package g1801_1900.s1803_count_pairs_with_xor_in_a_range;
2+
3+
// #Hard #Array #Bit_Manipulation #Trie #2022_05_02_Time_77_ms_(100.00%)_Space_70.3_MB_(61.54%)
4+
5+
public class Solution {
6+
public int countPairs(int[] nums, int low, int high) {
7+
Trie root = new Trie();
8+
int pairsCount = 0;
9+
for (int num : nums) {
10+
int pairsCountHigh = countPairsWhoseXorLessThanX(num, root, high + 1);
11+
int pairsCountLow = countPairsWhoseXorLessThanX(num, root, low);
12+
pairsCount += (pairsCountHigh - pairsCountLow);
13+
root.insertNumber(num);
14+
}
15+
16+
return pairsCount;
17+
}
18+
19+
private int countPairsWhoseXorLessThanX(int num, Trie root, int x) {
20+
int pairs = 0;
21+
Trie curr = root;
22+
for (int i = 14; i >= 0 && curr != null; i--) {
23+
int numIthBit = (num >> i) & 1;
24+
int xIthBit = (x >> i) & 1;
25+
if (xIthBit == 1) {
26+
if (curr.child[numIthBit] != null) {
27+
pairs += curr.child[numIthBit].count;
28+
}
29+
curr = curr.child[1 - numIthBit];
30+
} else {
31+
curr = curr.child[numIthBit];
32+
}
33+
}
34+
35+
return pairs;
36+
}
37+
38+
static class Trie {
39+
Trie[] child;
40+
int count;
41+
42+
public Trie() {
43+
child = new Trie[2];
44+
count = 0;
45+
}
46+
47+
public void insertNumber(int num) {
48+
Trie curr = this;
49+
50+
for (int i = 14; i >= 0; i--) {
51+
int ithBit = (num >> i) & 1;
52+
53+
if (curr.child[ithBit] == null) {
54+
curr.child[ithBit] = new Trie();
55+
}
56+
57+
curr.child[ithBit].count++;
58+
59+
curr = curr.child[ithBit];
60+
}
61+
}
62+
}
63+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
1803\. Count Pairs With XOR in a Range
2+
3+
Hard
4+
5+
Given a **(0-indexed)** integer array `nums` and two integers `low` and `high`, return _the number of **nice pairs**_.
6+
7+
A **nice pair** is a pair `(i, j)` where `0 <= i < j < nums.length` and `low <= (nums[i] XOR nums[j]) <= high`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,4,2,7], low = 2, high = 6
12+
13+
**Output:** 6
14+
15+
**Explanation:** All nice pairs (i, j) are as follows:
16+
17+
- (0, 1): nums[0] XOR nums[1] = 5
18+
19+
- (0, 2): nums[0] XOR nums[2] = 3
20+
21+
- (0, 3): nums[0] XOR nums[3] = 6
22+
23+
- (1, 2): nums[1] XOR nums[2] = 6
24+
25+
- (1, 3): nums[1] XOR nums[3] = 3
26+
27+
- (2, 3): nums[2] XOR nums[3] = 5
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [9,8,4,2,1], low = 5, high = 14
32+
33+
**Output:** 8
34+
35+
**Explanation:** All nice pairs (i, j) are as follows:
36+
37+
- (0, 2): nums[0] XOR nums[2] = 13
38+
39+
- (0, 3): nums[0] XOR nums[3] = 11
40+
41+
- (0, 4): nums[0] XOR nums[4] = 8
42+
43+
- (1, 2): nums[1] XOR nums[2] = 12
44+
45+
- (1, 3): nums[1] XOR nums[3] = 10
46+
47+
- (1, 4): nums[1] XOR nums[4] = 9
48+
49+
- (2, 3): nums[2] XOR nums[3] = 6
50+
51+
- (2, 4): nums[2] XOR nums[4] = 5
52+
53+
**Constraints:**
54+
55+
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
56+
* <code>1 <= nums[i] <= 2 * 10<sup>4</sup></code>
57+
* <code>1 <= low <= high <= 2 * 10<sup>4</sup></code>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g1801_1900.s1805_number_of_different_integers_in_a_string;
2+
3+
// #Easy #String #Hash_Table #2022_05_02_Time_3_ms_(79.55%)_Space_42.3_MB_(64.37%)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class Solution {
9+
10+
public int numDifferentIntegers(String word) {
11+
Set<String> ints = new HashSet<>();
12+
char[] chars = word.toCharArray();
13+
int start = -1;
14+
int stop = 0;
15+
for (int i = 0; i < chars.length; i++) {
16+
if (chars[i] >= '0' && chars[i] <= '9') {
17+
if (start == -1) {
18+
start = i;
19+
}
20+
stop = i;
21+
} else if (start != -1) {
22+
ints.add(extractInt(chars, start, stop));
23+
start = -1;
24+
}
25+
}
26+
if (start != -1) {
27+
ints.add(extractInt(chars, start, stop));
28+
}
29+
return ints.size();
30+
}
31+
32+
private String extractInt(char[] chrs, int start, int stop) {
33+
StringBuilder stb = new StringBuilder();
34+
while (start <= stop && chrs[start] == '0') {
35+
start++;
36+
}
37+
if (start >= stop) {
38+
stb.append(chrs[stop]);
39+
} else {
40+
while (start <= stop) {
41+
stb.append(chrs[start++]);
42+
}
43+
}
44+
return stb.toString();
45+
}
46+
}

0 commit comments

Comments
 (0)