---
# System prepended metadata

title: 1564. Put Boxes Into the Warehouse I
tags: [Leetcode, Two Pointers, Sorting, Medium, Greedy]

---

# 1564. Put Boxes Into the Warehouse I
###### tags: `Leetcode` `Medium` `Greedy` `Sorting` `Two Pointers`
Link: https://leetcode.com/problems/put-boxes-into-the-warehouse-i/description/
## 思路
先处理warehouse array由于箱子要推进去 所以只要后面的高度高于前面的最矮高度就没有用
所以我们可以遍历warehouse让每一个位置的高度都变成当前最矮的高度
这样得到的warehouse就是降序的
所以我们只需要给boxes排序 用双指针的方法看 到底能放几个箱子
## Code
```java=
class Solution {
    public int maxBoxesInWarehouse(int[] boxes, int[] warehouse) {
        int n = warehouse.length;
        int min = Integer.MAX_VALUE;
        for(int i=0; i<n; i++){
            warehouse[i] = Math.min(warehouse[i], min);
            min = Math.min(min, warehouse[i]);
        }
        Arrays.sort(boxes);
        int m = boxes.length;
        int j = m-1;
        int ans = 0;
        for(int i=0; i<n; i++){
            while(j>=0 && boxes[j]>warehouse[i]){
                j--;
            }
            if(j<0) break;
            ans++;
            j--;
        }
        return ans;
    }
}
```