Skip to content

Commit b0f3b99

Browse files
committed
Runtime: 3346 ms (Top 89.52%) | Memory: 46.9 MB (Top 30.64%)
1 parent fdf30e5 commit b0f3b99

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
# Runtime: 3346 ms (Top 89.52%) | Memory: 46.9 MB (Top 30.64%)
12
class Solution:
23
def waysToPartition(self, nums: List[int], k: int) -> int:
34
prefix_sums = list(accumulate(nums))
45
total_sum = prefix_sums[-1]
56
best = 0
67
if total_sum % 2 == 0:
7-
best = prefix_sums[:-1].count(total_sum // 2) # If no change
8+
best = prefix_sums[:-1].count(total_sum // 2) # If no change
89

910
after_counts = Counter(total_sum - 2 * prefix_sum
1011
for prefix_sum in prefix_sums[:-1])
1112
before_counts = Counter()
1213

13-
best = max(best, after_counts[k - nums[0]]) # If we change first num
14+
best = max(best, after_counts[k - nums[0]]) # If we change first num
1415

1516
for prefix, x in zip(prefix_sums, nums[1:]):
1617
gap = total_sum - 2 * prefix
@@ -19,4 +20,4 @@ def waysToPartition(self, nums: List[int], k: int) -> int:
1920

2021
best = max(best, after_counts[k - x] + before_counts[x - k])
2122

22-
return best
23+
return best

0 commit comments

Comments
 (0)