Translate all code to English (#1836)

* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
This commit is contained in:
Yudong Jin
2025-12-31 07:44:52 +08:00
committed by GitHub
parent 45e1295241
commit 2778a6f9c7
1284 changed files with 71557 additions and 3275 deletions
@@ -1,4 +1,4 @@
# Fractional knapsack problem
# Fractional Knapsack Problem
!!! question
@@ -15,7 +15,7 @@ The difference is that this problem allows selecting only a portion of an item.
![Value of items per unit weight](fractional_knapsack_problem.assets/fractional_knapsack_unit_value.png)
### Greedy strategy determination
### Greedy Strategy Determination
Maximizing the total value of items in the knapsack **is essentially maximizing the value per unit weight of items**. From this, we can derive the greedy strategy shown in the figure below.
@@ -25,7 +25,7 @@ Maximizing the total value of items in the knapsack **is essentially maximizing
![Greedy strategy for the fractional knapsack problem](fractional_knapsack_problem.assets/fractional_knapsack_greedy_strategy.png)
### Code implementation
### Code Implementation
We created an `Item` class to facilitate sorting items by unit value. We loop to make greedy selections, breaking when the knapsack is full and returning the solution:
@@ -39,7 +39,7 @@ Apart from sorting, in the worst case the entire item list needs to be traversed
Since an `Item` object list is initialized, **the space complexity is $O(n)$**.
### Correctness proof
### Correctness Proof
Using proof by contradiction. Suppose item $x$ has the highest unit value, and some algorithm yields a maximum value of `res`, but this solution does not include item $x$.
+5 -5
View File
@@ -1,4 +1,4 @@
# Greedy algorithm
# Greedy Algorithm
<u>Greedy algorithm</u> is a common algorithm for solving optimization problems. Its basic idea is to make the seemingly best choice at each decision stage of the problem, that is, to greedily make locally optimal decisions in hopes of obtaining a globally optimal solution. Greedy algorithms are simple and efficient, and are widely applied in many practical problems.
@@ -25,7 +25,7 @@ The implementation code is as follows:
You might exclaim: So clean! The greedy algorithm solves the coin change problem in about ten lines of code.
## Advantages and limitations of greedy algorithms
## Advantages and Limitations of Greedy Algorithms
**Greedy algorithms are not only straightforward and simple to implement, but are also usually very efficient**. In the code above, if the smallest coin denomination is $\min(coins)$, the greedy choice loops at most $amt / \min(coins)$ times, giving a time complexity of $O(amt / \min(coins))$. This is an order of magnitude smaller than the time complexity of the dynamic programming solution $O(n \times amt)$.
@@ -44,7 +44,7 @@ Generally, the applicability of greedy algorithms falls into the following two s
1. **Can guarantee finding the optimal solution**: In this situation, greedy algorithms are often the best choice, because they tend to be more efficient than backtracking and dynamic programming.
2. **Can find an approximate optimal solution**: Greedy algorithms are also applicable in this situation. For many complex problems, finding the global optimal solution is very difficult, and being able to find a suboptimal solution with high efficiency is also very good.
## Characteristics of greedy algorithms
## Characteristics of Greedy Algorithms
So the question arises: what kind of problems are suitable for solving with greedy algorithms? Or in other words, under what conditions can greedy algorithms guarantee finding the optimal solution?
@@ -65,7 +65,7 @@ For example, in the coin change problem, although we can easily provide countere
Pearson, D. A polynomial-time algorithm for the change-making problem[J]. Operations Research Letters, 2005, 33(3): 231-234.
## Steps for solving problems with greedy algorithms
## Steps for Solving Problems with Greedy Algorithms
The problem-solving process for greedy problems can generally be divided into the following three steps.
@@ -82,7 +82,7 @@ To ensure correctness, we should rigorously mathematically prove the greedy stra
However, correctness proofs may also not be easy. If we have no clue, we usually choose to debug the code based on test cases, step by step modifying and verifying the greedy strategy.
## Typical problems solved by greedy algorithms
## Typical Problems Solved by Greedy Algorithms
Greedy algorithms are often applied to optimization problems that satisfy greedy choice property and optimal substructure. Below are some typical greedy algorithm problems.
@@ -1,4 +1,4 @@
# Max capacity problem
# Max Capacity Problem
!!! question
@@ -20,7 +20,7 @@ $$
Let the array length be $n$, then the number of combinations of two partitions (total number of states) is $C_n^2 = \frac{n(n - 1)}{2}$. Most directly, **we can exhaustively enumerate all states** to find the maximum capacity, with time complexity $O(n^2)$.
### Greedy strategy determination
### Greedy Strategy Determination
This problem has a more efficient solution. As shown in the figure below, select a state $[i, j]$ where index $i < j$ and height $ht[i] < ht[j]$, meaning $i$ is the short partition and $j$ is the long partition.
@@ -72,7 +72,7 @@ The figure below shows the execution process of the greedy strategy.
=== "<9>"
![max_capacity_greedy_step9](max_capacity_problem.assets/max_capacity_greedy_step9.png)
### Code implementation
### Code Implementation
The code loops at most $n$ rounds, **therefore the time complexity is $O(n)$**.
@@ -82,7 +82,7 @@ Variables $i$, $j$, and $res$ use a constant amount of extra space, **therefore
[file]{max_capacity}-[class]{}-[func]{max_capacity}
```
### Correctness proof
### Correctness Proof
The reason greedy is faster than exhaustive enumeration is that each round of greedy selection "skips" some states.
@@ -1,4 +1,4 @@
# Max product cutting problem
# Max Product Cutting Problem
!!! question
@@ -20,7 +20,7 @@ $$
We need to think about: how large should the splitting count $m$ be, and what should each $n_i$ be?
### Greedy strategy determination
### Greedy Strategy Determination
Based on experience, the product of two integers is often greater than their sum. Suppose we split out a factor of $2$ from $n$, then their product is $2(n-2)$. We compare this product with $n$:
@@ -53,7 +53,7 @@ In summary, the following greedy strategies can be derived.
3. When the remainder is $2$, do not continue splitting, keep it.
4. When the remainder is $1$, since $2 \times 2 > 1 \times 3$, the last $3$ should be replaced with $2$.
### Code implementation
### Code Implementation
As shown in the figure below, we don't need to use loops to split the integer, but can use integer division to get the count of $3$s as $a$, and modulo operation to get the remainder as $b$, at which point we have:
@@ -76,7 +76,7 @@ Please note that for the edge case of $n \leq 3$, a $1$ must be split out, with
Variables $a$ and $b$ use a constant amount of extra space, **therefore the space complexity is $O(1)$**.
### Correctness proof
### Correctness Proof
Using proof by contradiction, only analyzing the case where $n \geq 4$.
+2
View File
@@ -1,5 +1,7 @@
# Summary
### Key Review
- Greedy algorithms are typically used to solve optimization problems. The principle is to make locally optimal decisions at each decision stage in hopes of obtaining a globally optimal solution.
- Greedy algorithms iteratively make one greedy choice after another, transforming the problem into a smaller subproblem in each round, until the problem is solved.
- Greedy algorithms are not only simple to implement, but also have high problem-solving efficiency. Compared to dynamic programming, greedy algorithms typically have lower time complexity.