mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-22 19:52:54 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -6,18 +6,18 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Max capacity: Greedy algorithm */
|
||||
/* Maximum container capacity: greedy algorithm */
|
||||
int maxCapacity(vector<int> &ht) {
|
||||
// Initialize i, j to be at both ends of the array
|
||||
// Initialize i and j at opposite ends of the array
|
||||
int i = 0, j = ht.size() - 1;
|
||||
// Initial max capacity is 0
|
||||
// The initial maximum capacity is 0
|
||||
int res = 0;
|
||||
// Loop for greedy selection until the two boards meet
|
||||
// Repeatedly make greedy choices until the two sides meet
|
||||
while (i < j) {
|
||||
// Update max capacity
|
||||
// Update the maximum capacity
|
||||
int cap = min(ht[i], ht[j]) * (j - i);
|
||||
res = max(res, cap);
|
||||
// Move the shorter board inward
|
||||
// Move the shorter side inward
|
||||
if (ht[i] < ht[j]) {
|
||||
i++;
|
||||
} else {
|
||||
@@ -27,13 +27,13 @@ int maxCapacity(vector<int> &ht) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
vector<int> ht = {3, 8, 5, 2, 7, 7, 3, 4};
|
||||
|
||||
// Greedy algorithm
|
||||
int res = maxCapacity(ht);
|
||||
cout << "Maximum capacity is " << res << endl;
|
||||
cout << "Maximum capacity: " << res << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user