title
stringlengths
1
100
titleSlug
stringlengths
3
77
Java
int64
0
1
Python3
int64
1
1
content
stringlengths
28
44.4k
voteCount
int64
0
3.67k
question_content
stringlengths
65
5k
question_hints
stringclasses
970 values
Tiny LL(1) Compiler in Python3 - Compiles expression to RPN and evaluate
basic-calculator
0
1
# Approach\nFirst generate AST (Abstract Syntax Tree) that contains the information needed to generate RPN, aka reverse polish notation, which is easy to evaluate on. Then doing tree traversal on AST to emit RPN. Finally evaluate the RPN.\n\nThe Backus\u2013Naur Form grammar: \n```\nexpr ::= base term\nterm ::= + base ...
1
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return _the result of the evaluation_. **Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`. **Example 1:** **Input:** s = "1 + 1 " **O...
null
Shunting Yard Algorithm (Simple and Concise)
basic-calculator
0
1
# Intuition\nI was rather disappointed with this problem until I came across the really beautiful algorithm in Wikipedia.\n\nIt is hard to come up with a solution yourself if you are not familiar with [Shunting Yard Algorithm](https://en.wikipedia.org/wiki/Shunting_yard_algorithm) and [Polish Postfix Notation](https://...
1
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return _the result of the evaluation_. **Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`. **Example 1:** **Input:** s = "1 + 1 " **O...
null
[Python 3] Using recursion, without stack
basic-calculator
0
1
```\nclass Solution:\n def calculate(self, s):\n def evaluate(i):\n res, digit, sign = 0, 0, 1\n \n while i < len(s):\n if s[i].isdigit():\n digit = digit * 10 + int(s[i])\n elif s[i] in \'+-\':\n res += digit...
15
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return _the result of the evaluation_. **Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`. **Example 1:** **Input:** s = "1 + 1 " **O...
null
✅ Explained - Simple and Clear Python3 Code✅
basic-calculator
0
1
The solution implements a basic calculator to evaluate a given expression string without using built-in functions like eval().\n\nThe evaluate_expression method handles the addition and subtraction operations within a given expression. It splits the expression at \'+\' characters and then further splits the resulting s...
7
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return _the result of the evaluation_. **Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`. **Example 1:** **Input:** s = "1 + 1 " **O...
null
224: Time 93.3%, Solution with step by step explanation
basic-calculator
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis code implements a basic calculator to evaluate a given expression represented as a string s.\n\nThe algorithm processes the string character by character and maintains three variables ans, num, and sign, and a stack sta...
14
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return _the result of the evaluation_. **Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`. **Example 1:** **Input:** s = "1 + 1 " **O...
null
Simple Python Solution using stack with explanation inline
basic-calculator
0
1
```\n def calculate(self, s: str) -> int:\n """\n 1. Take 3 containers:\n num -> to store current num value only\n sign -> to store sign value, initially +1\n res -> to store sum\n When ( comes these containers used for calculate sum of intergers within () brackets.\n ...
61
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return _the result of the evaluation_. **Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`. **Example 1:** **Input:** s = "1 + 1 " **O...
null
Python (Faster than 98%) | O(N) stack solution
basic-calculator
0
1
![image.png](https://assets.leetcode.com/users/images/96403984-e4da-4825-aacd-83323d7db386_1668934190.883803.png)\n\n\n\n```\nclass Solution:\n def calculate(self, s: str) -> int:\n output, curr, sign, stack = 0, 0, 1, []\n for c in s:\n if c.isdigit():\n curr = (curr * 10) + ...
8
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return _the result of the evaluation_. **Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`. **Example 1:** **Input:** s = "1 + 1 " **O...
null
Lexer, Parser, Interpreter | Abstraction | O(N) time and O(N) space
basic-calculator
0
1
# Intuition\nScanning a language and evaluating it\'s result in another language can be acheived with a lexer, parser, and interpreter.\n\nThis level of abstraction introduces overhead that affects performance, this is more of an exercise on interpreters than an optimized solution.\n\n# Approach\n- **Lexer**: Generat...
2
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return _the result of the evaluation_. **Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`. **Example 1:** **Input:** s = "1 + 1 " **O...
null
Binbin solved another hard question!!! very happy!
basic-calculator
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return _the result of the evaluation_. **Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`. **Example 1:** **Input:** s = "1 + 1 " **O...
null
Python | O(n) | Recursive | Comments added
basic-calculator
0
1
\n```\nclass Solution:\n def calculate(self, expression: str) -> int:\n #recurse when brackets encountered\n def helper(s,index):\n result = 0\n sign = 1\n currnum = 0\n i = index\n while i<len(s):\n c = s[i]\n if c.is...
1
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return _the result of the evaluation_. **Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`. **Example 1:** **Input:** s = "1 + 1 " **O...
null
Beats 92% TC & 97% SC || Python Solution
implement-stack-using-queues
0
1
\n\n# Complexity\n- Time complexity:\n91.54%\n- Space complexity:\n97.2%\n# Code\n```\nclass MyStack:\n\n def __init__(self):\n self.queue = []\n\n def push(self, x: int) -> None:\n self.queue.append(x)\n\n def pop(self) -> int:\n return self.queue.pop()\n\n def top(self) -> int:\n ...
1
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (`push`, `top`, `pop`, and `empty`). Implement the `MyStack` class: * `void push(int x)` Pushes element x to the top of the stack. * `int pop()` Removes the element on the top...
null
Python3 Single queue operation easy understanding
implement-stack-using-queues
0
1
\n\n# Code\n```\nclass MyStack:\n\n def __init__(self):\n self.q = collections.deque()\n\n def push(self, x: int) -> None:\n self.q.append(x)\n\n def pop(self) -> int:\n for i in range(len(self.q) - 1):\n self.push(self.q.popleft())\n return self.q.popleft()\n\n def to...
1
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (`push`, `top`, `pop`, and `empty`). Implement the `MyStack` class: * `void push(int x)` Pushes element x to the top of the stack. * `int pop()` Removes the element on the top...
null
✅ 99.74% One-Queue Approach
implement-stack-using-queues
1
1
# Interview Guide - Implement Stack using Queues:\n\n## Problem Understanding\n\n### **Description**: \nThe task is to implement a stack (Last In, First Out - LIFO) using queues. The stack should support the following functions: `push`, `pop`, `top`, and `empty`. These operations should behave just as they do in a typ...
70
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (`push`, `top`, `pop`, and `empty`). Implement the `MyStack` class: * `void push(int x)` Pushes element x to the top of the stack. * `int pop()` Removes the element on the top...
null
✅Easy Solution🔥Python3/C#/C++/Java🔥With🗺️Image🗺️
implement-stack-using-queues
1
1
![Screenshot 2023-08-20 065922.png](https://assets.leetcode.com/users/images/4cbf9773-3eca-4dd4-b646-58dd64798a4d_1693196637.7072523.png)\n\n```Python3 []\nclass MyStack(object):\n\n def __init__(self):\n self.q = deque()\n\n def push(self, x):\n self.q.append(x)\n \n\n def pop(self):\n ...
23
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (`push`, `top`, `pop`, and `empty`). Implement the `MyStack` class: * `void push(int x)` Pushes element x to the top of the stack. * `int pop()` Removes the element on the top...
null
【Video】Ex-Amazon explains a solution with Python, JavaScript, Java and C++
implement-stack-using-queues
1
1
# Intuition\nIn my opinion, solution with two queues is more complicated than solution with one queue.\n\n# Solution Video\n\n### Please subscribe to my channel from here. I have 250 videos as of August 28th, 2023.\n\n**\u25A0 Subscribe URL**\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n...
24
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (`push`, `top`, `pop`, and `empty`). Implement the `MyStack` class: * `void push(int x)` Pushes element x to the top of the stack. * `int pop()` Removes the element on the top...
null
🟢 Implementing a Stack Using a Single Queue || Full Explanation || Mr. Robot
implement-stack-using-queues
1
1
---\n\n## Implementing a Stack Using a Single Queue in C++\n\n**Introduction:**\nStacks are fundamental data structures used in programming for managing data in a last-in, first-out (LIFO) manner. In this blog post, we\'ll delve into an interesting challenge: implementing a stack using just a single queue. We\'ll explo...
2
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (`push`, `top`, `pop`, and `empty`). Implement the `MyStack` class: * `void push(int x)` Pushes element x to the top of the stack. * `int pop()` Removes the element on the top...
null
Python || One Queue || Two Approaches || Easy
implement-stack-using-queues
0
1
```\nfrom queue import Queue\n\nclass MyStack: # Using One Queue\n\n def __init__(self):\n self.q=Queue()\n\n def push(self, x: int) -> None:\n self.q.put(x)\n for i in range(self.q.qsize()-1):\n self.q.put(self.q.get())\n\n def pop(self) -> int:\n return self.q.get()\n\n...
2
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (`push`, `top`, `pop`, and `empty`). Implement the `MyStack` class: * `void push(int x)` Pushes element x to the top of the stack. * `int pop()` Removes the element on the top...
null
Python short and clean. Functional programming.
implement-stack-using-queues
0
1
# Approach\nIn every `push(x)` create a new queue with `x` and nest the existing queue as second element.\n\nIn `pop()`, unpack the existing queue and return the top value.\n\n# Complexity\n- Time complexity: $$O(1)$$ for all methods.\n\n- Space complexity: $$O(1)$$ for all methods.\n\n# Code\n```python\nclass MyStack:...
1
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (`push`, `top`, `pop`, and `empty`). Implement the `MyStack` class: * `void push(int x)` Pushes element x to the top of the stack. * `int pop()` Removes the element on the top...
null
Simple solution
implement-stack-using-queues
0
1
\n\n# Complexity\n- Time complexity: $$\u041E(1)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$\u041E(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass MyStack:\n\n def __init__(self):\n self.q = deque()\n\n def push(self, x: int) -> None...
1
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (`push`, `top`, `pop`, and `empty`). Implement the `MyStack` class: * `void push(int x)` Pushes element x to the top of the stack. * `int pop()` Removes the element on the top...
null
Simple Python solution Beats 80% run time
implement-stack-using-queues
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (`push`, `top`, `pop`, and `empty`). Implement the `MyStack` class: * `void push(int x)` Pushes element x to the top of the stack. * `int pop()` Removes the element on the top...
null
225: Solution with step by step explanation
implement-stack-using-queues
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis solution also uses a deque from the collections module, but instead of using two queues, it only uses one queue. The trick is to use the push operation to move the newly added element to the front of the queue, effectiv...
10
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (`push`, `top`, `pop`, and `empty`). Implement the `MyStack` class: * `void push(int x)` Pushes element x to the top of the stack. * `int pop()` Removes the element on the top...
null
🔥0 ms🔥|🚀Simplest Solution🚀||🔥Full Explanation||🔥C++🔥|| Python3
invert-binary-tree
0
1
# Consider\uD83D\uDC4D\n```\n Please Upvote If You Find It Helpful\n```\n# Intuition\nIn this question we have to **Invert the binary tree**.\nSo we use **Post Order Treversal** in which first we go in **Left subtree** and then in **Right subtree** then we return back to **Parent node**.\nWhen we com...
331
Given the `root` of a binary tree, invert the tree, and return _its root_. **Example 1:** **Input:** root = \[4,2,7,1,3,6,9\] **Output:** \[4,7,2,9,6,3,1\] **Example 2:** **Input:** root = \[2,1,3\] **Output:** \[2,3,1\] **Example 3:** **Input:** root = \[\] **Output:** \[\] **Constraints:** * The number of n...
null
Easy || 100% || Fully Explained || Java, C++, Python, JS, C, Python3 || Recursive & Iterative
invert-binary-tree
1
1
# **Java Solution (Recursive & Iterative Approach):**\n```\n/** Recursive Approach:**/\n// Runtime: 0 ms, faster than 100.00% of Java online submissions for Invert Binary Tree.\n// Time Complexity : O(n)\n// Space Complexity : O(n)\nclass Solution {\n public TreeNode invertTree(TreeNode root) {\n // Base case...
185
Given the `root` of a binary tree, invert the tree, and return _its root_. **Example 1:** **Input:** root = \[4,2,7,1,3,6,9\] **Output:** \[4,7,2,9,6,3,1\] **Example 2:** **Input:** root = \[2,1,3\] **Output:** \[2,3,1\] **Example 3:** **Input:** root = \[\] **Output:** \[\] **Constraints:** * The number of n...
null
Simplest Python Recursive! O(N) Time
invert-binary-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe main intuition is to swap the left child of the root with the right child. This process of swapping should occur in each sub tree recursively.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThis is a recurse app...
1
Given the `root` of a binary tree, invert the tree, and return _its root_. **Example 1:** **Input:** root = \[4,2,7,1,3,6,9\] **Output:** \[4,7,2,9,6,3,1\] **Example 2:** **Input:** root = \[2,1,3\] **Output:** \[2,3,1\] **Example 3:** **Input:** root = \[\] **Output:** \[\] **Constraints:** * The number of n...
null
if i made it then you also can do it . just simple solution
invert-binary-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
Given the `root` of a binary tree, invert the tree, and return _its root_. **Example 1:** **Input:** root = \[4,2,7,1,3,6,9\] **Output:** \[4,7,2,9,6,3,1\] **Example 2:** **Input:** root = \[2,1,3\] **Output:** \[2,3,1\] **Example 3:** **Input:** root = \[\] **Output:** \[\] **Constraints:** * The number of n...
null
✅Easy Solution🔥Python/C/C#/C++/Java🔥WTF⁉️1 Line Code⁉️
invert-binary-tree
1
1
![Screenshot 2023-08-20 065922.png](https://assets.leetcode.com/users/images/0cda5bfc-f88e-44af-af82-bfee4e4d614c_1693337317.6462772.png)\n\n\n```python3 []\nclass Solution:\n def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:\n return root and TreeNode(root.val, self.invertTree(root.right)...
21
Given the `root` of a binary tree, invert the tree, and return _its root_. **Example 1:** **Input:** root = \[4,2,7,1,3,6,9\] **Output:** \[4,7,2,9,6,3,1\] **Example 2:** **Input:** root = \[2,1,3\] **Output:** \[2,3,1\] **Example 3:** **Input:** root = \[\] **Output:** \[\] **Constraints:** * The number of n...
null
Binbin's very fast solution
invert-binary-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
Given the `root` of a binary tree, invert the tree, and return _its root_. **Example 1:** **Input:** root = \[4,2,7,1,3,6,9\] **Output:** \[4,7,2,9,6,3,1\] **Example 2:** **Input:** root = \[2,1,3\] **Output:** \[2,3,1\] **Example 3:** **Input:** root = \[\] **Output:** \[\] **Constraints:** * The number of n...
null
✅Python3 easy solution 30ms 🔥🔥🔥
invert-binary-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Travelling tree with changing left right node.**\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- If current node is not None, then travel further.\n- while travelling change left node with right node.\n- now tr...
4
Given the `root` of a binary tree, invert the tree, and return _its root_. **Example 1:** **Input:** root = \[4,2,7,1,3,6,9\] **Output:** \[4,7,2,9,6,3,1\] **Example 2:** **Input:** root = \[2,1,3\] **Output:** \[2,3,1\] **Example 3:** **Input:** root = \[\] **Output:** \[\] **Constraints:** * The number of n...
null
Solution
invert-binary-tree
1
1
```C++ []\nclass Solution {\npublic:\n TreeNode* invertTree(TreeNode* root) {\n if(root==NULL){\n return NULL;\n }\n invertTree(root->left);\n invertTree(root->right);\n TreeNode* temp=root->right;\n root->right=root->left;\n root->left=temp;\n retur...
19
Given the `root` of a binary tree, invert the tree, and return _its root_. **Example 1:** **Input:** root = \[4,2,7,1,3,6,9\] **Output:** \[4,7,2,9,6,3,1\] **Example 2:** **Input:** root = \[2,1,3\] **Output:** \[2,3,1\] **Example 3:** **Input:** root = \[\] **Output:** \[\] **Constraints:** * The number of n...
null
🔥🔥|| 1 Line Code || 🔥🔥
invert-binary-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
Given the `root` of a binary tree, invert the tree, and return _its root_. **Example 1:** **Input:** root = \[4,2,7,1,3,6,9\] **Output:** \[4,7,2,9,6,3,1\] **Example 2:** **Input:** root = \[2,1,3\] **Output:** \[2,3,1\] **Example 3:** **Input:** root = \[\] **Output:** \[\] **Constraints:** * The number of n...
null
One-Liner.py 🐍
invert-binary-tree
0
1
# Code\n```\nclass Solution:\n def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:\n return TreeNode(root.val, self.invertTree(root.right), self.invertTree(root.left)) if root else None\n```\n<h1>Upvote if you like this...</h1>\n<br>\n\n![](https://media.tenor.com/czmTz_drok8AAAAC/that-sound...
17
Given the `root` of a binary tree, invert the tree, and return _its root_. **Example 1:** **Input:** root = \[4,2,7,1,3,6,9\] **Output:** \[4,7,2,9,6,3,1\] **Example 2:** **Input:** root = \[2,1,3\] **Output:** \[2,3,1\] **Example 3:** **Input:** root = \[\] **Output:** \[\] **Constraints:** * The number of n...
null
[Python] - DFS (Recursion) - Clean & Simple - 21ms
invert-binary-tree
0
1
\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n For Recursive call Stack Space used at max can be $$O(n)$$.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\nUsing Another function for Recursive Calls\n```Python [1]...
3
Given the `root` of a binary tree, invert the tree, and return _its root_. **Example 1:** **Input:** root = \[4,2,7,1,3,6,9\] **Output:** \[4,7,2,9,6,3,1\] **Example 2:** **Input:** root = \[2,1,3\] **Output:** \[2,3,1\] **Example 3:** **Input:** root = \[\] **Output:** \[\] **Constraints:** * The number of n...
null
py clean & short sol. using stack
basic-calculator-ii
0
1
\n# Complexity\n- Time complexity:$$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def calculate(self, s: str) -> int:\n n = len(s)\n if s == \'0\':\n retur...
4
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
Single Pass | Stack Postfix conversion solution
basic-calculator-ii
0
1
# Complexity\n- Time complexity:\n O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def calculate(self, s: str) -> int:\n s=s.replace(" ","")\n N=len(s)\n prec...
1
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
python stack
basic-calculator-ii
0
1
![image](https://assets.leetcode.com/users/jefferyzzy/image_1575429494.png)\n\n```\ndef calculate(self, s: str) -> int:\n num = 0\n res = 0\n pre_op = \'+\'\n s+=\'+\'\n stack = []\n for c in s:\n if c.isdigit():\n num = num*10 + int(c)\n el...
61
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
227: Time 91.15%, Solution with step by step explanation
basic-calculator-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis solution uses a stack to evaluate the expression. The stack variable stores the intermediate results and the cur variable stores the current operand. The op variable stores the current operator and is initialized with +...
13
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
Very detailed explanation, no stack needed!
basic-calculator-ii
0
1
# Intuition\nYou can think of an expression as multiple sections divided by + or -. For example, \n```\n1 + 2 * 3 * 4 + 2 / 2\n```\nhas 3 sections, each section is numbers connected with * or /. \n![Screen Shot 2023-09-06 at 11.56.56 PM.png](https://assets.leetcode.com/users/images/74e817fb-0227-43e2-b220-aac3d91eb16f_...
0
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
python - without any stack and beat 99%
basic-calculator-ii
0
1
```\nclass Solution:\n def calculate(self, s: str) -> int:\n curr_res = 0\n res = 0\n num = 0\n op = "+" # keep the last operator we have seen\n \n\t\t# append a "+" sign at the end because we can catch the very last item\n for ch in s + "+":\n if ch.isdigit():\n...
24
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
Binbin learned this brillian solution today
basic-calculator-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
Python (Simple Stack)
basic-calculator-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
Python3: O(1) space-complexity approach (no stack)
basic-calculator-ii
0
1
```\nclass Solution:\n\n def calculate(self, s: str) -> int:\n # Edge cases\n if len(s) == 0: # empty string\n return 0\n \n # remove all spaces\n s = s.replace(" ", "")\n \n\t\t# Initialization \n curr_number = prev_number = result = 0 \n operation ...
3
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
Python clean stack solution (easy understanding)
basic-calculator-ii
0
1
```\nclass Solution:\n def calculate(self, s: str) -> int:\n num, ope, stack = 0, \'+\', []\n \n for cnt, i in enumerate(s):\n if i.isnumeric():\n num = num * 10 + int(i)\n if i in \'+-*/\' or cnt == len(s) - 1:\n if ope == \'+\':\n ...
10
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
✔️ [Python3] GENERATOR, O(1) Space ฅ^•ﻌ•^ฅ, , Explained
basic-calculator-ii
0
1
We can create a helper function that would return us the term for addition and substraction. Inside the function we would handle terms that fromed by multiplication and division. In helper we simply iterate over characthers and accumulate substring that form the integer. As soon as we meet any terminate symbol for curr...
4
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
Python 3, Runtime 76ms, Stack Approach
basic-calculator-ii
0
1
```\nclass Solution:\n def calculate(self, s: str) -> int:\n num, presign, stack=0, "+", []\n for i in s+\'+\':\n if i.isdigit():\n num = num*10 +int(i)\n elif i in \'+-*/\':\n if presign ==\'+\':\n stack.append(num)\n ...
16
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
✅ [Python] Simple Solutions Stack [ 98%]
basic-calculator-ii
0
1
\u2714\uFE0F Complexity\nNow time complexity it is O(n), space is still O(n).\n\n```\nclass Solution:\n def calculate(self, s: str) -> int:\n stack, p, sign, total = [], 0, 1, 0\n s = s.replace(" ","")\n while p < len(s):\n char = s[p]\n if char.isdigit():\n ...
4
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
Python easy to read and understand | stack
basic-calculator-ii
0
1
```\nclass Solution:\n def update(self, sign, num, stack):\n if sign == "+": stack.append(num)\n if sign == "-": stack.append(-num)\n if sign == "*": stack.append(stack.pop()*num)\n if sign == "/": stack.append(int(stack.pop()/num))\n return stack\n \n def solve(self, i, s):\...
2
Given a string `s` which represents an expression, _evaluate this expression and return its value_. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of `[-231, 231 - 1]`. **Note:** You are not allowed to use any ...
null
Python🔥Java🔥C++🔥Simple Solution🔥Easy to Understand
summary-ranges
1
1
# An Upvote will be encouraging \uD83D\uDC4D\n\n# Approach and Intuition\n\n- First, we check if the input list nums is empty. If it is, we return an empty list since there are no ranges to summarize.\n\n- Initialize an empty list called ranges to store the summarized ranges.\n\n- Set the start variable to the first el...
47
You are given a **sorted unique** integer array `nums`. A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive). Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is...
null
Simple solution using a simple interval
summary-ranges
0
1
# Intuition\nTreat the values in the list like an interval. Then return their formatted result per the format required for the problem.\n\n# Approach\nBut each entry in the interval by updating the range when the value is consecutive to the start value in the interval. When the value is not, start another interval.\n\n...
0
You are given a **sorted unique** integer array `nums`. A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive). Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is...
null
Easiest python O(n) solution clearly explained
summary-ranges
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires summarizing consecutive ranges of numbers in a given list. We can iterate through the list and identify the start and end of each range by checking if the numbers are consecutive.\n\n# Approach\n<!-- Describe your app...
6
You are given a **sorted unique** integer array `nums`. A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive). Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is...
null
[Python 3] Find ranges x->y and return formatted result || beats 97% || 27ms
summary-ranges
0
1
```\nclass Solution:\n def summaryRanges(self, nums: List[int]) -> List[str]:\n ranges = [] # [start, end] or [x, y]\n for n in nums:\n if ranges and ranges[-1][1] == n-1:\n ranges[-1][1] = n\n else:\n ranges.append([n, n])\n\n return [f\'{x}->...
35
You are given a **sorted unique** integer array `nums`. A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive). Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is...
null
python simple code
summary-ranges
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
You are given a **sorted unique** integer array `nums`. A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive). Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is...
null
228: Solution with step by step explanation
summary-ranges
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis code implements the solution to the problem of finding the summary ranges for a given sorted unique integer array nums. The main idea is to iterate through nums and find the beginning and ending of each range.\n\nThe fu...
12
You are given a **sorted unique** integer array `nums`. A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive). Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is...
null
Python short and clean 1-liner. Functional programming.
summary-ranges
0
1
# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(n)$$, including output.\n\nwhere, `n is length of nums.`\n\n# Code\nImperative:\n```python\nclass Solution:\n def summaryRanges(self, nums: list[int]) -> list[str]:\n ranges = [[-inf, -inf]]\n for x in nums:\n if x == range...
3
You are given a **sorted unique** integer array `nums`. A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive). Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is...
null
Beats 100% O(N) 🔥| 2 Pointer 🔥Approach | Full Optmized Code 🔥
summary-ranges
1
1
\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n![image.png](https://assets.leetcode.com/users/images/bf405874-a4e5-41f1-b9e1-cf1960056a42_1686538193.728684.png)\n\n- **Its Very Simple LOOK !!**\n- Set to pointer start and end \n- Start to set at the starting of the range and end for the end of...
5
You are given a **sorted unique** integer array `nums`. A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive). Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is...
null
[ Python ] ✅✅ Simple Python Solution Using Iterative Approach || O(n) 🔥✌
summary-ranges
0
1
# If You like the Solution, Don\'t Forget To UpVote Me, Please UpVote! \uD83D\uDD3C\uD83D\uDE4F\n# Runtime: 34 ms, faster than 71.53% of Python3 online submissions for Summary Ranges.\n# Memory Usage: 13.8 MB, less than 88.25% of Python3 online submissions for Summary Ranges.\n# Time Complexity : - O(n)\n\n\tclass Solu...
44
You are given a **sorted unique** integer array `nums`. A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive). Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is...
null
Python Simple Solution! Beats 93%! TC- O(n)
summary-ranges
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIt is sorted and in a range the element next to "n" has to be \n"n+1". it can be used to derive a simple solution to this problem.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTake a variable to keep the startin...
2
You are given a **sorted unique** integer array `nums`. A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive). Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is...
null
Easy.py
summary-ranges
0
1
# Code\n```\nclass Solution:\n def summaryRanges(self, nums: List[int]) -> List[str]:\n if len(nums)==0:return []\n ans,i=[],0\n st=nums[0]\n for i in range(1,len(nums)):\n if nums[i]!=nums[i-1]+1:\n if st==nums[i-1]:\n ans.append(str(st))\n ...
3
You are given a **sorted unique** integer array `nums`. A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive). Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is...
null
python3 Solution
summary-ranges
0
1
\n# Code\n```\nclass Solution:\n def summaryRanges(self,nums:List[int])->List[int]:\n if not nums:\n return None\n\n ans=[]\n j=0\n for i in range(len(nums)):\n if i+1==len(nums) or nums[i]+1!=nums[i+1]:\n if j==i:\n ans.append(str(n...
8
You are given a **sorted unique** integer array `nums`. A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive). Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is...
null
Very Easy || 100% || Fully Explained || Java, C++, Python, Javascript, Python3
summary-ranges
1
1
# **Java Solution:**\n```\n// Runtime: 3 ms, faster than 92.99% of Java online submissions for Summary Ranges.\n// Time Complexity : O(N)\n// Space Complexity : O(1)\nclass Solution {\n public List<String> summaryRanges(int[] nums) {\n // Create a list of string to store the output result...\n List<Str...
22
You are given a **sorted unique** integer array `nums`. A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive). Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is...
null
🚀 99.7% || HashMap & Boyer-Moore Majority Voting || Explained Intuition 🚀
majority-element-ii
1
1
# Problem Description\nThe problem is to **identify** elements in an integer array, `nums`, of size `n`, that appear more than `\u230An/3\u230B` times and **return** them as an output.\n\n- **Constraints:**\n - `1 <= nums.length <= 5 * 10e4`\n - `-10e9 <= nums[i] <= 10e9`\n\nSeems Easy! \uD83D\uDE03\n\n---\n\n\n#...
234
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
🚀 99.7% || HashMap & Boyer-Moore Majority Voting || Explained Intuition 🚀
majority-element-ii
1
1
# Problem Description\nThe problem is to **identify** elements in an integer array, `nums`, of size `n`, that appear more than `\u230An/3\u230B` times and **return** them as an output.\n\n- **Constraints:**\n - `1 <= nums.length <= 5 * 10e4`\n - `-10e9 <= nums[i] <= 10e9`\n\nSeems Easy! \uD83D\uDE03\n\n---\n\n\n#...
234
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Video Solution | Explanation With Drawings | In Depth | C++ | Java | Python 3
majority-element-ii
1
1
# Intuition and approach discussed in detail in video solution\nhttps://youtu.be/l4wooU0Tgws\n# Code\nC++\n```\nclass Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int freq1 = 0, freq2 = 0;\n int cand1 = 23, cand2 = 0;\n for(auto & num : nums){\n if(num == c...
2
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Video Solution | Explanation With Drawings | In Depth | C++ | Java | Python 3
majority-element-ii
1
1
# Intuition and approach discussed in detail in video solution\nhttps://youtu.be/l4wooU0Tgws\n# Code\nC++\n```\nclass Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int freq1 = 0, freq2 = 0;\n int cand1 = 23, cand2 = 0;\n for(auto & num : nums){\n if(num == c...
2
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Python3 Solution
majority-element-ii
0
1
\n```\nclass Solution:\n def majorityElement(self, nums: List[int]) -> List[int]:\n count=Counter(nums)\n ans=[]\n n=len(nums)\n for key,value in count.items():\n if value>(n//3):\n ans.append(key)\n return ans \n \n```
2
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Python3 Solution
majority-element-ii
0
1
\n```\nclass Solution:\n def majorityElement(self, nums: List[int]) -> List[int]:\n count=Counter(nums)\n ans=[]\n n=len(nums)\n for key,value in count.items():\n if value>(n//3):\n ans.append(key)\n return ans \n \n```
2
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
🧠 >99% Python One-liner
majority-element-ii
0
1
# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution:\n def majorityElement(self, nums: List[int]) -> List[int]:\n return [ k for k, v in Counter(nums).items() if v > len(nums) // 3 ]\n```
2
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
🧠 >99% Python One-liner
majority-element-ii
0
1
# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution:\n def majorityElement(self, nums: List[int]) -> List[int]:\n return [ k for k, v in Counter(nums).items() if v > len(nums) // 3 ]\n```
2
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
✅97.21%🔥Easy Solution🔥1 line code🔥
majority-element-ii
1
1
# Problem\n#### The problem you\'re describing is about finding elements in an integer array that appear more than a third of the total number of elements in the array. The task is to return all such elements.\n---\n# python Solution\n\n##### 1. Defining the majorityElement method: This method takes one argument, nums,...
29
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
✅97.21%🔥Easy Solution🔥1 line code🔥
majority-element-ii
1
1
# Problem\n#### The problem you\'re describing is about finding elements in an integer array that appear more than a third of the total number of elements in the array. The task is to return all such elements.\n---\n# python Solution\n\n##### 1. Defining the majorityElement method: This method takes one argument, nums,...
29
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
✅ One Line Python || BEATS 5% ✅
majority-element-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n***If you can do it in many lines... you can do it in one!***\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n^2)$$\n\n# Code\n```\nclass Solution:\n def majorityElement(self, nums: List...
1
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
✅ One Line Python || BEATS 5% ✅
majority-element-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n***If you can do it in many lines... you can do it in one!***\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n^2)$$\n\n# Code\n```\nclass Solution:\n def majorityElement(self, nums: List...
1
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Easy Python Solution
majority-element-ii
0
1
\n\n# Code\n```\nclass Solution:\n def majorityElement(self, nums: List[int]) -> List[int]:\n count = len(nums)//3\n ans = []\n hashmap = {}\n for n in nums:\n hashmap[n] = 1 + hashmap.get(n, 0)\n for key in hashmap:\n if hashmap[key] > count:\n ...
1
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Easy Python Solution
majority-element-ii
0
1
\n\n# Code\n```\nclass Solution:\n def majorityElement(self, nums: List[int]) -> List[int]:\n count = len(nums)//3\n ans = []\n hashmap = {}\n for n in nums:\n hashmap[n] = 1 + hashmap.get(n, 0)\n for key in hashmap:\n if hashmap[key] > count:\n ...
1
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Two appoarches (TypeScript / Python3) || Simple solution with HashMap DS
majority-element-ii
0
1
# Intuition\nHere\'s a brief explanation of the problem:\n- there\'s a list of `nums`\n- our goal is to define such nums, that fits the next constraint\n```\nfreqOfNum > nums.length / 3\n```\n\nThis approach is using a [HashMap DS](https://en.wikipedia.org/wiki/Hash_table) to store frequencies of integers, and next ite...
1
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Two appoarches (TypeScript / Python3) || Simple solution with HashMap DS
majority-element-ii
0
1
# Intuition\nHere\'s a brief explanation of the problem:\n- there\'s a list of `nums`\n- our goal is to define such nums, that fits the next constraint\n```\nfreqOfNum > nums.length / 3\n```\n\nThis approach is using a [HashMap DS](https://en.wikipedia.org/wiki/Hash_table) to store frequencies of integers, and next ite...
1
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Beginner's friendly Solution using Hashmap
majority-element-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Beginner's friendly Solution using Hashmap
majority-element-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
✅ 92.99% Boyer-Moore Voting Algorithm
majority-element-ii
1
1
# Intuition\nWhen faced with the problem of finding elements that appear more than $$ \\left\\lfloor \\frac{n}{3} \\right\\rfloor $$ times, our initial thought may lean towards using a hash map to count the occurrences of each element. However, the problem challenges us to think beyond this obvious solution, specifical...
50
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
✅ 92.99% Boyer-Moore Voting Algorithm
majority-element-ii
1
1
# Intuition\nWhen faced with the problem of finding elements that appear more than $$ \\left\\lfloor \\frac{n}{3} \\right\\rfloor $$ times, our initial thought may lean towards using a hash map to count the occurrences of each element. However, the problem challenges us to think beyond this obvious solution, specifical...
50
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Solved using 2 Approaches
majority-element-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1st intuition was to use dictionary, will iterate through the array and store the integer in the dictionary and update its count as the integer is encountered again\n# Approach 1\n<!-- Describe your approach to solving the problem. -->\nA...
2
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Solved using 2 Approaches
majority-element-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1st intuition was to use dictionary, will iterate through the array and store the integer in the dictionary and update its count as the integer is encountered again\n# Approach 1\n<!-- Describe your approach to solving the problem. -->\nA...
2
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
💯Faster✅💯 Lesser✅5 Methods🔥Simple Counting🔥Hashmap🔥Moore Voting Algorithm🔥Boyer-Moore Voting
majority-element-ii
1
1
# \uD83D\uDE80 Hi, I\'m [Mohammed Raziullah Ansari](https://leetcode.com/Mohammed_Raziullah_Ansari/), and I\'m excited to share 5 ways to solve this question with detailed explanation of each approach:\n\n# Problem Explaination: \nThe "Majority Element II" problem is a well-known problem in computer science and data an...
46
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
💯Faster✅💯 Lesser✅5 Methods🔥Simple Counting🔥Hashmap🔥Moore Voting Algorithm🔥Boyer-Moore Voting
majority-element-ii
1
1
# \uD83D\uDE80 Hi, I\'m [Mohammed Raziullah Ansari](https://leetcode.com/Mohammed_Raziullah_Ansari/), and I\'m excited to share 5 ways to solve this question with detailed explanation of each approach:\n\n# Problem Explaination: \nThe "Majority Element II" problem is a well-known problem in computer science and data an...
46
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
【Video】How we think about a solution - Boyer-Moore Majority Vote Algorithm
majority-element-ii
1
1
Welcome to my post! This post starts with "How we think about a solution". In other words, that is my thought process to solve the question. This post explains how I get to my solution instead of just posting solution codes or out of blue algorithms. I hope it is helpful for someone.\n\n# Intuition\nFind two majority c...
33
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
【Video】How we think about a solution - Boyer-Moore Majority Vote Algorithm
majority-element-ii
1
1
Welcome to my post! This post starts with "How we think about a solution". In other words, that is my thought process to solve the question. This post explains how I get to my solution instead of just posting solution codes or out of blue algorithms. I hope it is helpful for someone.\n\n# Intuition\nFind two majority c...
33
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Beats 94.15% solution, PYTHON easy!!
majority-element-ii
0
1
# Intuition\nThe Question gives us an array called nums, and it wants us to return all the elements the occur more than len(nums)/3 times.\nSo, if the length of our nums array is 6, then **6/3 = 2**, we need to return all the elements that occur **more than 2 times**.\n<!-- Describe your first thoughts on how to solve ...
0
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Beats 94.15% solution, PYTHON easy!!
majority-element-ii
0
1
# Intuition\nThe Question gives us an array called nums, and it wants us to return all the elements the occur more than len(nums)/3 times.\nSo, if the length of our nums array is 6, then **6/3 = 2**, we need to return all the elements that occur **more than 2 times**.\n<!-- Describe your first thoughts on how to solve ...
0
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
BEGINNER FRIENDLY - 100% - ONE LINE TRAVERSAL IN C++/JAVA/PYTHON(TIME - O(N),PSACE-O(1))
majority-element-ii
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n If the array contains the majority of elements, their occurrence must be greater than the floor(N/3). Now, we can say that the count of minority elements and majority elements is equal up to a certain point in the array. So when we trave...
0
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
BEGINNER FRIENDLY - 100% - ONE LINE TRAVERSAL IN C++/JAVA/PYTHON(TIME - O(N),PSACE-O(1))
majority-element-ii
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n If the array contains the majority of elements, their occurrence must be greater than the floor(N/3). Now, we can say that the count of minority elements and majority elements is equal up to a certain point in the array. So when we trave...
0
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
[C++] [Python] Brute Force Approach || Too Easy || Fully Explained
majority-element-ii
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\nSuppose the input vector `nums` is `{3, 2, 3, 3, 1, 2, 2}`.\n\n1. Create an empty unordered map `mp` to store the frequency count of each element.\n2. Create an empty vector `ans` to store the elements that occur more than `n = nums.size() / 3` times,...
2
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
[C++] [Python] Brute Force Approach || Too Easy || Fully Explained
majority-element-ii
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\nSuppose the input vector `nums` is `{3, 2, 3, 3, 1, 2, 2}`.\n\n1. Create an empty unordered map `mp` to store the frequency count of each element.\n2. Create an empty vector `ans` to store the elements that occur more than `n = nums.size() / 3` times,...
2
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Easiest Approach to solve this Question in Python3
majority-element-ii
0
1
\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHere\'s a step-by-step explanation of the code\'s approach:\n\n1. element_count = Counter(nums): This line creates a dictionary-like object element_count using Python\'s Counter class. It counts the occurrences of each unique element in the nums l...
1
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Easiest Approach to solve this Question in Python3
majority-element-ii
0
1
\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHere\'s a step-by-step explanation of the code\'s approach:\n\n1. element_count = Counter(nums): This line creates a dictionary-like object element_count using Python\'s Counter class. It counts the occurrences of each unique element in the nums l...
1
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Python || 99.90 beats || Simple Code || 2 Approach (HashMap and set , count ,list)
majority-element-ii
0
1
**If you got help from this,... Plz Upvote .. it encourage me**\n# Code\n**1st Approach -> Set, Count, List (Beats 99.90)**\n```\nclass Solution:\n def majorityElement(self, nums: List[int]) -> List[int]:\n d = []\n for i in set(nums):\n if nums.count(i) > len(nums) / 3:\n d.a...
2
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Python || 99.90 beats || Simple Code || 2 Approach (HashMap and set , count ,list)
majority-element-ii
0
1
**If you got help from this,... Plz Upvote .. it encourage me**\n# Code\n**1st Approach -> Set, Count, List (Beats 99.90)**\n```\nclass Solution:\n def majorityElement(self, nums: List[int]) -> List[int]:\n d = []\n for i in set(nums):\n if nums.count(i) > len(nums) / 3:\n d.a...
2
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
✅ Python 🔥|| Beats 99%🚀|| Beginner Friendly ✔️🔥
majority-element-ii
0
1
# Intuition\n\nThis algorithm efficiently tracks two potential majority elements while traversing the array twice. It cancels out non-majority elements and validates the candidates\' counts to determine the final result. This approach provides a **linear time solution** with **minimal space usage**, making it effective...
1
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
✅ Python 🔥|| Beats 99%🚀|| Beginner Friendly ✔️🔥
majority-element-ii
0
1
# Intuition\n\nThis algorithm efficiently tracks two potential majority elements while traversing the array twice. It cancels out non-majority elements and validates the candidates\' counts to determine the final result. This approach provides a **linear time solution** with **minimal space usage**, making it effective...
1
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
🚩Beats 99.07% | Solution Using Boyer-Moore Majority Vote algorithm | 📈O(n) Solution
majority-element-ii
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks us to find all elements that appear more than \u230A n/3 \u230B times in a given integer array. To solve this efficiently, we can adapt the Boyer-Moore Majority Vote algorithm to find at most two candidates that might ful...
6
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
🚩Beats 99.07% | Solution Using Boyer-Moore Majority Vote algorithm | 📈O(n) Solution
majority-element-ii
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks us to find all elements that appear more than \u230A n/3 \u230B times in a given integer array. To solve this efficiently, we can adapt the Boyer-Moore Majority Vote algorithm to find at most two candidates that might ful...
6
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!
Simple Python3 Solution || 1 Line Code O(n) || Upto 98 % Space Saver O(1)
majority-element-ii
0
1
# Intuition\nWe use Counter to generate a Dictionary of nums and count of each distint number. Using this we filter the only the ones that are repeated more than n/3 times where n is the lenght of nums.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution:\n def major...
0
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 <...
How many majority elements could it possibly have? Do you have a better hint? Suggest it!