aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-08-05 10:06:30 +0200
committerJack Sangdahl <[email protected]>2025-08-05 10:06:30 +0200
commit9cee92d538c64f43a3dfc1b25675e006e2ee7b91 (patch)
tree7207b3a48a0da7684cb4db698566ca18a1c20535
parentce6f29aefa492ee055732324adce4686b1b6d361 (diff)
downloadleetcode-9cee92d538c64f43a3dfc1b25675e006e2ee7b91.tar.gz
leetcode-9cee92d538c64f43a3dfc1b25675e006e2ee7b91.zip
3477. fruits into baskets 2
-rw-r--r--fruit_into_baskets2.c41
-rw-r--r--fruit_into_baskets2.py39
2 files changed, 80 insertions, 0 deletions
diff --git a/fruit_into_baskets2.c b/fruit_into_baskets2.c
new file mode 100644
index 0000000..cc3e073
--- /dev/null
+++ b/fruit_into_baskets2.c
@@ -0,0 +1,41 @@
+// 3477. Fruits Into Baskets II
+#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 place in
+ * the leftmost available basket with a capacity greater than or equal to the
+ * quantity of that fruit type. return the number of fruit types that remain
+ * unplaced after all possible allocations are made.
+ */
+
+int numOfUnplacedFruits(int *fruits, int fruitsSize, int *baskets,
+ int basketsSize) {
+ bool *used = (bool *)calloc(fruitsSize, sizeof(bool));
+ int unplaced = 0;
+ for (int i = 0; i < fruitsSize; i++) {
+ bool placed = false;
+ for (int j = 0; j < fruitsSize; j++) {
+ if (!used[j] && baskets[j] >= fruits[i]) {
+ used[j] = true;
+ placed = true;
+ break;
+ }
+ }
+ if (!placed)
+ unplaced++;
+ }
+ free(used);
+ return unplaced;
+}
+
+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_baskets2.py b/fruit_into_baskets2.py
new file mode 100644
index 0000000..b16aa48
--- /dev/null
+++ b/fruit_into_baskets2.py
@@ -0,0 +1,39 @@
+# 3477. Fruits Into Baskets II
+
+"""
+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 place in
+the leftmost available basket with a capacity greater than or 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 = len(fruits)
+ used = [False] * n
+ unplaced = 0
+ for i in fruits:
+ placed = False
+ for j in range(n):
+ if not used[j] and baskets[j] >= i:
+ used[j] = True
+ placed = True
+ break
+ if not placed:
+ unplaced += 1
+ return unplaced
+
+
+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]))