当前位置:网站首页>365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序
365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序
2022-07-29 05:08:00 【ShowM3TheCode】
814. 二叉树剪枝

代码实现(自解)
类似DFS,自底向上地解决问题
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
if (!root) return NULL;
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
if (!root->left && !root->right && !root->val) root = NULL;
return root;
}
};
1508. 子数组和排序后的区间和

代码实现(自解)
利用最小堆求解
class Solution {
public:
int rangeSum(vector<int>& nums, int n, int left, int right) {
priority_queue<int, vector<int>, greater<int>> pq;
for (const auto num : nums) {
pq.push(num);
}
int sum;
for (int i = 0; i < nums.size(); i++) {
sum = nums[i];
for (int j = i + 1; j < nums.size(); j++) {
sum += nums[j];
pq.push(sum);
}
}
for (int i = 1; i < left; i++) {
pq.pop();
}
long long ans = 0;
for (int i = 1; i <= right - left + 1; i++) {
ans += pq.top();
pq.pop();
}
return ans % 1000000007;
}
};
1574. 删除最短的子数组使剩余数组有序

代码实现(部分看题解)
找到左边和右边的有序数组,那么中间剩下的,肯定不有序,是肯定要删除的,然后对每个左边的有序数组中的数,二分查找它在右边的位置,已保证二者合起来之后还是有序的
class Solution {
public:
int findLengthOfShortestSubarray(vector<int>& arr) {
int n = arr.size();
int l = 0, r = n - 1;
while (l < n - 1 && arr[l] <= arr[l + 1]) l++;
while (r >= 1 && arr[r - 1] <= arr[r]) r--;
if (r == 0) return 0;
int ans = r;
for (int i = 0; i <= l; i++) {
int bound = lower_bound(arr.begin() + r, arr.end(), arr[i]) -
arr.begin();
ans = min(ans, bound - 1 - i);
}
return ans;
}
};
边栏推荐
- Database course design of online assistant teaching platform for high school chemistry
- C language handwritten qq-ai version
- Unity metaverse (III), protobuf & socket realize multi person online
- 基于注解的三层项目的改造及添加包扫描的方式
- About the configuration and use of thymeleaf
- Qt版的贪食蛇游戏项目
- 365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球
- CryEngine5 Shader调试
- Mysql语句中的函数
- Arfoundation starts from scratch 8-geospatial API (geospatial) development
猜你喜欢

数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录

向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!

OCCT学习003-----MFC单文档工程

C语言用指向指针的指针对n个整数排序

Modification of annotation based three-tier project and the way of adding package scanning

研发效能生态完整图谱&DevOps工具选型必看

01-01-osg GL3 环境搭建

CMU15-213 Malloc Lab实验记录

Jackson parsing JSON detailed tutorial

ARFoundation从零开始8-Geospatial API(地理空间)开发
随机推荐
Google GTEST event mechanism
Database course design of online assistant teaching platform for high school chemistry
Diagram of odoo development tutorial
Rimworld通过SteamCMD上传创意工坊的方法
APP常用跨端技术栈深入分析
365天挑战LeetCode1000题——Day 038 公交站间的距离 + 基于时间的键值存储 + 转变数组后最接近目标值的数组和 + 有界数组中指定下标处的最大值
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
为啥谷歌的内部工具不适合你?
Jackson parsing JSON detailed tutorial
TMUX essays
TCP three handshakes and four waves
365天挑战LeetCode1000题——Day 041 二分查找完结纪念 + 第 N 个神奇数字 + 在线选举
osgSimplegl3例子分析
Deep learning brush a bunch of tricks of SOTA
Handwritten student management system
传奇开区网站如何添加流量统计代码
ARFoundation从零开始3-创建ARFoundation项目
一文带你搞懂环绕通知@Around与最终通知@After的实现
研发效能|Kubernetes核心技术剖析和DevOps落地经验
CryEngine3 调试Shader方法