当前位置:网站首页>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;
}
边栏推荐
- Market segmentation of supermarket customers based on purchase behavior data (RFM model)
- In English translation of papers, how to do a good translation?
- After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
- UDP攻击是什么意思?UDP攻击防范措施
- Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
- Do you really know the use of idea?
- 将ue4程序嵌入qt界面显示
- 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
- Leetcode daily question (1870. minimum speed to arrive on time)
- librosa音频处理教程
猜你喜欢
How effective is the Chinese-English translation of international economic and trade contracts
Chinese English comparison: you can do this Best of luck
18.多级页表与快表
My creation anniversary
(practice C language every day) reverse linked list II
19.段页结合的实际内存管理
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
万丈高楼平地起,每个API皆根基
随机推荐
How much is the price for the seal of the certificate
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
Monotonic stack
医疗软件检测机构怎么找,一航软件测评是专家
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
LeetCode每日一题(1870. Minimum Speed to Arrive on Time)
C语言_双创建、前插,尾插,遍历,删除
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
Redis Foundation
Biomedical localization translation services
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
BUU的MISC(不定时更新)
On the first day of clock in, click to open a surprise, and the switch statement is explained in detail
Pymongo gets a list of data
kubernetes集群搭建Zabbix监控平台
Lesson 7 tensorflow realizes convolutional neural network
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)
How to do a good job in financial literature translation?
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
【Hot100】739. 每日溫度