当前位置:网站首页>[question skimming diary] and a subarray of K
[question skimming diary] and a subarray of K
2022-06-28 17:03:00 【Fu er'er】
And for K Subarray
Given a Integers Array and an integer k , Please find the array with k The number of consecutive subarrays of .
Example 1:
Input :nums = [1,1,1], k = 2
Output : 2
explain : This question [1,1] And [1,1] For two different situations
Example 2:
Input :nums = [1,2,3], k = 3
Output : 2
Tips :
1 <= nums.length <= 2 * 10^4-1000 <= nums[i] <= 1000-10^7 <= k <= 10^7
【 Sliding window thinking 】
- Determine the left and right boundaries
- Find the conditions for sliding the window left and right
Suitable for solving related problems [ Continuous subarray ] subject
【 limitations 】 But in existence == negative == Cannot be used in an array of
Window sliding conditions :
while When the elements in the window exceed or do not meet the conditionsThe sliding window ;If there is a negative number , It is not certain that the left boundary is moved or Expand the right boundary
Ideas : The prefix and + Hashtable
Applicable problem solving : Subscript of circular array N when , Need to be used before N-1 The calculation result of the term ( and / product )
How to save ?
- The title clearly requires that no additional space is allowed , Directly modify the array in place
- When space complexity is not limited , It is better to open up additional space for computing , avoid Data pollution
- When calculating, you only need to obtain Last time Cumulative results of ===> Use Array Storage , Get the last element of the array every time
- It is necessary to obtain each time during calculation A few or more times before The results are compared ===> Use Hashtable Storage , Reduce time complexity
Algorithm steps
Prefix and difference = The sum of successive subarrays
- Find the sum of successive subarrays as K <==> look for
pre[j] - pre[i-1] == kSubscript pair of[i,j] pre[j] - pre[i - 1] == kEquivalent topre[j] - k == pre[i - 1]- At present The prefix and - k The income is front Of certain elements The prefix and
Hashtable
hashmap==> Save the existing prefix and The emergence of frequency , not only 1example :
[-1,1,0], k = 0When traversing to the last element
presum = 0presum - k = 0, Prefixes and are 0 Some of them are[-1,1]and[-1,1,0]
The current prefix and... Are incrementally calculated
presum, And decidepresum - kWhether inhashmapin , If exist , result + frequencyhashmap[presum-k], And save the current prefix andpresumTohashmapin
【 The border problem 】 if nums[0] = k , But now hashmap It's empty , No statistics ==> initialization hashmap by {0:1}
Code
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
map<int, int> hashmap;
int presum = 0;
int res = 0;
hashmap[0] = 1;
for(int i = 0;i < nums.size();i ++ ) {
presum += nums[i];
// Judge presum-k Whether the resulting prefix and exist
if(hashmap.find(presum - k) != hashmap.end())
res += hashmap[presum-k] ;
// Save the current prefix and
hashmap[presum] ++ ;
}
return res;
}
};
Time complexity :O(n)
Spatial complexity :O(n)
边栏推荐
- Can SQL queries be used in the tablestore to find out all the data in the table?
- [tcaplusdb knowledge base] view tcapdir directory server
- 抓取手机端变体组合思路设想
- 高并发、高可用、弹性扩展,天翼云护航企业云上业务
- PotPlayer播放百度雲盤視頻
- NOIP1998-2018 CSP-S2 2019 2021提高组解题报告与视频
- Cloud sports, 360 ° witnessing speed and passion
- np tips: random 创建随机矩阵 sample = np.random.random([19, 64 , 64, 3])
- [tcapulusdb knowledge base] tcapulusdb technical support introduction
- Gartner announces five privacy trends from today to 2024
猜你喜欢

Use open connector to integrate hubspot and SAP systems

visio 使用

You have a chance to collect wool. Click "earn" and you will have a chance to earn a high commission

免费、强大、高颜值的笔记软件评测: OneNote、Heptabase、氢图、FlowUs

【TcaplusDB知识库】TcaplusDB限制条件介绍

ICML 2022 | 基于解耦梯度优化的可迁移模仿学习方法

中国SSD行业企业势力全景图
![[tcapulusdb knowledge base] Introduction to tcapulusdb restrictions](/img/d3/27f09f7f5ab8e27d1ab87a35a9c0f3.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb restrictions

12 SQL optimization schemes summarized by old drivers (very practical)

After the first failure, AMEC rushed to the Hong Kong stock exchange for the second time, and the financial principal changed frequently
随机推荐
【尚硅谷与腾讯云官方合作】硅谷课堂项目视频发布
2019 CSP J2 entry group csp-s2 improvement group round 2 video and question solution
【TcaplusDB知识库】WebClient用户如何读取和修改数据
Have you ever encountered the error that the main key of this setting is consistent with the database?
PotPlayer播放百度云盘视频
批量修改指定字符文件名 bat脚本
Potplayer play Baidu Cloud disk video
[golang] how to install iris
提升可观测性 - 业务指标监控实践
This simple little function saves 213 hours for our production research team in half a year
基于Krack的网络攻击「建议收藏」
Cardinality sorting - common sorting method (2/8)
如何清除 WordPress 中的缓存
logback 日志输出格式
彻底凉了!腾讯知名软件全线下架,网友一片唏嘘。。。
免费、强大、高颜值的笔记软件评测: OneNote、Heptabase、氢图、FlowUs
[laravel] about the composer installation of laravel8
AutoSAR 软件开发培训
[tcapulusdb knowledge base] Introduction to tcapulusdb restrictions
【TcaplusDB知识库】TcaplusDB限制条件介绍