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 ...
随机推荐
- 2017-2018-2 20165206 实验三 《敏捷开发与XP实践》实验报告
2017-2018-2 20165206 实验三 <敏捷开发与XP实践>实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:韩啸 学号:20165206 指导教师: ...
- PAT Basic 1065 单身狗
单身狗(25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue "单身狗"是中文对于单身人士的 ...
- 微信小程序如何自动弹出提示微信授权?
我想在一进入页面的时候就进行判断提示并且弹出提示授权!请问该如何处理比较合理 wx.authorize({}) //可以通过 wx.getSetting 先查询一下用户是否授权了 "scop ...
- haoi2018
题解: 题目相对其他省难一点 不过弱省省选知识点都这么集中的么.. 4道数学题... 1.[HAOI2018]奇怪的背包 这题考场做就gg了... 其实我想到了那个性质.. 就是这个一定要是gcd的倍 ...
- WPF数据爬取小工具-某宝推广位批量生成,及订单爬取 记:接单最痛一次的感悟
项目由来:上月闲来无事接到接到一个单子,自动登录 X宝平台,然后重定向到指定页面批量生成推广位信息:与此同时自动定时同步订单数据到需求提供方的Java服务. 当然期间遇到一个小小的问题就是界面样式的问 ...
- SSM整合——完全版
1, 2, 3, 4,项目建立好后: 覆盖pom.xml,地址在:https://blog.csdn.net/mark_lirenhe/article/details/80875266 alt+F5= ...
- union表关联模糊查询servlet,action方法
2018-11-14 servletxml层 public String getSql(String keyword) { StringBuffer sqlSb = new StringBuffer( ...
- supervisor 监控redis & mongodb
安装 安装python brew install python 安装pipwget https://bootstrap.pypa.io/get-pip.pysudo python get-pip.py ...
- ZOJ 3795 Grouping (强连通缩点+DP最长路)
<题目链接> 题目大意: n个人,m条关系,每条关系a >= b,说明a,b之间是可比较的,如果还有b >= c,则说明b,c之间,a,c之间都是可以比较的.问至少需要多少个集 ...
- 李宏毅机器学习笔记6:Why deep、Semi-supervised
李宏毅老师的机器学习课程和吴恩达老师的机器学习课程都是都是ML和DL非常好的入门资料,在YouTube.网易云课堂.B站都能观看到相应的课程视频,接下来这一系列的博客我都将记录老师上课的笔记以及自己对 ...