progress

~/problems / Arrays & hashing / Complexity analysis

Cross-team handshake energy

On a phone? Coding is easier on a laptop: email this problem to yourself . Meanwhile: quiz this topic or fight a boss.

easy ~20 min

At a company mixer, guest i has energy energy[i] and team team[i]. Every two guests from different teams shake hands once, making energy[i] * energy[j] buzz. Teammates skip it (they see each other every day).

Write mixer_buzz(energy: list[int], team: list[str]) -> int that returns the total buzz.

mixer_buzz([2, 3, 5], ["red", "blue", "red"])
# pairs from different teams: (0,1) -> 6, (1,2) -> 15; total 21

mixer_buzz([4, 1], ["ops", "ops"])   # 0, nobody shakes hands
  • 0 <= len(energy) == len(team) <= 2 * 10^5; energies are in [-10^4, 10^4].
  • ⭐ Bonus: a speed test with 200,000 guests earns a star in one pass (O(n)).
Show hint

guest j adds energy[j] times the total energy of earlier guests on other teams. Keep a running total overall and one per team.

Topic: Complexity analysis. Big-O from constraints: n = 10^5 means O(n log n); know the Python constant factors.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc