当前位置:网站首页>365天挑战LeetCode1000题——Day 037 元素和小于等于阈值的正方形的最大边长 + 满足条件的子序列数目
365天挑战LeetCode1000题——Day 037 元素和小于等于阈值的正方形的最大边长 + 满足条件的子序列数目
2022-07-29 05:08:00 【ShowM3TheCode】
1292. 元素和小于等于阈值的正方形的最大边长
这题需要用到前缀和,官方的题解讲得不错
代码实现(部分看题解)
class Solution {
private:
bool validate(vector<vector<int>>& _mat, int threshold,
int x1, int y1, int x2, int y2) {
return _mat[x2][y2] - _mat[x1 - 1][y2] - _mat[x2][y1 - 1]
+ _mat[x1 - 1][y1 - 1] <= threshold;
}
public:
int maxSideLength(vector<vector<int>>& mat, int threshold) {
int m = mat.size();
int n = mat[0].size();
vector<vector<int>> _mat(m + 1, vector<int>(n + 1));
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++)
_mat[i][j] = _mat[i - 1][j] + _mat[i][j - 1] -
_mat[i - 1][j - 1] + mat[i - 1][j - 1];
}
int l = 0;
int r = min(m, n);
while (l < r) {
int mid = (l + r + 1) >> 1;
bool flag = false;
for (int i = 1; i <= m - mid + 1; i++) {
for (int j = 1; j <= n - mid + 1; j++) {
if (validate(_mat, threshold, i, j, i + mid - 1,
j + mid - 1)) {
flag = true;
break;
}
}
if (flag) break;
}
if (flag) l = mid;
else r = mid - 1;
}
return l;
}
};
1498. 满足条件的子序列数目
这题对于子序列数目需要做一个预处理,否则会超过上限
代码实现(部分看题解)
class Solution {
private:
static constexpr int P = int(1E9) + 7;
static constexpr int MAX_N = int(1E5) + 5;
void pretreatment(vector<int>& po) {
int f = 1;
po.push_back(f);
for (int i = 1; i < MAX_N; i++) {
f = f * 2 % P;
po.push_back(f);
}
}
public:
int numSubseq(vector<int>& nums, int target) {
vector<int> po;
pretreatment(po);
sort(nums.begin(), nums.end());
int n = nums.size();
long long ans = 0;
for (int i = 0; i < n; i++) {
if (2 * nums[i] > target) break;
int t = target - nums[i];
int bound = upper_bound(nums.begin() + i, nums.end(), t) -
nums.begin();
ans += po[bound - i - 1];
}
return ans % P;
}
};
边栏推荐
- On AspectJ framework
- MFC集成qt验证及问题处理
- Mysql多对多关系,分组拼接把多个数据查询到一条数据上
- JS (in ES6) sync & await understanding
- 深度学习刷SOTA的一堆trick
- How to add a map to the legendary server
- Lenovo Savior r7000+ add ssd+ copy and partition the information of the original D disk to the new SSD
- js(forEach)出现return无法结束函数的解决方法
- MySQL many to many relationship, grouping and splicing to query multiple data to one data
- ODOO开发教程之透视表
猜你喜欢
C语言用指向指针的指针对n个整数排序
How to add a map to the legendary server
[untitled]
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
Scikit learn -- steps and understanding of machine learning application development
最新坦克大战2022-全程开发笔记-1
QtCreator+CMake编译器设置
深度学习刷SOTA的一堆trick
Deep learning brush a bunch of tricks of SOTA
JS (foreach) return cannot end the function solution
随机推荐
The latest tank battle 2022 - Notes on the whole development -2
Apache POI实现Excel导入读取数据和写入数据并导出
Deadlock analysis using jstack, jconsole, and jvisualvm
The representation of time series analysis: is the era of learning coming?
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
ARFoundation从零开始3-创建ARFoundation项目
Teardown 解除时间限制的方法
Self join and joint query of MySQL
Visual Basic .Net 如何获取命令参数
Pytorch learning notes
OCCT学习003-----MFC单文档工程
学习数据库的第一个程序
2021-11-02
Functions in MySQL statements
About the configuration and use of thymeleaf
Lenovo Savior r7000+ add ssd+ copy and partition the information of the original D disk to the new SSD
Mysql的自连接和联合查询
Scikit learn -- steps and understanding of machine learning application development
Adb常用命令列表
[from_bilibili_DR_CAN][【Advanced控制理论】9_状态观测器设计][学习记录]