dengxinyi 5 anos atrás
pai
commit
1c3822b383
1 arquivos alterados com 41 adições e 0 exclusões
  1. 41 0
      leetcode/medium/713.cc

+ 41 - 0
leetcode/medium/713.cc

@@ -0,0 +1,41 @@
+/* BEGIN */
+
+#include <vector>
+
+using std::vector;
+
+#define INF 1e6
+
+class Solution {
+public:
+    int numSubarrayProductLessThanK(vector<int>& nums, int k) {
+        int cnt = 0;
+        int l = 0, r = 0; // two pointers
+        int prod = 1;
+        while (r < nums.size()) {
+            prod *= nums[r];
+            while (prod >= k && l <= r) {
+                prod /= nums[l];
+                l++;
+            }
+            cnt += r - l + 1;
+            r++;
+        }
+        return cnt;
+    }
+};
+
+/* END */
+
+#include <cstdio>
+
+int main() {
+    Solution *sol = new Solution();
+    vector<int> vec = {10, 5, 2, 6};
+
+    int ans = sol->numSubarrayProductLessThanK(vec, 100);
+    printf("%d\n", ans);
+
+    delete sol;
+    return 0;
+}