cpp-algo-cases/chapter_greedy/max_capacity.cpp

40 lines
778 B
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: max_capacity.cpp
* Created Time: 2023-07-21
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̰<EFBFBD><CCB0> */
int maxCapacity(vector<int> &ht) {
// <20><>ʼ<EFBFBD><CABC> i, j<><6A>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int i = 0, j = ht.size() - 1;
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 0
int res = 0;
// ѭ<><D1AD>̰<EFBFBD><CCB0>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while (i < j) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int cap = min(ht[i], ht[j]) * (j - i);
res = max(res, cap);
// <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6>̰<EFBFBD>
if (ht[i] < ht[j]) {
i++;
} else {
j--;
}
}
return res;
}
/* Driver Code */
int main() {
vector<int> ht = {3, 8, 5, 2, 7, 7, 3, 4};
// ̰<><CCB0><EFBFBD>
int res = maxCapacity(ht);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << res << endl;
return 0;
}