Range Sum Query - Mutable
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的更多相关文章
- [Leetcode Week16]Range Sum Query - Mutable
Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...
- 【刷题-LeetCode】307. Range Sum Query - Mutable
Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [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 ...
- leetcode笔记:Range Sum Query - Mutable
一. 题目描写叙述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...
- 307. Range Sum Query - Mutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- 几种常见的Shell
Unix/Linux上常见的Shell脚本解释器有bash.sh.csh.ksh等,习惯上把它们称作一种Shell.我们常说有多少种Shell,其实说的是Shell脚本解释器. bash bash是L ...
- Spring MVC学习笔记——完整的用户登录
1.搭建环境的第一步是导包,把下面这些包都导入工程中 /media/common/工作/Ubuntu软件/SpringMVC_jar包整理/aop/media/common/工作/Ubuntu软件/S ...
- errored out in DoExecute, couldn't PrepareToExecuteJITExpression
error: Couldn't materialize struct: size of variable <varName> disagrees with the ValueObject' ...
- Android SDK升级后报错error when loading the sdk 发现了元素 d:skin 开头无效内容
把错误位置的devices.xml这个文件删除,再把sdk里面tools\lib下的这个文件拷贝到你删除的那个文件夹里,重启eclipse
- 移动端开发概览【webview和touch事件】
作为一个前端,而且作为一个做移动端开发的前端,那意味着你要有三头六臂,跟iOS开发哥哥一起打酱油,跟Android开发哥哥一起修bug... Android vs Ios 我在webkit内核的chr ...
- Markdown段落首行缩进的实现办法
添加:
- 关于unity如何制作mmo
昨天去看了下unity的成都openday,还是有很多收获的,之前我对于这类的活动始终提不起来兴趣,不过看来日后还是要多参加下类似的活动长长见识. 公司打算开发3d mmo手游,昨天好玩123恰好也分 ...
- opencart邮件模板
在opencart中,几乎所有的版权信息都写在language里,我们找到catalog/language/your language/mail/order.php这个语言文件,找到 $_['text ...
- 预处理指令#pragma
#pragma介绍 #pragma是一个预处理指令,pragma的中文意思是『编译指示』.它不是Objective-C中独有的东西(貌似在C/C++中使用比较多),最开始的设计初衷是为了保证代码在不同 ...
- HTML 内容居中方式总结
在HTML网页排版经常会用到关于对其方式的情况,水平居中和垂直居中.特别是水平居中,并不是一个简单的text-align就可以解决所有的情况. 开始之前普及一点HTML知识,目标很明显,不同的页面结构 ...