当前位置:网站首页>Prefix and array series
Prefix and array series
2022-07-06 06:51:00 【Wang Liuliu's it daily】
It involves three questions :
303. Area and retrieval - The array is immutable
304. Two dimensional region and retrieval - The matrix is immutable
560. And for K Subarray
If you want to get 「 Interval and 」, The easiest way to think of is to traverse the desired interval , Add circularly . If there are many such needs , here , The time complexity is O(n^2)
Based on the scenario described above , We can use it 「 The prefix and 」 Optimize , The prefix and the value of each element in the array are interval [0…i] Elements and .
Be careful :
Prefixes and apply to invariant arrays ; For changing arrays , have access to 「 Line segment tree 」, The detailed introduction of segment tree can be seen Line tree details
Area and retrieval - The array is immutable
Area and retrieval - The array is immutable
Details of the topic can be seen : Area and retrieval - The array is immutable
Suggest :preSum[] Overall backward offset by one bit , Simple handling
If we find the interval [2,4] And , Just calculate preSum[4 + 1] - preSum[2] that will do
Code :
class NumArray {
// Record an array of prefixes and
private int[] preSum;
public NumArray(int[] nums) {
// preSum from 1 Start , Avoid cross-border problems
preSum = new int[nums.length + 1];
for (int i = 1; i < preSum.length; i++) {
preSum[i] = preSum[i - 1] + nums[i - 1];
}
}
public int sumRange(int left, int right) {
return preSum[right + 1] - preSum[left];
}
}
Two dimensional region and retrieval - The matrix is immutable
Details of the topic can be seen : Two dimensional region and retrieval - The matrix is immutable
If you sum the red intervals , Demand only preSum[4,4] - preSum[1,4] - preSum[4,1] + preSum[1,1] that will do
● preSum[4,4]: yellow + blue + green + red
● preSum[1,4]: yellow + blue
● preSum[4,1]: yellow + green
● preSum[1,1]: yellow
Code :
class NumMatrix {
private int[][] preSum;
public NumMatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
preSum = new int[m + 1][n + 1];
for (int i = 1; i < m + 1; i++) {
for (int j = 1; j < n + 1; j++) {
preSum[i][j] = preSum[i - 1][j] + preSum[i][j - 1] - preSum[i - 1][j - 1] + matrix[i - 1][j - 1];
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
return preSum[row2 + 1][col2 + 1] - preSum[row1][col2 + 1] - preSum[row2 + 1][col1] + preSum[row1][col1];
}
}
And for K Subarray
Details of the topic can be seen And for K Subarray
reference 「 Sum of two numbers 」 The idea of , utilize HashMap.
Code :
public int subarraySum(int[] nums, int k) {
Map<Integer, Integer> preSum = new HashMap<>();
preSum.put(0, 1);
int sum = 0;
int res = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
int target = sum - k;
if (preSum.containsKey(target)) res += preSum.get(target);
preSum.put(sum, preSum.getOrDefault(sum, 0) + 1);
}
return res;
}
边栏推荐
- 从autojs到冰狐智能辅助的心里历程
- Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
- pymongo获取一列数据
- 【Hot100】739. 每日溫度
- MySQL high frequency interview 20 questions, necessary (important)
- Huawei equipment configuration ospf-bgp linkage
- 女生学软件测试难不难 入门门槛低,学起来还是比较简单的
- Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
- E-book CHM online CS
- My seven years with NLP
猜你喜欢
【每日一题】729. 我的日程安排表 I
Do you really know the use of idea?
万丈高楼平地起,每个API皆根基
How to reconstruct the class explosion caused by m*n strategies?
In English translation of papers, how to do a good translation?
[ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)
Traffic encryption of red blue confrontation (OpenSSL encrypted transmission, MSF traffic encryption, CS modifying profile for traffic encryption)
ECS accessKey key disclosure and utilization
Leetcode daily question (971. flip binary tree to match preorder traversal)
顶测分享:想转行,这些问题一定要考虑清楚!
随机推荐
My seven years with NLP
librosa音频处理教程
机器学习植物叶片识别
PCL实现选框裁剪点云
UDP攻击是什么意思?UDP攻击防范措施
How to do a good job in financial literature translation?
P5706 [deep foundation 2. Example 8] redistributing fat house water -- February 13, 2022
E-book CHM online CS
Wish Dragon Boat Festival is happy
《从0到1:CTFer成长之路》书籍配套题目(周更)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
ROS2安装及基础知识介绍
C语言_双创建、前插,尾插,遍历,删除
What are the commonly used English words and sentences about COVID-19?
Delete external table source data
Blue Bridge Cup zero Foundation National Championship - day 20
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
Changes in the number of words in English papers translated into Chinese