Skip to content

Commit a93e761

Browse files
committed
feat: add Majority Element
1 parent 49412c3 commit a93e761

File tree

7 files changed

+193
-1
lines changed

7 files changed

+193
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"problem_name": "majority_element",
3+
"solution_class_name": "Solution",
4+
"problem_number": "169",
5+
"problem_title": "Majority Element",
6+
"difficulty": "Easy",
7+
"topics": "Array, Hash Table, Divide and Conquer, Sorting, Counting",
8+
"tags": ["grind-75"],
9+
"readme_description": "Given an array `nums` of size `n`, return the majority element.\n\nThe majority element is the element that appears more than `\u230an / 2\u230b` times. You may assume that the majority element always exists in the array.",
10+
"readme_examples": [
11+
{ "content": "```\nInput: nums = [3,2,3]\nOutput: 3\n```" },
12+
{ "content": "```\nInput: nums = [2,2,1,1,1,2,2]\nOutput: 2\n```" }
13+
],
14+
"readme_constraints": "- n == nums.length\n- 1 <= n <= 5 * 10^4\n- -10^9 <= nums[i] <= 10^9",
15+
"readme_additional": "**Follow-up:** Could you solve the problem in linear time and in O(1) space?",
16+
"solution_imports": "",
17+
"solution_methods": [
18+
{
19+
"name": "majority_element",
20+
"parameters": "nums: list[int]",
21+
"return_type": "int",
22+
"dummy_return": "0"
23+
}
24+
],
25+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
26+
"test_class_name": "MajorityElement",
27+
"test_helper_methods": [
28+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
29+
],
30+
"test_methods": [
31+
{
32+
"name": "test_majority_element",
33+
"parametrize": "nums, expected",
34+
"parametrize_typed": "nums: list[int], expected: int",
35+
"test_cases": "[([3,2,3], 3), ([2,2,1,1,1,2,2], 2), ([1], 1), ([1,1,2], 1), ([2,2,2,1,1], 2)]",
36+
"body": "result = self.solution.majority_element(nums)\nassert result == expected"
37+
}
38+
],
39+
"playground_imports": "from solution import Solution",
40+
"playground_test_case": "# Example test case\nnums = [3,2,3]\nexpected = 3",
41+
"playground_execution": "result = Solution().majority_element(nums)\nresult",
42+
"playground_assertion": "assert result == expected"
43+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHON_VERSION = 3.13
2-
PROBLEM ?= climbing_stairs
2+
PROBLEM ?= majority_element
33
FORCE ?= 0
44
COMMA := ,
55

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Majority Element
2+
3+
**Difficulty:** Easy
4+
**Topics:** Array, Hash Table, Divide and Conquer, Sorting, Counting
5+
**Tags:** grind-75
6+
7+
**LeetCode:** [Problem 169](https://leetcode.com/problems/majority-element/description/)
8+
9+
## Problem Description
10+
11+
Given an array `nums` of size `n`, return the majority element.
12+
13+
The majority element is the element that appears more than `⌊n / 2⌋` times. You may assume that the majority element always exists in the array.
14+
15+
## Examples
16+
17+
### Example 1:
18+
19+
```
20+
Input: nums = [3,2,3]
21+
Output: 3
22+
```
23+
24+
### Example 2:
25+
26+
```
27+
Input: nums = [2,2,1,1,1,2,2]
28+
Output: 2
29+
```
30+
31+
## Constraints
32+
33+
- n == nums.length
34+
- 1 <= n <= 5 \* 10^4
35+
- -10^9 <= nums[i] <= 10^9
36+
37+
**Follow-up:** Could you solve the problem in linear time and in O(1) space?

leetcode/majority_element/__init__.py

Whitespace-only changes.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "imports",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from solution import Solution"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"id": "setup",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Example test case\n",
21+
"nums = [3, 2, 3]\n",
22+
"expected = 3"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 3,
28+
"id": "execute",
29+
"metadata": {},
30+
"outputs": [
31+
{
32+
"data": {
33+
"text/plain": [
34+
"3"
35+
]
36+
},
37+
"execution_count": 3,
38+
"metadata": {},
39+
"output_type": "execute_result"
40+
}
41+
],
42+
"source": [
43+
"result = Solution().majority_element(nums)\n",
44+
"result"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 4,
50+
"id": "test",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"assert result == expected"
55+
]
56+
}
57+
],
58+
"metadata": {
59+
"kernelspec": {
60+
"display_name": "leetcode-py-py3.13",
61+
"language": "python",
62+
"name": "python3"
63+
},
64+
"language_info": {
65+
"codemirror_mode": {
66+
"name": "ipython",
67+
"version": 3
68+
},
69+
"file_extension": ".py",
70+
"mimetype": "text/x-python",
71+
"name": "python",
72+
"nbconvert_exporter": "python",
73+
"pygments_lexer": "ipython3",
74+
"version": "3.13.7"
75+
}
76+
},
77+
"nbformat": 4,
78+
"nbformat_minor": 5
79+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
# Time: O(n)
3+
# Space: O(1)
4+
def majority_element(self, nums: list[int]) -> int:
5+
# Boyer-Moore Voting Algorithm
6+
candidate = 0
7+
count = 0
8+
9+
for num in nums:
10+
if count == 0:
11+
candidate = num
12+
count += 1 if num == candidate else -1
13+
14+
return candidate

leetcode/majority_element/tests.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pytest
2+
3+
from leetcode_py.test_utils import logged_test
4+
5+
from .solution import Solution
6+
7+
8+
class TestMajorityElement:
9+
def setup_method(self):
10+
self.solution = Solution()
11+
12+
@pytest.mark.parametrize(
13+
"nums, expected",
14+
[([3, 2, 3], 3), ([2, 2, 1, 1, 1, 2, 2], 2), ([1], 1), ([1, 1, 2], 1), ([2, 2, 2, 1, 1], 2)],
15+
)
16+
@logged_test
17+
def test_majority_element(self, nums: list[int], expected: int):
18+
result = self.solution.majority_element(nums)
19+
assert result == expected

0 commit comments

Comments
 (0)