https://leetcode.com/problems/range-sum-query-mutable/

因为数组会变动,所以缓存机制受到了挑战。。。每次更新数组意味着缓存失效,这样一更新一查找的话相当于每次都重新计算了。

所以要设计一个更好的缓存机制,尽量降低更新带来的影响。

我选择分段缓存,就是把原数组的缓存分别放在多段缓存里,这样数组变动的时候只用更新一段缓存。

我选择分成log(n) 个段,并没有什么道理。

这样在查询rangeSum 的时候就复杂了,如果范围在同一个缓存段内就很好,当跨越缓存段的时候,要分别处理两头的两个段,然后还别忘了中间被跨越的那些段。

因为比较菜,实现得非常繁琐。但是终归accpeted 了

/**
* @constructor
* @param {number[]} nums
*/
var NumArray = function(nums) {
this.nums = nums;
this.cache = []; var cacheSize = 0;
var numsLen = nums.length;
while (numsLen > 0) {
cacheSize++;
numsLen = numsLen >> 1;
} this.cacheSize = cacheSize;
this.segSize = Math.floor(nums.length / cacheSize); var idx = 0;
for (var i = 0; i < cacheSize; i++) {
var cacheStart = idx;
var segSize = this.segSize;
if (i === cacheSize - 1) {
segSize = Math.max(Math.floor(nums.length / cacheSize), nums.length - idx);
}
var cacheEnd = idx + segSize - 1; var thatCache = [];
var thatAcc = 0;
for (var j = cacheStart; j <= cacheEnd; j++) {
thatAcc += this.nums[j];
thatCache.push(thatAcc);
}
this.cache.push(thatCache);
idx = cacheEnd + 1;
}
}; /**
* @param {number} i
* @param {number} val
* @return {void}
*/
NumArray.prototype.update = function(i, val) {
var residual = val - this.nums[i];
var cachePos = Math.min(Math.floor(i / this.segSize), this.cacheSize);
this.nums[i] = val;
var cache = this.cache[cachePos];
var idx = i - cachePos * this.segSize;
for (var j = idx; j < cache.length; j++) {
cache[j] += residual;
}
}; /**
* @param {number} i
* @param {number} j
* @return {number}
*/
NumArray.prototype.sumRange = function(i, j) {
if (this.cache.length === 0) return 0;
var cachePosi = Math.min(Math.floor(i / this.segSize), this.cacheSize - 1);
var cachePosj = Math.min(Math.floor(j / this.segSize), this.cacheSize - 1);
if (cachePosi === cachePosj) {
var cache = this.cache[cachePosi];
var local_i = i - cachePosi * this.segSize;
var local_j = j - cachePosi * this.segSize;
return cache[local_j] - cache[local_i] + this.nums[i];
} else {
var cache_i = this.cache[cachePosi];
var cache_j = this.cache[cachePosj]; var local_i = i - cachePosi * this.segSize;
var local_j = j - cachePosj * this.segSize; var ret_i = cache_i[cache_i.length - 1] - cache_i[local_i] + this.nums[i];
var ret_j = cache_j[local_j]; var ret = 0;
for (var k = cachePosi + 1; k < cachePosj; k++) {
ret += this.cache[k][this.cache[k].length - 1];
}
return ret + ret_i + ret_j;
}
};

Range Sum Query - Mutable的更多相关文章

  1. [Leetcode Week16]Range Sum Query - Mutable

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

  2. 【刷题-LeetCode】307. Range Sum Query - Mutable

    Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...

  3. [LeetCode] 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笔记:Range Sum Query - Mutable

    一. 题目描写叙述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...

  6. 307. Range Sum Query - Mutable

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

  7. Leetcode: Range Sum Query - Mutable && Summary: Segment Tree

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

  8. 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 ...

  9. [Swift]LeetCode307. 区域和检索 - 数组可修改 | Range Sum Query - Mutable

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

  10. [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 ...

随机推荐

  1. Z字形扫描(201412-2)

    问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...

  2. pdf在线处理网站

    https://smallpdf.com/unlock-pdf

  3. 关于编写性能高效的javascript事件的技术

    如何能做出高效的web前端程序是我每次做前端开发都会不自觉去考虑的问题.几年前雅虎里牛逼的前端工程师们出了一本关于提升web前端性能的书籍,轰动了整个web开发技术界,让神秘的web前端优化问题成为了 ...

  4. SQL 从指定表筛选指定行信息 获取表行数

    1.获取指定表的行数 --获取表中数据行数 --select max([列名]) from 表名 2.筛选指定表的指定行数据(数据表分页获取) http://www.cnblogs.com/morni ...

  5. tips~function pointer

    An simple example: #include<stdio.h> int plus(int a,int b) { return a+b; } int main() { int (* ...

  6. Altium Designer 的entry sheet ,offsheet和port作用(转载)

    1.图纸结构 图纸包括两种结构关系: 一种是层次式图纸,该连接关系是纵向的,也就是某一层次的图纸只能和相邻的上级或下级有关系: 另一种是扁平式图纸,该连接关系是横向的,任何两张图纸之间都可以建立信号连 ...

  7. flickrf 分布式主键生成方案【mysql】

    [相关链接:http://blog.csdn.net/bluishglc/article/details/7710738] 具体做法: 1:找两台服务器,分别配置: TicketServer1: au ...

  8. 全文检索解决方案(lucene工具类以及sphinx相关资料)

    介绍两种全文检索的技术. 1.  lucene+ 中文分词(IK) 关于lucene的原理,在这里可以得到很好的学习. http://www.blogjava.net/zhyiwww/archive/ ...

  9. required - HTML5里的input标签的required属性提示文字修改

    input 里面增加这样的语句: <input type="text" placeholder="您的姓名" required oninvalid=&qu ...

  10. Ajax中get请求和post请求

    我们在使用Ajax向服务器发送数据时,可以采用Get方式请求服务器,也可以使用Post方式请求服务器,那么什么时候该采用Get方式,什么时候该采用Post方式呢? Get请求和Post请求的区别: 1 ...