第一题

题解:模拟题

class Solution {
public:
int countVowelSubstrings(string w) {
int n = w.length();
int i = 0, res = 0;
string p = "aeiou";
while(i < n){
if(p.find(w[i]) != -1){
int j = i;
while(j < n && p.find(w[j]) != -1){
if(j < i+4) {
j++;
continue;
}
string s = w.substr(i, j-i+1);
set<char> set;
for(auto c : s){
if(!set.count(c)){
set.insert(c);
}
}
if(set.size() >=5){
res++;
}
j++;
}
}
i++;
}
return res;
}
};

第二题

题解:不能直接求子集判断超时,将问题转化为求每一个字母被使用的次数,第i个字母会在出现在 (n - i) * (i + 1) 个子串中

class Solution {
public:
long long countVowels(string w) {
string p = "aeiou";
long long res = 0;
for(int i=0; i<w.length(); i++){
if(p.find(w[i]) != -1){
res += (long long)(i+1) *(w.length()-i);
}
}
return res;
}
};

第三题



二分满足每一个商店商品数量 大于等于 k,通过商品数组中的商品数量计算店铺数量是否超过 n 来进行二分。

class Solution {
public:
int minimizedMaximum(int n, vector<int>& q) {
int l = 1;
int r = 0;
for(auto c : q) r = max(r, c);
while(l < r){
int mid = l+r >>1;
if(check(q, mid, n)){
r = mid;
}else{
l = mid+1;
}
}
return r;
} bool check(vector<int> q, int k, int n){
int cnt = 0;
for(auto c : q){
if(c%k == 0){
cnt += c/k;
}else cnt += (c/k)+1;
if(cnt > n) return false;
}
return true;
}
};

第四题



题解:dfs, 注意 路径中节点值只能计算一次, 另外方法传递vector参数使用引用,不用最后一个用例超时了。

#define PII pair<int, int>
class Solution {
public:
unordered_map<int, vector<PII>> g;
int res = 0, maxTimeV;
int maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime) {
maxTimeV = maxTime;
for(auto e : edges){
// 无向边
g[e[0]].push_back({e[1], e[2]});
g[e[1]].push_back({e[0], e[2]});
}
vector<int> path; // 记录路径
dfs(values, 0, 0, 0, path);
return res;
}
void dfs(vector<int>& values,int u, int time, int value, vector<int> path){
path.push_back(u);
int prevVal = values[u];
value += values[u];
// 置为 0,节点只计算一次value
values[u] = 0;
if(u == 0){
res = max(res, value);
}
for(auto c : g[u]){
if(time + c.second > maxTimeV) continue;
dfs(values, c.first, time+c.second, value, path);
}
// 恢复现场
path.pop_back();
values[u] = prevVal;
value -= prevVal;
}
};

LeetCode Weekly Contest 266的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  3. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  4. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  5. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  6. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  7. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  8. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  9. 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

随机推荐

  1. Hyper-V CPU设置

    前言 最近在用Hyper-V测试项目,发现在运行过程中发现项目总数崩掉,几经发现有一个共性,CPU占用率100%,分析问题发现问题出在Hyper-V CPU设置上,Hyper-V装系统就不赘述了,网上 ...

  2. 提问式复习:图文回顾 redo log 相关知识

    原文链接:提问式复习:图文回顾 redo log 相关知识 1.如何提升 redo日志 的写性能? 为了保证 redo日志 不丢失,会在磁盘中开辟一块空间将日志保存起来.但是这样会有一个问题,磁盘的读 ...

  3. IDEA Web渲染插件开发(二)— 自定义JsDialog

    <IDEA Web渲染插件开发(一)>中,我们了解到了如何编写一款用于显示网页的插件,所需要的核心知识点就是IDEA插件开发和JCEF,在本文中,我们将继续插件的开发,为该插件的JS Di ...

  4. 打开属性页,分别在Debug和Release下将其配置类型改为:静态库(.lib);

    右键工程->属性 配置类型里面的下拉菜单选择静态库

  5. Midway Serverless 发布 2.0,一体化让前端研发再次提效

    作者 | 张挺 来源 | Serverless 公众号 自去年 Midway Serverless 1.0 发布之后,许多业务开始尝试其中,并利用 Serverless 容器的弹性能力,减少了大量研发 ...

  6. Rigidbody钢体移动时抖动问题

    Rigidbody移动时抖动问题 撞墙抖动 Unity中物体移动有非常多的方式: 比如: transform.position += dir*speed*Time.deltaTime; transfo ...

  7. Ubuntu安装 配置GCC和Vim

    VMware14.0和Ubuntu镜像安装 https://mp.weixin.qq.com/s/045wrbdv92PkUQn2abddvA 适应屏幕设置 按照下列流程检查虚拟机设置 查看 -> ...

  8. Arp欺骗和DNS投毒的实验性分析

    1.中间人攻击之Arp欺骗/毒化 本文涉及网络安全攻击知识,随时可能被永久删除.请Star我的GitHub仓库 实现原理: 这种攻击手段也叫做中间人攻击MITM(Man-in-the-Middle) ...

  9. C++ 与 Visual Studio 2019 和 WSL(三)

    头文件 如果不小心修改了 Linux C/C++ 标准头文件,可以下面这样操作进行恢复: 项目 → 重新扫描解决方案

  10. python socket zmq

    本篇博客将介绍zmq应答模式,所谓应答模式,就是一问一答,规则有这么几条 1. 必须先提问,后回答 2. 对于一个提问,只能回答一次 3. 在没有收到回答前不能再次提问 上代码,服务端: #codin ...