↓Skip to main content
  1. LeetCode/

LeetCode 3834: Merge Adjacent Equal Elements

·2 mins· ·
LeetCode Medium Stack Simulation Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-02-08

  • Submissions: Accepted (first try)

Approach
#

The rule: repeatedly take the leftmost pair of equal adjacent elements and replace it with their sum, until no adjacent pair is equal.

Following the rule literally means searching from the start each time, O(n^2).

Process left to right with a stack holding “the finished part with no equal neighbors”. When a new element equals the top, merge them into double; the merged value may equal the new top, so keep merging with a while.

Why this matches “merge the leftmost pair each time”: the stack never contains equal neighbors, so the only possible equal pair is the new element and the one to its left, which is the leftmost pair.

My Solution
#

class Solution:
    def mergeAdjacent(self, nums: List[int]) -> List[int]:
        stack = []
        for x in nums:
            while stack and stack[-1] == x:
                x += stack.pop()
            stack.append(x)
        return stack

My original code pushed first and then checked while len(stack) > 1 and stack[-1] == stack[-2], which is equivalent. Merging before pushing saves a pop and a push.

Complexity
#

Time O(n): each merge removes one element, so there are at most n merges. Space O(n).

Mistakes to Avoid
#

  • Stopping after one merge: the merged value may equal the previous element, so use while.
  • Following the rule literally and searching from the start each time: O(n^2).

Interview Explanation
#

I process the array left to right with a stack that never holds equal neighbors. If a new element equals the top, I merge them into double, and keep merging while it equals the new top, then push it. Since the stack has no equal neighbors, each merge is the leftmost pair, matching the rule. Every merge removes an element, so the total is O(n).

Related

LeetCode 735: Asteroid Collision
·2 mins
LeetCode Medium Array Stack Simulation
LeetCode 735 note: scan left to right with a stack; only a right-moving top meeting a left-moving asteroid collides, the smaller explodes and equal sizes both do
LeetCode 853: Car Fleet
·2 mins
LeetCode Daily Medium Array Stack Greedy Simulation Sorting
Solving the Car Fleet problem using stack-based approach
LeetCode 2105: Watering Plants II
·2 mins
LeetCode Medium Two-Pointers Simulation Array
LeetCode 2105 note: Alice from the left and Bob from the right, simulated with converging pointers; when they meet at the same plant, whoever has more water waters it
LeetCode 3819: Rotate Non Negative Elements
·1 min
LeetCode Medium Array Simulation
LeetCode 3819 note: pull out the non-negative values, rotate them left by k, and put them back into the non-negative positions in order; with none, do not take a modulo by 0
LeetCode 1222: Queens That Can Attack the King
·2 mins
LeetCode Medium Array Matrix Simulation
LeetCode 1222 note: step outward from the king in eight directions; the first queen met in each direction can attack, with queens in a set for lookups
LeetCode 3494: Find the Minimum Amount of Time to Brew Potions
·2 mins
LeetCode Medium Simulation Prefix-Sum Array
LeetCode 3494 note: for each potion, push forward to get the last wizard’s finish time (everyone must be free), then subtract backward to get each wizard’s finish time