[leetcode]Weekly Contest 68 (767. Reorganize String&&769. Max Chunks To Make Sorted&&768. Max Chunks To Make Sorted II)
766. Toeplitz Matrix
第一题不说,贼麻瓜,好久没以比赛的状态写题,这个题浪费了快40分钟,我真是。。。。。。
767. Reorganize String
就是给你一个字符串,能不能转换成:任意一个字符S[i],使得S[i-1]和S[i+1]都不等于S[i]。如果能输出,不能就输出一个空字符串。
大一的时候好像有印象,队友做出来的,贪心吧,如果遇到相邻两个一样的,就从后面找一个插到中间,具体原理我也不知道为什么,反正是对的,从头到尾扫一遍,再倒着来一遍,为什么要两边我也不知道,反正是对的。。。我也很凌乱啊!!!
class Solution {
public:
string reorganizeString(string S) {
int len = S.size();
string ts = S;
bool ans1 = true;
for(int i=;i<len;i++)
{
if(ts[i]==ts[i+]) {
if(i == len-) ans1 = false;
for(int j=i+;j<len;j++)
{
if(ts[j]!=ts[i] && ts[j]!=ts[i+])
{
swap(ts[j], ts[i+]);
break;
}
if(j == len-) {
ans1 = false;
}
}
}
}
string ts2 = ts;
reverse(ts2.begin(), ts2.end());
bool ans2 = true;
for(int i=;i<len;i++)
{
if(ts2[i]==ts2[i+]) {
if(i == len-) ans2 = false;
for(int j=i+;j<len;j++)
{
if(ts2[j]!=ts2[i] && ts2[j]!=ts2[i+])
{
swap(ts2[j], ts2[i+]);
break;
}
if(j == len-) {
ans2 = false;
}
}
}
}
if(ans1){
return ts;
}
else if(ans2) {
return ts2;
}
else {
return "";
}
}
};
769. Max Chunks To Make Sorted
给你一个序列,问你把这个序列分成n块,每个块单独排序,且排完序后和整体排序得到的结果要一模一样,求n的最大值
因为这个题说了,序列的内容是0——length-1,所以序列的内容唯一且连续
遍历序列从i到length-1,对前i的内容进行求最大值max,和最小值min,若在前i的序列中,min == 下标的最小值,max == 下标的最大值,即最小块,只是比赛时想到的,虽然也对,但太麻烦
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
int ans = ;
int start = ;
while(start != arr.size()){
int min = arr[start], max = arr[start];
for(int end=start;end<arr.size();end++)
{
max = max>arr[end]?max:arr[end];
min = min<arr[end]?min:arr[end];
if(min == start&&max == end) {
ans ++;
start = end + ;
break;
}
}
}
return ans;
}
};
题解,只用求最大值就可以了,因为所以序列的内容唯一且连续,所以若arr[i]为最大值且 i == arr[i],则说明,小于 i的部分已经可以且一定顺序连续地排序
class Solution(object):
def maxChunksToSorted(self, arr):
ans = ma = 0
for i, x in enumerate(arr):
ma = max(ma, x)
if ma == i: ans += 1
return ans
768. Max Chunks To Make Sorted II
这是第二题的升级版
序列里的内容不一定(唯一且连续)了,其他都是一样的
我们来看一个特殊的序列1,1,0,0,1
对于正确的序列来说,是0,0,1,1,1
如果用上一种方法,那么到第三个数的时候,ans就要加一了
所以判断是否是最小且正确的块的时候,还要判断,前面出现的内容,和全部排好序的序列,是否一致
我在这里引入一个已遍历的序列和作为判断,这个算法的正确性,我还不得而知,我自己甚至认为可能不对,因为我数学学得是真的烂。。。。。。
希望若有人证明是正确的可以告诉我
class Solution:
def maxChunksToSorted(self, arr):
ans = 0
sum1 = 0
sum2 = 0
arr_pre_max = arr[0]
tarr = sorted(arr)
for x, y in zip(arr, tarr):
sum1 += x
sum2 += y
arr_pre_max = max(arr_pre_max, x)
if arr_pre_max == y and sum1 == sum2:
ans += 1
return ans
最后一题没做看起来很难的样子
[leetcode]Weekly Contest 68 (767. Reorganize String&&769. Max Chunks To Make Sorted&&768. Max Chunks To Make Sorted II)的更多相关文章
- 767. Reorganize String - LeetCode
Question 767. Reorganize String Solution 题目大意: 给一个字符串,将字符按如下规则排序,相邻两个字符一同,如果相同返回空串否则返回排序后的串. 思路: 首先找 ...
- 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 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 ...
- LeetCode Weekly Contest 18B
1. 496. Next Greater Element I 暴力的话,复杂度也就1000 * 1000 = 1e6, 在1s的时限内完全可以. 当然,有许多优化方法,利用stack维护递减序列的方法 ...
随机推荐
- shell脚本使用记录一:操作文件
一,连接远程数据库(保证在服务器上能使用mysql命令行,至少要安装mysql客户端) #!/bin/bash HOSTNAME="ip" PORT=" USERNAME ...
- winform自定义控件开发
1.添加控件属性 //添加私有的控件属性 private string djm;//单据名 //添加属性描述 [Browsable(true)] [Description("djm" ...
- VS2015 + OPENCV + CUDA 安装流程
VS2015 https://blog.csdn.net/guxiaonuan/article/details/73775519?locationNum=2&fps=1 OPENCV htt ...
- C#复习笔记(3)--C#2:解决C#1的问题(进入快速通道的委托)
委托 前言:C#1中就已经有了委托的概念,但是其繁杂的用法并没有引起开发者太多的关注,在C#2中,进行了一些编译器上的优化,可以用匿名方法来创建一个委托.同时,还支持的方法组和委托的转换.顺便的,C# ...
- [转帖]LCD与LED的区别之背光原理与优缺点对比介绍
LCD与LED的区别之背光原理与优缺点对比介绍 http://m.elecfans.com/article/620376.html 时下液晶面板与液晶电视技术已经达到炉火纯青的境界,并已经成为大屏幕平 ...
- webservice服务的提供及调用完整代码示例
服务提供方: applicationContext.xml applicationContext-webService.xml 服务调用方:
- hive排序
1.升序排序 hive > select id,name,sal from emp order by sal; 2.降序 添加关键字desc hive > select id,nam ...
- scrapy暂停和重启,及url去重原理,telenet简单使用
一.scrapy暂停与重启 1.要暂停,就要保留一些中间信息,以便重启读取中间信息并从当前位置继续爬取,则需要一个目录存放中间信息: scrapy crawl spider_name -s JOBDI ...
- k8s使用Glusterfs动态生成pv
一.环境介绍 [root@k8s-m ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4 ...
- 洛谷 P1032 子串变换
题目链接 https://www.luogu.org/problemnew/show/P1032 本题是一道bfs问题,从a串开始,每一步完成替换一对字符串(但是一个一步替换可以将这对字符串替换好几次 ...