当前位置:网站首页>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;
}
边栏推荐
- Call, apply, bind rewrite, easy to understand with comments
- 万丈高楼平地起,每个API皆根基
- Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
- Biomedical localization translation services
- Database basics exercise part 2
- Leetcode daily question (971. flip binary tree to match preorder traversal)
- How much is it to translate Chinese into English for one minute?
- The internationalization of domestic games is inseparable from professional translation companies
- 【刷题】怎么样才能正确的迎接面试?
- Reflex WMS中阶系列3:显示已发货可换组
猜你喜欢
[English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)
How to translate professional papers and write English abstracts better
UWA Pipeline 2.2.1 版本更新说明
On the first day of clock in, click to open a surprise, and the switch statement is explained in detail
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
Database basics exercise part 2
Apache DolphinScheduler源码分析(超详细)
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
Chapter 7 - thread pool of shared model
Monotonic stack
随机推荐
Changes in the number of words in English papers translated into Chinese
Machine learning plant leaf recognition
成功解决AttributeError: Can only use .cat accessor with a ‘category‘ dtype
Leetcode daily question (971. flip binary tree to match preorder traversal)
The registration password of day 239/300 is 8~14 alphanumeric and punctuation, and at least 2 checks are included
SQL Server Manager studio (SSMS) installation tutorial
成功解决TypeError: data type ‘category‘ not understood
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
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
Wish Dragon Boat Festival is happy
ROS2安装及基础知识介绍
医疗软件检测机构怎么找,一航软件测评是专家
基于PyTorch和Fast RCNN快速实现目标识别
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
【Hot100】739. 每日温度
Do you really know the use of idea?
Monotonic stack
C语言_双创建、前插,尾插,遍历,删除