diff --git a/solution/1500-1599/1564.Put Boxes Into the Warehouse I/README.md b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/README.md index dea324eb55d10..8be2a4e28c8d8 100644 --- a/solution/1500-1599/1564.Put Boxes Into the Warehouse I/README.md +++ b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/README.md @@ -69,6 +69,16 @@ +**方法一:预处理 + 排序 + 双指针** + +我们可以先对仓库的房间进行预处理,得到一个数组 $left$,其中 $left[i]$ 表示下标 $i$ 可以放入的最大箱子高度。 + +然后对箱子的高度进行排序,从小到大依次放入仓库中。我们使用两个指针 $i$ 和 $j$ 分别指向箱子的第一个位置以及仓库的最后一个位置,如果 $left[j] \lt boxes[i]$,说明当前仓库无法放入箱子 $i$,我们将指针 $j$ 循环向左移动,直到 $left[j] \ge boxes[i]$ 或者 $j \lt 0$。如果此时 $j \lt 0$,说明仓库已经没有空间可以放入箱子 $i$,我们可以直接退出循环。否则说明仓库可以放入箱子 $i$,我们将指针 $i$ 循环向右移动,指针 $j$ 循环向左移动。继续进行上述操作,直到指针 $i$ 超出箱子的范围。 + +最后我们返回指针 $i$ 的值即可。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为仓库的房间数。 + ### **Python3** @@ -76,7 +86,21 @@ ```python - +class Solution: + def maxBoxesInWarehouse(self, boxes: List[int], warehouse: List[int]) -> int: + n = len(warehouse) + left = [warehouse[0]] * n + for i in range(1, n): + left[i] = min(left[i - 1], warehouse[i]) + boxes.sort() + i, j = 0, n - 1 + while i < len(boxes): + while j >= 0 and left[j] < boxes[i]: + j -= 1 + if j < 0: + break + i, j = i + 1, j - 1 + return i ``` ### **Java** @@ -84,7 +108,117 @@ ```java +class Solution { + public int maxBoxesInWarehouse(int[] boxes, int[] warehouse) { + int n = warehouse.length; + int[] left = new int[n]; + left[0] = warehouse[0]; + for (int i = 1; i < n; ++i) { + left[i] = Math.min(left[i - 1], warehouse[i]); + } + Arrays.sort(boxes); + int i = 0, j = n - 1; + while (i < boxes.length) { + while (j >= 0 && left[j] < boxes[i]) { + --j; + } + if (j < 0) { + break; + } + ++i; + --j; + } + return i; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int maxBoxesInWarehouse(vector& boxes, vector& warehouse) { + int n = warehouse.size(); + int left[n]; + left[0] = warehouse[0]; + for (int i = 1; i < n; ++i) { + left[i] = min(left[i - 1], warehouse[i]); + } + sort(boxes.begin(), boxes.end()); + int i = 0, j = n - 1; + while (i < boxes.size()) { + while (j >= 0 && left[j] < boxes[i]) { + --j; + } + if (j < 0) { + break; + } + ++i; + --j; + } + return i; + } +}; +``` + +### **Go** + +```go +func maxBoxesInWarehouse(boxes []int, warehouse []int) int { + n := len(warehouse) + left := make([]int, n) + left[0] = warehouse[0] + for i := 1; i < n; i++ { + left[i] = min(left[i-1], warehouse[i]) + } + sort.Ints(boxes) + i, j := 0, n-1 + for i < len(boxes) { + for j >= 0 && left[j] < boxes[i] { + j-- + } + if j < 0 { + break + } + i, j = i+1, j-1 + } + return i +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} +``` +### **TypeScript** + +```ts +function maxBoxesInWarehouse(boxes: number[], warehouse: number[]): number { + const n = warehouse.length; + const left: number[] = new Array(n); + left[0] = warehouse[0]; + for (let i = 1; i < n; ++i) { + left[i] = Math.min(left[i - 1], warehouse[i]); + } + boxes.sort((a, b) => a - b); + let i = 0; + let j = n - 1; + while (i < boxes.length) { + while (j >= 0 && left[j] < boxes[i]) { + --j; + } + if (j < 0) { + break; + } + ++i; + --j; + } + return i; +} ``` ### **...** diff --git a/solution/1500-1599/1564.Put Boxes Into the Warehouse I/README_EN.md b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/README_EN.md index a8ab06e142159..6c9fc79cc3cc9 100644 --- a/solution/1500-1599/1564.Put Boxes Into the Warehouse I/README_EN.md +++ b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/README_EN.md @@ -66,13 +66,137 @@ Swapping the orange and green boxes is also valid, or swapping one of them with ### **Python3** ```python - +class Solution: + def maxBoxesInWarehouse(self, boxes: List[int], warehouse: List[int]) -> int: + n = len(warehouse) + left = [warehouse[0]] * n + for i in range(1, n): + left[i] = min(left[i - 1], warehouse[i]) + boxes.sort() + i, j = 0, n - 1 + while i < len(boxes): + while j >= 0 and left[j] < boxes[i]: + j -= 1 + if j < 0: + break + i, j = i + 1, j - 1 + return i ``` ### **Java** ```java +class Solution { + public int maxBoxesInWarehouse(int[] boxes, int[] warehouse) { + int n = warehouse.length; + int[] left = new int[n]; + left[0] = warehouse[0]; + for (int i = 1; i < n; ++i) { + left[i] = Math.min(left[i - 1], warehouse[i]); + } + Arrays.sort(boxes); + int i = 0, j = n - 1; + while (i < boxes.length) { + while (j >= 0 && left[j] < boxes[i]) { + --j; + } + if (j < 0) { + break; + } + ++i; + --j; + } + return i; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int maxBoxesInWarehouse(vector& boxes, vector& warehouse) { + int n = warehouse.size(); + int left[n]; + left[0] = warehouse[0]; + for (int i = 1; i < n; ++i) { + left[i] = min(left[i - 1], warehouse[i]); + } + sort(boxes.begin(), boxes.end()); + int i = 0, j = n - 1; + while (i < boxes.size()) { + while (j >= 0 && left[j] < boxes[i]) { + --j; + } + if (j < 0) { + break; + } + ++i; + --j; + } + return i; + } +}; +``` + +### **Go** + +```go +func maxBoxesInWarehouse(boxes []int, warehouse []int) int { + n := len(warehouse) + left := make([]int, n) + left[0] = warehouse[0] + for i := 1; i < n; i++ { + left[i] = min(left[i-1], warehouse[i]) + } + sort.Ints(boxes) + i, j := 0, n-1 + for i < len(boxes) { + for j >= 0 && left[j] < boxes[i] { + j-- + } + if j < 0 { + break + } + i, j = i+1, j-1 + } + return i +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} +``` +### **TypeScript** + +```ts +function maxBoxesInWarehouse(boxes: number[], warehouse: number[]): number { + const n = warehouse.length; + const left: number[] = new Array(n); + left[0] = warehouse[0]; + for (let i = 1; i < n; ++i) { + left[i] = Math.min(left[i - 1], warehouse[i]); + } + boxes.sort((a, b) => a - b); + let i = 0; + let j = n - 1; + while (i < boxes.length) { + while (j >= 0 && left[j] < boxes[i]) { + --j; + } + if (j < 0) { + break; + } + ++i; + --j; + } + return i; +} ``` ### **...** diff --git a/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.cpp b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.cpp new file mode 100644 index 0000000000000..6fea641a4b04e --- /dev/null +++ b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int maxBoxesInWarehouse(vector& boxes, vector& warehouse) { + int n = warehouse.size(); + int left[n]; + left[0] = warehouse[0]; + for (int i = 1; i < n; ++i) { + left[i] = min(left[i - 1], warehouse[i]); + } + sort(boxes.begin(), boxes.end()); + int i = 0, j = n - 1; + while (i < boxes.size()) { + while (j >= 0 && left[j] < boxes[i]) { + --j; + } + if (j < 0) { + break; + } + ++i; + --j; + } + return i; + } +}; \ No newline at end of file diff --git a/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.go b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.go new file mode 100644 index 0000000000000..acbb4f9cdbe98 --- /dev/null +++ b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.go @@ -0,0 +1,27 @@ +func maxBoxesInWarehouse(boxes []int, warehouse []int) int { + n := len(warehouse) + left := make([]int, n) + left[0] = warehouse[0] + for i := 1; i < n; i++ { + left[i] = min(left[i-1], warehouse[i]) + } + sort.Ints(boxes) + i, j := 0, n-1 + for i < len(boxes) { + for j >= 0 && left[j] < boxes[i] { + j-- + } + if j < 0 { + break + } + i, j = i+1, j-1 + } + return i +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} \ No newline at end of file diff --git a/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.java b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.java new file mode 100644 index 0000000000000..8aff34fb1ec5c --- /dev/null +++ b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.java @@ -0,0 +1,23 @@ +class Solution { + public int maxBoxesInWarehouse(int[] boxes, int[] warehouse) { + int n = warehouse.length; + int[] left = new int[n]; + left[0] = warehouse[0]; + for (int i = 1; i < n; ++i) { + left[i] = Math.min(left[i - 1], warehouse[i]); + } + Arrays.sort(boxes); + int i = 0, j = n - 1; + while (i < boxes.length) { + while (j >= 0 && left[j] < boxes[i]) { + --j; + } + if (j < 0) { + break; + } + ++i; + --j; + } + return i; + } +} \ No newline at end of file diff --git a/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.py b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.py new file mode 100644 index 0000000000000..4ba914ce8debe --- /dev/null +++ b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.py @@ -0,0 +1,15 @@ +class Solution: + def maxBoxesInWarehouse(self, boxes: List[int], warehouse: List[int]) -> int: + n = len(warehouse) + left = [warehouse[0]] * n + for i in range(1, n): + left[i] = min(left[i - 1], warehouse[i]) + boxes.sort() + i, j = 0, n - 1 + while i < len(boxes): + while j >= 0 and left[j] < boxes[i]: + j -= 1 + if j < 0: + break + i, j = i + 1, j - 1 + return i diff --git a/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.ts b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.ts new file mode 100644 index 0000000000000..6ddefe28e084a --- /dev/null +++ b/solution/1500-1599/1564.Put Boxes Into the Warehouse I/Solution.ts @@ -0,0 +1,22 @@ +function maxBoxesInWarehouse(boxes: number[], warehouse: number[]): number { + const n = warehouse.length; + const left: number[] = new Array(n); + left[0] = warehouse[0]; + for (let i = 1; i < n; ++i) { + left[i] = Math.min(left[i - 1], warehouse[i]); + } + boxes.sort((a, b) => a - b); + let i = 0; + let j = n - 1; + while (i < boxes.length) { + while (j >= 0 && left[j] < boxes[i]) { + --j; + } + if (j < 0) { + break; + } + ++i; + --j; + } + return i; +}