LeetCode Weekly Contest 266
第一题

题解:模拟题
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的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
随机推荐
- DS博客作业05--查找
这个作业属于哪个班级 数据结构--网络2011/2012 这个作业的地址 DS博客作业05--查找 这个作业的目标 学习查找的相关结构 姓名 黄静 目录 0.PTA得分截图 1.本周学习总结 1.1 ...
- 使用form插件 和ajax 结合使用 没有调用success的原因
当我做文件上传的时候出现不调用success方法 将datype: 'json' 注释掉后成功响应 浏览器显示200状态码,并且响应头为json格式,格式ajax不认为它是json,所以一直执行错误 ...
- 牛客挑战赛48C-铬合金之声【Prufer序列】
正题 题目链接:https://ac.nowcoder.com/acm/contest/11161/C 题目大意 \(n\)个点加\(m\)条边使得不存在环,每种方案的权值是所有联通块的大小乘积. 求 ...
- Jmeter压测学习3---通过正则表达式提取token
上一个随笔记录的是用json提取器提取token,这个随笔记录用正则表达式提取token 一.添加正则表达式 登录右击添加->后置处理器->正则表达式提取器 正则提取器参数说明: 要检查的 ...
- Dapr + .NET Core实战(八)服务监测
服务监测 分布式服务性能指标,链路追踪,运行状况,日志记录都很重要,我们日常开发中为了实现这些功能需要集成很多功能,替换监控组件时成本也很高. Dapr 可观测性模块将服务监测与应用程序分离.它自动捕 ...
- HashMap的tableSizeFor解析
我们都知道,对于HashMap来说,数组的容量为2的倍数,但是我们可以在创建map的时候传入一个数组的大小 此时,这个初始化数组大小会传给一个双参的构造器 1. 创建HashMap public st ...
- 解决 Asp.Net5 在视频文件下载预览时无法快进的问题
前情提要 https://www.cnblogs.com/puzhiwei/p/15265005.html 在解决.Net5 如何修改Content-Disposition实现在线预览的功能后,我又遇 ...
- 树莓派使用python+继电器控制220V灯泡
需要的材料 1.继电器:继电器是一种电控制器件,它实际上是用小电流去控制大电流运作的一种"自动开关",我们这里用它来控制电灯.控制了继电器就等于控制了电灯. 我购买的是某宝上3块钱 ...
- Windows 11 正式版 Build 22000.194 官方简体中文版、英文版(消费者版、商业版)下载
昨天阿三正式发布了 Windows 11,版本号竟然是 22000.194,也就是 9 月 16 日的 测试版 22000.194,仅仅是文件改了个名,特别是消费者版本 hash 校验都是一致的. W ...
- gRPC,爆赞
原文链接: gRPC,爆赞 gRPC 这项技术真是太棒了,接口约束严格,性能还高,在 k8s 和很多微服务框架中都有应用. 作为一名程序员,学就对了. 之前用 Python 写过一些 gRPC 服务, ...