From faac42beacc1173e4987094fbb1ec248e0458cd8 Mon Sep 17 00:00:00 2001 From: Joshna Waikar Date: Fri, 6 Oct 2023 09:37:13 -0700 Subject: [PATCH 1/6] Create Majorityelement#229 --- Majorityelement#229 | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Majorityelement#229 diff --git a/Majorityelement#229 b/Majorityelement#229 new file mode 100644 index 0000000..a19b616 --- /dev/null +++ b/Majorityelement#229 @@ -0,0 +1,48 @@ +class Solution { + public List majorityElement(int[] nums) { + List result = new ArrayList<>(); + + // Initialize two candidate elements and their corresponding counters. + int candidate1 = 0, candidate2 = 0; + int count1 = 0, count2 = 0; + + // Phase 1: Find the two potential candidates. + for (int num : nums) { + if (num == candidate1) { + count1++; + } else if (num == candidate2) { + count2++; + } else if (count1 == 0) { + candidate1 = num; + count1 = 1; + } else if (count2 == 0) { + candidate2 = num; + count2 = 1; + } else { + count1--; + count2--; + } + } + + // Phase 2: Count occurrences of the two potential candidates. + count1 = 0; + count2 = 0; + for (int num : nums) { + if (num == candidate1) { + count1++; + } else if (num == candidate2) { + count2++; + } + } + + // Check if the candidates appear more than n/3 times and add them to the result. + if (count1 > nums.length / 3) { + result.add(candidate1); + } + if (count2 > nums.length / 3) { + result.add(candidate2); + } + + return result; + } +} From 1483e6ae686abd2cac202a5cf660f0234576a24d Mon Sep 17 00:00:00 2001 From: Joshna Waikar Date: Fri, 6 Oct 2023 22:15:22 -0700 Subject: [PATCH 2/6] Rename Majorityelement#229 to solution.java --- Majorityelement#229 => Solution.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Majorityelement#229 => Solution.java (100%) diff --git a/Majorityelement#229 b/Solution.java similarity index 100% rename from Majorityelement#229 rename to Solution.java From 4468eba35e87a802dabe187f9768aae5bf6bdd94 Mon Sep 17 00:00:00 2001 From: Joshna Waikar Date: Sat, 7 Oct 2023 00:22:32 -0700 Subject: [PATCH 3/6] Create Solution.cpp --- Medium/Solution.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Medium/Solution.cpp diff --git a/Medium/Solution.cpp b/Medium/Solution.cpp new file mode 100644 index 0000000..12faa27 --- /dev/null +++ b/Medium/Solution.cpp @@ -0,0 +1,35 @@ +class Solution { + public: + vector majorityElement(vector& nums) { + vector ans; + int candidate1 = 0; + int candidate2 = 1; // Any number different from candidate1 + int countSoFar1 = 0; // # of candidate1 so far + int countSoFar2 = 0; // # of candidate2 so far + + for (const int num : nums) + if (num == candidate1) { + ++countSoFar1; + } else if (num == candidate2) { + ++countSoFar2; + } else if (countSoFar1 == 0) { // Assign new candidate + candidate1 = num; + ++countSoFar1; + } else if (countSoFar2 == 0) { // Assign new candidate + candidate2 = num; + ++countSoFar2; + } else { // Meet a new number, so pair out previous counts + --countSoFar1; + --countSoFar2; + } + + const int count1 = count(nums.begin(), nums.end(), candidate1); + const int count2 = count(nums.begin(), nums.end(), candidate2); + + if (count1 > nums.size() / 3) + ans.push_back(candidate1); + if (count2 > nums.size() / 3) + ans.push_back(candidate2); + return ans; + } +}; From 196d8a7d8a9fd053a6450eaac72e89951b7ab213 Mon Sep 17 00:00:00 2001 From: Joshna Waikar Date: Sat, 7 Oct 2023 13:10:45 +0530 Subject: [PATCH 4/6] Majority Element II --- Medium/Readme.md | 19 +++++++++++++++++++ Medium/solution.cpp | 35 +++++++++++++++++++++++++++++++++++ Medium/solution.java | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 Medium/Readme.md create mode 100644 Medium/solution.cpp create mode 100644 Medium/solution.java diff --git a/Medium/Readme.md b/Medium/Readme.md new file mode 100644 index 0000000..0d84f28 --- /dev/null +++ b/Medium/Readme.md @@ -0,0 +1,19 @@ +Problem #229 (Majority Element II | Array, Counting, Hash Table, Sorting) +Medium + +Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ 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*104 +. -109<=nums[i]<=10^9 \ No newline at end of file diff --git a/Medium/solution.cpp b/Medium/solution.cpp new file mode 100644 index 0000000..3c811fa --- /dev/null +++ b/Medium/solution.cpp @@ -0,0 +1,35 @@ +class Solution { + public: + vector majorityElement(vector& nums) { + vector ans; + int candidate1 = 0; + int candidate2 = 1; // Any number different from candidate1 + int countSoFar1 = 0; // # of candidate1 so far + int countSoFar2 = 0; // # of candidate2 so far + + for (const int num : nums) + if (num == candidate1) { + ++countSoFar1; + } else if (num == candidate2) { + ++countSoFar2; + } else if (countSoFar1 == 0) { // Assign new candidate + candidate1 = num; + ++countSoFar1; + } else if (countSoFar2 == 0) { // Assign new candidate + candidate2 = num; + ++countSoFar2; + } else { // Meet a new number, so pair out previous counts + --countSoFar1; + --countSoFar2; + } + + const int count1 = count(nums.begin(), nums.end(), candidate1); + const int count2 = count(nums.begin(), nums.end(), candidate2); + + if (count1 > nums.size() / 3) + ans.push_back(candidate1); + if (count2 > nums.size() / 3) + ans.push_back(candidate2); + return ans; + } +}; diff --git a/Medium/solution.java b/Medium/solution.java new file mode 100644 index 0000000..4a034d6 --- /dev/null +++ b/Medium/solution.java @@ -0,0 +1,40 @@ +class Solution { + public List majorityElement(int[] nums) { + List ans = new ArrayList<>(); + int candidate1 = 0; + int candidate2 = 1; // Any number different from candidate1 + int countSoFar1 = 0; // # of candidate1 so far + int countSoFar2 = 0; // # of candidate2 so far + + for (final int num : nums) + if (num == candidate1) { + ++countSoFar1; + } else if (num == candidate2) { + ++countSoFar2; + } else if (countSoFar1 == 0) { // Assign new candidate + candidate1 = num; + ++countSoFar1; + } else if (countSoFar2 == 0) { // Assign new candidate + candidate2 = num; + ++countSoFar2; + } else { // Meet a new number, so pair out previous counts + --countSoFar1; + --countSoFar2; + } + + int count1 = 0; + int count2 = 0; + + for (final int num : nums) + if (num == candidate1) + ++count1; + else if (num == candidate2) + ++count2; + + if (count1 > nums.length / 3) + ans.add(candidate1); + if (count2 > nums.length / 3) + ans.add(candidate2); + return ans; + } +} From 54867ff155a986863f3fce5bf69f8f76a045ee6b Mon Sep 17 00:00:00 2001 From: Joshna Waikar Date: Sat, 7 Oct 2023 04:53:59 -0700 Subject: [PATCH 5/6] Delete Medium/Solution.cpp --- Medium/Solution.cpp | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 Medium/Solution.cpp diff --git a/Medium/Solution.cpp b/Medium/Solution.cpp deleted file mode 100644 index 12faa27..0000000 --- a/Medium/Solution.cpp +++ /dev/null @@ -1,35 +0,0 @@ -class Solution { - public: - vector majorityElement(vector& nums) { - vector ans; - int candidate1 = 0; - int candidate2 = 1; // Any number different from candidate1 - int countSoFar1 = 0; // # of candidate1 so far - int countSoFar2 = 0; // # of candidate2 so far - - for (const int num : nums) - if (num == candidate1) { - ++countSoFar1; - } else if (num == candidate2) { - ++countSoFar2; - } else if (countSoFar1 == 0) { // Assign new candidate - candidate1 = num; - ++countSoFar1; - } else if (countSoFar2 == 0) { // Assign new candidate - candidate2 = num; - ++countSoFar2; - } else { // Meet a new number, so pair out previous counts - --countSoFar1; - --countSoFar2; - } - - const int count1 = count(nums.begin(), nums.end(), candidate1); - const int count2 = count(nums.begin(), nums.end(), candidate2); - - if (count1 > nums.size() / 3) - ans.push_back(candidate1); - if (count2 > nums.size() / 3) - ans.push_back(candidate2); - return ans; - } -}; From 107fde25120959de0c0b3924c8128b1de3f760e8 Mon Sep 17 00:00:00 2001 From: Joshna Waikar Date: Sat, 7 Oct 2023 05:11:50 -0700 Subject: [PATCH 6/6] Delete Solution.java --- Solution.java | 48 ------------------------------------------------ 1 file changed, 48 deletions(-) delete mode 100644 Solution.java diff --git a/Solution.java b/Solution.java deleted file mode 100644 index a19b616..0000000 --- a/Solution.java +++ /dev/null @@ -1,48 +0,0 @@ -class Solution { - public List majorityElement(int[] nums) { - List result = new ArrayList<>(); - - // Initialize two candidate elements and their corresponding counters. - int candidate1 = 0, candidate2 = 0; - int count1 = 0, count2 = 0; - - // Phase 1: Find the two potential candidates. - for (int num : nums) { - if (num == candidate1) { - count1++; - } else if (num == candidate2) { - count2++; - } else if (count1 == 0) { - candidate1 = num; - count1 = 1; - } else if (count2 == 0) { - candidate2 = num; - count2 = 1; - } else { - count1--; - count2--; - } - } - - // Phase 2: Count occurrences of the two potential candidates. - count1 = 0; - count2 = 0; - for (int num : nums) { - if (num == candidate1) { - count1++; - } else if (num == candidate2) { - count2++; - } - } - - // Check if the candidates appear more than n/3 times and add them to the result. - if (count1 > nums.length / 3) { - result.add(candidate1); - } - if (count2 > nums.length / 3) { - result.add(candidate2); - } - - return result; - } -}