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,69 @@
package g1701_1800.s1775_equal_sum_arrays_with_minimum_number_of_operations;

// #Medium #Array #Hash_Table #Greedy #Counting
// #2022_04_30_Time_16_ms_(70.88%)_Space_106.1_MB_(19.23%)

import java.util.Arrays;

public class Solution {
public int minOperations(int[] nums1, int[] nums2) {
int[] longer = nums1.length > nums2.length ? nums1 : nums2;
int[] shorter = nums1.length > nums2.length ? nums2 : nums1;
if (longer.length > shorter.length * 6) {
return -1;
}
Arrays.sort(longer);
Arrays.sort(shorter);
int i = 0;
int j = 0;
int diff = 0;
while (i < longer.length || j < shorter.length) {
if (i < longer.length) {
diff += longer[i++];
}
if (j < shorter.length) {
diff -= shorter[j++];
}
}
int minOps = 0;
i = 0;
j = shorter.length - 1;
if (diff < 0) {
while (diff < 0) {
if (i < longer.length && j >= 0) {
if (6 - longer[i] < shorter[j] - 1) {
diff += shorter[j--] - 1;
} else {
diff += 6 - longer[i++];
}
} else if (i < longer.length) {
diff += 6 - longer[i++];
} else {
diff += shorter[j--] - 1;
}
minOps++;
}
return minOps;
} else if (diff > 0) {
i = longer.length - 1;
j = 0;
while (diff > 0) {
if (i >= 0 && j < shorter.length) {
if (longer[i] - 1 > 6 - shorter[j]) {
diff -= longer[i--] - 1;
} else {
diff -= 6 - shorter[j++];
}
} else if (i >= 0) {
diff -= longer[i--] - 1;
} else {
diff -= 6 - shorter[j++];
}
minOps++;
}
return minOps;
} else {
return minOps;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
1775\. Equal Sum Arrays With Minimum Number of Operations

Medium

You are given two arrays of integers `nums1` and `nums2`, possibly of different lengths. The values in the arrays are between `1` and `6`, inclusive.

In one operation, you can change any integer's value in **any** of the arrays to **any** value between `1` and `6`, inclusive.

Return _the minimum number of operations required to make the sum of values in_ `nums1` _equal to the sum of values in_ `nums2`_._ Return `-1` if it is not possible to make the sum of the two arrays equal.

**Example 1:**

**Input:** nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]

**Output:** 3

**Explanation:** You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed.

- Change nums2[0] to 6. nums1 = [1,2,3,4,5,6], nums2 = [**6**,1,2,2,2,2].

- Change nums1[5] to 1. nums1 = [1,2,3,4,5,**1**], nums2 = [6,1,2,2,2,2].

- Change nums1[2] to 2. nums1 = [1,2,**2**,4,5,1], nums2 = [6,1,2,2,2,2].

**Example 2:**

**Input:** nums1 = [1,1,1,1,1,1,1], nums2 = [6]

**Output:** -1

**Explanation:** There is no way to decrease the sum of nums1 or to increase the sum of nums2 to make them equal.

**Example 3:**

**Input:** nums1 = [6,6], nums2 = [1]

**Output:** 3

**Explanation:** You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed.

- Change nums1[0] to 2. nums1 = [**2**,6], nums2 = [1].

- Change nums1[1] to 2. nums1 = [2,**2**], nums2 = [1].

- Change nums2[0] to 4. nums1 = [2,2], nums2 = [**4**].

**Constraints:**

* <code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code>
* `1 <= nums1[i], nums2[i] <= 6`
44 changes: 44 additions & 0 deletions src/main/java/g1701_1800/s1776_car_fleet_ii/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package g1701_1800.s1776_car_fleet_ii;

// #Hard #Array #Math #Stack #Heap_Priority_Queue #Monotonic_Stack
// #2022_04_30_Time_19_ms_(93.81%)_Space_100.6_MB_(87.17%)

import java.util.Deque;
import java.util.LinkedList;

public class Solution {
public double[] getCollisionTimes(int[][] cars) {
Deque<Integer> stack = new LinkedList<>();

int n = cars.length;
double[] ans = new double[n];

for (int i = n - 1; i >= 0; i--) {
ans[i] = -1.0;
int[] presentCar = cars[i];
int presentCarSpeed = presentCar[1];

while (!stack.isEmpty()) {
int previousCar = stack.peekLast();
int previousCarSpeed = cars[previousCar][1];
if (presentCarSpeed > previousCarSpeed
&& (ans[previousCar] == -1.0
|| catchTime(cars, i, previousCar) <= ans[previousCar])) {
ans[i] = catchTime(cars, i, previousCar);
break;
}
stack.pollLast();
}

stack.offerLast(i);
}

return ans;
}

private double catchTime(int[][] cars, int presentCar, int previousCar) {
int dist = cars[previousCar][0] - cars[presentCar][0];
int speed = cars[presentCar][1] - cars[previousCar][1];
return 1.0 * dist / speed;
}
}
32 changes: 32 additions & 0 deletions src/main/java/g1701_1800/s1776_car_fleet_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
1776\. Car Fleet II

Hard

There are `n` cars traveling at different speeds in the same direction along a one-lane road. You are given an array `cars` of length `n`, where <code>cars[i] = [position<sub>i</sub>, speed<sub>i</sub>]</code> represents:

* <code>position<sub>i</sub></code> is the distance between the <code>i<sup>th</sup></code> car and the beginning of the road in meters. It is guaranteed that <code>position<sub>i</sub> < position<sub>i+1</sub></code>.
* <code>speed<sub>i</sub></code> is the initial speed of the <code>i<sup>th</sup></code> car in meters per second.

For simplicity, cars can be considered as points moving along the number line. Two cars collide when they occupy the same position. Once a car collides with another car, they unite and form a single car fleet. The cars in the formed fleet will have the same position and the same speed, which is the initial speed of the **slowest** car in the fleet.

Return an array `answer`, where `answer[i]` is the time, in seconds, at which the <code>i<sup>th</sup></code> car collides with the next car, or `-1` if the car does not collide with the next car. Answers within <code>10<sup>-5</sup></code> of the actual answers are accepted.

**Example 1:**

**Input:** cars = [[1,2],[2,1],[4,3],[7,2]]

**Output:** [1.00000,-1.00000,3.00000,-1.00000]

**Explanation:** After exactly one second, the first car will collide with the second car, and form a car fleet with speed 1 m/s. After exactly 3 seconds, the third car will collide with the fourth car, and form a car fleet with speed 2 m/s.

**Example 2:**

**Input:** cars = [[3,4],[5,4],[6,3],[9,1]]

**Output:** [2.00000,1.00000,1.50000,-1.00000]

**Constraints:**

* <code>1 <= cars.length <= 10<sup>5</sup></code>
* <code>1 <= position<sub>i</sub>, speed<sub>i</sub> <= 10<sup>6</sup></code>
* <code>position<sub>i</sub> < position<sub>i+1</sub></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g1701_1800.s1779_find_nearest_point_that_has_the_same_x_or_y_coordinate;

// #Easy #Array #Programming_Skills_I_Day_3_Conditional_Statements
// #2022_04_30_Time_1_ms_(100.00%)_Space_50.1_MB_(80.01%)

public class Solution {
public int nearestValidPoint(int x, int y, int[][] points) {
int nearestManDistance = Integer.MAX_VALUE;
int result = -1;
for (int i = 0; i < points.length; i++) {
int[] point = points[i];
if (point[0] == x || point[1] == y) {
int distance = Math.abs(point[0] - x) + Math.abs(point[1] - y);
if (distance < nearestManDistance) {
result = i;
nearestManDistance = distance;
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
1779\. Find Nearest Point That Has the Same X or Y Coordinate

Easy

You are given two integers, `x` and `y`, which represent your current location on a Cartesian grid: `(x, y)`. You are also given an array `points` where each <code>points[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents that a point exists at <code>(a<sub>i</sub>, b<sub>i</sub>)</code>. A point is **valid** if it shares the same x-coordinate or the same y-coordinate as your location.

Return _the index **(0-indexed)** of the **valid** point with the smallest **Manhattan distance** from your current location_. If there are multiple, return _the valid point with the **smallest** index_. If there are no valid points, return `-1`.

The **Manhattan distance** between two points <code>(x<sub>1</sub>, y<sub>1</sub>)</code> and <code>(x<sub>2</sub>, y<sub>2</sub>)</code> is <code>abs(x<sub>1</sub> - x<sub>2</sub>) + abs(y<sub>1</sub> - y<sub>2</sub>)</code>.

**Example 1:**

**Input:** x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]

**Output:** 2

**Explanation:** Of all the points, only [3,1], [2,4] and [4,4] are valid. Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1. [2,4] has the smallest index, so return 2.

**Example 2:**

**Input:** x = 3, y = 4, points = [[3,4]]

**Output:** 0

**Explanation:** The answer is allowed to be on the same location as your current location.

**Example 3:**

**Input:** x = 3, y = 4, points = [[2,3]]

**Output:** -1

**Explanation:** There are no valid points.

**Constraints:**

* <code>1 <= points.length <= 10<sup>4</sup></code>
* `points[i].length == 2`
* <code>1 <= x, y, a<sub>i</sub>, b<sub>i</sub> <= 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g1701_1800.s1780_check_if_number_is_a_sum_of_powers_of_three;

// #Medium #Math #2022_04_30_Time_2_ms_(19.71%)_Space_41.1_MB_(41.61%)

import java.util.ArrayList;
import java.util.List;

public class Solution {
public boolean checkPowersOfThree(int n) {
List<Integer> powers = new ArrayList<>();
int power = 1;
for (int i = 1; power <= n; i++) {
powers.add(power);
power = (int) Math.pow(3, i);
}
int i = powers.size() - 1;
while (n > 0 && i >= 0) {
if (n - powers.get(i) > 0) {
n -= powers.get(i--);
} else if (n - powers.get(i) == 0) {
return true;
} else {
i--;
}
}
return n == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
1780\. Check if Number is a Sum of Powers of Three

Medium

Given an integer `n`, return `true` _if it is possible to represent_ `n` _as the sum of distinct powers of three._ Otherwise, return `false`.

An integer `y` is a power of three if there exists an integer `x` such that <code>y == 3<sup>x</sup></code>.

**Example 1:**

**Input:** n = 12

**Output:** true

**Explanation:** 12 = 3<sup>1</sup> + 3<sup>2</sup>

**Example 2:**

**Input:** n = 91

**Output:** true

**Explanation:** 91 = 3<sup>0</sup> + 3<sup>2</sup> + 3<sup>4</sup>

**Example 3:**

**Input:** n = 21

**Output:** false

**Constraints:**

* <code>1 <= n <= 10<sup>7</sup></code>
63 changes: 63 additions & 0 deletions src/main/java/g1701_1800/s1782_count_pairs_of_nodes/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package g1701_1800.s1782_count_pairs_of_nodes;

// #Hard #Binary_Search #Two_Pointers #Graph
// #2022_04_30_Time_128_ms_(86.96%)_Space_175.4_MB_(39.13%)

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

public class Solution {
public int[] countPairs(int n, int[][] edges, int[] queries) {
Map<Integer, Integer> edgeCount = new HashMap<>();
int[] degree = new int[n];

for (int[] e : edges) {
int u = e[0] - 1;
int v = e[1] - 1;
degree[u]++;
degree[v]++;

int eId = Math.min(u, v) * n + Math.max(u, v);
edgeCount.put(eId, edgeCount.getOrDefault(eId, 0) + 1);
}

Map<Integer, Integer> degreeCount = new HashMap<>();
int maxDegree = 0;
for (int d : degree) {
degreeCount.put(d, degreeCount.getOrDefault(d, 0) + 1);
maxDegree = Math.max(maxDegree, d);
}

int[] count = new int[2 * maxDegree + 1];

for (Map.Entry<Integer, Integer> d1 : degreeCount.entrySet()) {
for (Map.Entry<Integer, Integer> d2 : degreeCount.entrySet()) {
count[d1.getKey() + d2.getKey()] +=
(d1 == d2)
? d1.getValue() * (d1.getValue() - 1)
: d1.getValue() * d2.getValue();
}
}
for (int i = 0; i < count.length; i++) {
count[i] /= 2;
}

for (Map.Entry<Integer, Integer> e : edgeCount.entrySet()) {
int u = e.getKey() / n;
int v = e.getKey() % n;

count[degree[u] + degree[v]]--;
count[degree[u] + degree[v] - e.getValue()]++;
}

for (int i = count.length - 2; i >= 0; i--) {
count[i] += count[i + 1];
}

int[] res = new int[queries.length];
for (int q = 0; q < queries.length; q++) {
res[q] = ((queries[q] + 1) >= count.length) ? 0 : count[queries[q] + 1];
}
return res;
}
}
Loading