LeetCode Weekly Contest 20
1. 520. Detect Capital
题目描述的很清楚,直接写,注意:字符串长度为1的时候,大写和小写都是满足要求的,剩下的情况单独判断。还有:我感觉自己写的代码很丑,判断条件比较多,需要改进,精简再精简。
class Solution {
public:
bool detectCapitalUse(string word) {
int n = word.size();
if(n == ) return ;
if(word[] <= 'Z') {
bool f = ;
for (int i = ; i < n; i++) {
if(word[i] >= 'a') {}
else {
f = ; break;
}
}
if(f) return ;
f = ;
for (int i = ; i < n; i++) {
if(word[i] < 'a') {}
else {
f = ; break;
}
}
if(f) return ;
} else {
bool f = ;
for (int i = ; i < n; i++) {
if(word[i] < 'a') {
f = ; break;
}
}
if(f) return ;
}
return ;
}
};
2. 526. Beautiful Arrangement
看完题目,感觉数据范围很小,1<<15 = 32000,(这里不对,应该是15!,这个数字很大,暴力是不行的,之前的分析是错的!)比较小的,使用next_permutation暴力,枚举, 然后tle, 我就想着本地打表计算,直接输出结果,谁知道13,14,15的结果根本出不来。
后来看11,12的答案挺小,然后想着预处理出每个位置可以放的数字,然后dfs进行解的搜索,本地测试1-15,出解的速度很快,然后提交,就ac了。
class Solution {
public:
vector<int> e[];
bool in[];
int res;
int tn;
void work(int u) {
if(u > tn) {
bool f = ;
for (int i = ; i <= tn; i++) {
if(!in[i]) {
f = ; break;
}
}
res += f;
return;
}
for (int x : e[u]) {
if(in[x]) continue;
in[x] = ;
work(u + );
in[x] = ;
}
}
int countArrangement(int n) {
if(n < ) return n;
tn = n;
for (int i = ; i <= n; i++) {
e[i].clear();
for (int j = ; j <= n; j++) {
if(j % i == || i % j == )
e[i].push_back(j);
}
}
memset(in, , sizeof in);
work();
return res;
}
};
3. 525. Contiguous Array
这道题是原题,但是看完,想不出来递推方程,只好找以前的答案,幸好保存了,我是传送门http://www.cnblogs.com/y119777/p/5851025.html,多亏当初还仔细分析了一下,考虑了各种情况。
然后抄,提交,ac。
class Solution {
public:
int getLongestString(vector<int> & v) {
int n = v.size();
if(n < ) return ;
map<int, int> m;
int s = ;
int res = ;
for(int i = ; i < n; i++) {
s += v[i];
if(s == ) {
res = max(res, i + ); continue;
}
if(!m.count(s)) {
m[s] = i;
} else {
int len = i - m[s];
res = max(res, len);
}
}
return res;
}
int findMaxLength(vector<int>& nums) {
int n = nums.size();
if(n < ) return ;
for (int i = ; i < n; i++)
if(nums[i] == ) nums[i] = -;
return getLongestString(nums);
}
};
4。 517. Super Washing Machines
先看数据范围[0,1e4], [1, 1e5], 1e9,使用int,不会溢出。(时刻牢记这个坑,有时候最后结果不会溢出,但是,中间结果可能溢出)。n^2的复杂度有点高,而且。我也不知道n^2的解法怎么写!
一看hard,想不出思路,感觉要跪!这时候,一定提醒自己,静下心来,从最简单的例子入手,分析,有什么优秀的性质可以利用,考虑解法。
第一:不难想到,长度小于2,直接返回0,然后求和,结果不是长度的倍数,就直接返回-1,剩下的情况一定可以出结果。
第二:想不出n^2的方法,那就是复杂度为n的枚举方法,按这种枚举出来的结果,就是最优的。然后记 s = s / n; s为每个数字最后的目标,小于s的需要,至少需要s-a次,大于s的需要a-s次,然后考虑是否可以同时进行,由于每个数字互不干扰,每个数字每次移出只能移出1,所以只需要统计每个数字移出的次数,最后的结果就是最大的那个数字。仔细,思考一下,最好画一画。
class Solution {
public:
int findMinMoves(vector<int>& mach) {
int n = mach.size();
if(n < ) return ;
int s = ;
for (int x : mach)
s += x;
if(s % n != ) {
return -;
}
if(s == ) return ;
vector<int> c(n, );
s /= n;
int res = ;
for (int i = ; i < n; i++) {
int t = mach[i] + c[i];
if(t == s) {
} else if(t < s) {
c[i + ] -= s - t;
} else if(t > s) {
c[i] -= t - s;
mach[i + ] += t - s;
//c[i + 1] += t - s;
}
res = max(res, abs(c[i]));
//cout << c[i] << endl;
}
return res;
}
};
LeetCode Weekly Contest 20的更多相关文章
- 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 ...
随机推荐
- SLAM:ORB-SLAM 位姿优化描述
只知道算法描述和代码,而不知道原理是比较扯的事情,还是把原理转载一下. 原文链接: http://www.cnblogs.com/luyb/p/5447497.html ORB-SLAM作为单目SLA ...
- 安装mysql遇到的几个坑
1. 官网下载压缩版mysql,配置太复杂 弃之 2. 官网下载最新版本mysql安装包 5.8.X,安装成功,一路next,安装成功后发现没有看到自定义安装路径,查看mysql安装完成的路径果然在C ...
- 详解CorelDRAW智能填充工具的运用
使用智能填充工具可以为任意的闭合区域填充颜色并设置轮廓.与其他填充工具不同,智能填充工具仅填充对象,它检测到区域的边缘并创建一个闭合路径,因此可以填充区域.例如,智能填充工具可以检测多个对象相交产生的 ...
- bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组_排序
Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题解: 方法一: 搞一个KDtree, ...
- Coloring Flame Graphs: Code Hues
转自:http://www.brendangregg.com/blog/2017-07-30/coloring-flamegraphs-code-type.html I recently improv ...
- c++ STL - priority_queue优先队列详解
简述 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出 (first in, l ...
- Golang Gin实践 番外 请入门 Makefile
Golang Gin实践 番外 请入门 Makefile 原文地址:Golang Gin实践 番外 请入门 Makefile 前言 含一定复杂度的软件工程,基本上都是先编译 A,再依赖 B,再编译 C ...
- NumPy常见的元素操作函数
ceil(): 向上最接近的整数,参数是 number 或 array floor(): 向下最接近的整数,参数是 number 或 array rint(): 四舍五入,参数是 number 或 a ...
- Oracle数据库的性能调整
oracle是一个高性能数据库软件.用户可以通过参数的调整,达到性能的优化.性能优化主要分为两部分:一是数据库管理员通过对系统参数的调整达到优化的目的,二是开发人员通过对应用程序的优化达到调整的目的. ...
- OO第四单元总结——查询UML类图 暨 OO课程总结
一.本单元两次作业的架构设计总结 作业一.UML类图查询 1. 统计信息图 2. 复杂度分析 基本复杂度(Essential Complexity (ev(G)).模块设计复杂度(Module Des ...