← 返回 pinterest 的题目列表Put Boxes Into the Warehouse - Maximize Total Box Height
类型:online_judge
Problem: Put Boxes Into the Warehouse and Maximize Total Box Height
You are given two integer arrays:
boxes[i] is the height of the i-th box.
warehouse[j] is the height of the j-th room in the warehouse.
The warehouse consists of rooms in a row. Boxes can only be pushed into the warehouse from the left entrance. To place a box in room j, the box must be able to pass through every room from the entrance to room j, so its height cannot exceed the minimum height on that path.
Each room can contain at most one box, and each box can be used at most once. You may reorder the boxes arbitrarily before inserting them.
Return the maximum possible total height of boxes that can be placed into the warehouse.
This is a follow-up to LeetCode 1564, where the original problem asks for the maximum number of boxes. Here, the goal is to maximize the total height value.
Input Format
n m
boxes[0] boxes[1] ... boxes[n-1]
warehouse[0] warehouse[1] ... warehouse[m-1]
Output Format
maximum_total_height
Constraints
1 <= n, m <= 10^5
1 <= boxes[i], warehouse[j] <= 10^9
Example 1
Input:
4 5
4 3 4 1
5 3 3 4 1
Output:
8
Explanation: The effective warehouse heights from left to right are [5, 3, 3, 3, 1]. We can place boxes of heights 4, 3, and 1, with total height 8.
Example
Input
4 5
4 3 4 1
5 3 3 4 1
Output
8