当前位置:网站首页>Leetcode-307: region and retrieval - array modifiable
Leetcode-307: region and retrieval - array modifiable
2022-07-22 13:33:00 【Chrysanthemum headed bat】
leetcode-307: Area and retrieval - Array can be modified
subject
Give you an array nums , Please complete two types of queries .
- One type of query requirements to update Array nums The value of the subscript
- Another type of query requires the return of an array nums Middle index left And index right Between ( contain ) Of nums Elemental and , among left <= right
Realization NumArray class :
- NumArray(int[] nums) Use an array of integers nums Initialize object
- void update(int index, int val) take nums[index] Value to update by val
- int sumRange(int left, int right) Returns an array of nums Middle index left And index right Between ( contain ) Of nums Elemental and ( namely ,nums[left] + nums[left + 1], …, nums[right])
Example 1:
Input :
["NumArray", "sumRange", "update", "sumRange"]
[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]
Output :
[null, 9, null, 8]
explain :
NumArray numArray = new NumArray([1, 3, 5]);
numArray.sumRange(0, 2); // return 1 + 3 + 5 = 9
numArray.update(1, 2); // nums = [1,2,5]
numArray.sumRange(0, 2); // return 1 + 2 + 5 = 8
Problem solving
Method 1 : Line segment tree
struct Node{
Node* left;
Node* right;
int val;
int add;
};
class NumArray {
public:
Node* root=new Node();
int N;
NumArray(vector<int>& nums) {
N=nums.size()-1;
for(int i=0;i<=N;i++){
update(root,0,N,i,i,nums[i]);
}
}
void update(int index, int val) {
update(root,0,N,index,index,val);
}
int sumRange(int left, int right) {
return query(root,0,N,left,right);
}
private:
void update(Node* node,int start,int end,int l,int r,int val){
if(l<=start&&end<=r){
node->val=(end-start+1)*val;
node->add=val;
return;
}
int mid=(start+end)>>1;
pushDown(node,mid-start+1,end-mid);
if(l<=mid) update(node->left,start,mid,l,r,val);
if(r>mid) update(node->right,mid+1,end,l,r,val);
pushUp(node);
}
int query(Node* node,int start,int end,int l,int r){
if(l<=start&&end<=r) return node->val;
int mid=(start+end)>>1;
int res=0;
pushDown(node,mid-start+1,end-mid);
if(l<=mid) res+=query(node->left,start,mid,l,r);
if(r>mid) res+=query(node->right,mid+1,end,l,r);
return res;
}
void pushUp(Node* node){
node->val=node->left->val+node->right->val;
}
void pushDown(Node* node,int leftNum,int rightNum){
if(node->left==nullptr) node->left=new Node();
if(node->right==nullptr) node->right=new Node();
if(node->add==0) return;
node->left->val=node->add*leftNum;
node->right->val=node->add*rightNum;
node->left->add=node->add;
node->right->add=node->add;
node->add=0;
}
};
边栏推荐
- What if Lao Xue's host disk space is full
- Talk about the top 10 mistakes often made in implementing data governance
- The LAAS solution of elephant swap has risen rapidly and built a new defi2.0 protocol
- [有趣] VS Code -- Live Server的一小段代码注入
- Leetcode-6113: the smallest number in an infinite set
- PCBA design of body fat scale
- 《性能之巅第2版》阅读笔记(五)--Disks监测
- Easy to use task queue beanstalkd
- ideal关于 log显示问号且双击无法打不开的解决方法
- 2022 ranking list of database audit products - must see!
猜你喜欢
Use the browser plug-in to run JS to delete the "disable copy" function of a specific website
Itop-rk3568 development board Debian system function test - wired network test
【C】信息管理系统/通讯录通用模板(介绍静态、动态、文件三个版本)
Privacy-Preserving Generative Deep Neural Networks Support Clinical Data Sharing
Some problems encountered in creating and using custom plug-ins in Qt5
Leetcode - zj - future04: distribution des marchandises en magasin
ApacheCon Asia 2022 开启报名:Pulsar 技术议题重磅亮相
One bite of Stream(8)
leetcode-6117:坐上公交的最晚时间
(iclr-2021) an image is equivalent to 16x16 words: a transformer for large-scale image recognition
随机推荐
解析云原生消息流系统 Apache Pulsar 能力及场景
[waiting for insurance] what does waiting for insurance rectification mean? What are the rectification contents?
ERP系统在元器件贸易企业中的应用
Go list modify element value
在Mysql中为什么定义varchar(255)?
LeetCode_动态规划_困难_44.通配符匹配
224. Basic calculator - recursive method
WebService interface test
【PostgreSQL 15】PostgreSQL 15对UNIQUE和NULL的改进
Addition, deletion, query and modification of [mysql] table (basic)
leetcode-2337:移动片段得到字符串
Leetcode-2337: move the fragment to get the string
[QNX hypervisor 2.2 user manual]8.7 virtual i/o (virtio)
Messy analysis view system
[有趣] VS Code -- Live Server的一小段代码注入
2022/07/20--- convert string to integer; Maximum value of sliding window
ideal关于 log显示问号且双击无法打不开的解决方法
稀土开发者大会|StreamNative 翟佳、刘德志分享云原生技术变革之路
C语言动态内存管理
Some methods of arrays and objects