LeetCode 第 15 场双周赛
下降和不能只保留原数组中最小的两个,hacked.
1287.有序数组中出现次数超过25%的元素
给你一个非递减的 有序 整数数组,已知这个数组中恰好有一个整数,它的出现次数超过数组元素总数的 25%。
请你找到并返回这个整数
示例:
**输入:** arr = [1,2,2,6,6,6,6,7,10]
**输出:** 6
提示 Hint
提示:
1 <= arr.length <= 10^4
0 <= arr[i] <= 10^5
代码
class Solution {
public:
int findSpecialInteger(vector<int>& arr) {
int n = arr.size(),val = arr[0];
pair<int,int>ans({0,0});
for(int i = 0,c = 0;i < n;++i){
if(arr[i] == val) c++,ans=max(ans,make_pair(c,val));
else c = 1,val = arr[i];
}
return ans.second;
}
};
1288.删除被覆盖区间
[1288.删除被覆盖区间](https://leetcode-cn.com/problems/Remove Covered Intervals)
给你一个区间列表,请你删除列表中被其他区间所覆盖的区间。
只有当 c <= a
且 b <= d
时,我们才认为区间 [a,b)
被区间 [c,d)
覆盖。
在完成所有删除操作后,请你返回列表中剩余区间的数目。
示例:
**输入:** intervals = [[1,4],[3,6],[2,8]]
**输出:** 2
**解释:** 区间 [3,6] 被区间 [2,8] 覆盖,所以它被删除了。
提示 Hint
提示:
1 <= intervals.length <= 1000
0 <= intervals[i][0] < intervals[i][1] <= 10^5
- 对于所有的
i != j
:intervals[i] != intervals[j]
代码
class Solution {
typedef pair<int, int>PII;
public:
static bool cmp(vector<int>& a, vector<int>& b) {
return (a[0] != b[0]) ? (a[0] < b[0]) : (a[1] > b[1]);
}
int removeCoveredIntervals(vector<vector<int>>& intervals) {
const int n = intervals.size();
sort(intervals.begin(), intervals.end(), cmp);
int l = intervals[0][1], ans = 1;
for(int i = 1; i < n; ++i) {
if(intervals[i][1] > l)
ans++, l = intervals[i][1];
}
return ans;
}
};
1286.字母组合迭代器
请你设计一个迭代器类,包括以下内容:
- 一个构造函数,输入参数包括:一个 **有序且字符唯一 **的字符串
characters
(该字符串只包含小写英文字母)和一个数字combinationLength
。 - 函数 _next() _,按 **字典序 **返回长度为
combinationLength
的下一个字母组合。 - 函数 _hasNext() _,只有存在长度为
combinationLength
的下一个字母组合时,才返回True
;否则,返回False
。
示例:
CombinationIterator iterator = new CombinationIterator("abc", 2); // 创建迭代器 iterator
iterator.next(); // 返回 "ab"
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 "ac"
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 "bc"
iterator.hasNext(); // 返回 false
提示 Hint
提示:
1 <= combinationLength <= characters.length <= 15
- 每组测试数据最多包含
10^4
次函数调用。 - 题目保证每次调用函数
next
时都存在下一个字母组合。
代码
class CombinationIterator {
public:
string characters;
string cur;
int combinationLength;
bool first;
CombinationIterator(string characters, int combinationLength) {
this->characters = characters;
this->combinationLength = combinationLength;
this->cur = characters.substr(0, combinationLength);
this->first = true;
}
string next() {
if(first) {
first = false;
return cur;
}
vector<int>pos;
for(int i = 0; i < combinationLength; i++) {
pos.push_back(characters.find_first_of(cur[i]));
}
for(int i = pos.size() - 1; i >= 0; --i) {
if((i == pos.size() - 1 && pos[i] < characters.length() - 1)
|| (i < pos.size() - 1 && pos[i] + 1 != pos[i + 1])) {
shiftPos(pos, i);
break;
}
}
cur = "";
for(int i = 0, sz = pos.size(); i < sz; ++i)
cur += characters[pos[i]];
return cur;
}
void shiftPos(vector<int>&pos, int i) {
pos[i]++;
for(int j = i + 1; j < combinationLength; ++j)
pos[j] = pos[j - 1] + 1;
}
bool hasNext() {
vector<int>pos;
for(int i = 0; i < combinationLength; i++) {
pos.push_back(characters.find_first_of(cur[i]));
}
//for(auto i : pos) cout << i << " "; cout << endl;
if(pos[0] == characters.length() - combinationLength)
return false;
return true;
}
};
/**
* Your CombinationIterator object will be instantiated and called as such:
* CombinationIterator* obj = new CombinationIterator(characters, combinationLength);
* string param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/
1289.下降路径最小和 II
给你一个整数方阵 arr
,定义「非零偏移下降路径」为:从 arr
数组中的每一行选择一个数字,且按顺序选出来的数字中,相邻数字不在原数组的同一列。
请你返回非零偏移下降路径数字和的最小值。
样例输入与样例输出 Sample Input and Sample Output
示例 1:
**输入:** arr = [[1,2,3],[4,5,6],[7,8,9]]
**输出:** 13
**解释:**
所有非零偏移下降路径包括:
[1,5,9], [1,5,7], [1,6,7], [1,6,8],
[2,4,8], [2,4,9], [2,6,7], [2,6,8],
[3,4,8], [3,4,9], [3,5,7], [3,5,9]
下降路径中数字和最小的是 [1,5,7] ,所以答案是 13 。
提示 Hint
提示:
1 <= arr.length == arr[i].length <= 200
-99 <= arr[i][j] <= 99
代码
class Solution {
public:
static const int inf = 0x3f3f3f3f;
int minFallingPathSum(vector<vector<int>>& arr) {
int n = arr.size(), m = arr[0].size();
int s[n][m];
for(int j = 0; j < m; ++j)
s[0][j] = arr[0][j];
for(int i = 1; i < n; ++i) {
for(int j = 0; j < m; ++j) {
s[i][j] = inf;
for(int k = 0; k < m; ++k) {
if(k == j) continue;
s[i][j] = min(s[i][j], s[i - 1][k] + arr[i][j]);
}
}
}
int ans = inf;
for(int i = 0;i < m;++i) ans = min(ans,s[n-1][i]);
return ans;
}
};
LeetCode 第 15 场双周赛的更多相关文章
- LeetCode第8场双周赛(Java)
这次我只做对一题. 原因是题目返回值类型有误,写的是 String[] ,实际上应该返回 List<String> . 好吧,只能自认倒霉.就当涨涨经验. 5068. 前后拼接 解题思路 ...
- Java实现 LeetCode第30场双周赛 (题号5177,5445,5446,5447)
这套题不算难,但是因为是昨天晚上太晚了,好久没有大晚上写过代码了,有点不适应,今天上午一看还是挺简单的 5177. 转变日期格式 给你一个字符串 date ,它的格式为 Day Month Yea ...
- LeetCode 第 14 场双周赛
基础的 api 还是不够熟悉啊 5112. 十六进制魔术数字 class Solution { public: char *lltoa(long long num, char *str, int ra ...
- LeetCode第29场双周赛题解
第一题 用一个新数组newSalary保存去掉最低和最高工资的工资列表,然后遍历newSalary,计算总和,除以元素个数,就得到了平均值. class Solution { public: doub ...
- leetcode-第11场双周赛-5089-安排会议日程
题目描述: 自己的提交: class Solution: def minAvailableDuration(self, slots1: List[List[int]], slots2: List[Li ...
- leetcode-第11场双周赛-5088-等差数列中缺失的数字
题目描述: 自己的提交: class Solution: def missingNumber(self, arr: List[int]) -> int: if len(arr) == 2: re ...
- leetcode-第五场双周赛-1134-阿姆斯特朗数
第一次提交: class Solution: def isArmstrong(self, N: int) -> bool: n = N l = len(str(N)) res = 0 while ...
- leetcode-第五场双周赛-1133-最大唯一数
第一次提交: class Solution: def largestUniqueNumber(self, A: List[int]) -> int: dict = {} for i in A: ...
- LeetCode 第 165 场周赛
LeetCode 第 165 场周赛 5275. 找出井字棋的获胜者 5276. 不浪费原料的汉堡制作方案 5277. 统计全为 1 的正方形子矩阵 5278. 分割回文串 III C 暴力做的,只能 ...
随机推荐
- UOJ269. 【清华集训2016】如何优雅地求和 [生成函数]
传送门 思路 神仙题.jpg 脑子一抽,想把\(f(x)\)表示成下降幂的形式,也就是 \[ f(x)=\sum_{i=0}^m f_ix_{(i)}\\ x_{(i)}=\prod_{k=0}^{i ...
- Android Jenkins 自动化打包构建
前言 在测试app项目过程中,通常都是需要开发打测试包给到测试,但是无论是iOS还是Android的打包过程都是相当漫长的,频繁的回归测试需要频繁的打包,对于开发同学影响还是蛮大的.因此在这种情况下, ...
- MySQL数据分析-(1) 数据库前言
(一)开场白 大家好,欢迎大家跟我一起学习<MySQL数据分析实战>这门课程,对于数据分析师来说,数据库是每一个从业者都必须掌握的课程,我们这门课是从实战的角度出发,我会帮助大家梳理MyS ...
- input上传mp3格式文件,预览并且获取时间
<input type="file" id="file" name="file" class="upfile" o ...
- 2015 ACM Arabella Collegiate Programming Contest
题目链接:https://vjudge.net/contest/154238#overview. ABCDE都是水题. F题,一开始分类讨论,结果似乎写挫了,WA了一发.果断换并查集上,A了. G题, ...
- JAVA基础知识|HTTP协议-两个特性
一.无连接 无连接:服务器与浏览器之间的一次连接只处理一个http请求,请求处理结束后,连接断开.下一次请求再重新建立连接. 然而随着互联网的发展,一台服务器同一时间处理的请求越来越多,如果依然采用原 ...
- pwn学习日记Day8 基础知识积累
知识杂项 aslr:是一种针对缓冲区溢出的安全保护技术,通过对堆.栈.共享库映射等线性区布局的随机化,通过增加攻击者预测目的地址的难度,防止攻击者直接定位攻击代码位置,达到阻止溢出攻击的目的的一种技术 ...
- Linux发行版的选择
1,需要稳定的服务器,选择CentOS 或 RHEL 2,需要自己定制的桌面系统,选择Ubuntu 3,摸索linux 各方面的知识,选择Gentoo 4,需要稳定性高的系统,选择FreeBSD 5, ...
- 安装Chrome扩展程序xpath
最近工作用到xpath,直接从浏览器复制下来路径时常会出错而且长度很长,于是我想到之前用过的一款chrome插件,可以直接编写xpath语句,并实时出现解析出的结果,检验xpath语句是否编写正确.效 ...
- Ajax提交之后,Method从POST变成GET
https://developer.aliyun.com/ask/68268?spm=a2c6h.13159736 https://blog.csdn.net/uzizi/article/detail ...