第 191 场周赛 Posted on 2020-05-31 | In leetcode , Weekly-Contest | | Words count in article: 569 | Reading time ≈ 3 1464. 数组中两元素的最大乘积123456789101112class Solution {public: int maxProduct(vector<int>& nums) { int res = 0; for(int i=0; i<nums.size(); i++) { for(int j=i+1; j<nums.size(); j++) { res = max(res, (nums[i]-1) * (nums[j]-1)); } } return res; }}; 1465. 切割后面积最大的蛋糕12345678910111213141516171819202122class Solution {public: int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) { long long maxh = 0, maxw = 0; sort(horizontalCuts.begin(), horizontalCuts.end()); sort(verticalCuts.begin(), verticalCuts.end()); long long pre = 0; long long hh = h, ww = w; for(long long h: horizontalCuts) { maxh = max(maxh, h-pre); pre = h; } maxh = max(hh-pre, maxh); pre = 0; for(long long v: verticalCuts) { maxw = max(v-pre, maxw); pre = v; } maxw = max(ww-pre, maxw); return (int)(maxh*maxw %1000000007); }}; 1466. 重新规划路线1234567891011121314151617class Solution {public: int minReorder(int n, vector<vector<int>>& connections) { set<int> st; st.insert(0); int res = 0; for(auto c: connections) { if(st.count(c[1])) { st.insert(c[0]); } else { res ++; st.insert(c[1]); } } return res; }}; 1467. 两个盒子中球的颜色数相同的概率123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263class Solution {public: vector<int> left, right, bas; int ln = 0, rn = 0; int alls = 0; double tot, ak; double fac(int a){ // 求阶乘 double ans = 1; for(int i = 1; i <= a; i ++) ans *= i; return ans; } double get(vector<int>& bas){ // 求排列数 int n = 0; for(auto x:bas) n += x; double ans = fac(n); for(auto x:bas) ans /= fac(x); return ans; } void dfs(int i){ if(ln > alls / 2 || rn > alls / 2) return ; // 剪枝 if(i == left.size()){ // 叶子节点 // for(auto x:left) cout << x << " "; // cout << endl; // for(auto x:right) cout << x <<" "; // cout << endl; int a = 0, b = 0; for(auto x:left){ if(x) a ++; } for(auto x:right){ if(x) b ++; } if(a == b) // 两个箱子中的球的颜色种类相同 ak += get(left) * get(right); return; } for(int j = 0; j <= bas[i]; j++){ // 枚举当前颜色球,左右箱子中的个数 left[i] = j; right[i] = bas[i] - j; ln += j; // 记录两个箱子中球的总数,用于剪枝 rn += bas[i] - j; dfs(i + 1); // 递归调用 ln -= j; // 恢复现场 rn -= bas[i] - j; } } double getProbability(vector<int>& balls) { bas = balls; left = vector<int>(balls.size(), 0); right = vector<int>(balls.size(), 0); tot = get(balls); // 求分母 for(auto x:balls) alls += x; dfs(0); // 求分子 // cout << ak << " " << tot << endl; return ak / tot; }}; Donate? comment? Donate WeChat Pay Alipay