# 713-Subarray Product Less Than K ###### tags: `Medium` ## Question https://leetcode.com/problems/subarray-product-less-than-k/ ## Key 1. Two pointer 對陣列中每個數進行迭代h,每次初始product為乘積總和,再使用一個left pointer(t)去跟product乘積,如果product大於k會再除left pointer對應的數,因此可以避免乘上相同的數值,每次迭代計算subarray數就是用count紀錄,count為h-t+1 ## Reference ## Solution ```cpp= class Solution { public: int numSubarrayProductLessThanK(vector<int>& nums, int k) { int p = 1; int a = 0; int h = 0, t = 0; for (h=0; h<nums.size(); h++) { p = p * nums[h]; while (t<=h && p>=k) { p = p / nums[t]; t++; } a += h-t+1; } return a; } }; ```