淘宝做女鞋在哪个网站找货,网站被,做一个公司的网站应做哪些准备,西安微信商城网站设计Every day a Leetcode
题目来源#xff1a;3219. 切蛋糕的最小总开销 II
解法1#xff1a;贪心
谁的开销更大#xff0c;就先切谁#xff0c;并且这个先后顺序与切的次数无关。
代码#xff1a;
/** lc appleetcode.cn id3219 langcpp** [3219] 切蛋糕的最小总开销 I…Every day a Leetcode
题目来源3219. 切蛋糕的最小总开销 II
解法1贪心
谁的开销更大就先切谁并且这个先后顺序与切的次数无关。
代码
/** lc appleetcode.cn id3219 langcpp** [3219] 切蛋糕的最小总开销 II*/// lc codestart
class Solution
{
public:long long minimumCost(int m, int n, vectorint horizontalCut, vectorint verticalCut){sort(horizontalCut.begin(), horizontalCut.end(), greaterint());sort(verticalCut.begin(), verticalCut.end(), greaterint());long long ans 0;int cnt_h 1, cnt_v 1;int i 0, j 0;while (i m - 1 || j n - 1){if (j n - 1 || i m - 1 horizontalCut[i] verticalCut[j]){ans horizontalCut[i] * cnt_h; // 横切cnt_v; // 需要竖切的蛋糕块增加}else{ans verticalCut[j] * cnt_v; // 竖切cnt_h; // 需要横切的蛋糕块增加}}return ans;}
};
// lc codeend结果 复杂度分析
时间复杂度O(mlogmnlogn)瓶颈在排序上。
空间复杂度O(1)。