当前位置:网站首页>【LeetCode】307. Area and retrieval - array modifiable
【LeetCode】307. Area and retrieval - array modifiable
2022-07-16 07:29:00 【pass night】
subject
307. Area and retrieval - Array can be modified
Give you an array nums , Please complete two types of queries .
- One type of query requirements to update Array
numsThe value of the subscript - Another type of query requires the return of an array
numsMiddle indexleftAnd indexrightBetween ( contain ) Of nums Elemental and , amongleft <= right
Realization NumArray class :
NumArray(int[] nums)Use an array of integersnumsInitialize objectvoid update(int index, int val)takenums[index]Value to update byvalint sumRange(int left, int right)Returns an array ofnumsMiddle indexleftAnd indexrightBetween ( contain ) Of nums Elemental and ( namely ,nums[left] + nums[left + 1], ..., nums[right])
Example 1:
Input :
["NumArray", "sumRange", "update", "sumRange"]
[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]
Output :
[null, 9, null, 8]
explain :
NumArray numArray = new NumArray([1, 3, 5]);
numArray.sumRange(0, 2); // return 1 + 3 + 5 = 9
numArray.update(1, 2); // nums = [1,2,5]
numArray.sumRange(0, 2); // return 1 + 2 + 5 = 8
Tips :
1 <= nums.length <= 3 * 104-100 <= nums[i] <= 1000 <= index < nums.length-100 <= val <= 1000 <= left <= right < nums.length- call
pdateandsumRangeThe number of methods shall not be greater than3 * 104
Answer key 1(TLE)
Ideas
- Violent simulation
Code
class NumArray:
def __init__(self, nums: List[int]):
self.nums = nums
def update(self, index: int, val: int) -> None:
self.nums[index] = val
def sumRange(self, left: int, right: int) -> int:
return sum(self.nums[left:right+1])
# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# obj.update(index,val)
# param_2 = obj.sumRange(left,right)
Complexity
- initialization
- Time complexity : O ( 1 ) O(1) O(1)
- Spatial complexity : O ( n ) O(n) O(n)
- to update
- Time complexity : O ( 1 ) O(1) O(1)
- Spatial complexity : O ( 1 ) O(1) O(1)
- Sum up
- Time complexity : O ( n ) O(n) O(n)
- Spatial complexity : O ( 1 ) O(1) O(1)
Answer key 2(TLE)
Ideas
- Use an array to store prefixes and
Code
class NumArray:
def __init__(self, nums: List[int]):
self.nums = nums
self.sum = []
tempSum = nums[0]
self.sum.append(tempSum)
for n in nums[1:]:
tempSum+= n
self.sum.append(tempSum)
def update(self, index: int, val: int) -> None:
d = val - self.nums[index]
self.nums[index] = val
for i in range(index, len(self.nums)):
self.sum[i] += d
def sumRange(self, left: int, right: int) -> int:
return self.sum[right] - self.sum[left] + self.nums[left]
# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# obj.update(index,val)
# param_2 = obj.sumRange(left,right)
Complexity
- initialization
- Time complexity : O ( n ) O(n) O(n)
- Spatial complexity : O ( n ) O(n) O(n)
- to update
- Time complexity : O ( n ) O(n) O(n)
- Spatial complexity : O ( 1 ) O(1) O(1)
- Sum up
- Time complexity : O ( 1 ) O(1) O(1)
- Spatial complexity : O ( 1 ) O(1) O(1)
Answer key 3
Ideas
- Divide the array into ⌊ n n ⌋ \lfloor \frac{n}{\sqrt{n}} \rfloor ⌊nn⌋ paragraph , Save each paragraph and
Code
import math
class NumArray:
def __init__(self, nums: List[int]):
self.nums = nums
self.size = int(len(nums)**0.5)
self.segmentSum = [0]* math.ceil(len(nums)/self.size)
for i,n in enumerate(nums):
self.segmentSum[i//self.size] += n
def update(self, index: int, val: int) -> None:
self.segmentSum[index//self.size] += (val - self.nums[index])
self.nums[index] = val
def sumRange(self, left: int, right: int) -> int:
a,b = left//self.size*self.size, (right//self.size+1)*self.size
return sum(self.segmentSum[a//self.size:b//self.size]) - sum(self.nums[a:left]) - sum(self.nums[right+1:b])
# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# obj.update(index,val)
# param_2 = obj.sumRange(left,right)
Complexity
- initialization
- Time complexity : O ( n ) O(n) O(n)
- Spatial complexity : O ( n ) O(\sqrt{n}) O(n)
- to update
- Time complexity : O ( 1 ) O(1) O(1)
- Spatial complexity : O ( 1 ) O(1) O(1)
- Sum up
- Time complexity : O ( n ) O(\sqrt{n}) O(n)
- Spatial complexity : O ( 1 ) O(1) O(1)
边栏推荐
- 浅谈框架中xml文件格式的含义
- 《MySQL数据库原理、设计与应用》课后习题及答案 黑马程序员编著
- Node. MySQL operation of JS
- 【LeetCode】2028. Find the missing observation data
- [learning progress on June 3]
- Observer mode
- ReentrantLock的公平与非公平核心区别
- Interviewer: let's talk about how you solve the transaction problem in the distributed scenario?
- 题:二叉树的最近公共祖先
- 【软件质量保障笔记】软件质量保障
猜你喜欢

When byte hung up, the interviewer asked me DDD, but I didn't know

ScheduledThreadPoolExecutor源码和误区详解

SAP BW 抽取层错误S:AA 821 (bukrs)

DDD - in my dream, can I let you bully me?

Basic knowledge of redis - rookie tutorial

Associative word matching - Summary

SAP Logon 无法正常启动

JVM principle and Practice

Go seckill system 3 -- work mode, publish mode

SAP ABAP Selection Screen 选择屏幕看这一篇就够了(持续更新)
随机推荐
Interviewer: let's talk about how you solve the transaction problem in the distributed scenario?
【LeetCode】面试题 01.05. 一次编辑
Unity3d ray
【6月5号学习记录】
SAP BW extraction layer error s:aa 821 (bukrs)
Mid year summary - rotten
From function test to automatic test, to double the salary, I collated the super complete learning guide [with learning notes]
HeadFirst 状态模式 源码
week3
三、FreeNas实现SMB共享、FTP搭建实验报告
MySQL error messages errors
【LeetCode】954. Double pair array
IOT learning journey - initial
题:二叉树的最近公共祖先
从功能测试到自动化测试,实现薪资翻倍,我整理的超全学习指南【附学习笔记】
Week4
2021/12/12 攻防世界 crypto做题记录
SAP ABAP BAPI_ACC_DOCUMENT_POST 创建会计凭证
浅谈框架中xml文件格式的含义
2021-11-13攻防世界做题记录01MISC