content: update content, build
All checks were successful
Build and Deploy Quartz / build-and-deploy (push) Successful in 1m45s
All checks were successful
Build and Deploy Quartz / build-and-deploy (push) Successful in 1m45s
This commit is contained in:
@@ -0,0 +1,85 @@
|
|||||||
|
---
|
||||||
|
date: 2026-02-07
|
||||||
|
tags:
|
||||||
|
- Array
|
||||||
|
- Prefix
|
||||||
|
---
|
||||||
|
# Description
|
||||||
|
|
||||||
|
## Question
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
struct ProductInfo {
|
||||||
|
// higher number means better quality
|
||||||
|
int quality; // [0, 10000]
|
||||||
|
int price; // [0, INT_MAX)
|
||||||
|
```
|
||||||
|
|
||||||
|
Given a collection of `ProductInfo`, then answer several queries.
|
||||||
|
|
||||||
|
Each query gives you a number represents the quality requirement. You need to return the minimum possible cost to buy a product that satisfy the quality requirement.
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
|
We could also say: Given array of pair `{a, b}`, when query gives `k`, find pair from array that:
|
||||||
|
|
||||||
|
- `pair.a >= k`
|
||||||
|
- `pair.b` is the minimum possible value when satisfying the rule above
|
||||||
|
|
||||||
|
## Complexity Requirement
|
||||||
|
|
||||||
|
Answer query should be in $O(1)$ time complexity after $O(n)$ pre-processing. Where $n$ is the range size of `pair.a`
|
||||||
|
|
||||||
|
# Solution
|
||||||
|
|
||||||
|
There are two key points:
|
||||||
|
|
||||||
|
First, value space of `quality` is discrete and limited to `[0, 10000]`, in another word, it's "iterable" and the cost is acceptable if in pre-processing.
|
||||||
|
|
||||||
|
Second, `answer(requirement)` only relies on those `ProductionInfo` which's `quality >= requirement`.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
int answer[MAX_QUALITY + 1];
|
||||||
|
|
||||||
|
bool cmp_greater_by_quality(const ProductInfo& a, const ProductInfo& b) {
|
||||||
|
return a.quality > b.quality;
|
||||||
|
}
|
||||||
|
|
||||||
|
void preprocess(vector<ProductInfo> info) {
|
||||||
|
// sort products in info based on quality in desc
|
||||||
|
sort(info.begin(), info.end(), cmp_greater_by_quality);
|
||||||
|
|
||||||
|
// init variables
|
||||||
|
int min_price = info[0].price;
|
||||||
|
int prev_quality = info[0].quality;
|
||||||
|
|
||||||
|
// iterate products from highest quality to lowest
|
||||||
|
for(int i = 0; i < info.size(); ++i) {
|
||||||
|
|
||||||
|
const auto& cur_product = info[i];
|
||||||
|
|
||||||
|
// write answer if quality is going to fall
|
||||||
|
// before updating min price
|
||||||
|
if(cur_product.quality < prev_quality) {
|
||||||
|
update_answer_interval(
|
||||||
|
cur_prodcut.quality,
|
||||||
|
prev_prodcut.quality,
|
||||||
|
min_price
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// update min price
|
||||||
|
min_price = std::min(min_price, cur_product.price);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
![[260207_update_strategy.jpg]]
|
||||||
|
|
||||||
|
Based on the way how we iterate these points, `cur_product.quality <= prev_quality` must be `true`.
|
||||||
|
|
||||||
|
When `cur_product.quality == prev_quality`, it means currently iterating point is on the same vertical line of the previous one, so the minimum cost may still be updated, and minimum possible price for this `quality` is not determined.
|
||||||
|
|
||||||
|
Else, when `cur_product.quality < prev.quality`, it means currently iterating point is on the left side of the previous one on the graph. The minimum possible value for `prev_quality` will not change anymore *(since all the following points must have lower `quality` value, and thus will not be able to affect the answer of higher quality queries)*.
|
||||||
|
|
||||||
|
In this case, we could "lock" the final answer for range $(\text{current quality}, \text{previous quality}]$. Note that we couldn't determine the answer for `cur_product.quality` because there could be some other points which have the same `quality` value but have lower `price`.
|
||||||
BIN
content/Classic Questions/assets/260207_update_strategy.jpg
LFS
Normal file
BIN
content/Classic Questions/assets/260207_update_strategy.jpg
LFS
Normal file
Binary file not shown.
@@ -0,0 +1,93 @@
|
|||||||
|
---
|
||||||
|
leetcode_url: https://leetcode.com/problems/minimum-cost-path-with-teleportations
|
||||||
|
date: 2026-02-06
|
||||||
|
tags:
|
||||||
|
- DynamicProgramming
|
||||||
|
---
|
||||||
|
# Thought
|
||||||
|
|
||||||
|
## State Transfer Overview
|
||||||
|
|
||||||
|
State transfer for `m_cost_arr[layer][r][c]`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# normal move
|
||||||
|
m_cost_arr[layer][r - 1][c]; # from above
|
||||||
|
m_cost_arr[layer][r][c - 1]; # from left
|
||||||
|
|
||||||
|
# teleportation
|
||||||
|
get_min_cost(
|
||||||
|
max_teleport = layer - 1,
|
||||||
|
min_node_weight = weight[r][c],
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
|
In above, `m_cost_arr[max_teleport][row_idx][column_idx]` is a 3D array storing the dynamic programming solutions for sub-questions:
|
||||||
|
|
||||||
|
![[260206_m_cost_arr.png]]
|
||||||
|
|
||||||
|
The green highlighted area is the **sub-question area** which's solution stored in the array.
|
||||||
|
|
||||||
|
That is: **value in `m_cost_arr[layer][i][j]` is the solution for sub-question when only considering highlighted nodes from $(0, 0)$ to $(i,j)$ with maximum allowed transportation count `layer`.**
|
||||||
|
|
||||||
|
> [!note]
|
||||||
|
>
|
||||||
|
> The **sub-question area is not a strict triangular**, this definition is required based on how we deal with transportation when performing state transfer. Keep reading for more info.
|
||||||
|
|
||||||
|
> [!note]
|
||||||
|
>
|
||||||
|
> `layer` in this article usually refers to max allowed transportation count.
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
|
We actually split up the state transfer into two different cases: The last operation to the destination point $(r,c)$:
|
||||||
|
|
||||||
|
1. Is a Normal Move operation.
|
||||||
|
2. Is a Teleportation operation.
|
||||||
|
|
||||||
|
It's obvious that the first case could be done with $O(1)$ complexity. The point is to figure out how to perform state transfer for the second case in an efficient way, that is, the transportation as the last operation to point $(r,c)$.
|
||||||
|
|
||||||
|
## Teleportation As Last Operation
|
||||||
|
|
||||||
|
We know that:
|
||||||
|
|
||||||
|
- Teleportation itself **do not impose cost**
|
||||||
|
- Teleportation will use teleportation quota
|
||||||
|
|
||||||
|
So we have:
|
||||||
|
|
||||||
|
```python
|
||||||
|
cost_from_origin_to(x, y, teleport_limit = n)
|
||||||
|
= cost_from_origin_to(i, j, teleport_limit = n - 1) + 0 # cost nothing
|
||||||
|
```
|
||||||
|
|
||||||
|
So the optimal solution when last operation is Teleportation should be:
|
||||||
|
|
||||||
|
$$
|
||||||
|
\text{min}(\text{CostFromOriginTo}(x, y, \text{layer} - 1)), \quad \text{where: }
|
||||||
|
\begin{cases}
|
||||||
|
(x,y) \in \text{SubQuestionSpace} \\
|
||||||
|
\text{weight}(x,y) \ge \text{weight}(i,j)
|
||||||
|
\end{cases}
|
||||||
|
$$
|
||||||
|
|
||||||
|
![[260206_optimal_for_teleportation.png]]
|
||||||
|
For example, still consider sub-question `[layer][i][j]`, as the image show, let's say all the green nodes have nodes weight $<v$, and all the blue nodes have weight $\ge v$, then the optimal value for "teleportation-as-last-operation" transfer should be the **min value of the sub-question of those blue-highlighted nodes in layer `layer - 1`.**
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
|
To do this efficiently, we maintain one more info array:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
struct MinInfo {
|
||||||
|
weight_t weight;
|
||||||
|
cost_t cost;
|
||||||
|
};
|
||||||
|
|
||||||
|
// schema: min_solution[layer] = [{weight, cost}, {weight, cost}, ...];
|
||||||
|
vector<vector<MinInfo>> min_solution;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Min Cost
|
||||||
BIN
content/Leetcode Questions/assets/260206_m_cost_arr.png
LFS
Normal file
BIN
content/Leetcode Questions/assets/260206_m_cost_arr.png
LFS
Normal file
Binary file not shown.
BIN
content/Leetcode Questions/assets/260206_optimal_for_teleportation.png
LFS
Normal file
BIN
content/Leetcode Questions/assets/260206_optimal_for_teleportation.png
LFS
Normal file
Binary file not shown.
BIN
content/assets/Minimum Cost Path With Teleportations.drawio
LFS
Normal file
BIN
content/assets/Minimum Cost Path With Teleportations.drawio
LFS
Normal file
Binary file not shown.
@@ -1,9 +0,0 @@
|
|||||||
use std::io;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let mut test = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call_function<CbFunc: Fn(i32)>(func: CbFunc, data: i32) {
|
|
||||||
func(data);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user