1. Range Sum Query - Mutable

Given an integer array nums, find the sum of the elements between indices i and j (ij), inclusive.

The update(i, val) function modifies nums by updating the element at index i to val.

Example:

Given nums = [1, 3, 5]

sumRange(0, 2) -> 9
update(1, 2)
sumRange(0, 2) -> 8

Constraints:

  • The array is only modifiable by the update function.
  • You may assume the number of calls to update and sumRange function is distributed evenly.
  • 0 <= i <= j <= nums.length - 1

解法1 将查询区间的数字直接求和

class NumArray {
public:
vector<int>Array;
NumArray(vector<int>& nums) {
Array = nums;
} void update(int i, int val) {
Array[i] = val;
} int sumRange(int i, int j) {
int res = 0;
for(int k = i; k <= j; ++k)res += Array[k];
return res;
}
};

解法2 求和数组。先将数组的前n项和计算出来,更新的时候将前k项和(k>= i)更新即可

class NumArray {
public:
vector<int>S{0};
vector<int>Array;
NumArray(vector<int>& nums) {
Array = nums;
for(int i = 0; i < nums.size(); ++i){
S.push_back(S.back() + nums[i]);
}
} void update(int i, int val) {
int d = val - Array[i];
Array[i] = val;
for(int j = i + 1; j < S.size(); ++j)S[j] += d;
} int sumRange(int i, int j) {
return S[j+1] - S[i];
}
};

解法3 分块求和。解法2中update函数花费时间较多,更新的平均时间复杂度为\(O(n/2)\),为了控制更新的范围,将数组划分为多个块,更新控制在对应的块内,将块的尺寸取为\(\sqrt{n}\),更新的时间复杂度为\(O(\sqrt{n})\)

class NumArray {
public:
int block_size;
vector<int>Array;
vector<int>S;
NumArray(vector<int>& nums) {
Array = nums;
block_size = int(sqrt(nums.size()));
int sum = 0;
for(int i = 0; i < nums.size(); ++i){
sum += nums[i];
if((i+1) % block_size == 0 || i + 1 == nums.size()){
S.push_back(sum);
sum = 0;
}
}
} void update(int i, int val) {
S[i / block_size] += val - Array[i];
Array[i] = val;
} int sumRange(int i, int j) {
int res = 0;
int s_b = i / block_size, e_b = j / block_size;
if(s_b == e_b){
for(int k = i; k <= j; ++k)res += Array[k];
}
else{
for(int k = i; k < (s_b+1)*block_size; ++k)res += Array[k];
for(int b =s_b + 1; b < e_b; ++b)res += S[b];
for(int k = e_b*block_size; k <= j; ++k)res += Array[k];
}
return res;
}
};

解法4 线段树(不想看了。。。)

【刷题-LeetCode】307. Range Sum Query - Mutable的更多相关文章

  1. [LeetCode] 307. Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  2. leetcode@ [307] Range Sum Query - Mutable / 线段树模板

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  3. [LeetCode] 307. Range Sum Query - Mutable 解题思路

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  4. LeetCode - 307. Range Sum Query - Mutable

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  5. leetcode 307. Range Sum Query - Mutable(树状数组)

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  6. [Leetcode Week16]Range Sum Query - Mutable

    Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...

  7. 307. Range Sum Query - Mutable

    题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...

  8. leetcode 307 Range Sum Query

    问题描述:给定一序列,求任意区间(i, j)的元素和:修改任意一元素,实现快速更新 树状数组 树状数组的主要特点是生成一棵树,树的高度为logN.每一层的高度为k,分布在这一层的序列元素索引的二进制表 ...

  9. 【leetcode】307. Range Sum Query - Mutable

    题目如下: 解题思路:就三个字-线段树.这个题目是线段树用法最经典的场景. 代码如下: class NumArray(object): def __init__(self, nums): " ...

随机推荐

  1. CF938B Run For Your Prize 题解

    Content 有两个人,一个在 \(1\) 处,一个在 \(10^6\) 处,在他们之间有 \(n\) 个奖品,第 \(i\) 个奖品在 \(a_i\) 处.一开始在 \(1\) 处的人每秒可向右移 ...

  2. CF166A Rank List 题解

    Content 有 \(n\) 个元素,第 \(i\) 个元素包含两个值 \(a_i,b_i\),按照以下规则排序: 如果对于 \(i\neq j\) 有 \(a_i\neq a_j\),则按照 \( ...

  3. CF581B Luxurious Houses 题解

    Content 一条大街上有 \(n\) 个房子,第 \(i\) 个房子的楼层数量是 \(h_i\).如果一个房子的楼层数量大于位于其右侧的所有房屋,则房屋是豪华的.对于第 \(i\) 个房子,请求出 ...

  4. HTML标签一览

    html标签属性大全 嵌套的html窗口<iframe > <iframe src="https://www.baidu.com"></iframe& ...

  5. ☕【Java实战系列】「技术盲区」Double与Float的坑与解决办法以及BigDecimal的取而代之!

    探究背景 涉及诸如float或者double这两种浮点型数据的处理时,偶尔总会有一些怪怪的现象,不知道大家注意过没,举几个常见的栗子: 条件判断超预期 System.out.println( 1f = ...

  6. P7990-[USACO21DEC]Closest Cow Wins S【堆,贪心】

    正题 题目链接:https://www.luogu.com.cn/problem/P7990 题目大意 数轴上有\(k\)个点是草地,每个草地有不同收益,\(m\)个点是地方的点,现在你要放置\(n\ ...

  7. 1087 - Diablo

      1087 - Diablo    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 64 MB All of ...

  8. 快速登陆linux服务器

    前言 本文适用于喜欢原生终端的用户,钟爱第三方ssh客户端的可以无视....客户端可以保存用户信息和密码,比较无脑.mac可以使用终端,win可以使用git的bash. 上次分享了配置非对称秘钥免密登 ...

  9. IDEA 延长使用

    压缩包下载地址:https://i.cnblogs.com/files 1.先以试用的形式进入idea,然后help -> Edit Custom VM Options 2.插入 :-javaa ...

  10. 第二十五个知识点:使用特殊的素数定义$GF(p)$和$GF(2^n)$的方法。

    第二十五个知识点:使用特殊的素数定义\(GF(p)\)和\(GF(2^n)\)的方法. 在我们之前看到的博客中,当实现密码学方案时,一个最频繁调用的操作就是模运算.不幸的是,尽管模块化的使用非常广泛, ...