> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/greedy/1564.-put-boxes-into-the-warehouse-i.md).

# 1564. Put Boxes Into the Warehouse I

![](/files/zOWKiowRGfAXtpUK0UCO)

![](/files/ZtlP0oj83j3HXehg4afH)

![](/files/1wfNXsQbdRFa8eHlUdVF)

![](/files/AGWQXVAHFeZRFCv6CCxj)

## naive

sort boxes, then put them one by one into warehouse,&#x20;

T: (mlogm + n^2), m is number of boxes, n is number of warehouses

```
[1,2,2,3,4], warehouse = [3,4,1,2]

put w[3], make sure box <= w[0~2]
put w[2], make sure box <= w[0~1]
put w[1], make sure box <= w[0]

for (i = w.length -1; ) {
	for (j = i - 1; j >= 0 ; j--) {
	   if (
	}
}
```

## DP

```
[1,3,4,4]  
 i i i 
 
[5,3,3,4,1]
 5 3 3 3 1
 4     3 1
 
dp[i] min warehouse's height for box height

dp[0] = w[0];
dp[1] = Math.min(dp[0], w[1])

dp[i] = Math.min(dp[i-1], w[i])


T: O(n + mlogm + n), m is number of boxes, n is number of warehouse
S: O(n)
```

```java
class Solution {
    public int maxBoxesInWarehouse(int[] boxes, int[] warehouse) {
        int n = warehouse.length;
        int dp[] = new int[n];
        dp[0] = warehouse[0];
        
        for (int i = 1; i < n; i++) {
            dp[i] = Math.min(dp[i-1], warehouse[i]);
        }
        Arrays.sort(boxes);
        
        int count = 0;
        for (int i = n - 1; i >= 0; i--) {
            if (count < boxes.length && boxes[count] <= dp[i]) {
                count++; // after set to warehouse, boxIdx++
            }
        }
        return count;
    }
}j
```

but can be more simple

## greedy

like greedy idea, use warehouse itself

```
T: O(n + mlogm + n), m is number of boxes, n is number of warehouse
S: O(1)
```

```java
class Solution {
    public int maxBoxesInWarehouse(int[] boxes, int[] warehouse) {
        int n = warehouse.length;
        for (int i = 1; i < n; i++) {
            warehouse[i] = Math.min(warehouse[i-1], warehouse[i]);
        }
        Arrays.sort(boxes);
        int count = 0;
        for (int i = n - 1; i >= 0; i--) {
            if (count < boxes.length && boxes[count] <= warehouse[i]) {
                count++;
            }
        }
        return count;
    }
}
```

but if interview ask, you can't use in-place, how to achive space O(1)

sort boxes, from large to small, set to warehouse, then it'll abandon box which is too tall !

see leetcode solution 2: <https://leetcode.com/problems/put-boxes-into-the-warehouse-i/solution/>

```
T: O(mlogm + m), m is number of boxes
S: O(1)
```

```java
class Solution {
    public int maxBoxesInWarehouse(int[] boxes, int[] warehouse) {
        Arrays.sort(boxes);
        
        int count = 0;
        for (int i = boxes.length - 1; i >= 0; i--) {
            if (count < warehouse.length && boxes[i] <= warehouse[count]) {
                count++;
            }
        }
        return count;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/greedy/1564.-put-boxes-into-the-warehouse-i.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
