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. .net Socket 通信简单实例(初级入门)

    c/s控制台应用程序,Server.Client分别在两个项目中 服务端 using System; using System.Collections.Generic; using System.Li ...

  2. SQL中部分语法整理

    1.SELECT DISTINCT 语句 关键词DISTINCT用于返回唯一不同的值. 语法: SELECT DISTINCT 列名称 FROM 表名称 2.SELECT INTO语句 SELECT ...

  3. 美国usan数据库——PDF提取

    QQ:231469242 原创 单个PDF内容提取 # -*- coding: utf-8 -*- """ io.open() is the preferred, hig ...

  4. diff生成补丁与patch打补丁

    1.使用diff生成补丁: diff是Linux下的文件比较命令,参数这里就不说了,直接man一下就行了,不仅可以比较文件,也可以比较两个目录,并且可以将不同之处生成补丁文件,其实就是一种打补丁的命令 ...

  5. Python *与** 参数问题

    问题:     Python的函数定义中有两种特殊的情况,即出现*,**的形式.     如:def myfun1(username, *keys)或def myfun2(username, **ke ...

  6. Bubble Cup 8 finals E. Spectator Riots (575E)

    题意: 一个长宽是100000单位的球场上有很多暴动的观众,每个观众都有一个速度v, 在一秒内,观众会等概率地移动到与原位置的曼哈顿距离<=v的地方(不会移动到界外). 你需要选取三个位置,这三 ...

  7. java发送 email

    public class EmailUtils implements IAction { private static Logger logger = Logger.getLogger(EmailUt ...

  8. MySQL索引类型总结和使用技巧以及注意事项

    索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常见的MySQL索引类型 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytable ...

  9. centos7 编译php56

    编译安装php5.6 centos7环境 步骤: //下载php5.6 wget http://cn2.php.net/distributions/php-5.6.26.tar.bz2 //安装依赖 ...

  10. nuget的搭建及多源冲突

    为什么使用nuget来管理类库引用就不再阐述,好处真的一抓一把.在使用nuget的时候,我们如果总去访问别人的nuget源,受限于网络情况的好坏,速度真的没法保证,更别说访问国外的源了.那好,我们来自 ...