3218. 切蛋糕的最小总开销 I
题目链接:3218. 切蛋糕的最小总开销 I
代码如下:
class Solution
{
public:int minimumCost(int m, int n, vector<int>& horizontalCut, vector<int>& verticalCut){ranges::sort(horizontalCut);ranges::sort(verticalCut);int res = 0;int i = 0, j = 0;while (i < m - 1 || j < n - 1){if (j == n - 1|| i < m - 1 && horizontalCut[i] < verticalCut[j]){res += horizontalCut[i++] * (n - j);//上下连边}else{res += verticalCut[j++] * (m - i);//左右连边}}return res;}
};