当前位置:网站首页>[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;
}
边栏推荐
- Resample
- Statsmodels Library -- linear regression model
- Multipass Chinese document - use multipass service to authorize the client
- 2.< tag-动态规划和常规问题>lt.343. 整数拆分
- One of token passing between microservices @feign's token passing
- YOLOV5超参数设置与数据增强解析
- Computer Vision Tools Chain
- 0622 horse palm fell 9%
- YOLOv5-6.0的一些参数设置和特征图可视化
- FastAdmin Apache下设置伪静态
猜你喜欢

MySql如何删除所有多余的重复数据

torchvision_ Transform (image enhancement)

Decipher the AI black technology behind sports: figure skating action recognition, multi-mode video classification and wonderful clip editing

86. (cesium chapter) cesium overlay surface receiving shadow effect (gltf model)

Final review of brain and cognitive science

UWB超高精度定位系统架构图

图像翻译/GAN:Unsupervised Image-to-Image Translation with Self-Attention Networks基于自我注意网络的无监督图像到图像的翻译

Tp5.0框架 PDO连接mysql 报错:Too many connections 解决方法

PSIM software learning ---08 call of C program block

localStorage浏览器本地储存,解决游客不登录的情况下限制提交表单次数。
随机推荐
PSIM software learning ---08 call of C program block
Happy New Year!
Wechat applet exits the applet (navigator and api--wx.exitminiprogram)
How MySQL deletes all redundant duplicate data
MySql如何删除所有多余的重复数据
Dbeaver installation and configuration of offline driver
ssh连win10报错:Permission denied (publickey,keyboard-interactive).
2022.2.10
86. (cesium chapter) cesium overlay surface receiving shadow effect (gltf model)
Classic theory: detailed explanation of three handshakes and four waves of TCP protocol
Tensorflow and deep learning day 3
Mise en œuvre du routage dynamique par zuul
Simple application of KMP
微信小程序保存圖片的方法
localStorage浏览器本地储存,解决游客不登录的情况下限制提交表单次数。
Differences between TCP and UDP
6.1 - 6.2 公鑰密碼學簡介
Zhongshanshan: engineers after being blasted will take off | ONEFLOW u
Multipass Chinese document - setup driver
图解OneFlow的学习率调整策略