LeetCode Weekly Contest 32
581. Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.
You need to find the shortest such subarray and output its length.
Input: [2, 6, 4, 8, 10, 9, 15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
排好序,然后对比一下就行了
class Solution {
public:
int findUnsortedSubarray(vector<int>& nums) {
int n=nums.size();
vector<int> nums2;
nums2.insert(nums2.end(),nums.begin(),nums.end());
sort(nums2.begin(),nums2.end());
int st=,ed=n-;
while(st!=n&&nums2[st]==nums[st]) st++;
while(ed!=-&&nums2[ed]==nums[ed]) ed--;
if(st==n) return ;
return ed-st+;
} };
582. Kill Process
删除一个树上的节点,求出所有子节点编号
真的很生疏了,树都不知道怎么遍历了
由于可能编号开的很大,因此用map作下映射,不说数据范围真坑,一直TLE(leetcode有TLE?),最后改了下数组大小AC了
Input:
pid = [1, 3, 10, 5]
ppid = [3, 0, 5, 3]
kill = 5
Output: [5,10]
Explanation:
3
/ \
1 5
/
10
Kill 5 will also kill 10.
class Solution {
public:
vector<int> ans;
map<int,int> mp;
map<int,int> fmp;
int tot;
vector<int> g[];
void dfs(int i){
ans.push_back(fmp[i]);
for(int j=;j<g[i].size();j++){
dfs(g[i][j]);
}
}
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
int n=pid.size();
int i,st;
tot=;
for(i=;i<n;i++){
if(!mp[ppid[i]]){
mp[ppid[i]]=tot;
fmp[tot]=ppid[i];
tot++;
}
if(!mp[pid[i]]){
mp[pid[i]]=tot;
fmp[tot]=pid[i];
tot++;
}
int u=mp[ppid[i]],v=mp[pid[i]];
g[u].push_back(v);
}
dfs(mp[kill]);
return ans; }
};
583. Delete Operation for Two Strings
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.
Input: "sea", "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
求下最长公共序列,注意是序列不是子串,因为对于abcab,abab这样的情况,公共子串就无法解决,求完之后减一下就可以了
class Solution {
public:
int Max(int a,int b)
{
return (a>b)?a:b;
}
int creatDp(string s1,string s2,int *dp)
{
int len1 = s1.length();
int len2 = s2.length();
//int *dp = new int[len1*len2];
//先求出第一行
for(int j = ;j<len2;j++)
{
if(s1[] == s2[j]){
*(dp+*len2+j) = ;
for(;j<len2;j++)
*(dp+*len2+j) = ;
break;
}
else
*(dp+*len2+j) = ;
}
//然后求第一列
for(int i = ;i<len1;i++)
{
if(s1[i] == s2[]){
*(dp+i*len2+) = ;
for(;i<len1;i++)
*(dp+i*len2+) = ;
break;
}
else
*(dp+i*len2+) = ;
}
//求其他的数据
for(int i =;i<len1;i++)
{
for(int j =;j<len2;j++)
{
if(s1[i] == s2[j])
*(dp+i*len2+j) = *(dp+(i-)*len2+(j-))+;
else
*(dp+i*len2+j) = Max(*(dp+(i-)*len2+j),*(dp+i*len2+j-));
}
}
return dp[len1*len2-];
}
int minDistance(string word1, string word2) {
int len1 = word1.length();
int len2 = word2.length();
int *dp = new int[len1*len2];
int w=creatDp(word1, word2,dp);
return len1-w+len2-w;
}
};
587. Erect the Fence
裸凸包问题,找了几个模板都没套进去,重载操作符没法用,23333,leetcode真坑
LeetCode Weekly Contest 32的更多相关文章
- 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 ...
随机推荐
- appium 手势
1.2 appium玩转安卓手机 智能手机发展到今天,形成了一整套有关手势操作的操作习惯,如手指左右上下滑动,及双指缩放,还有手指的滑动解锁,摇晃手机等动作.那么我们怎么在python中利用appiu ...
- java/springboot自定义注解实现AOP
java注解 即是注释了,百度解释:也叫元数据.一种代码级别的说明. 个人理解:就是内容可以被代码理解的注释,一般是一个类. 元数据 也叫元注解,是放在被定义的一个注解类的前面 ,是对注解一种限制. ...
- RFC2119:RFC协议动词含义
协议地址:http://www.ietf.org/rfc/rfc2119.txt MUST 必须的.通过它描述的对象,是强制要求的.它与REQUIRED和SHALL含义相同. MUST NOT 不允许 ...
- 页面注册系统--使用forms表单结合ajax
页面注册系统--使用forms表单结合ajax 在Django中通过forms构建一个表单 1.urls.py 配置路由 from django.conf.urls import url from d ...
- Oracle数据库查看表空间是否为自增的
表空间是有数据文件组成的,所以看表空间是否自增即看数据文件,如下查自增的表空间: select tablespace_name,file_name,autoextensible from dba_da ...
- nginx proxy_pass指令’/’注意事项
1. proxy_pass配置说明 不带/ location /test/ { proxy_pass http://t6:8300; } 带/ location /test/ { proxy_pass ...
- Java基础总结01:JDK与JRE概述
1)JRE(Java Runtime Environment,Java运行时环境) 包括Java虚拟机(JVM Java Virtual Machine)和Java程序所需的核心类库等,如果想要运行一 ...
- CSS选择器 + Xpath + 正则表达式整理(有空再整理)
选择器 例子 例子描述 CSS .class .intro 选择 class="intro" 的所有元素. 1 #id #firstname 选择 id="firstna ...
- 总结几种清除浏览器的缓存,适用于明明已经修改bug,但是测试人员说问题还存在的情况下
1.meta方法<METAHTTP-EQUIV="pragma"CONTENT="no-cache"><METAHTTP-EQUIV=&quo ...
- pyqt pyside QAction 代码中触发
pyqt pyside QAction 代码中触发 一般如果多个QAction(在同一个 QButtonGroup 中,而且是 checkable 的),不同情况下我们希望其中某个默认checked: ...