Перейти к содержимому

Maximum Alternating Sum of Squares | LeetCode 3727 Daily | Greedy + Sorting

Prateek Goel

0:00 / 0:00

Maximum Alternating Sum of Squares | LeetCode 3727 Daily | Greedy + Sorting

66 просмотров · 11 ч назад
Prateek Goel
1,68 тыс. подписчиков
66 просмотров · 11 ч назад
Today's LeetCode Daily (13 Oct 2026): 3727. Maximum Alternating Sum of Squares (Medium), solved three ways, from the first idea to the interview answer. The trick: positions don't matter, only which numbers you subtract. Squares erase the signs, and every number on a minus slot costs you twice. So the score is the total of all squares minus two times the smallest floor(n/2) of them. What you'll learn • Brute force: try every rearrangement, all 720 orders for 6 numbers, O(n! × n) • Better: square, sort, answer = total − 2 × the smallest floor(n/2) squares, O(n log n) • Optimal: counting buckets on |v| ≤ 40000, no sort needed, O(n + M) • Why the greedy is right, plus the edge cases: n = 1, zeros, equal values, the extremes Chapters 0:00 Intro: 720 orders, only 7 scores 0:22 The problem and Example 1 1:27 Brute force: try every order 3:34 What decides the score: total − 2 × minus-sum 5:09 Better: sort the squares, O(n log n) 6:35 Optimal: counting buckets, O(n + M) 9:22 Complexity comparison 9:51 Edge cases 11:01 Takeaways Problem: https://leetcode.com/problems/maximum... More visual DSA walkthroughs: https://growasengineers.com #leetcode #leetcodedaily #dsa #python #codinginterview