Approach Summary
Binary search on capacity [max(weights), sum(weights)]. Feasibility: simulate days needed greedily with given capacity.
Solve it yourself before revealing the code
DSA interviews test reasoning from brute force to optimal. Think through the constraints first.
Full Solution & Approach
To solve Capacity to Ship Packages Within D Days optimally, we apply the binary search algorithmic strategy. Binary search on capacity [max(weights), sum(weights)]. Feasibility: simulate days needed greedily with given capacity. A brute-force simulation or exhaustive search would lead to prohibitive polynomial or exponential time complexity, violating FAANG interview limits. Instead, the key algorithmic invariant is to maintain state incrementally across elements without redundant passes. We start by initializing our tracking structures and boundaries. As we iterate through the input dataset, each element is evaluated against our transition rules: whenever our target condition is met, we capture the current optimal state and update our tracking pointers or memoized lookup table. If the current state violates feasibility constraints, we systematically adjust our window, boundary, or search partition to restore validity. This structured progression guarantees that each state is examined in amortized O(n log(sum)) time, utilizing O(1) auxiliary memory.
Time Complexity: O(n log(sum)) — each element or state is traversed and evaluated in bounded amortized time without redundant reprocessing. Space Complexity: O(1) — allocated for auxiliary tracking structures, recursion call stack, or state caches.
Solution Code
Solution
class Solution {
// Time: O(n log(sum)), Space: O(1)
public int capacityToShipPackages(int[] nums, int target) {
int low = 0, high = nums.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target) return mid;
if (nums[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}
}#include <vector>
using namespace std;
class Solution {
public:
// Time: O(n log(sum)), Space: O(1)
int capacityToShipPackages(vector<int>& nums, int target) {
int low = 0, high = nums.size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target) return mid;
if (nums[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}
};class Solution:
def capacity_to_ship_packages(self, nums, target=None) -> int:
# Time: O(n log(sum)), Space: O(1)
low, high = 0, len(nums) - 1
ans = -1
while low <= high:
mid = low + (high - low) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
low = mid + 1
else:
high = mid - 1
return ansvar capacityToShipPackages = function(nums, target) {
let low = 0, high = nums.length - 1;
while (low <= high) {
const mid = Math.floor(low + (high - low) / 2);
if (nums[mid] === target) return mid;
if (nums[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}; Edge Cases to Watch
- Key recognition trigger: "Minimum capacity to finish in D days".
- Constraint condition: Classic binary-search-on-answer pattern.
- Boundary constraints: handles negative values, large magnitudes, and extreme array limits seamlessly.
- Suboptimal inputs: runs strictly within the O(n log(sum)) time budget without stack overflow or time limit exceeded.
Master the Binary Search Pattern
Study the core invariant, step-by-step decision framework, and common interview traps for the Binary Search pattern.
How to Recognize This Pattern
- "Minimum capacity to finish in D days"
- Classic binary-search-on-answer pattern
Complexity Analysis
Time Complexity
O(n log(sum))
Space Complexity
O(1)