aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-08-06 11:53:26 +0200
committerJack Sangdahl <[email protected]>2025-08-06 11:53:26 +0200
commitc6c6abdc55f6dc40c411193e97aaea13103727cd (patch)
treed8690f65e12dac27b764c5d555c85936ac48a325
parent9cee92d538c64f43a3dfc1b25675e006e2ee7b91 (diff)
downloadleetcode-c6c6abdc55f6dc40c411193e97aaea13103727cd.tar.gz
leetcode-c6c6abdc55f6dc40c411193e97aaea13103727cd.zip
3479. fruits into baskets 3
-rw-r--r--fruit_into_baskets3.c89
-rw-r--r--fruit_into_baskets3.py50
2 files changed, 139 insertions, 0 deletions
diff --git a/fruit_into_baskets3.c b/fruit_into_baskets3.c
new file mode 100644
index 0000000..8d5a85b
--- /dev/null
+++ b/fruit_into_baskets3.c
@@ -0,0 +1,89 @@
+// 3479. Fruits Into Baskets III
+#include "leetcode.h"
+
+/*
+ * you are given two arrays of integers, 'fruits' and 'baskets', each of length
+ * 'n', where 'fruits[i]' represents the quantity of the i'th type of fruit, and
+ * 'baskets[j]' represents the capacity of the j'th basket. from left to right,
+ * place the fruits according to these rules: each fruit type must be placed in
+ * the leftmost available basket with a capacity greater than equal to the
+ * quantity of that fruit type. return the number of fruit types that remain
+ * unplaced after all possible allocations are made.
+ */
+
+struct node {
+ int val;
+ struct node *parent;
+ struct node *left_child;
+ struct node *right_child;
+};
+
+int tree_size(int n) {
+ return 1 << (32 - __builtin_clz(n) +
+ (__builtin_clz(n) + __builtin_clz(n) < 31));
+}
+
+int tree_init(struct node *tree, struct node *parent, int icurr, int *data,
+ int left, int right) {
+ struct node *curr = &tree[icurr];
+ curr->parent = parent;
+ if (left == right) {
+ curr->left_child = 0;
+ curr->right_child = 0;
+ return curr->val = data[left];
+ } else {
+ int i_left_child = icurr * 2 + 1, i_right_child = i_left_child + 1,
+ mid = (left + right) / 2;
+ curr->left_child = &tree[i_left_child];
+ curr->right_child = &tree[i_right_child];
+ int val_left_child = tree_init(tree, curr, i_left_child, data, left, mid);
+ int val_right_child =
+ tree_init(tree, curr, i_right_child, data, mid + 1, right);
+ return curr->val = fmax(val_left_child, val_right_child);
+ }
+}
+
+void set_node_val(struct node *obj, int val) {
+ obj->val = val;
+ if (obj->parent) {
+ struct node *curr = obj->parent;
+ int new = fmax(curr->left_child->val, curr->right_child->val);
+ if (new != obj->val)
+ set_node_val(curr, new);
+ }
+}
+
+struct node *left_node(struct node *tree, const int min) {
+ struct node *curr = &tree[0];
+ while (curr->left_child) {
+ if (curr->left_child->val >= min)
+ curr = curr->left_child;
+ else
+ curr = curr->right_child;
+ }
+ return curr;
+}
+
+int numOfUnplacedFruits(int *fruits, int fruitsSize, int *baskets,
+ int basketsSize) {
+ int ans = 0, n = tree_size(basketsSize);
+ struct node *tree = (struct node *)malloc(n * sizeof(struct node));
+ tree_init(tree, (struct node *)0, 0, baskets, 0, basketsSize - 1);
+ for (int i = 0; i < fruitsSize; i++) {
+ if (tree[0].val >= fruits[i])
+ set_node_val(left_node(tree, fruits[i]), 0);
+ else
+ ans++;
+ }
+ free(tree);
+ return ans;
+}
+
+int main() {
+ int f1[] = {4, 2, 5}, b1[] = {3, 5, 4};
+ int f2[] = {3, 6, 1}, b2[] = {6, 4, 7};
+ printf("%d\n", numOfUnplacedFruits(f1, ARRAY_SIZE(f1), b1,
+ ARRAY_SIZE(b1))); // expect: 1
+ printf("%d\n", numOfUnplacedFruits(f2, ARRAY_SIZE(f2), b2,
+ ARRAY_SIZE(b2))); // expect: 0
+}
diff --git a/fruit_into_baskets3.py b/fruit_into_baskets3.py
new file mode 100644
index 0000000..3cdf123
--- /dev/null
+++ b/fruit_into_baskets3.py
@@ -0,0 +1,50 @@
+# 3479. Fruits Into Baskets III
+
+"""
+you are given two arrays of integers, 'fruits' and 'baskets', each of length
+'n', where 'fruits[i]' represents the quantity of the i'th type of fruit, and
+'baskets[j]' represents the capacity of the j'th basket. from left to right,
+place the fruits according to these rules: each fruit type must be placed in
+the leftmost available basket with a capacity greater than equal to the
+quantity of that fruit type. return the number of fruit types that remain
+unplaced after all possible allocations are made.
+"""
+
+
+class Solution(object):
+ def numOfUnplacedFruits(self, fruits, baskets):
+ """
+ :type fruits: List[int]
+ :type baskets: List[int]
+ :rtype: int
+ """
+ n, m = len(baskets), 1
+ while m <= n:
+ m <<= 1
+ tree = [0] * (2 * m)
+ for i in range(n):
+ tree[m + i] = baskets[i]
+ for i in range(m - 1, 0, -1):
+ tree[i] = max(tree[2 * i], tree[2 * i + 1])
+ ans = 0
+ for i in fruits:
+ curr = 1
+ if tree[curr] < i:
+ ans += 1
+ continue
+ while curr < m:
+ if tree[2 * curr] >= i:
+ curr *= 2
+ else:
+ curr = 2 * curr + 1
+ tree[curr] = -1
+ while curr > 1:
+ curr //= 2
+ tree[curr] = max(tree[2 * curr], tree[2 * curr + 1])
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.numOfUnplacedFruits(fruits=[4, 2, 5], baskets=[3, 5, 4]))
+ print(obj.numOfUnplacedFruits(fruits=[3, 6, 1], baskets=[6, 4, 7]))