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,41 @@
package g1401_1500.s1401_circle_and_rectangle_overlapping;

// #Medium #Math #Geometry #2022_03_25_Time_2_ms_(6.67%)_Space_41.2_MB_(25.00%)

public class Solution {
public boolean checkOverlap(
int radius, int xCenter, int yCenter, int x1, int y1, int x2, int y2) {
if (x1 <= xCenter && x2 >= xCenter && y1 <= yCenter && y2 >= yCenter) {
return true;
}
int circleDistance = radius * radius;
for (int x = x1; x <= x2; x++) {
if (dist(x, y1, xCenter, yCenter) <= circleDistance) {
return true;
}
}

for (int x = x1; x <= x2; x++) {
if (dist(x, y2, xCenter, yCenter) <= circleDistance) {
return true;
}
}

for (int y = y1; y <= y2; y++) {
if (dist(x1, y, xCenter, yCenter) <= circleDistance) {
return true;
}
}

for (int y = y1; y <= y2; y++) {
if (dist(x2, y, xCenter, yCenter) <= circleDistance) {
return true;
}
}
return false;
}

private int dist(int x1, int y1, int x2, int y2) {
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
1401\. Circle and Rectangle Overlapping

Medium

You are given a circle represented as `(radius, xCenter, yCenter)` and an axis-aligned rectangle represented as `(x1, y1, x2, y2)`, where `(x1, y1)` are the coordinates of the bottom-left corner, and `(x2, y2)` are the coordinates of the top-right corner of the rectangle.

Return `true` _if the circle and rectangle are overlapped otherwise return_ `false`. In other words, check if there is **any** point <code>(x<sub>i</sub>, y<sub>i</sub>)</code> that belongs to the circle and the rectangle at the same time.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/02/20/sample_4_1728.png)

**Input:** radius = 1, xCenter = 0, yCenter = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1

**Output:** true

**Explanation:** Circle and rectangle share the point (1,0).

**Example 2:**

**Input:** radius = 1, xCenter = 1, yCenter = 1, x1 = 1, y1 = -3, x2 = 2, y2 = -1

**Output:** false

**Example 3:**

![](https://assets.leetcode.com/uploads/2020/02/20/sample_2_1728.png)

**Input:** radius = 1, xCenter = 0, yCenter = 0, x1 = -1, y1 = 0, x2 = 0, y2 = 1

**Output:** true

**Constraints:**

* `1 <= radius <= 2000`
* <code>-10<sup>4</sup> <= xCenter, yCenter <= 10<sup>4</sup></code>
* <code>-10<sup>4</sup> <= x1 < x2 <= 10<sup>4</sup></code>
* <code>-10<sup>4</sup> <= y1 < y2 <= 10<sup>4</sup></code>
26 changes: 26 additions & 0 deletions src/main/java/g1401_1500/s1402_reducing_dishes/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g1401_1500.s1402_reducing_dishes;

// #Hard #Array #Dynamic_Programming #Sorting #Greedy
// #2022_03_25_Time_3_ms_(66.20%)_Space_41.3_MB_(80.80%)

import java.util.Arrays;

public class Solution {
public int maxSatisfaction(int[] satisfaction) {
Arrays.sort(satisfaction);

int sum = 0;
int mulSum = 0;
for (int i = 0; i < satisfaction.length; i++) {
sum += satisfaction[i];
mulSum += (i + 1) * satisfaction[i];
}
int maxVal = Math.max(0, mulSum);
for (int j : satisfaction) {
mulSum -= sum;
sum -= j;
maxVal = Math.max(maxVal, mulSum);
}
return maxVal;
}
}
41 changes: 41 additions & 0 deletions src/main/java/g1401_1500/s1402_reducing_dishes/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
1402\. Reducing Dishes

Hard

A chef has collected data on the `satisfaction` level of his `n` dishes. Chef can cook any dish in 1 unit of time.

**Like-time coefficient** of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. `time[i] * satisfaction[i]`.

Return _the maximum sum of **like-time coefficient** that the chef can obtain after dishes preparation_.

Dishes can be prepared in **any** order and the chef can discard some dishes to get this maximum value.

**Example 1:**

**Input:** satisfaction = [-1,-8,0,5,-9]

**Output:** 14

**Explanation:** After Removing the second and last dish, the maximum total **like-time coefficient** will be equal to (-1\*1 + 0\*2 + 5\*3 = 14). Each dish is prepared in one unit of time.

**Example 2:**

**Input:** satisfaction = [4,3,2]

**Output:** 20

**Explanation:** Dishes can be prepared in any order, (2\*1 + 3\*2 + 4\*3 = 20)

**Example 3:**

**Input:** satisfaction = [-1,-4,-5]

**Output:** 0

**Explanation:** People do not like the dishes. No dish is prepared.

**Constraints:**

* `n == satisfaction.length`
* `1 <= n <= 500`
* `-1000 <= satisfaction[i] <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package g1401_1500.s1403_minimum_subsequence_in_non_increasing_order;

// #Easy #Array #Sorting #Greedy #2022_03_25_Time_13_ms_(6.30%)_Space_42.2_MB_(90.93%)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Solution {
public List<Integer> minSubsequence(int[] nums) {
List<Integer> list =
Arrays.stream(nums)
.boxed()
.sorted(Collections.reverseOrder())
.collect(Collectors.toCollection(() -> new ArrayList<>(nums.length)));
int sum = list.stream().mapToInt(num -> num).sum();
int minSum = 0;
List<Integer> result = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (minSum > (sum - minSum)) {
return result;
}
minSum += list.get(i);
result.add(list.get(i));
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
1403\. Minimum Subsequence in Non-Increasing Order

Easy

Given the array `nums`, obtain a subsequence of the array whose sum of elements is **strictly greater** than the sum of the non included elements in such subsequence.

If there are multiple solutions, return the subsequence with **minimum size** and if there still exist multiple solutions, return the subsequence with the **maximum total sum** of all its elements. A subsequence of an array can be obtained by erasing some (possibly zero) elements from the array.

Note that the solution with the given constraints is guaranteed to be **unique**. Also return the answer sorted in **non-increasing** order.

**Example 1:**

**Input:** nums = [4,3,10,9,8]

**Output:** [10,9]

**Explanation:** The subsequences [10,9] and [10,8] are minimal such that the sum of their elements is strictly greater than the sum of elements not included, however, the subsequence [10,9] has the maximum total sum of its elements.

**Example 2:**

**Input:** nums = [4,4,7,6,7]

**Output:** [7,7,6]

**Explanation:** The subsequence [7,7] has the sum of its elements equal to 14 which is not strictly greater than the sum of elements not included (14 = 4 + 4 + 6). Therefore, the subsequence [7,6,7] is the minimal satisfying the conditions. Note the subsequence has to returned in non-decreasing order.

**Example 3:**

**Input:** nums = [6]

**Output:** [6]

**Constraints:**

* `1 <= nums.length <= 500`
* `1 <= nums[i] <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g1401_1500.s1404_number_of_steps_to_reduce_a_number_in_binary_representation_to_one;

// #Medium #String #Bit_Manipulation #2022_03_25_Time_1_ms_(62.90%)_Space_42.3_MB_(26.70%)

public class Solution {
public int numSteps(String s) {
int steps = 0;
int carry = 0;
for (int i = s.length() - 1; i >= 1; i--) {
if (carry == 0) {
if (s.charAt(i) == '1') {
steps += 2;
carry = 1;
} else {
steps++;
}
} else {
if (s.charAt(i) == '0') {
steps += 2;
}
else {
steps++;
}
}
}
return steps + carry;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
1404\. Number of Steps to Reduce a Number in Binary Representation to One

Medium

Given the binary representation of an integer as a string `s`, return _the number of steps to reduce it to_ `1` _under the following rules_:

* If the current number is even, you have to divide it by `2`.

* If the current number is odd, you have to add `1` to it.


It is guaranteed that you can always reach one for all test cases.

**Example 1:**

**Input:** s = "1101"

**Output:** 6

**Explanation:** "1101" corressponds to number 13 in their decimal representation.

Step 1) 13 is odd, add 1 and obtain 14.

Step 2) 14 is even, divide by 2 and obtain 7.

Step 3) 7 is odd, add 1 and obtain 8.

Step 4) 8 is even, divide by 2 and obtain 4.

Step 5) 4 is even, divide by 2 and obtain 2.

Step 6) 2 is even, divide by 2 and obtain 1.

**Example 2:**

**Input:** s = "10"

**Output:** 1

**Explanation:** "10" corressponds to number 2 in their decimal representation.

Step 1) 2 is even, divide by 2 and obtain 1.

**Example 3:**

**Input:** s = "1"

**Output:** 0

**Constraints:**

* `1 <= s.length <= 500`
* `s` consists of characters '0' or '1'
* `s[0] == '1'`
44 changes: 44 additions & 0 deletions src/main/java/g1401_1500/s1405_longest_happy_string/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package g1401_1500.s1405_longest_happy_string;

// #Medium #String #Greedy #Heap_Priority_Queue
// #2022_03_25_Time_0_ms_(100.00%)_Space_40.9_MB_(69.62%)

public class Solution {
public String longestDiverseString(int a, int b, int c) {
StringBuilder sb = new StringBuilder();
int[] remains = new int[] {a, b, c};
char[] chars = new char[] {'a', 'b', 'c'};
int preIndex = -1;

do {
int index;
boolean largest;
if (preIndex != -1
&& remains[preIndex]
== Math.max(remains[0], Math.max(remains[1], remains[2]))) {
if (preIndex == 0) {
index = remains[1] > remains[2] ? 1 : 2;
} else if (preIndex == 1) {
index = remains[0] > remains[2] ? 0 : 2;
} else {
index = remains[0] > remains[1] ? 0 : 1;
}
largest = false;
} else {
index = remains[0] > remains[1] ? 0 : 1;
index = remains[index] > remains[2] ? index : 2;
largest = true;
}
remains[index]--;
sb.append(chars[index]);

if (remains[index] > 0 && largest) {
remains[index]--;
sb.append(chars[index]);
}
preIndex = index;

} while (remains[0] + remains[1] + remains[2] != remains[preIndex]);
return sb.toString();
}
}
36 changes: 36 additions & 0 deletions src/main/java/g1401_1500/s1405_longest_happy_string/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
1405\. Longest Happy String

Medium

A string `s` is called **happy** if it satisfies the following conditions:

* `s` only contains the letters `'a'`, `'b'`, and `'c'`.
* `s` does not contain any of `"aaa"`, `"bbb"`, or `"ccc"` as a substring.
* `s` contains **at most** `a` occurrences of the letter `'a'`.
* `s` contains **at most** `b` occurrences of the letter `'b'`.
* `s` contains **at most** `c` occurrences of the letter `'c'`.

Given three integers `a`, `b`, and `c`, return _the **longest possible happy** string_. If there are multiple longest happy strings, return _any of them_. If there is no such string, return _the empty string_ `""`.

A **substring** is a contiguous sequence of characters within a string.

**Example 1:**

**Input:** a = 1, b = 1, c = 7

**Output:** "ccaccbcc"

**Explanation:** "ccbccacc" would also be a correct answer.

**Example 2:**

**Input:** a = 7, b = 1, c = 0

**Output:** "aabaa"

**Explanation:** It is the only correct answer in this case.

**Constraints:**

* `0 <= a, b, c <= 100`
* `a + b + c > 0`
Loading