Minimum Pizza Delivery Cost
You are given an integer array price, where price[i] represents the pizza price in building i + 1.
There are n buildings on a street, numbered from 1 to n. Each building contains both a pizzeria and an apartment.
If you order a pizza from building a and it is delivered to building b, then the total cost is: price[a - 1] + |a - b|
You are also given a 2D integer array queries, where each query is one of the following types:
- [1, k, x] : Update the pizza price of building k to x.
- [2, k] : Find the minimum possible cost to get a pizza delivered to building k from any building.
Return an array containing the answer for every query of type [2, k] in the same order.
Example 1:
Input: price = [5, 9, 3, 8], queries = [[2, 1], [2, 4], [1, 2, 2], [2, 1], [2, 2]]
Output: [5, 4, 3, 2]
Explanation:
- Query [2, 1]: Minimum cost for building 1 is min(5+0, 9+1, 3+2, 8+3) = min(5, 10, 5, 11) = 5
- Query [2, 4]: Minimum cost for building 4 is min(5+3, 9+2, 3+1, 8+0) = min(8, 11, 4, 8) = 4
- Query [1, 2, 2]: Update building 2 price to 2. Now price = [5, 2, 3, 8]
- Query [2, 1]: Minimum cost is min(5+0, 2+1, 3+2, 8+3) = min(5, 3, 5, 11) = 3
- Query [2, 2]: Minimum cost is min(5+1, 2+0, 3+1, 8+2) = min(6, 2, 4, 10) = 2
Example 2:
Input: price = [10, 1, 10, 10, 10], queries = [[2, 5], [1, 4, 2], [2, 5], [1, 2, 9], [2, 3]]
Output: [4, 3, 3]
Explanation:
- Query [2, 5]: Minimum cost is min(10+4, 1+3, 10+2, 10+1, 10+0) = min(14, 4, 12, 11, 10) = 4
- Query [1, 4, 2]: Update building 4 price to 2. Now price = [10, 1, 10, 2, 10]
- Query [2, 5]: Minimum cost is min(14, 4, 12, 3, 10) = 3
- Query [1, 2, 9]: Update building 2 price to 9. Now price = [10, 9, 10, 2, 10]
- Query [2, 3]: Minimum cost is min(10+2, 9+1, 10+0, 2+1, 10+2) = min(12, 10, 10, 3, 12) = 3
Now Your Turn!
Pick the correct output for the given inputInput: price = [7, 4, 6], queries = [[1, 1, 2], [2, 3], [2, 1]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n == price.length <= 2 * 105
- 1 <= queries.length <= 2 * 105
- 1 <= price[i] <= 109
- For an update query [1, k, x]: 1 <= k <= n, 1 <= x <= 109
- For a search query [2, k]: 1 <= k <= n
- Each query is either of the form [1, k, x] or [2, k]