diff --git a/content/Classic Questions/Lowest Price With Specified Quality.md b/content/Classic Questions/Lowest Price With Specified Quality.md new file mode 100644 index 0000000..639df5a --- /dev/null +++ b/content/Classic Questions/Lowest Price With Specified Quality.md @@ -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 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`. \ No newline at end of file diff --git a/content/Classic Questions/assets/260207_update_strategy.jpg b/content/Classic Questions/assets/260207_update_strategy.jpg new file mode 100644 index 0000000..266a37e --- /dev/null +++ b/content/Classic Questions/assets/260207_update_strategy.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:38d6b6490bf383787c05196b6f3357ef9bd37d8b73f75eb1852a663101b8a657 +size 530929 diff --git a/content/Leetcode Questions/Minimum Cost Path with Teleportations.md b/content/Leetcode Questions/Minimum Cost Path with Teleportations.md new file mode 100644 index 0000000..ecfc8f1 --- /dev/null +++ b/content/Leetcode Questions/Minimum Cost Path with Teleportations.md @@ -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 $> min_solution; +``` + +## Min Cost diff --git a/content/Leetcode Questions/assets/260206_m_cost_arr.png b/content/Leetcode Questions/assets/260206_m_cost_arr.png new file mode 100644 index 0000000..368a7eb --- /dev/null +++ b/content/Leetcode Questions/assets/260206_m_cost_arr.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9bc291c90d4122c1e9e60d303dbb5204d949039f5b07ad19fb1fcff0497f2109 +size 32760 diff --git a/content/Leetcode Questions/assets/260206_optimal_for_teleportation.png b/content/Leetcode Questions/assets/260206_optimal_for_teleportation.png new file mode 100644 index 0000000..f14a246 --- /dev/null +++ b/content/Leetcode Questions/assets/260206_optimal_for_teleportation.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:56b8284f655b59d0f97cb7881f44e45f87bb08e347bf84ce23050eecd30ea0f7 +size 56816 diff --git a/content/assets/Minimum Cost Path With Teleportations.drawio b/content/assets/Minimum Cost Path With Teleportations.drawio new file mode 100644 index 0000000..4c9b670 --- /dev/null +++ b/content/assets/Minimum Cost Path With Teleportations.drawio @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d17b06585b64b54e217704ef8fbb781a435871ee023130935f6dc6ab99d1a37e +size 43551 diff --git a/rust_code/test.rs b/rust_code/test.rs deleted file mode 100644 index f1e4787..0000000 --- a/rust_code/test.rs +++ /dev/null @@ -1,9 +0,0 @@ -use std::io; - -fn main() { - let mut test = 1; -} - -fn call_function(func: CbFunc, data: i32) { - func(data); -}