当前位置:网站首页>365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions
365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions
2022-07-29 05:25:00 【ShowM3TheCode】
List of articles
1292. The maximum side length of an element and a square less than or equal to the threshold

This question needs to use The prefix and , The official explanation of the question is very good
Code implementation ( Part depends on the solution )
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. The number of subsequences satisfying the condition

This problem requires a pretreatment for the number of subsequences , Otherwise, the upper limit will be exceeded
Code implementation ( Part depends on the solution )
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;
}
};
边栏推荐
- About realizing page Jump of website in Servlet
- CMU15-213 Shell Lab实验记录
- Thousands of databases, physical machines all over the country, JD logistics full volume cloud live record | interview with excellent technical team
- 直播预告|如何通过“智能边缘安全”提升企业免疫力?
- In depth analysis of common cross end technology stacks of app
- 365天挑战LeetCode1000题——Day 039 完全二叉树插入器 + 寻找峰值 II + 快照数组
- [from_bilibili_DR_CAN][【Advanced控制理论】9_状态观测器设计][学习记录]
- 如视技术副总裁杨永林:当传统产业遇到“数字空间”
- 7.3-function-templates
- 法线可视化
猜你喜欢

321, Jingdong Yanxi × Nlpcc 2022 challenge starts!

平行云CEO 李岩:CloudXR ,开启通往元宇宙的通道

最新坦克大战2022-全程开发笔记-2

Qt版的贪食蛇游戏项目

来!看排名一年上升16位的ClickHouse,如何在京东落地实践

Live broadcast preview | how to save 30% labor cost and shorten 80% trademark processing cycle?

CMU15-213 Shell Lab实验记录

2022年泰迪杯数据挖掘挑战赛C题方案及赛后总结

Adb常用命令列表

365天挑战LeetCode1000题——Day 041 二分查找完结纪念 + 第 N 个神奇数字 + 在线选举
随机推荐
How to get command parameters in Visual Basic.Net
直播预告:京东云DevOps与JFrog制品库的融合
365天挑战LeetCode1000题——Day 035 每日一题 + 二分查找 13
Li Yan, CEO of parallel cloud: cloudxr, opens the channel to the metauniverse
QT learning: qdropevent drag event
Unity3D - 物体太远看不见的问题
2022年泰迪杯数据挖掘挑战赛C题方案及赛后总结
源码编译pytorch坑
最新坦克大战2022-全程开发笔记-2
7.2-function-overloading
During the appointment, the 2022 JD cloud industrial integration new product launch was launched online
321, Jingdong Yanxi × Nlpcc 2022 challenge starts!
C语言数组入门到精通(数组精讲)
JD cloud golden autumn cloud special offer is in progress! Code scanning participation activities
基于注解的三层项目的改造及添加包扫描的方式
QT学习:使用JSON/XML等非ts文件实现多语言国际化
Button for QT custom switch effect
CMU15-213 Malloc Lab实验记录
来!看排名一年上升16位的ClickHouse,如何在京东落地实践
The latest tank battle 2022 - full development notes-3