亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

Range Sum Query

zhongmeizhi / 1137人閱讀

摘要:表現(xiàn)在二進(jìn)制上的規(guī)律每次加上最末尾的求末尾的就拿這個(gè)數(shù)和它的補(bǔ)碼于。還有要求不是,要轉(zhuǎn)換一下,和之前那道的思路差不多。這里用一個(gè)的表示從到的和。

Range Sum Query - Immutable

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

Example: Given nums = [-2, 0, 3, -5, 2, -1]
sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -3

Note:
You may assume that the array does not change.
There are many calls to sumRange function.

用dp解。dp[i]表示sumRange(0, i-1)
function是dp[i+1] = dp[i] + nums[i]
所以sumRange(i, j) = dp[j+1] - dp[i]

public class NumArray {
    int[] dp;
    public NumArray(int[] nums) {
        dp = new int[nums.length + 1];
        for(int i = 0; i < nums.length; i++) {
            dp[i+1] = dp[i] + nums[i];
        }
    }
    
    public int sumRange(int i, int j) {
        return dp[j + 1] - dp[i];
    }
}
Range Sum Query - Mutable

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

The update(i, val) function modifies nums by updating the element at index i to val.
Example: Given nums = [1, 3, 5]

sumRange(0, 2) -> 9
update(1, 2)
sumRange(0, 2) -> 8

Note:

The array is only modifiable by the update function.

You may assume the number of calls to update and sumRange function is distributed evenly.

和上一題的區(qū)別在于,這次數(shù)組里面的值會(huì)動(dòng)態(tài)的變化,所以不能和上面一題一樣,最開始初始化一個(gè)dp數(shù)組之后就不管了。如果還是用dp數(shù)組,那么每次update之后都得重新再改一遍,O(N)時(shí)間復(fù)雜度太高。
用binary index tree來做,update和sum的時(shí)間復(fù)雜度都是O(NlogN)。原理如下:
index: 1------2------3------4------5------6------7-----8-----9
binary: 0001-0010-0011-0100-0101-0110-0111-1000-1001
value: 2------5------6------3------1------7------5-----3-----2

tree[i]: [1,1]--[1,2]--[3,3]--[1,4]--[5,5]--[5,6]--[7,7]--[1,8]--[9,9]

Level1: 2------7------------16------------------------34

Level2: --------------6-------------1-----8-------------------9

right index in level1 has one "1" in binary representation.
right index in level2 has two "1" in binary representation.
......

2個(gè)主要的method:add 和 sum
add(idx, val): add val to nums[i]
sum(idx): get the sum from 1 to idx

add的時(shí)候改怎么操作呢?
舉個(gè)例子,比如add(5, 2), 我想在index = 5的地方加2,那么:

start從idx = 5開始,tree[5] += 2

接著,要更新tree[6],也就是sum(5, 6)

再接著更新tree[8],也就是sum(1, 8)

在圖里的表現(xiàn)就是我先從start開始,把start同一level所有往右相鄰的更新一遍,沒有相鄰的之后,就往上一面一層走,重復(fù)更新的過程。直到idx超過長(zhǎng)度。

表現(xiàn)在二進(jìn)制上的規(guī)律:

5 -------> 6 ------> 8

0101 -> 0110 -> 1000

每次加上最末尾的‘1’

0101 + 0001 = 0110

0110 + 0010 = 1000

求末尾的‘1’就拿這個(gè)數(shù)和它的補(bǔ)碼于。
假設(shè)這個(gè)數(shù)是x = n(1,0)-1-m(0)
m(0)表示這個(gè)數(shù)末尾有m個(gè)0,n(1,0)表示這個(gè)數(shù)前面有n個(gè)1或者0。那么第一個(gè)1就出現(xiàn)在從右數(shù)第m+1位上。

現(xiàn)在求x的反碼,~x = ~n(1,0)-0-m(1)

那么x的補(bǔ)碼就是:-x = ~n(1,0)-1-m(0)

所以: x&-x = n(0)-1-m(0)

void add(int idx, int val) {
    while(idx < tree.length) {
        tree[idx] += val;
        idx += i & -i;
    }
}
int sum(int idx) {
    int result = 0;
    while(idx > 0) {
        result += tree[idx];
        idx += i & -i;
    }
    return result;
}

sum的方法同理,init的直接用add即可。這道題注意下數(shù)組的index是從0開始,所以每次要+1。還有要求update不是add,要轉(zhuǎn)換一下,update(idx, val) = add(idx, val - nums[idx])

public class NumArray {
    // binary index tree
    int[] tree;
    int[] nums;
    public NumArray(int[] nums) {
        this.nums = nums;
        tree = new int[nums.length + 1];
        for(int i = 0; i < nums.length; i++) {
            add(i, nums[i]);
        }
    }
    
    private void add(int i, int val) {
        i += 1;
        while(i < tree.length) {
            tree[i] += val;
            i += i & -i;
        }
    }
    
    public void update(int i, int val) {
        int diff = val - nums[i];
        nums[i] = val;
        add(i, diff);
    }
    
    public int sumRange(int i, int j) {
        return sum(j) - sum(i - 1);
    }
    
    // return the sumRange(0, i)
    private int sum(int i) {
        i+= 1;
        int result = 0;
        while(i > 0) {
            result += tree[i];
            i -= i & -i;
        }
        return result;
    }
}
Range Sum Query 2D - Immutable

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

Range Sum Query 2D

The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.

Example:
Given matrix = [
[3, 0, 1, 4, 2],
[5, 6, 3, 2, 1],
[1, 2, 0, 1, 5],
[4, 1, 0, 1, 7],
[1, 0, 3, 0, 5] ]

sumRegion(2, 1, 4, 3) -> 8
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12
Note:
You may assume that the matrix does not change.
There are many calls to sumRegion function. You may assume that row1 ≤ row2 and col1 ≤ col2.

和之前那道1d的思路差不多。這里用一個(gè)2d的prefix array:
dp[i + 1] [j + 1] 表示從 [0, 0] 到 [i, j] 的和。function是:
dp[i + 1] [j + 1] = dp[i + 1] [j] + dp[i] [j + 1] - dp[i] [j] + matrix[i] [j]
所以sum(row1, col1, row2, col2) = dp[row2 + 1] [col2 + 1] - dp[row1] [col2 + 1] - dp[row2 + 1] [col1] + dp[row1] [col1]

public class NumMatrix {
    // dp
    int[][] dp;
    public NumMatrix(int[][] matrix) {
        if(matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) return;
        dp = new int[matrix.length + 1][matrix[0].length + 1];
        for(int i = 0; i < matrix.length; i++) {
            for(int j = 0; j < matrix[0].length; j++) {
                dp[i+1][j+1] = dp[i+1][j] + dp[i][j+1] - dp[i][j] + matrix[i][j];
            }
        }
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
        return dp[row2 + 1][col2 + 1] - dp[row1][col2 + 1] - dp[row2 + 1][col1] + dp[row1][col1];
    }
}
Range Sum Query 2D - Mutable

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

Range Sum Query 2D
The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.

Example:
Given matrix = [
[3, 0, 1, 4, 2],
[5, 6, 3, 2, 1],
[1, 2, 0, 1, 5],
[4, 1, 0, 1, 7],
[1, 0, 3, 0, 5]
]

sumRegion(2, 1, 4, 3) -> 8
update(3, 2, 2)
sumRegion(2, 1, 4, 3) -> 10

Note:

The matrix is only modifiable by the update function.

You may assume the number of calls to update and sumRegion function is distributed evenly.

You may assume that row1 ≤ row2 and col1 ≤ col2.

和1d的mutable的差不多。還是用binary index tree來做。時(shí)間還是都是O(NlogN)。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/66563.html

相關(guān)文章

  • leetcode303-304 Range Sum Query Immutable

    摘要:假設(shè)有一個(gè)整數(shù)數(shù)組,計(jì)算下標(biāo)從到包含和的數(shù)字的和。求和的請(qǐng)求將會(huì)在同一個(gè)整數(shù)數(shù)組上多次請(qǐng)求。這一題思路很簡(jiǎn)單,因?yàn)?。而利用?dòng)態(tài)規(guī)劃則很容易知道。這里將原先的一維數(shù)組替換成二維數(shù)組。要求計(jì)算一個(gè)矩形內(nèi)的所有元素的值。 Range Sum Query Immutable Given an integer array nums, find the sum of the elements be...

    Worktile 評(píng)論0 收藏0
  • leetcode307. Range Sum Query - Mutable

    摘要:題目要求可以先參考數(shù)組不發(fā)生變動(dòng)時(shí)的題目。最后的葉節(jié)點(diǎn)為當(dāng)前數(shù)組的值,非葉結(jié)點(diǎn)則記錄了子數(shù)組的范圍以及該子數(shù)組中所有元素的和。它是一個(gè)非常輕量級(jí)的數(shù)據(jù)結(jié)構(gòu),而且?guī)缀蹙褪菫檫@種題目量身打造。 題目要求 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc...

    Lemon_95 評(píng)論0 收藏0
  • [LeetCode] 304. Range Sum Query 2D - Immutable

    Problem Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). https://leetcode.com/static/i...s...

    chinafgj 評(píng)論0 收藏0
  • [LeetCode] Range Sum Query Immutable

    Problem Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRan...

    LiuZh 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<