Skip to content

Commit 074d297

Browse files
committed
daily
1 parent 404b278 commit 074d297

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

problems/problem_1052/solution_1.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
6+
class Solution:
7+
def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
8+
tmp = sum(grumpy[i] * customers[i] for i in range(minutes))
9+
non_satisfied = tmp
10+
satisfied = 0
11+
12+
for i in range(len(customers) - minutes):
13+
satisfied += customers[i] * (grumpy[i] ^ 1)
14+
tmp += -grumpy[i] * customers[i] + grumpy[i + minutes] * customers[i + minutes]
15+
non_satisfied = max(non_satisfied, tmp)
16+
17+
for i in range(len(customers) - minutes, len(customers)):
18+
satisfied += customers[i] * (grumpy[i] ^ 1)
19+
20+
return satisfied + non_satisfied
21+
22+
23+
tests = [
24+
(
25+
([1, 0, 1, 2, 1, 1, 7, 5], [0, 1, 0, 1, 0, 1, 0, 1], 3),
26+
16,
27+
),
28+
(
29+
([1], [0], 1),
30+
1,
31+
),
32+
]
33+
34+
35+
@pytest.mark.timeout(2)
36+
@pytest.mark.parametrize(
37+
"inputs, expected",
38+
tests,
39+
)
40+
def test_validator(inputs, expected):
41+
output = Solution().maxSatisfied(*inputs)
42+
assert output == expected

problems/problem_1052/solution_2.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List
2+
import numpy as np
3+
import pytest
4+
5+
6+
class Solution:
7+
def maxSatisfied(self, c: List[int], g: List[int], m: int) -> int:
8+
c = np.array(c)
9+
g = np.array(g)
10+
return int((c * (g ^ 1)).sum() + np.convolve(c * g, np.ones(m), mode="valid").max())
11+
12+
13+
tests = [
14+
(
15+
([1, 0, 1, 2, 1, 1, 7, 5], [0, 1, 0, 1, 0, 1, 0, 1], 3),
16+
16,
17+
),
18+
(
19+
([1], [0], 1),
20+
1,
21+
),
22+
]
23+
24+
25+
@pytest.mark.timeout(2)
26+
@pytest.mark.parametrize("inputs, expected", tests)
27+
def test_validator(inputs, expected):
28+
output = Solution().maxSatisfied(*inputs)
29+
assert output == expected

0 commit comments

Comments
 (0)