Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package g1701_1800.s1706_where_will_the_ball_fall;

// #Medium #Array #Dynamic_Programming #Depth_First_Search #Matrix #Simulation
// #2022_04_24_Time_2_ms_(64.55%)_Space_54.5_MB_(25.38%)

public class Solution {
public int[] findBall(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
int[] res = new int[n];
for (int j = 0; j < n; j++) {
int currentJ = j;
int currentI = 0;
while (currentJ < n && currentI < m) {
if (grid[currentI][currentJ] == 1) {
currentJ++;
if (currentJ < n && grid[currentI][currentJ] == 1) {
currentI++;
} else {
break;
}
} else {
currentJ--;
if (currentJ >= 0 && grid[currentI][currentJ] == -1) {
currentI++;
} else {
break;
}
}
}

if (currentI == m) {
res[j] = currentJ;
} else {
res[j] = -1;
}
}
return res;
}
}
55 changes: 55 additions & 0 deletions src/main/java/g1701_1800/s1706_where_will_the_ball_fall/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
1706\. Where Will the Ball Fall

Medium

You have a 2-D `grid` of size `m x n` representing a box, and you have `n` balls. The box is open on the top and bottom sides.

Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.

* A board that redirects the ball to the right spans the top-left corner to the bottom-right corner and is represented in the grid as `1`.
* A board that redirects the ball to the left spans the top-right corner to the bottom-left corner and is represented in the grid as `-1`.

We drop one ball at the top of each column of the box. Each ball can get stuck in the box or fall out of the bottom. A ball gets stuck if it hits a "V" shaped pattern between two boards or if a board redirects the ball into either wall of the box.

Return _an array_ `answer` _of size_ `n` _where_ `answer[i]` _is the column that the ball falls out of at the bottom after dropping the ball from the_ <code>i<sup>th</sup></code> _column at the top, or `-1` _if the ball gets stuck in the box_._

**Example 1:**

**![](https://assets.leetcode.com/uploads/2019/09/26/ball.jpg)**

**Input:** grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]

**Output:** [1,-1,-1,-1,-1]

**Explanation:** This example is shown in the photo.

Ball b0 is dropped at column 0 and falls out of the box at column 1.

Ball b1 is dropped at column 1 and will get stuck in the box between column 2 and 3 and row 1.

Ball b2 is dropped at column 2 and will get stuck on the box between column 2 and 3 and row 0.

Ball b3 is dropped at column 3 and will get stuck on the box between column 2 and 3 and row 0.

Ball b4 is dropped at column 4 and will get stuck on the box between column 2 and 3 and row 1.

**Example 2:**

**Input:** grid = [[-1]]

**Output:** [-1]

**Explanation:** The ball gets stuck against the left wall.

**Example 3:**

**Input:** grid = [[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]]

**Output:** [0,1,2,3,4,-1]

**Constraints:**

* `m == grid.length`
* `n == grid[i].length`
* `1 <= m, n <= 100`
* `grid[i][j]` is `1` or `-1`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package g1701_1800.s1707_maximum_xor_with_an_element_from_array;

// #Hard #Array #Bit_Manipulation #Trie #2022_04_24_Time_150_ms_(97.24%)_Space_91.7_MB_(99.31%)

import java.util.Arrays;
import java.util.Comparator;

public class Solution {
static class QueryComparator implements Comparator<int[]> {
public int compare(int[] a, int[] b) {
return Integer.compare(a[1], b[1]);
}
}

static class Node {
Node zero;
Node one;

public Node() {
this.zero = null;
this.one = null;
}
}

public int[] maximizeXor(int[] nums, int[][] queries) {
Arrays.sort(nums);

int len = queries.length;
int[][] queryWithIndex = new int[len][3];
for (int i = 0; i < len; i++) {
queryWithIndex[i][0] = queries[i][0];
queryWithIndex[i][1] = queries[i][1];
queryWithIndex[i][2] = i;
}
Arrays.sort(queryWithIndex, new QueryComparator());

int numId = 0;
int[] ans = new int[len];

Node root = new Node();
for (int i = 0; i < len; i++) {
while (numId < nums.length && nums[numId] <= queryWithIndex[i][1]) {
addNumToTree(nums[numId], root);
numId++;
}

ans[queryWithIndex[i][2]] = maxXOR(queryWithIndex[i][0], root);
}

return ans;
}

private void addNumToTree(int num, Node node) {
for (int i = 31; i >= 0; i--) {
int digit = (num >> i) & 1;
if (digit == 1) {
if (node.one == null) {
node.one = new Node();
}
node = node.one;
} else {
if (node.zero == null) {
node.zero = new Node();
}
node = node.zero;
}
}
}

private int maxXOR(int num, Node node) {
if (node.one == null && node.zero == null) {
return -1;
}

int ans = 0;
for (int i = 31; i >= 0 && node != null; i--) {
int digit = (num >> i) & 1;
if (digit == 1) {
if (node.zero != null) {
ans += (1 << i);
node = node.zero;
} else {
node = node.one;
}
} else {
if (node.one != null) {
ans += (1 << i);
node = node.one;
} else {
node = node.zero;
}
}
}

return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
1707\. Maximum XOR With an Element From Array

Hard

You are given an array `nums` consisting of non-negative integers. You are also given a `queries` array, where <code>queries[i] = [x<sub>i</sub>, m<sub>i</sub>]</code>.

The answer to the <code>i<sup>th</sup></code> query is the maximum bitwise `XOR` value of <code>x<sub>i</sub></code> and any element of `nums` that does not exceed <code>m<sub>i</sub></code>. In other words, the answer is <code>max(nums[j] XOR x<sub>i</sub>)</code> for all `j` such that <code>nums[j] <= m<sub>i</sub></code>. If all elements in `nums` are larger than <code>m<sub>i</sub></code>, then the answer is `-1`.

Return _an integer array_ `answer` _where_ `answer.length == queries.length` _and_ `answer[i]` _is the answer to the_ <code>i<sup>th</sup></code> _query._

**Example 1:**

**Input:** nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]

**Output:** [3,3,7]

**Explanation:**

1) 0 and 1 are the only two integers not greater than 1. 0 XOR 3 = 3 and 1 XOR 3 = 2. The larger of the two is 3.

2) 1 XOR 2 = 3.

3) 5 XOR 2 = 7.

**Example 2:**

**Input:** nums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]]

**Output:** [15,-1,5]

**Constraints:**

* <code>1 <= nums.length, queries.length <= 10<sup>5</sup></code>
* `queries[i].length == 2`
* <code>0 <= nums[j], x<sub>i</sub>, m<sub>i</sub> <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package g1701_1800.s1710_maximum_units_on_a_truck;

// #Easy #Array #Sorting #Greedy #2022_04_24_Time_9_ms_(78.69%)_Space_42.8_MB_(78.53%)

import java.util.Arrays;

public class Solution {
public int maximumUnits(int[][] boxTypes, int truckSize) {
Arrays.sort(boxTypes, (b1, b2) -> Integer.compare(b2[1], b1[1]));
int maxUnits = 0;
int i = 0;
while (truckSize > 0 && i < boxTypes.length) {
if (boxTypes[i][0] <= truckSize) {
maxUnits += boxTypes[i][0] * boxTypes[i][1];
truckSize -= boxTypes[i][0];
} else {
maxUnits += Math.min(truckSize, boxTypes[i][0]) * boxTypes[i][1];
truckSize -= Math.min(truckSize, boxTypes[i][0]);
}
i++;
}
return maxUnits;
}
}
38 changes: 38 additions & 0 deletions src/main/java/g1701_1800/s1710_maximum_units_on_a_truck/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
1710\. Maximum Units on a Truck

Easy

You are assigned to put some amount of boxes onto **one truck**. You are given a 2D array `boxTypes`, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:

* <code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type `i`.
* <code>numberOfUnitsPerBox<sub>i</sub></code> is the number of units in each box of the type `i`.

You are also given an integer `truckSize`, which is the **maximum** number of **boxes** that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed `truckSize`.

Return _the **maximum** total number of **units** that can be put on the truck._

**Example 1:**

**Input:** boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4

**Output:** 8

**Explanation:** There are:

- 1 box of the first type that contains 3 units.

- 2 boxes of the second type that contain 2 units each.

- 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 \* 3) + (2 \* 2) + (1 \* 1) = 8.

**Example 2:**

**Input:** boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10

**Output:** 91

**Constraints:**

* `1 <= boxTypes.length <= 1000`
* <code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code>
* <code>1 <= truckSize <= 10<sup>6</sup></code>
43 changes: 43 additions & 0 deletions src/main/java/g1701_1800/s1711_count_good_meals/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package g1701_1800.s1711_count_good_meals;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Solution {

public int countPairs(int[] d) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int k : d) {
map.put(k, map.getOrDefault(k, 0) + 1);
}

long result = 0;
for (Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<Integer, Integer> elem = it.next();
int key = elem.getKey();
long value = elem.getValue();

for (int j = 21; j >= 0; j--) {
int find = (1 << j) - key;
if (find < 0) {
break;
}

if (map.containsKey(find)) {
if (find == key) {
result += (((value - 1) * value) / 2);

} else {
result += (value * map.get(find));
}
}
}

it.remove();
}

int mod = 1_000_000_007;
return (int) (result % mod);
}
}
32 changes: 32 additions & 0 deletions src/main/java/g1701_1800/s1711_count_good_meals/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
1711\. Count Good Meals

Medium

A **good meal** is a meal that contains **exactly two different food items** with a sum of deliciousness equal to a power of two.

You can pick **any** two different foods to make a good meal.

Given an array of integers `deliciousness` where `deliciousness[i]` is the deliciousness of the <code>i<sup>th</sup></code> item of food, return _the number of different **good meals** you can make from this list modulo_ <code>10<sup>9</sup> + 7</code>.

Note that items with different indices are considered different even if they have the same deliciousness value.

**Example 1:**

**Input:** deliciousness = [1,3,5,7,9]

**Output:** 4

**Explanation:** The good meals are (1,3), (1,7), (3,5) and, (7,9). Their respective sums are 4, 8, 8, and 16, all of which are powers of 2.

**Example 2:**

**Input:** deliciousness = [1,1,1,3,3,3,7]

**Output:** 15

**Explanation:** The good meals are (1,1) with 3 ways, (1,3) with 9 ways, and (1,7) with 3 ways.

**Constraints:**

* <code>1 <= deliciousness.length <= 10<sup>5</sup></code>
* <code>0 <= deliciousness[i] <= 2<sup>20</sup></code>
Loading