1287.有序数组中出现次数超过25%的元素

1288.删除被覆盖区间

1286.字母组合迭代器

1289.下降路径最小和 II

下降和不能只保留原数组中最小的两个,hacked.

1287.有序数组中出现次数超过25%的元素

1287.有序数组中出现次数超过25%的元素

给你一个非递减的 有序 整数数组,已知这个数组中恰好有一个整数,它的出现次数超过数组元素总数的 25%。

请你找到并返回这个整数

示例:

  1. **输入:** arr = [1,2,2,6,6,6,6,7,10]
  2. **输出:** 6

提示 Hint

提示:

  • 1 <= arr.length <= 10^4
  • 0 <= arr[i] <= 10^5

代码

  1. class Solution {
  2. public:
  3. int findSpecialInteger(vector<int>& arr) {
  4. int n = arr.size(),val = arr[0];
  5. pair<int,int>ans({0,0});
  6. for(int i = 0,c = 0;i < n;++i){
  7. if(arr[i] == val) c++,ans=max(ans,make_pair(c,val));
  8. else c = 1,val = arr[i];
  9. }
  10. return ans.second;
  11. }
  12. };

1288.删除被覆盖区间

[1288.删除被覆盖区间](https://leetcode-cn.com/problems/Remove Covered Intervals)

给你一个区间列表,请你删除列表中被其他区间所覆盖的区间。

只有当 c <= ab <= d 时,我们才认为区间 [a,b) 被区间 [c,d) 覆盖。

在完成所有删除操作后,请你返回列表中剩余区间的数目。

示例:

  1. **输入:** intervals = [[1,4],[3,6],[2,8]]
  2. **输出:** 2
  3. **解释:** 区间 [3,6] 被区间 [2,8] 覆盖,所以它被删除了。

提示 Hint

提示: ​​​​​​

  • 1 <= intervals.length <= 1000
  • 0 <= intervals[i][0] < intervals[i][1] <= 10^5
  • 对于所有的 i != jintervals[i] != intervals[j]

代码

  1. class Solution {
  2. typedef pair<int, int>PII;
  3. public:
  4. static bool cmp(vector<int>& a, vector<int>& b) {
  5. return (a[0] != b[0]) ? (a[0] < b[0]) : (a[1] > b[1]);
  6. }
  7. int removeCoveredIntervals(vector<vector<int>>& intervals) {
  8. const int n = intervals.size();
  9. sort(intervals.begin(), intervals.end(), cmp);
  10. int l = intervals[0][1], ans = 1;
  11. for(int i = 1; i < n; ++i) {
  12. if(intervals[i][1] > l)
  13. ans++, l = intervals[i][1];
  14. }
  15. return ans;
  16. }
  17. };

1286.字母组合迭代器

1286.字母组合迭代器

请你设计一个迭代器类,包括以下内容:

  • 一个构造函数,输入参数包括:一个 **有序且字符唯一 **的字符串 characters(该字符串只包含小写英文字母)和一个数字 combinationLength
  • 函数 _next() _,按 **字典序 **返回长度为 combinationLength 的下一个字母组合。
  • 函数 _hasNext() _,只有存在长度为 combinationLength 的下一个字母组合时,才返回 True;否则,返回 False

示例:

  1. CombinationIterator iterator = new CombinationIterator("abc", 2); // 创建迭代器 iterator
  2. iterator.next(); // 返回 "ab"
  3. iterator.hasNext(); // 返回 true
  4. iterator.next(); // 返回 "ac"
  5. iterator.hasNext(); // 返回 true
  6. iterator.next(); // 返回 "bc"
  7. iterator.hasNext(); // 返回 false

提示 Hint

提示:

  • 1 <= combinationLength <= characters.length <= 15
  • 每组测试数据最多包含 10^4 次函数调用。
  • 题目保证每次调用函数 next 时都存在下一个字母组合。

代码

  1. class CombinationIterator {
  2. public:
  3. string characters;
  4. string cur;
  5. int combinationLength;
  6. bool first;
  7. CombinationIterator(string characters, int combinationLength) {
  8. this->characters = characters;
  9. this->combinationLength = combinationLength;
  10. this->cur = characters.substr(0, combinationLength);
  11. this->first = true;
  12. }
  13. string next() {
  14. if(first) {
  15. first = false;
  16. return cur;
  17. }
  18. vector<int>pos;
  19. for(int i = 0; i < combinationLength; i++) {
  20. pos.push_back(characters.find_first_of(cur[i]));
  21. }
  22. for(int i = pos.size() - 1; i >= 0; --i) {
  23. if((i == pos.size() - 1 && pos[i] < characters.length() - 1)
  24. || (i < pos.size() - 1 && pos[i] + 1 != pos[i + 1])) {
  25. shiftPos(pos, i);
  26. break;
  27. }
  28. }
  29. cur = "";
  30. for(int i = 0, sz = pos.size(); i < sz; ++i)
  31. cur += characters[pos[i]];
  32. return cur;
  33. }
  34. void shiftPos(vector<int>&pos, int i) {
  35. pos[i]++;
  36. for(int j = i + 1; j < combinationLength; ++j)
  37. pos[j] = pos[j - 1] + 1;
  38. }
  39. bool hasNext() {
  40. vector<int>pos;
  41. for(int i = 0; i < combinationLength; i++) {
  42. pos.push_back(characters.find_first_of(cur[i]));
  43. }
  44. //for(auto i : pos) cout << i << " "; cout << endl;
  45. if(pos[0] == characters.length() - combinationLength)
  46. return false;
  47. return true;
  48. }
  49. };
  50. /**
  51. * Your CombinationIterator object will be instantiated and called as such:
  52. * CombinationIterator* obj = new CombinationIterator(characters, combinationLength);
  53. * string param_1 = obj->next();
  54. * bool param_2 = obj->hasNext();
  55. */

1289.下降路径最小和 II

1289.下降路径最小和 II

给你一个整数方阵 arr ,定义「非零偏移下降路径」为:从 arr 数组中的每一行选择一个数字,且按顺序选出来的数字中,相邻数字不在原数组的同一列。

请你返回非零偏移下降路径数字和的最小值。

样例输入与样例输出 Sample Input and Sample Output

示例 1:

  1. **输入:** arr = [[1,2,3],[4,5,6],[7,8,9]]
  2. **输出:** 13
  3. **解释:**
  4. 所有非零偏移下降路径包括:
  5. [1,5,9], [1,5,7], [1,6,7], [1,6,8],
  6. [2,4,8], [2,4,9], [2,6,7], [2,6,8],
  7. [3,4,8], [3,4,9], [3,5,7], [3,5,9]
  8. 下降路径中数字和最小的是 [1,5,7] ,所以答案是 13

提示 Hint

提示:

  • 1 <= arr.length == arr[i].length <= 200
  • -99 <= arr[i][j] <= 99

代码

  1. class Solution {
  2. public:
  3. static const int inf = 0x3f3f3f3f;
  4. int minFallingPathSum(vector<vector<int>>& arr) {
  5. int n = arr.size(), m = arr[0].size();
  6. int s[n][m];
  7. for(int j = 0; j < m; ++j)
  8. s[0][j] = arr[0][j];
  9. for(int i = 1; i < n; ++i) {
  10. for(int j = 0; j < m; ++j) {
  11. s[i][j] = inf;
  12. for(int k = 0; k < m; ++k) {
  13. if(k == j) continue;
  14. s[i][j] = min(s[i][j], s[i - 1][k] + arr[i][j]);
  15. }
  16. }
  17. }
  18. int ans = inf;
  19. for(int i = 0;i < m;++i) ans = min(ans,s[n-1][i]);
  20. return ans;
  21. }
  22. };

LeetCode 第 15 场双周赛的更多相关文章

  1. LeetCode第8场双周赛(Java)

    这次我只做对一题. 原因是题目返回值类型有误,写的是 String[] ,实际上应该返回 List<String> . 好吧,只能自认倒霉.就当涨涨经验. 5068. 前后拼接 解题思路 ...

  2. Java实现 LeetCode第30场双周赛 (题号5177,5445,5446,5447)

    这套题不算难,但是因为是昨天晚上太晚了,好久没有大晚上写过代码了,有点不适应,今天上午一看还是挺简单的 5177. 转变日期格式   给你一个字符串 date ,它的格式为 Day Month Yea ...

  3. LeetCode 第 14 场双周赛

    基础的 api 还是不够熟悉啊 5112. 十六进制魔术数字 class Solution { public: char *lltoa(long long num, char *str, int ra ...

  4. LeetCode第29场双周赛题解

    第一题 用一个新数组newSalary保存去掉最低和最高工资的工资列表,然后遍历newSalary,计算总和,除以元素个数,就得到了平均值. class Solution { public: doub ...

  5. leetcode-第11场双周赛-5089-安排会议日程

    题目描述: 自己的提交: class Solution: def minAvailableDuration(self, slots1: List[List[int]], slots2: List[Li ...

  6. leetcode-第11场双周赛-5088-等差数列中缺失的数字

    题目描述: 自己的提交: class Solution: def missingNumber(self, arr: List[int]) -> int: if len(arr) == 2: re ...

  7. leetcode-第五场双周赛-1134-阿姆斯特朗数

    第一次提交: class Solution: def isArmstrong(self, N: int) -> bool: n = N l = len(str(N)) res = 0 while ...

  8. leetcode-第五场双周赛-1133-最大唯一数

    第一次提交: class Solution: def largestUniqueNumber(self, A: List[int]) -> int: dict = {} for i in A: ...

  9. LeetCode 第 165 场周赛

    LeetCode 第 165 场周赛 5275. 找出井字棋的获胜者 5276. 不浪费原料的汉堡制作方案 5277. 统计全为 1 的正方形子矩阵 5278. 分割回文串 III C 暴力做的,只能 ...

随机推荐

  1. [Luogu] 消息扩散

    https://www.luogu.org/problemnew/show/2002 Tarjan 缩点 + 入度判断 #include <iostream> #include <c ...

  2. Codevs 1768 种树 3(差分约束)

    1768 种树 3 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 为了绿化乡村,H村积极响应号召,开始种树了. H村里有n幢 ...

  3. 内存管理2-set方法的内存管理

    1.对象之间的内存管理: 每个学生都有一本书 book类 @price 学生类  @age @book -------------------- #import "book.h" ...

  4. python 识别图片中的汉字

    我们就识别上面的汉字. 安装软件tesseract和python库 https://www.cnblogs.com/sea-stream/p/10961580.html 然后新建一个文件夹test,把 ...

  5. 2015-2016 ACM ICPC Baltic Selection Contest

    这是上礼拜三的训练赛,以前做过一次,这次仅剩B题没补.题目链接:https://vjudge.net/contest/153192#overview. A题,水题. C题,树形DP,其实是一个贪心问题 ...

  6. 数据库安装后无法访问且mysql重启报错的解决方法

    数据库安装后无法访问,mysql重启报错: 或报错:MySQL is running but PID file could not be found 解决方法: 第一种方法:看磁盘是否已满:df –h ...

  7. spring配置hibernate在使用oracle驱动时报错Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver '

    在看到这个错误的时候就感觉有点不对劲了,在错误的结尾和引号之间还有空间,如果敏锐的点应该察觉到可能是空格问题.由于本人的粗心导致这个问题一直困扰了我接近一个上午. 在排查这个问题的时候首先想到的就是关 ...

  8. linux内核中rtc框架选用什么接口来注册rtc设备呢?

    1. 有哪些接口? 1.1 devm_rtc_device_register 1.2  devm_rtc_allocate_device和 rtc_register_device 2. 1.1与1.2 ...

  9. 阿里内部分享:我们是如何?深度定制高性能MySQL的

    阿里云资深数据库工程师赵建伟在“云栖大会上海峰会”的分享.核心是阿里云的数据库服务和MySQL分支的深度定制实践分享. 阿里巴巴MySQL在全球都是有名的.不仅是因为其性能,还因为其是全世界少数拥有M ...

  10. Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.5:test

    解决方法: 打包跳过测试有两种方法 一是命令行 mvn clean package -Dmaven.test.skip=true 二是写入pom文件 <plugin> <groupI ...