From af759ea0e7cbd3a2870e80135d863eb9f49f31be Mon Sep 17 00:00:00 2001 From: Ben Makusha Date: Wed, 4 Oct 2023 23:45:30 -0400 Subject: [PATCH] Create 0229-majority-element-ii.java --- java/0229-majority-element-ii.java | 65 ++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 java/0229-majority-element-ii.java diff --git a/java/0229-majority-element-ii.java b/java/0229-majority-element-ii.java new file mode 100644 index 000000000..389223c9f --- /dev/null +++ b/java/0229-majority-element-ii.java @@ -0,0 +1,65 @@ +class Solution { + /** + * First solution utilizes a hashmap and then does the due diligience of adding the + * appropriate values that appear more than n/3 times + * Runtime O(n) : Space O(n) + */ + public List majorityElement(int[] nums) { + List res = new ArrayList<>(); + Map map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + if (map.containsKey(nums[i])) + map.put(nums[i], map.get(nums[i]) + 1); + else + map.put(nums[i], 1); + } + + for (Map.Entry entry: map.entrySet()) { + int potentialCandidate = entry.getValue(); + if (potentialCandidate > nums.length / 3) + res.add(entry.getKey()); + } + + return res; + } + + + /** + * This is called Boyer-Moore Vote algorithm and the idea here is having candidates + * with diff values and two counters. + * For each number in the array we see if it equals the candidate and increment the count. + * The two numbers left after this process are the majority candidates. + * Loop through the array again then make sure that each candidate does indeed have more than n/3 occurrences + * + * Runtime O(n) : Space O(1) + */ + public List majorityElement_2(int[] nums) { + int candidate1 = 0, candidate2 = 0, count1 = 0, count2 = 0; + + for (int num : nums) { + if (num == candidate1) count1++; + else if (num == candidate2) count2++; + else if (count1 == 0) { + candidate1 = num; + count1++; + } else if (count2 == 0) { + candidate2 = num; + count2++; + } else { + count1--; + count2--; + } + } + + count1 = count2 = 0; + for (int num : nums) { + if (num == candidate1) count1++; + else if (num == candidate2) count2++; + } + + List res = new ArrayList<>(); + if (count1 > nums.length / 3) res.add(candidate1); + if (count2 > nums.length / 3) res.add(candidate2); + return res; + } +} \ No newline at end of file