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. 精通css 高级web标准解决方案——可视化格式模型-定位模型

    CSS 中有三种定位机制:普通流.浮动.绝对定位.(默认为普通流) 改变文档流:display: inline-block; (支持到ie8及以上) 1-匿名块框: <div> 你好! & ...

  2. 命名空间jquery

    命名空间的意思就是 同一个元素有绑定了2个相同的事件,比如2个scroll,这个时候你做其他交互的时候只想触发第二个scroll事件  就可以用命名空间做了 <button id="b ...

  3. form表单练习

    注册页面的设计 <body leftmargin="400px" topmargin="200px"> <form method=" ...

  4. 用uniq来处理文件重复数据--交集,差集,计数等(转)

    经常有这样的需求:两个文本文件要求取重复的行或只取不重复的,简单的情况下用sort和uniq来处理是非常方便的: 利用现存两个文件,生成一个新的文件 取出两个文件的并集(重复的行只保留一份) 取出两个 ...

  5. Android之ListView性能优化——一行代码绑定数据——万能适配器

    如下图,加入现在有一个这样的需求图,你会怎么做?作为一个初学者,之前我都是直接用SimpleAdapter结合一个Item的布局来实现的,感觉这样实现起来很方便(基本上一行代码就可以实现),而且也没有 ...

  6. Linux2.6.11版本:classic RCU的实现

    转载自:http://www.wowotech.net/kernel_synchronization/linux2-6-11-RCU.html 一.前言 无论你愿意或者不愿意,linux kernel ...

  7. java文件上传

    jsp界面代码: <body>  <form action="servlet/UploadServlet" enctype="multipart/for ...

  8. V4L2框架分析学习二

    转载于:http://www.techbulo.com/1198.html v4l2_device v4l2_device在v4l2框架中充当所有v4l2_subdev的父设备,管理着注册在其下的子设 ...

  9. Linux下实现秒级的crontab定时任务

    crontab的格式如下 * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 ...

  10. 【Android自学日记】两种适配器的使用

    ArrayAdapter适配器: (1)用于显示基本的文字内容 (2)基本使用过程:新建适配器---创建或加载数据源---适配器加载数据源---视图加载适配器 ArrayAdapter(上下文,当前L ...