Advertisement

Majority Element II - LeetCode 229 Solution

Majority Element II - Complete Solution Guide

Majority Element II is LeetCode problem 229, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer array of size n , find all elements that appear more than &lfloor; n/3 &rfloor; times. Example 1: Input: nums = [3,2,3] Output: [3] Example 2: Input: nums = [1] Output: [1] Example 3: Input: nums = [1,2] Output: [1,2] Constraints: 1 <= nums.length <= 5 * 10 4 -10 9 <= nums[i] <= 10 9 Follow up: Could you solve the problem in linear time and in O(1) space?

Detailed Explanation

The problem asks us to find all elements in an integer array that appear more than `⌊ n/3 ⌋` times, where `n` is the length of the array. The problem specifies that the output should be a list of these 'majority elements'. The constraints indicate the array size can be up to 50,000 and element values are between -10^9 and 10^9. The follow up question challenges us to find a solution that uses linear time complexity and constant space complexity.

Solution Approach

The provided code implements the Boyer-Moore Majority Vote Algorithm to find up to two potential majority elements. The algorithm works in two phases. First, it iterates through the array, maintaining two candidates and their counts. If the current element matches a candidate, its count is incremented. If a count is zero, the current element becomes a new candidate. If the current element doesn't match either candidate and both counts are non-zero, both counts are decremented. The second phase verifies the counts of the candidates to ensure they indeed appear more than `⌊ n/3 ⌋` times, and returns the candidates that meet the condition.

Step-by-Step Algorithm

  1. Step 1: Initialize two candidate variables (`candidate1`, `candidate2`) and their corresponding count variables (`count1`, `count2`) to `None` (or 0 for primitive types in Java and C/C++) and 0, respectively.
  2. Step 2: Iterate through the input array `nums`. For each element `num`:
  3. Step 3: If `num` is equal to `candidate1`, increment `count1`.
  4. Step 4: Else if `num` is equal to `candidate2`, increment `count2`.
  5. Step 5: Else if `count1` is 0, assign `num` to `candidate1` and set `count1` to 1.
  6. Step 6: Else if `count2` is 0, assign `num` to `candidate2` and set `count2` to 1.
  7. Step 7: Else, decrement both `count1` and `count2`.
  8. Step 8: After the first pass, reset `count1` and `count2` to 0.
  9. Step 9: Iterate through the input array `nums` again. Count the actual occurrences of `candidate1` and `candidate2`.
  10. Step 10: Create a list (or array) to store the results.
  11. Step 11: If `count1` is greater than `⌊ n/3 ⌋`, add `candidate1` to the result.
  12. Step 12: If `count2` is greater than `⌊ n/3 ⌋` and `candidate2` is different from `candidate1`, add `candidate2` to the result.
  13. Step 13: Return the result list (or array).

Key Insights

  • Insight 1: An array can have at most two elements that appear more than `⌊ n/3 ⌋` times. If there were three such elements, the sum of their counts would be greater than `n`, which is impossible.
  • Insight 2: The Boyer-Moore Majority Vote Algorithm can be adapted to find up to two potential majority elements efficiently. This algorithm helps to identify candidate elements without requiring extra space.
  • Insight 3: After identifying the candidate majority elements, a second pass through the array is needed to verify if their counts are actually greater than `⌊ n/3 ⌋`.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Hash Table, Sorting, Counting.

Companies

Asked at: Accenture, Atlassian, Darwinbox, Salesforce, Zenefits.