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
55 changes: 55 additions & 0 deletions src/main/java/g1701_1800/s1739_building_boxes/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package g1701_1800.s1739_building_boxes;

// #Hard #Math #Greedy #Binary_Search #2022_04_29_Time_1_ms_(84.38%)_Space_39.2_MB_(84.38%)

public class Solution {
static final double ONE_THIRD = 1.0d / 3.0d;

public int minimumBoxes(int n) {
int k = findLargestTetrahedralNotGreaterThan(n);
int used = tetrahedral(k);
int floor = triangular(k);
int unused = (n - used);
if (unused == 0) {
return floor;
}
int r = findSmallestTriangularNotLessThan(unused);
return (floor + r);
}

private int findLargestTetrahedralNotGreaterThan(int te) {
int a = (int) Math.ceil(Math.pow(product(6, te), ONE_THIRD));
while (tetrahedral(a) > te) {
a--;
}
return a;
}

private int findSmallestTriangularNotLessThan(int t) {
int a = -1 + (int) Math.floor(Math.sqrt(product(t, 2)));
while (triangular(a) < t) {
a++;
}
return a;
}

private int tetrahedral(int a) {
return (int) ratio(product(a, a + 1, a + 2), 6);
}

private int triangular(int a) {
return (int) ratio(product(a, a + 1), 2);
}

private long product(long... vals) {
long product = 1L;
for (long val : vals) {
product *= val;
}
return product;
}

private long ratio(long a, long b) {
return (a / b);
}
}
44 changes: 44 additions & 0 deletions src/main/java/g1701_1800/s1739_building_boxes/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
1739\. Building Boxes

Hard

You have a cubic storeroom where the width, length, and height of the room are all equal to `n` units. You are asked to place `n` boxes in this room where each box is a cube of unit side length. There are however some rules to placing the boxes:

* You can place the boxes anywhere on the floor.
* If box `x` is placed on top of the box `y`, then each side of the four vertical sides of the box `y` **must** either be adjacent to another box or to a wall.

Given an integer `n`, return _the **minimum** possible number of boxes touching the floor._

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/01/04/3-boxes.png)

**Input:** n = 3

**Output:** 3

**Explanation:** The figure above is for the placement of the three boxes. These boxes are placed in the corner of the room, where the corner is on the left side.

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/01/04/4-boxes.png)

**Input:** n = 4

**Output:** 3

**Explanation:** The figure above is for the placement of the four boxes. These boxes are placed in the corner of the room, where the corner is on the left side.

**Example 3:**

![](https://assets.leetcode.com/uploads/2021/01/04/10-boxes.png)

**Input:** n = 10

**Output:** 6

**Explanation:** The figure above is for the placement of the ten boxes. These boxes are placed in the corner of the room, where the corner is on the back side.

**Constraints:**

* <code>1 <= n <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package g1701_1800.s1742_maximum_number_of_balls_in_a_box;

// #Easy #Hash_Table #Math #Counting #2022_04_29_Time_7_ms_(98.87%)_Space_38.9_MB_(97.97%)

public class Solution {
public int countBalls(int lowLimit, int highLimit) {
int maxValue = 0;
int[] countArray = new int[46];
int currentSum = getDigitSum(lowLimit);
countArray[currentSum]++;
maxValue = 1;
for (int i = lowLimit + 1; i <= highLimit; i++) {
if (i % 10 == 0) {
currentSum = getDigitSum(i);
} else {
currentSum++;
}
countArray[currentSum]++;
if (countArray[currentSum] > maxValue) {
maxValue = countArray[currentSum];
}
}
return maxValue;
}

private int getDigitSum(int num) {
int currentSum = 0;
while (num > 0) {
currentSum += num % 10;
num = num / 10;
}
return currentSum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
1742\. Maximum Number of Balls in a Box

Easy

You are working in a ball factory where you have `n` balls numbered from `lowLimit` up to `highLimit` **inclusive** (i.e., `n == highLimit - lowLimit + 1`), and an infinite number of boxes numbered from `1` to `infinity`.

Your job at this factory is to put each ball in the box with a number equal to the sum of digits of the ball's number. For example, the ball number `321` will be put in the box number `3 + 2 + 1 = 6` and the ball number `10` will be put in the box number `1 + 0 = 1`.

Given two integers `lowLimit` and `highLimit`, return _the number of balls in the box with the most balls._

**Example 1:**

**Input:** lowLimit = 1, highLimit = 10

**Output:** 2

**Explanation:**

Box Number: 1 2 3 4 5 6 7 8 9 10 11 ...

Ball Count: 2 1 1 1 1 1 1 1 1 0 0 ...

Box 1 has the most number of balls with 2 balls.

**Example 2:**

**Input:** lowLimit = 5, highLimit = 15

**Output:** 2

**Explanation:**

Box Number: 1 2 3 4 5 6 7 8 9 10 11 ...

Ball Count: 1 1 1 1 2 2 1 1 1 0 0 ...

Boxes 5 and 6 have the most number of balls with 2 balls in each.

**Example 3:**

**Input:** lowLimit = 19, highLimit = 28

**Output:** 2

**Explanation:**

Box Number: 1 2 3 4 5 6 7 8 9 10 11 12 ...

Ball Count: 0 1 1 1 1 1 1 1 1 2 0 0 ...

Box 10 has the most number of balls with 2 balls.

**Constraints:**

* <code>1 <= lowLimit <= highLimit <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package g1701_1800.s1743_restore_the_array_from_adjacent_pairs;

// #Medium #Array #Hash_Table #2022_04_29_Time_95_ms_(99.08%)_Space_101.6_MB_(84.84%)

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
public int[] restoreArray(int[][] adjacentPairs) {
if (adjacentPairs == null || adjacentPairs.length == 0) {
return new int[0];
}
if (adjacentPairs.length == 1) {
return adjacentPairs[0];
}

Map<Integer, List<Integer>> graph = new HashMap<>();

for (int[] pair : adjacentPairs) {
graph.computeIfAbsent(pair[0], k -> new ArrayList<>()).add(pair[1]);
graph.computeIfAbsent(pair[1], k -> new ArrayList<>()).add(pair[0]);
}

int[] res = new int[graph.size()];

for (Map.Entry<Integer, List<Integer>> entry : graph.entrySet()) {
if (entry.getValue().size() == 1) {
res[0] = entry.getKey();
break;
}
}

res[1] = graph.get(res[0]).get(0);
for (int i = 2; i < res.length; i++) {
for (int cur : graph.get(res[i - 1])) {
if (cur != res[i - 2]) {
res[i] = cur;
break;
}
}
}

return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
1743\. Restore the Array From Adjacent Pairs

Medium

There is an integer array `nums` that consists of `n` **unique** elements, but you have forgotten it. However, you do remember every pair of adjacent elements in `nums`.

You are given a 2D integer array `adjacentPairs` of size `n - 1` where each <code>adjacentPairs[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that the elements <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> are adjacent in `nums`.

It is guaranteed that every adjacent pair of elements `nums[i]` and `nums[i+1]` will exist in `adjacentPairs`, either as `[nums[i], nums[i+1]]` or `[nums[i+1], nums[i]]`. The pairs can appear **in any order**.

Return _the original array_ `nums`_. If there are multiple solutions, return **any of them**_.

**Example 1:**

**Input:** adjacentPairs = [[2,1],[3,4],[3,2]]

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

**Explanation:** This array has all its adjacent pairs in adjacentPairs. Notice that adjacentPairs[i] may not be in left-to-right order.

**Example 2:**

**Input:** adjacentPairs = [[4,-2],[1,4],[-3,1]]

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

**Explanation:** There can be negative numbers. Another solution is [-3,1,4,-2], which would also be accepted.

**Example 3:**

**Input:** adjacentPairs = [[100000,-100000]]

**Output:** [100000,-100000]

**Constraints:**

* `nums.length == n`
* `adjacentPairs.length == n - 1`
* `adjacentPairs[i].length == 2`
* <code>2 <= n <= 10<sup>5</sup></code>
* <code>-10<sup>5</sup> <= nums[i], u<sub>i</sub>, v<sub>i</sub> <= 10<sup>5</sup></code>
* There exists some `nums` that has `adjacentPairs` as its pairs.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g1701_1800.s1744_can_you_eat_your_favorite_candy_on_your_favorite_day;

// #Medium #Array #Prefix_Sum #2022_04_29_Time_5_ms_(100.00%)_Space_91.1_MB_(76.00%)

public class Solution {
public boolean[] canEat(int[] candiesCount, int[][] queries) {
boolean[] result = new boolean[queries.length];
long[] candiesComm = new long[candiesCount.length + 1];
for (int i = 1; i <= candiesCount.length; i++) {
candiesComm[i] = candiesComm[i - 1] + candiesCount[i - 1];
}
for (int i = 0; i < queries.length; i++) {
int type = queries[i][0];
long day = queries[i][1];
long cap = queries[i][2];
result[i] = ((day + 1) * cap > candiesComm[type]) && day < candiesComm[type + 1];
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
1744\. Can You Eat Your Favorite Candy on Your Favorite Day?

Medium

You are given a **(0-indexed)** array of positive integers `candiesCount` where `candiesCount[i]` represents the number of candies of the <code>i<sup>th</sup></code> type you have. You are also given a 2D array `queries` where <code>queries[i] = [favoriteType<sub>i</sub>, favoriteDay<sub>i</sub>, dailyCap<sub>i</sub>]</code>.

You play a game with the following rules:

* You start eating candies on day `**0**`.
* You **cannot** eat **any** candy of type `i` unless you have eaten **all** candies of type `i - 1`.
* You must eat **at least** **one** candy per day until you have eaten all the candies.

Construct a boolean array `answer` such that `answer.length == queries.length` and `answer[i]` is `true` if you can eat a candy of type <code>favoriteType<sub>i</sub></code> on day <code>favoriteDay<sub>i</sub></code> without eating **more than** <code>dailyCap<sub>i</sub></code> candies on **any** day, and `false` otherwise. Note that you can eat different types of candy on the same day, provided that you follow rule 2.

Return _the constructed array_ `answer`.

**Example 1:**

**Input:** candiesCount = [7,4,5,3,8], queries = [[0,2,2],[4,2,4],[2,13,1000000000]]

**Output:** [true,false,true]

**Explanation:**

1- If you eat 2 candies (type 0) on day 0 and 2 candies (type 0) on day 1, you will eat a candy of type 0 on day 2.

2- You can eat at most 4 candies each day.

If you eat 4 candies every day, you will eat 4 candies (type 0) on day 0 and 4 candies (type 0 and type 1) on day 1.

On day 2, you can only eat 4 candies (type 1 and type 2), so you cannot eat a candy of type 4 on day 2.

3- If you eat 1 candy each day, you will eat a candy of type 2 on day 13.

**Example 2:**

**Input:** candiesCount = [5,2,6,4,1], queries = [[3,1,2],[4,10,3],[3,10,100],[4,100,30],[1,3,1]]

**Output:** [false,true,true,false,false]

**Constraints:**

* <code>1 <= candiesCount.length <= 10<sup>5</sup></code>
* <code>1 <= candiesCount[i] <= 10<sup>5</sup></code>
* <code>1 <= queries.length <= 10<sup>5</sup></code>
* `queries[i].length == 3`
* <code>0 <= favoriteType<sub>i</sub> < candiesCount.length</code>
* <code>0 <= favoriteDay<sub>i</sub> <= 10<sup>9</sup></code>
* <code>1 <= dailyCap<sub>i</sub> <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g1701_1800.s1745_palindrome_partitioning_iv;

// #Hard #String #Dynamic_Programming #2022_04_29_Time_10_ms_(100.00%)_Space_44_MB_(95.27%)

public class Solution {
public boolean checkPartitioning(String s) {
final int len = s.length();
char[] ch = s.toCharArray();
int[] dp = new int[len + 1];
dp[0] = 0x01;
for (int i = 0; i < len; i++) {
for (int l : new int[] {i - 1, i}) {
int r = i;
while (l >= 0 && r < len && ch[l] == ch[r]) {
dp[r + 1] |= dp[l] << 1;
l--;
r++;
}
}
}
return (dp[len] & 0x08) == 0x08;
}
}
Loading