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. H5+CSS3知识点

    概要:CSS3美化样式.自定义字体图标.滤镜设置.CSS3选择器.transform2D转换.新增表单控件.vaild表单验证.表单样式美化等. 属性选择器: E[attr]只使用属性名,但没有确定任 ...

  2. dict与list的in 操作的速度

    今天刷一道题,计算一串数字中其中两个数字相加等于目标值的题目,且取其中最早的两个数字(最后一个数字的位置靠前). 如[1,25,32,4,3,6,9,5]  targer:9  输出 [3,6]   ...

  3. Web API系列(二)接口安全和参数校验

    以前简单介绍过web api 的设计,但是还是有很多朋友问我,如何合理的设计和实现web api.比如,接口安全,异常处理,统一数据返回等问题.所以有必要系统的总结总结 web api 的设计和实现. ...

  4. ReportView报表开发记录(一)

    在公司开发,使用到ReportView技术,写下自己的经验. 1.在工具箱中找到 ReportViewer,ScriptManager放到test.aspx页面. 如果找不到报表项,请参考http:/ ...

  5. 浅谈JavaScript、ES5、ES6

    // http://es6.ruanyifeng.com/#docs/intro (ES6 文档) 什么是JavaScript JavaScript一种动态类型.弱类型.基于原型的客户端脚本语言,用来 ...

  6. MATLAB读取一张RGB图片转成YUV格式

    1.读入照片 控制输出的标志定义 clc;close all;clear YES = 1; NO = 0; %YES表示输出该文件,请用户配置 yuv444_out_txt = 1; yuv444_o ...

  7. chrome谷歌浏览器插件制作简易教程

    1.在磁盘上创建一个目录,用来放应用的代码和资源 2.在这个目录中,创建一个文本文件,命名为manifest.json,其内容为: { "manifest_version": 2, ...

  8. SQL Server群集知识介绍

    集群CLUSTER种类介绍 基于iSCSI的SQL Server 2012群集测试(一)--SQL群集安装 SQL Server群集如何在线检测 群集中的MS DTC分布式事务协调器 一.SQL Se ...

  9. Tomcat 知识点

    Tomcat(重点) Tomcat是一个符合于Java EE Web标准的最小web容器,所有的jsp程序一定需要有WEB容器的的支持才可以运行,而且在给定的WEB容器里面会支持事务处理操作. Tom ...

  10. Redhat6.4下安装Oracle10g

    Oracle10g_Redhat6.4 安装指南 文档说明 本文借鉴<Redhat_Linux_6.4下Oracle_10g安装配置手册><Redhat 6.4 安装 Oracle1 ...