当前位置:网站首页>[leetcode] 713: subarray with product less than k
[leetcode] 713: subarray with product less than k
2022-06-26 05:07:00 【f0.0y】
Title source
leetcode link :https://leetcode-cn.com/problems/subarray-product-less-than-k/
Their thinking
Define a subarray by sliding the window frame , The product of all elements in the sliding window is
product = e[i] * e[i+1] * e[i+2] * ... * e[j]
among ,i Is the left boundary of the sliding window ,j Is the right boundary of the sliding window ,0 <= i <= n,0 <= j <= n,n Represents the array size .
The initial state
Left border of sliding window left = 1, Right border right = 1.
product = e[1], If at this time product < k, The number of qualified subarrays in the sliding window count = 1.
In the middle
left = i,right = j - 1
product = e[i] * e[i+1] * ... * e[j-1]
Suppose this time product < k, And the cumulative number of qualified sub arrays has been calculated count = x.
Next state
Slide the right edge of the window one unit to the right , At this time, the left and right boundaries of the sliding window are updated to
left = i,right = j
product = e[i] * e[i+1] * ... * e[j-1] * e[j]
If product < k, explain [i, j] Is a sub array that meets the conditions . Sliding window inner ratio [i, j] Subarray length is short , And contains e[j] The subarrays of are all qualified subarrays . therefore , The number of new sub arrays that meet the conditions is calculated to be (j - i + 1) individual , to update count = x + (j - i + 1).
If product >= k, explain [i, j] Not a qualified subarray , Now move the left boundary of the sliding window to the right by one unit , to update left = i + 1, new product = e[i+1] * e[i+2] * ... * e[j]. Continue to investigate [i+1, j] The number of qualified subarrays in the range , And according to the new product Update the left and right boundaries of the sliding window .
End state
When the left and right boundaries of the sliding window left = n,right = n when , End of the search .
The algorithm code is as follows
int numSubarrayProductLessThanK(int* nums, int numsSize, int k)
{
int i, left, right;
int product = 1;
int res = 0;
for (left = 0, right = 0; right < numsSize; right++) {
product *= nums[right];
for (i = left; i <= right; i++) {
if (product < k) {
res += (right - left + 1);
break;
} else {
product /= nums[left];
left++;
}
}
}
return res;
}
边栏推荐
- Beidou navigation technology and industrial application of "chasing dreams in space and feeling for Beidou"
- Using requests library and re library to crawl web pages
- Learn from small samples and run to the sea of stars
- 5. < tag stack and general problems > supplement: lt.946 Verify the stack sequence (the same as the push in and pop-up sequence of offer 31. stack)
- Astype conversion data type
- Zuul implements dynamic routing
- Status of processes and communication between processes
- Sklearn Library -- linear regression model
- Difference between return and yield
- [geek] product manager training camp
猜你喜欢

6.1 - 6.2 公鑰密碼學簡介

Record a circular reference problem

A method of quickly transplanting library function code to register code by single chip microcomputer

Happy New Year!

86.(cesium篇)cesium叠加面接收阴影效果(gltf模型)

Statsmodels Library -- linear regression model

Codeforces Round #802 (Div. 2)(A-D)
Technical problems to be faced in mobile terminal im development

Rsync common error messages (common errors on the window)

天才制造者:獨行俠、科技巨頭和AI|深度學習崛起十年
随机推荐
ROS notes (07) - Implementation of client and server
Image translation /gan:unsupervised image-to-image translation with self attention networks
Douban top250
YOLOV5训练结果的解释
dijkstra
Wechat applet exits the applet (navigator and api--wx.exitminiprogram)
【quartz】从数据库中读取配置实现动态定时任务
[unity3d] human computer interaction input
6.1 - 6.2 introduction to public key cryptography
Genius makers: lone Rangers, technology giants and AI | ten years of the rise of in-depth learning
Tensorflow and deep learning day 3
The best Chinese open source class of vision transformer, ten hours of on-site coding to play with the popular model of Vit!
Multipass Chinese document - use packer to package multipass image
Pycharm package import error without warning
ThreadPoolExecutor实现文件上传批量插入数据
Sentimentin tensorflow_ analysis_ cell
Technical problems to be faced in mobile terminal im development
[geek] product manager training camp
Créateur de génie: cavalier solitaire, magnat de la technologie et ai | dix ans d'apprentissage profond
Collections and dictionaries