[Brush questions diary] 307. Area and retrieval - array can be modified

Get into the habit of writing together! This is the 4th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the details of the event .

[Brush questions diary] 307. Area and retrieval - array can be modified

The 24th article of this brushing diary is titled: 307. Region and Retrieval - Array Modifiable , Medium

1. Topic description:

On the second day of the Qingming holiday, let's continue to brush the leetcode question of the day

How do I feel when I see this question, my first feeling is that maybe my time to go out to play will be shortened, eh, it's dishes

This question may not be the same as other questions. This is a small function, not just a function , but it's okay, we can still solve it, let's analyze it together

2. What idea does this question examine? What is your thinking?

Let's take a closer look at this question and give us what key information:

  • The known conditions given by the title, specific operands and specific arrays, have two requirements for us. The first is to modify the value at the specified index position to the desired value through update, and the second is to give the left and right intervals. , we can quickly query their interval and
  • After reading this question, we need to do the following things:
    • Complete the definition of NumArray
    • Handling of constructors
    • Implementation of the Update function
    • Implementation of SumRange function

For those of us who have no experience in writing such questions, if we think about it a little bit, we can write code that can calculate the expected result. That is the normal practice, traversing the array and accumulating

Something like this:

Do you want to say what's wrong with this code?

You say there is a problem, in fact, he also solved some problems, you say there is no problem, in fact, when the amount of data in the use case is large, it will time out, and there is no way to AC

Then we have to consider other ways, think about it, and temporarily use a simple method to calculate the interval sum.

We can use segmented grouping to deal with

Here we use an example to deduce how to calculate the interval sum. For the update function, this will not be explained too much.

For example, we can customize an example, the corresponding array is [1, 3, 4, 5, 7, 8, 9], then let's look at the picture

According to the above example, xdm can also customize an example to deduce it, and it can be quickly understood.

3. Coding

According to the above logic and analysis, we can translate it into the following code, here we need to pay attention to the classification of SumRange

The encoding is as follows:

type NumArray struct {
    nums ,sums[]int
    size int

}

func Constructor(nums []int) NumArray {
    length := len(nums)
    size :=int(math.Sqrt(float64(length))) 
    // 定义个记录每一块区间和的切片,大小为
    sums := make([]int,(length / size)+1)

    for i,n := range nums{
        // 对于每一个区间求和
        sums[i/size] += n
    }

    return NumArray{nums,sums,size}
}


func (this *NumArray) Update(index int, val int)  {
    // 处理该值所在区间的数字
    this.sums[index/this.size] += val - this.nums[index]
    this.nums[index] = val
}


func (this *NumArray) SumRange(left int, right int) (ans int) {

    a := left/this.size
    b := right/this.size

    // 说明 left 和 right 对应的区间是同一块
    if a == b{
        for i:=left;i<=right;i++ {
         ans += this.nums[i]
        }
        return
    }

    // left 和 right 不是同一块,至少是 2 块以上的情况
   
    // 处理  left 到 所在块的内容
    for i := left ; i<(a+1)*this.size;i++{
         ans += this.nums[i]
    }
    // 处理left 下一块到 right 所在块的内容,此处正好可以使用 sum 数组中的内容
    for i :=a+1;i<b;i++{
         ans += this.sums[i]
    }
    // 处理 right 所在块的起点,到 right 的内容
    for i:=b*this.size;i<= right ;i++ {
        ans += this.nums[i]
    }

    return
}

复制代码

4. Summary:

The time complexity of this problem needs to be distinguished according to the function

  • Constructor time complexity is O(n) ,
  • update time complexity is O(1)
  • SumRange time complexity is O(root n)

In terms of space complexity, we have introduced a sums array and the length of our sums array depends on the number of copies of the array we cut, so it is O(root number n)

Original title address: 307. Area and retrieval - array can be modified

I am here today, what I have learned, if there are any deviations, please correct me

Welcome to like, follow, favorite

Friends, your support and encouragement are the motivation for me to persist in sharing and improve quality

Okay, here it is this time

Technology is open, and our mentality should be open. Embrace change, live in the sun, and move forward.

I'm the little devil boy Nezha , welcome to like, follow and collect, see you next time~

Guess you like

Origin juejin.im/post/7082713303356538916