Skip to content

Commit f0b051d

Browse files
committed
Feb-14
1 parent 9c7ee88 commit f0b051d

File tree

36 files changed

+889
-330
lines changed

36 files changed

+889
-330
lines changed

README.md

Lines changed: 17 additions & 10 deletions
Large diffs are not rendered by default.

leetcode/1052.grumpy-bookstore-owner.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,16 @@ class Solution {
3939
int n = grumpy.size();
4040
int res = 0;
4141
int count = 0;
42-
int i = 0;
43-
for (int j = 0; j < n; j++) {
44-
if (grumpy[j]) {
45-
count += customers[j];
42+
for (int i = 0; i < n; i++) {
43+
if (grumpy[i]) {
44+
count += customers[i];
4645
}
4746

48-
if (j - i + 1 == minutes) {
47+
if (i >= minutes - 1) {
4948
res = max(res, count);
50-
if (grumpy[i]) {
51-
count -= customers[i];
49+
if (grumpy[i - minutes + 1]) {
50+
count -= customers[i - minutes + 1];
5251
}
53-
i += 1;
5452
}
5553
}
5654

leetcode/1052.grumpy-bookstore-owner.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@ def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) ->
3838
n = len(grumpy)
3939
count = 0
4040
res = 0
41-
i = 0
42-
for j in range(n):
43-
if grumpy[j] == 1:
44-
count += customers[j]
41+
for i in range(n):
42+
if grumpy[i] == 1:
43+
count += customers[i]
4544

46-
if j - i + 1 == minutes:
45+
if i >= minutes - 1:
4746
res = max(res, count)
48-
if grumpy[i] == 1:
49-
count -= customers[i]
50-
i += 1
47+
if grumpy[i - minutes + 1] == 1:
48+
count -= customers[i - minutes + 1]
5149

5250
for i in range(n):
5351
if grumpy[i] == 0:

leetcode/1343.number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ class Solution {
3434
int n = arr.size();
3535
int res = 0;
3636
int total = 0;
37-
int i = 0;
38-
for (int j = 0; j < n; j++) {
39-
total += arr[j];
40-
if (j - i + 1 == k) {
37+
for (int i = 0; i < n; i++) {
38+
total += arr[i];
39+
if (i >= k - 1) {
4140
if (total >= threshold * k) {
4241
res += 1;
4342
}
44-
total -= arr[i++];
43+
total -= arr[i - k + 1];
4544
}
4645
}
4746
return res;

leetcode/1343.number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
3333
n = len(arr)
3434
total = 0
3535
res = 0
36-
i = 0
37-
for j in range(n):
38-
total += arr[j]
39-
if j - i + 1 == k:
36+
for i in range(n):
37+
total += arr[i]
38+
if i >= k - 1:
4039
if total >= threshold * k:
4140
res += 1
42-
total -= arr[i]
43-
i += 1
41+
total -= arr[i - k + 1]
4442

4543
return res
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Tag: Array, Math, Design, Data Stream, Prefix Sum
2+
// Time: O(1)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/yu5VfA1Tp98
7+
8+
// Design an algorithm that accepts a stream of integers and retrieves the product of the last k integers of the stream.
9+
// Implement the ProductOfNumbers class:
10+
//
11+
// ProductOfNumbers() Initializes the object with an empty stream.
12+
// void add(int num) Appends the integer num to the stream.
13+
// int getProduct(int k) Returns the product of the last k numbers in the current list. You can assume that always the current list has at least k numbers.
14+
//
15+
// The test cases are generated so that, at any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing.
16+
//  
17+
// Example:
18+
//
19+
// Input
20+
// ["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"]
21+
// [[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]]
22+
//
23+
// Output
24+
// [null,null,null,null,null,null,20,40,0,null,32]
25+
//
26+
// Explanation
27+
// ProductOfNumbers productOfNumbers = new ProductOfNumbers();
28+
// productOfNumbers.add(3); // [3]
29+
// productOfNumbers.add(0); // [3,0]
30+
// productOfNumbers.add(2); // [3,0,2]
31+
// productOfNumbers.add(5); // [3,0,2,5]
32+
// productOfNumbers.add(4); // [3,0,2,5,4]
33+
// productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 * 4 = 20
34+
// productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40
35+
// productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0
36+
// productOfNumbers.add(8); // [3,0,2,5,4,8]
37+
// productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32
38+
//
39+
//  
40+
// Constraints:
41+
//
42+
// 0 <= num <= 100
43+
// 1 <= k <= 4 * 104
44+
// At most 4 * 104 calls will be made to add and getProduct.
45+
// The product of the stream at any point in time will fit in a 32-bit integer.
46+
//
47+
//  
48+
// Follow-up: Can you implement both GetProduct and Add to work in O(1) time complexity instead of O(k) time complexity?
49+
50+
class ProductOfNumbers {
51+
public:
52+
vector<int> prefix = {1};
53+
ProductOfNumbers() {
54+
55+
}
56+
57+
void add(int num) {
58+
if (num == 0) {
59+
prefix = {1};
60+
} else {
61+
prefix.push_back(prefix.back() * num);
62+
}
63+
}
64+
65+
int getProduct(int k) {
66+
int n = prefix.size();
67+
if (k >= n) {
68+
return 0;
69+
}
70+
return prefix[n - 1] / prefix[n - k - 1];
71+
}
72+
};
73+
74+
/**
75+
* Your ProductOfNumbers object will be instantiated and called as such:
76+
* ProductOfNumbers* obj = new ProductOfNumbers();
77+
* obj->add(num);
78+
* int param_2 = obj->getProduct(k);
79+
*/
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Tag: Array, Math, Design, Data Stream, Prefix Sum
2+
# Time: O(1)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/yu5VfA1Tp98
7+
8+
# Design an algorithm that accepts a stream of integers and retrieves the product of the last k integers of the stream.
9+
# Implement the ProductOfNumbers class:
10+
#
11+
# ProductOfNumbers() Initializes the object with an empty stream.
12+
# void add(int num) Appends the integer num to the stream.
13+
# int getProduct(int k) Returns the product of the last k numbers in the current list. You can assume that always the current list has at least k numbers.
14+
#
15+
# The test cases are generated so that, at any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing.
16+
#  
17+
# Example:
18+
#
19+
# Input
20+
# ["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"]
21+
# [[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]]
22+
#
23+
# Output
24+
# [null,null,null,null,null,null,20,40,0,null,32]
25+
#
26+
# Explanation
27+
# ProductOfNumbers productOfNumbers = new ProductOfNumbers();
28+
# productOfNumbers.add(3); // [3]
29+
# productOfNumbers.add(0); // [3,0]
30+
# productOfNumbers.add(2); // [3,0,2]
31+
# productOfNumbers.add(5); // [3,0,2,5]
32+
# productOfNumbers.add(4); // [3,0,2,5,4]
33+
# productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 * 4 = 20
34+
# productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40
35+
# productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0
36+
# productOfNumbers.add(8); // [3,0,2,5,4,8]
37+
# productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32
38+
#
39+
#  
40+
# Constraints:
41+
#
42+
# 0 <= num <= 100
43+
# 1 <= k <= 4 * 104
44+
# At most 4 * 104 calls will be made to add and getProduct.
45+
# The product of the stream at any point in time will fit in a 32-bit integer.
46+
#
47+
#  
48+
# Follow-up: Can you implement both GetProduct and Add to work in O(1) time complexity instead of O(k) time complexity?
49+
50+
class ProductOfNumbers:
51+
52+
def __init__(self):
53+
self.prefix = [1]
54+
55+
def add(self, num: int) -> None:
56+
if num == 0:
57+
self.prefix = [1]
58+
else:
59+
self.prefix.append(self.prefix[-1] * num)
60+
61+
def getProduct(self, k: int) -> int:
62+
n = len(self.prefix)
63+
if k >= n:
64+
return 0
65+
66+
return self.prefix[n - 1] // self.prefix[n - k - 1]
67+
68+
69+
# Your ProductOfNumbers object will be instantiated and called as such:
70+
# obj = ProductOfNumbers()
71+
# obj.add(num)
72+
# param_2 = obj.getProduct(k)

leetcode/1423.maximum-points-you-can-obtain-from-cards.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,24 @@ class Solution {
4040
public:
4141
int maxScore(vector<int>& cardPoints, int k) {
4242
int n = cardPoints.size();
43+
k = n - k;
4344
int total = 0;
4445
int tmp = 0;
4546
int res = INT_MAX;
46-
int i = 0;
47-
for (int j = 0; j < n; j++) {
48-
total += cardPoints[j];
49-
tmp += cardPoints[j];
50-
if (j - i + 1 == n - k) {
47+
48+
for (int i = 0; i < n; i++) {
49+
if (i >= k) {
50+
tmp -= cardPoints[i - k];
51+
}
52+
53+
total += cardPoints[i];
54+
tmp += cardPoints[i];
55+
56+
if (i >= k - 1) {
5157
res = min(res, tmp);
52-
tmp -= cardPoints[i];
53-
i += 1;
5458
}
5559
}
5660

57-
return n == k ? total : total - res;
61+
return total - res;
5862
}
5963
};

leetcode/1423.maximum-points-you-can-obtain-from-cards.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,19 @@
3939
class Solution:
4040
def maxScore(self, cardPoints: List[int], k: int) -> int:
4141
n = len(cardPoints)
42+
k = n - k
4243
res = float('inf')
4344
tmp = 0
4445
total = 0
45-
i = 0
4646

47-
for j in range(n):
48-
total += cardPoints[j]
49-
tmp += cardPoints[j]
47+
for i in range(n):
48+
if i >= k:
49+
tmp -= cardPoints[i - k]
5050

51-
if j - i + 1 == n - k:
52-
res = min(res, tmp)
53-
tmp -= cardPoints[i]
54-
i += 1
51+
total += cardPoints[i]
52+
tmp += cardPoints[i]
5553

56-
return total if k == n else total - res
54+
if i >= k - 1:
55+
res = min(res, tmp)
56+
57+
return total - res

leetcode/1456.maximum-number-of-vowels-in-a-substring-of-given-length.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,16 @@ class Solution {
4040
int n = s.size();
4141
int res = 0;
4242
int count = 0;
43-
int i = 0;
44-
for (int j = 0; j < n; j++) {
45-
if (isVowel(s[j])) {
43+
for (int i = 0; i < n; i++) {
44+
if (isVowel(s[i])) {
4645
count += 1;
4746
}
4847

49-
if (j - i + 1 == k) {
48+
if (i >= k - 1) {
5049
res = max(res, count);
51-
if (isVowel(s[i])) {
50+
if (isVowel(s[i - k + 1])) {
5251
count -= 1;
5352
}
54-
i += 1;
5553
}
5654
}
5755

leetcode/1456.maximum-number-of-vowels-in-a-substring-of-given-length.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,15 @@
3737
class Solution:
3838
def maxVowels(self, s: str, k: int) -> int:
3939
n = len(s)
40-
i = 0
4140
count = 0
4241
res = 0
4342
vowels = {'a','e','i','o','u'}
44-
for j in range(n):
45-
if s[j] in vowels:
43+
for i in range(n):
44+
if s[i] in vowels:
4645
count += 1
4746

48-
if j - i + 1 == k:
47+
if i >= k - 1:
4948
res = max(res, count)
50-
if s[i] in vowels:
49+
if s[i - k + 1] in vowels:
5150
count -= 1
52-
i += 1
5351
return res

0 commit comments

Comments
 (0)