3479. Fruits Into Baskets III #2018
-
Topics: You are given two arrays of integers, From left to right, place the fruits according to these rules:
Return the number of fruit types that remain unplaced after all possible allocations are made. Example 1:
Since one fruit type remains unplaced, we return 1. Example 2:
Since all fruits are successfully placed, we return 0. Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to efficiently allocate fruits to baskets following specific rules: each fruit must be placed in the leftmost available basket with a capacity greater than or equal to the fruit's quantity. The solution involves sorting the baskets by their capacity and original indices, then using a segment tree to efficiently query and update the smallest original index of available baskets that meet the capacity requirement for each fruit. Approach
Let's implement this solution in PHP: 3479. Fruits Into Baskets III <?php
/**
* @param Integer[] $fruits
* @param Integer[] $baskets
* @return Integer
*/
function numOfUnplacedFruits($fruits, $baskets) {
$n = count($fruits);
if ($n == 0) return 0;
$baskets_arr = [];
for ($i = 0; $i < $n; $i++) {
$baskets_arr[] = [$baskets[$i], $i];
}
usort($baskets_arr, function($a, $b) {
if ($a[0] != $b[0]) {
return $a[0] - $b[0];
}
return $a[1] - $b[1];
});
$arr = array_fill(0, $n, 0);
$pos_in_sorted = array_fill(0, $n, 0);
for ($j = 0; $j < $n; $j++) {
$original_index = $baskets_arr[$j][1];
$arr[$j] = $original_index;
$pos_in_sorted[$original_index] = $j;
}
$N = $n;
$tree = array_fill(0, 2 * $N, PHP_INT_MAX);
for ($i = 0; $i < $n; $i++) {
$tree[$N + $i] = $arr[$i];
}
for ($i = $N - 1; $i >= 1; $i--) {
$tree[$i] = min($tree[2 * $i], $tree[2 * $i + 1]);
}
$query_tree = function($l, $r) use (&$tree, $N) {
$res = PHP_INT_MAX;
$l += $N;
$r += $N;
while ($l <= $r) {
if ($l % 2 == 1) {
$res = min($res, $tree[$l]);
$l++;
}
if ($r % 2 == 0) {
$res = min($res, $tree[$r]);
$r--;
}
$l = $l >> 1;
$r = $r >> 1;
}
return $res;
};
$update_tree = function($pos, $value) use (&$tree, $N) {
$p = $N + $pos;
$tree[$p] = $value;
while ($p > 1) {
$p = $p >> 1;
$tree[$p] = min($tree[2 * $p], $tree[2 * $p + 1]);
}
};
$unplaced = 0;
$INF = PHP_INT_MAX;
foreach ($fruits as $fruit) {
$low = 0;
$high = $n - 1;
$j0 = $n;
while ($low <= $high) {
$mid = (int)(($low + $high) / 2);
if ($baskets_arr[$mid][0] >= $fruit) {
$j0 = $mid;
$high = $mid - 1;
} else {
$low = $mid + 1;
}
}
if ($j0 == $n) {
$unplaced++;
continue;
}
$min_original_index = $query_tree($j0, $n - 1);
if ($min_original_index == $INF) {
$unplaced++;
continue;
}
$j_pos = $pos_in_sorted[$min_original_index];
$update_tree($j_pos, $INF);
}
return $unplaced;
}
// Test cases
$fruits = array(4, 2, 5);
$baskets = array(3, 5, 4);
echo numOfUnplacedFruits($fruits, $baskets) . "\n"; // Output: 1
$fruits2 = array(3, 6, 1);
$baskets2 = array(6, 4, 7);
echo numOfUnplacedFruits($fruits2, $baskets2) . "\n"; // Output: 0
?> Explanation:
This approach efficiently handles the constraints by leveraging sorting, binary search, and segment tree operations to ensure optimal performance. |
Beta Was this translation helpful? Give feedback.
We need to efficiently allocate fruits to baskets following specific rules: each fruit must be placed in the leftmost available basket with a capacity greater than or equal to the fruit's quantity. The solution involves sorting the baskets by their capacity and original indices, then using a segment tree to efficiently query and update the smallest original index of available baskets that meet the capacity requirement for each fruit.
Approach
Problem Analysis: The problem requires placing each fruit in the leftmost basket that can accommodate it (i.e., basket capacity >= fruit quantity). Each basket can hold only one type of fruit. The challenge is to efficiently determine, for each fru…