Skip to content

Commit

Permalink
update 1011 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 24, 2024
1 parent 042c79e commit fe7b358
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

// https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/description/

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

/**
* 1011. Capacity To Ship Packages Within D Days
Expand Down Expand Up @@ -54,8 +56,75 @@
public class CapacityToShipPackagesWithinDDays {

// V0
// IDEA : BINARY SEARCH (modified by GPT)
// IDEA : BINARY SEARCH
public int shipWithinDays(int[] weights, int days) {

if (weights.length==1){
return weights[0] / days;
}

List<Integer> weightsList = new ArrayList<>();
/**
* NOTE !!!
*
* we get mex weight, and total weight from weights array
*/
int maxWeight = 0;
int totalWeight = 0;
for (int w : weights){
weightsList.add(w);
maxWeight = Math.max(maxWeight, w);
totalWeight += w;
}

/**
* NOTE !!!
*
* we use mex weight, and total weight as
* left, and right pointer of binary search
*
* (not mex, smallest weight or anything else)
* (since we want to get the least weight capacity within D days)
*/
int left = maxWeight;
int right = totalWeight;
// binary search
while(right > left){
int mid = (left + right) / 2;
int calculatedDays = getDays(weightsList, mid);
System.out.println(">>> mid = " + mid + ", maxWeight = " + maxWeight + ", totalWeight = " + totalWeight);
// need to return max possible speed within D days
if (calculatedDays <= days){
right = mid;
}else{
left = mid+1;
}
}

return left; // ??? or return mid
}

private int getDays(List<Integer> weightsList, int speed){
int cur = 0;
int days = 0;
for (Integer w :weightsList){
if (cur + w <= speed){
cur += w;
}else{
days += 1;
cur = w;
}
}
if (cur > 0){
days += 1;
}
System.out.println(">>> speed = " + speed + ", days = " + days);
return days;
}

// V0
// IDEA : BINARY SEARCH (modified by GPT)
public int shipWithinDays_0_1(int[] weights, int days) {
int maxWeight = 0;
int totalWeight = 0;

Expand Down
23 changes: 13 additions & 10 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,31 @@ public int shipWithinDays(int[] weights, int days) {
return weights[0] / days;
}

int res = 0;
List<Integer> weightsList = new ArrayList<>();
int fastest = 0;
int slowest = 1;
int maxWeight = 0;
int totalWeight = 0;
for (int w : weights){
weightsList.add(w);
fastest = Math.max(fastest, w);
maxWeight = Math.max(maxWeight, w);
totalWeight += w;
}

int left = maxWeight;
int right = totalWeight;
// binary search
while(fastest > slowest){
int mid = (fastest + slowest) / 2;
while(right > left){
int mid = (left + right) / 2;
int calculatedDays = getDays(weightsList, mid);
System.out.println(">>> mid = " + mid + ", fastest = " + fastest + ", slowest = " + slowest);
System.out.println(">>> mid = " + mid + ", maxWeight = " + maxWeight + ", totalWeight = " + totalWeight);
// need to return max possible speed within D days
if (calculatedDays <= days){
fastest = mid;
right = mid;
}else{
slowest = mid+1;
left = mid+1;
}
}

return slowest; // ??? or return mid
return left; // ??? or return mid
}

private int getDays(List<Integer> weightsList, int speed){
Expand Down

0 comments on commit fe7b358

Please sign in to comment.