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
BFS||Python solution
snakes-and-ladders
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nA simple BFS with the right conditions will determine the shortest path\n# Approach\n<!-- Describe your approach to solving the problem. -->\nBFS\n# Complexity\n- Time complexity:\nO(n^2)\n\n- Space complexity:\nO(n^2)\n# Code\n```\nclass...
1
You are given an `n x n` integer matrix `board` where the cells are labeled from `1` to `n2` in a [**Boustrophedon style**](https://en.wikipedia.org/wiki/Boustrophedon) starting from the bottom left of the board (i.e. `board[n - 1][0]`) and alternating direction each row. You start on square `1` of the board. In each ...
null
BFS||Python solution
snakes-and-ladders
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nA simple BFS with the right conditions will determine the shortest path\n# Approach\n<!-- Describe your approach to solving the problem. -->\nBFS\n# Complexity\n- Time complexity:\nO(n^2)\n\n- Space complexity:\nO(n^2)\n# Code\n```\nclass...
1
You are given an integer array `nums`. In one move, you can pick an index `i` where `0 <= i < nums.length` and increment `nums[i]` by `1`. Return _the minimum number of moves to make every value in_ `nums` _**unique**_. The test cases are generated so that the answer fits in a 32-bit integer. **Example 1:** **Input...
null
Simplest Way to Deal Complicate Board: O(N) Python BFS Solution
snakes-and-ladders
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```\nfrom collections import deque\nclass Solution:\n def snakesAndLadders(self, board: List[List[int]]) -> int:\n ...
1
You are given an `n x n` integer matrix `board` where the cells are labeled from `1` to `n2` in a [**Boustrophedon style**](https://en.wikipedia.org/wiki/Boustrophedon) starting from the bottom left of the board (i.e. `board[n - 1][0]`) and alternating direction each row. You start on square `1` of the board. In each ...
null
Simplest Way to Deal Complicate Board: O(N) Python BFS Solution
snakes-and-ladders
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```\nfrom collections import deque\nclass Solution:\n def snakesAndLadders(self, board: List[List[int]]) -> int:\n ...
1
You are given an integer array `nums`. In one move, you can pick an index `i` where `0 <= i < nums.length` and increment `nums[i]` by `1`. Return _the minimum number of moves to make every value in_ `nums` _**unique**_. The test cases are generated so that the answer fits in a 32-bit integer. **Example 1:** **Input...
null
Solution
snakes-and-ladders
1
1
```C++ []\nclass Solution {\npublic:\n int snakesAndLadders(vector<vector<int>>& board) {\n const int n = board.size();\n int ans = 0;\n queue<int> q{{1}};\n vector<bool> seen(1 + n * n);\n vector<int> A(1 + n * n); // 2D -> 1D\n\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < n; ++j...
1
You are given an `n x n` integer matrix `board` where the cells are labeled from `1` to `n2` in a [**Boustrophedon style**](https://en.wikipedia.org/wiki/Boustrophedon) starting from the bottom left of the board (i.e. `board[n - 1][0]`) and alternating direction each row. You start on square `1` of the board. In each ...
null
Solution
snakes-and-ladders
1
1
```C++ []\nclass Solution {\npublic:\n int snakesAndLadders(vector<vector<int>>& board) {\n const int n = board.size();\n int ans = 0;\n queue<int> q{{1}};\n vector<bool> seen(1 + n * n);\n vector<int> A(1 + n * n); // 2D -> 1D\n\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < n; ++j...
1
You are given an integer array `nums`. In one move, you can pick an index `i` where `0 <= i < nums.length` and increment `nums[i]` by `1`. Return _the minimum number of moves to make every value in_ `nums` _**unique**_. The test cases are generated so that the answer fits in a 32-bit integer. **Example 1:** **Input...
null
Solution
smallest-range-ii
1
1
```C++ []\nclass Solution {\npublic:\n int smallestRangeII(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n int ans = nums.back() - *nums.begin();\n for (int i = 0; i < nums.size() - 1; ++i){\n int smallest = min(nums[0] + k, nums[i + 1] - k);\n int biggest =...
1
You are given an integer array `nums` and an integer `k`. For each index `i` where `0 <= i < nums.length`, change `nums[i]` to be either `nums[i] + k` or `nums[i] - k`. The **score** of `nums` is the difference between the maximum and minimum elements in `nums`. Return _the minimum **score** of_ `nums` _after changi...
null
Solution
smallest-range-ii
1
1
```C++ []\nclass Solution {\npublic:\n int smallestRangeII(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n int ans = nums.back() - *nums.begin();\n for (int i = 0; i < nums.size() - 1; ++i){\n int smallest = min(nums[0] + k, nums[i + 1] - k);\n int biggest =...
1
Given two integer arrays `pushed` and `popped` each with distinct values, return `true` _if this could have been the result of a sequence of push and pop operations on an initially empty stack, or_ `false` _otherwise._ **Example 1:** **Input:** pushed = \[1,2,3,4,5\], popped = \[4,5,3,2,1\] **Output:** true **Explana...
null
𝗪𝗵𝘆 𝗦𝗼𝗿𝘁𝗶𝗻𝗴? Explained with Visualization | 15 Languages | Beginner Level
smallest-range-ii
1
1
---\n\n# \u270D\uD83C\uDFFB Before Starting\n\nThis Solution is long, but it will slowly build the entire logic of solving the problem. Make sure you have\n\n- Time\n- Pen and Paper for visualization! \n\nHere is the Table of Contents for the Solution, although it\'s advisable to **read the solution thoroughly**. \n<!...
69
You are given an integer array `nums` and an integer `k`. For each index `i` where `0 <= i < nums.length`, change `nums[i]` to be either `nums[i] + k` or `nums[i] - k`. The **score** of `nums` is the difference between the maximum and minimum elements in `nums`. Return _the minimum **score** of_ `nums` _after changi...
null
𝗪𝗵𝘆 𝗦𝗼𝗿𝘁𝗶𝗻𝗴? Explained with Visualization | 15 Languages | Beginner Level
smallest-range-ii
1
1
---\n\n# \u270D\uD83C\uDFFB Before Starting\n\nThis Solution is long, but it will slowly build the entire logic of solving the problem. Make sure you have\n\n- Time\n- Pen and Paper for visualization! \n\nHere is the Table of Contents for the Solution, although it\'s advisable to **read the solution thoroughly**. \n<!...
69
Given two integer arrays `pushed` and `popped` each with distinct values, return `true` _if this could have been the result of a sequence of push and pop operations on an initially empty stack, or_ `false` _otherwise._ **Example 1:** **Input:** pushed = \[1,2,3,4,5\], popped = \[4,5,3,2,1\] **Output:** true **Explana...
null
4 line Python code
smallest-range-ii
0
1
\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def smallestRangeII(self, nums: List[int], k: int) -> int:\n nums.sort()\n res=(nums[-1...
0
You are given an integer array `nums` and an integer `k`. For each index `i` where `0 <= i < nums.length`, change `nums[i]` to be either `nums[i] + k` or `nums[i] - k`. The **score** of `nums` is the difference between the maximum and minimum elements in `nums`. Return _the minimum **score** of_ `nums` _after changi...
null
4 line Python code
smallest-range-ii
0
1
\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def smallestRangeII(self, nums: List[int], k: int) -> int:\n nums.sort()\n res=(nums[-1...
0
Given two integer arrays `pushed` and `popped` each with distinct values, return `true` _if this could have been the result of a sequence of push and pop operations on an initially empty stack, or_ `false` _otherwise._ **Example 1:** **Input:** pushed = \[1,2,3,4,5\], popped = \[4,5,3,2,1\] **Output:** true **Explana...
null
Python | sort | greedy
smallest-range-ii
0
1
# Code\n```\nclass Solution:\n def smallestRangeII(self, nums: List[int], k: int) -> int:\n nums.sort()\n # nums[i] -> +k or -k\n # To minimize (max - min), we\'ll have to increase small values while decrease large values.\n # Iterate i [0, N-1), and for each nums[i], try set it up for th...
0
You are given an integer array `nums` and an integer `k`. For each index `i` where `0 <= i < nums.length`, change `nums[i]` to be either `nums[i] + k` or `nums[i] - k`. The **score** of `nums` is the difference between the maximum and minimum elements in `nums`. Return _the minimum **score** of_ `nums` _after changi...
null
Python | sort | greedy
smallest-range-ii
0
1
# Code\n```\nclass Solution:\n def smallestRangeII(self, nums: List[int], k: int) -> int:\n nums.sort()\n # nums[i] -> +k or -k\n # To minimize (max - min), we\'ll have to increase small values while decrease large values.\n # Iterate i [0, N-1), and for each nums[i], try set it up for th...
0
Given two integer arrays `pushed` and `popped` each with distinct values, return `true` _if this could have been the result of a sequence of push and pop operations on an initially empty stack, or_ `false` _otherwise._ **Example 1:** **Input:** pushed = \[1,2,3,4,5\], popped = \[4,5,3,2,1\] **Output:** true **Explana...
null
Python3 solution beats 91.11% 🚀 || quibler7
online-election
0
1
\n\n# Code\n```\nclass TopVotedCandidate:\n\n def __init__(self, persons: List[int], times: List[int]):\n self.persons = []\n self.times = []\n self.dic = collections.defaultdict(int)\n self.m = 0\n self.idx = -1\n\n for i in range(len(times)):\n self.times.append...
1
You are given two integer arrays `persons` and `times`. In an election, the `ith` vote was cast for `persons[i]` at time `times[i]`. For each query at a time `t`, find the person that was leading the election at time `t`. Votes cast at time `t` will count towards our query. In the case of a tie, the most recent vote (...
null
Python3 solution beats 91.11% 🚀 || quibler7
online-election
0
1
\n\n# Code\n```\nclass TopVotedCandidate:\n\n def __init__(self, persons: List[int], times: List[int]):\n self.persons = []\n self.times = []\n self.dic = collections.defaultdict(int)\n self.m = 0\n self.idx = -1\n\n for i in range(len(times)):\n self.times.append...
1
On a 2D plane, we place `n` stones at some integer coordinate points. Each coordinate point may have at most one stone. A stone can be removed if it shares either **the same row or the same column** as another stone that has not been removed. Given an array `stones` of length `n` where `stones[i] = [xi, yi]` represen...
null
python3 solution beats 97% 🚀 || quibler7
online-election
0
1
# Code\n```\nclass TopVotedCandidate:\n\n def __init__(self, persons: List[int], times: List[int]):\n self.persons = []\n self.times = []\n self.dic = collections.defaultdict(int)\n self.m = 0\n self.idx = -1\n\n for i in range(len(times)):\n self.times.append(tim...
1
You are given two integer arrays `persons` and `times`. In an election, the `ith` vote was cast for `persons[i]` at time `times[i]`. For each query at a time `t`, find the person that was leading the election at time `t`. Votes cast at time `t` will count towards our query. In the case of a tie, the most recent vote (...
null
python3 solution beats 97% 🚀 || quibler7
online-election
0
1
# Code\n```\nclass TopVotedCandidate:\n\n def __init__(self, persons: List[int], times: List[int]):\n self.persons = []\n self.times = []\n self.dic = collections.defaultdict(int)\n self.m = 0\n self.idx = -1\n\n for i in range(len(times)):\n self.times.append(tim...
1
On a 2D plane, we place `n` stones at some integer coordinate points. Each coordinate point may have at most one stone. A stone can be removed if it shares either **the same row or the same column** as another stone that has not been removed. Given an array `stones` of length `n` where `stones[i] = [xi, yi]` represen...
null
Solution
online-election
1
1
```C++ []\nclass TopVotedCandidate {\npublic:\n TopVotedCandidate(vector<int> persons, vector<int> times) {\n int max_count = 0, candidate = 0, len = persons.size();\n int count[len + 1];\n memset(count, 0, sizeof count);\n candidates = vector<pair<int, int>>(len);\n for(int i = 0;...
2
You are given two integer arrays `persons` and `times`. In an election, the `ith` vote was cast for `persons[i]` at time `times[i]`. For each query at a time `t`, find the person that was leading the election at time `t`. Votes cast at time `t` will count towards our query. In the case of a tie, the most recent vote (...
null
Solution
online-election
1
1
```C++ []\nclass TopVotedCandidate {\npublic:\n TopVotedCandidate(vector<int> persons, vector<int> times) {\n int max_count = 0, candidate = 0, len = persons.size();\n int count[len + 1];\n memset(count, 0, sizeof count);\n candidates = vector<pair<int, int>>(len);\n for(int i = 0;...
2
On a 2D plane, we place `n` stones at some integer coordinate points. Each coordinate point may have at most one stone. A stone can be removed if it shares either **the same row or the same column** as another stone that has not been removed. Given an array `stones` of length `n` where `stones[i] = [xi, yi]` represen...
null
Counter and Binary Search, O(n) time complexity
online-election
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: 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. $...
0
You are given two integer arrays `persons` and `times`. In an election, the `ith` vote was cast for `persons[i]` at time `times[i]`. For each query at a time `t`, find the person that was leading the election at time `t`. Votes cast at time `t` will count towards our query. In the case of a tie, the most recent vote (...
null
Counter and Binary Search, O(n) time complexity
online-election
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: 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. $...
0
On a 2D plane, we place `n` stones at some integer coordinate points. Each coordinate point may have at most one stone. A stone can be removed if it shares either **the same row or the same column** as another stone that has not been removed. Given an array `stones` of length `n` where `stones[i] = [xi, yi]` represen...
null
[Python3] Good enough
online-election
0
1
``` Python3 []\nclass TopVotedCandidate:\n\n def __init__(self, persons: List[int], times: List[int]):\n self.snapshots = []\n votes = {}\n most = None\n\n for i in range(len(persons)):\n votes[persons[i]] = votes.get(persons[i],0)+1\n most = persons[i] if most is No...
0
You are given two integer arrays `persons` and `times`. In an election, the `ith` vote was cast for `persons[i]` at time `times[i]`. For each query at a time `t`, find the person that was leading the election at time `t`. Votes cast at time `t` will count towards our query. In the case of a tie, the most recent vote (...
null
[Python3] Good enough
online-election
0
1
``` Python3 []\nclass TopVotedCandidate:\n\n def __init__(self, persons: List[int], times: List[int]):\n self.snapshots = []\n votes = {}\n most = None\n\n for i in range(len(persons)):\n votes[persons[i]] = votes.get(persons[i],0)+1\n most = persons[i] if most is No...
0
On a 2D plane, we place `n` stones at some integer coordinate points. Each coordinate point may have at most one stone. A stone can be removed if it shares either **the same row or the same column** as another stone that has not been removed. Given an array `stones` of length `n` where `stones[i] = [xi, yi]` represen...
null
Simplest sol using dict of leading to maintain leading on at each index
online-election
0
1
# Code\n```\nclass TopVotedCandidate:\n\n def __init__(self, persons: List[int], times: List[int]):\n self.times = times\n self.votes = defaultdict(int)\n self.leading = []\n max_vote_count = 0\n\n for i,p in enumerate(persons):\n self.votes[p] += 1\n\n if not...
0
You are given two integer arrays `persons` and `times`. In an election, the `ith` vote was cast for `persons[i]` at time `times[i]`. For each query at a time `t`, find the person that was leading the election at time `t`. Votes cast at time `t` will count towards our query. In the case of a tie, the most recent vote (...
null
Simplest sol using dict of leading to maintain leading on at each index
online-election
0
1
# Code\n```\nclass TopVotedCandidate:\n\n def __init__(self, persons: List[int], times: List[int]):\n self.times = times\n self.votes = defaultdict(int)\n self.leading = []\n max_vote_count = 0\n\n for i,p in enumerate(persons):\n self.votes[p] += 1\n\n if not...
0
On a 2D plane, we place `n` stones at some integer coordinate points. Each coordinate point may have at most one stone. A stone can be removed if it shares either **the same row or the same column** as another stone that has not been removed. Given an array `stones` of length `n` where `stones[i] = [xi, yi]` represen...
null
Details solution with comments | Binary Search
online-election
0
1
# Idea:\n- save an array topCandidate to know which person is leading at a specific time times[i]\n- that array has the same length as persons and times array\n- for each query q, we find the upperBound of q in times array - called it t, then the \ntop candidates person at that q time is topCandidate[t-1]\n\n# # Time c...
0
You are given two integer arrays `persons` and `times`. In an election, the `ith` vote was cast for `persons[i]` at time `times[i]`. For each query at a time `t`, find the person that was leading the election at time `t`. Votes cast at time `t` will count towards our query. In the case of a tie, the most recent vote (...
null
Details solution with comments | Binary Search
online-election
0
1
# Idea:\n- save an array topCandidate to know which person is leading at a specific time times[i]\n- that array has the same length as persons and times array\n- for each query q, we find the upperBound of q in times array - called it t, then the \ntop candidates person at that q time is topCandidate[t-1]\n\n# # Time c...
0
On a 2D plane, we place `n` stones at some integer coordinate points. Each coordinate point may have at most one stone. A stone can be removed if it shares either **the same row or the same column** as another stone that has not been removed. Given an array `stones` of length `n` where `stones[i] = [xi, yi]` represen...
null
Python 3 (Update the winner in Hashmap)
online-election
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
You are given two integer arrays `persons` and `times`. In an election, the `ith` vote was cast for `persons[i]` at time `times[i]`. For each query at a time `t`, find the person that was leading the election at time `t`. Votes cast at time `t` will count towards our query. In the case of a tie, the most recent vote (...
null
Python 3 (Update the winner in Hashmap)
online-election
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
On a 2D plane, we place `n` stones at some integer coordinate points. Each coordinate point may have at most one stone. A stone can be removed if it shares either **the same row or the same column** as another stone that has not been removed. Given an array `stones` of length `n` where `stones[i] = [xi, yi]` represen...
null
Solution
sort-an-array
1
1
```C++ []\nint a[50001]={0};\nint b[50001]={0};\nclass Solution {\npublic:\n vector<int> sortArray(vector<int>& v) {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n for(auto x:v){\n if(x>=0)\n a[x]++;\n else\n b[-x]++;\n ...
1
Given an array of integers `nums`, sort the array in ascending order and return it. You must solve the problem **without using any built-in** functions in `O(nlog(n))` time complexity and with the smallest space complexity possible. **Example 1:** **Input:** nums = \[5,2,3,1\] **Output:** \[1,2,3,5\] **Explanation:*...
null
Solution
sort-an-array
1
1
```C++ []\nint a[50001]={0};\nint b[50001]={0};\nclass Solution {\npublic:\n vector<int> sortArray(vector<int>& v) {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n for(auto x:v){\n if(x>=0)\n a[x]++;\n else\n b[-x]++;\n ...
1
You have an initial **power** of `power`, an initial **score** of `0`, and a bag of `tokens` where `tokens[i]` is the value of the `ith` token (0-indexed). Your goal is to maximize your total **score** by potentially playing each token in one of two ways: * If your current **power** is at least `tokens[i]`, you may...
null
RADIX SORT PYTHON
sort-an-array
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 array of integers `nums`, sort the array in ascending order and return it. You must solve the problem **without using any built-in** functions in `O(nlog(n))` time complexity and with the smallest space complexity possible. **Example 1:** **Input:** nums = \[5,2,3,1\] **Output:** \[1,2,3,5\] **Explanation:*...
null
RADIX SORT PYTHON
sort-an-array
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
You have an initial **power** of `power`, an initial **score** of `0`, and a bag of `tokens` where `tokens[i]` is the value of the `ith` token (0-indexed). Your goal is to maximize your total **score** by potentially playing each token in one of two ways: * If your current **power** is at least `tokens[i]`, you may...
null
Solution
cat-and-mouse
1
1
```C++ []\nclass Solution {\npublic:\n int catMouseGame(vector<vector<int>>& graph) {\n enum class State { Draw, MouseWin, CatWin };\n const int n = static_cast<int>(graph.size());\n State states[50][50][2];\n int out_degree[50][50][2] = {};\n std::memset(states, 0, sizeof(states))...
1
A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns. The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph. The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n...
null
Solution
cat-and-mouse
1
1
```C++ []\nclass Solution {\npublic:\n int catMouseGame(vector<vector<int>>& graph) {\n enum class State { Draw, MouseWin, CatWin };\n const int n = static_cast<int>(graph.size());\n State states[50][50][2];\n int out_degree[50][50][2] = {};\n std::memset(states, 0, sizeof(states))...
1
Given an array `arr` of 4 digits, find the latest 24-hour time that can be made using each digit **exactly once**. 24-hour times are formatted as `"HH:MM "`, where `HH` is between `00` and `23`, and `MM` is between `00` and `59`. The earliest 24-hour time is `00:00`, and the latest is `23:59`. Return _the latest 24-h...
null
Python3 FT 90%, Basically SPFA to Address the Pitfalls of Naive DFS
cat-and-mouse
0
1
# Intro\n\nThis problem took me FOREVER to solve, but it really clarified a lot of things about how game problems, DFS, and SPFA relate to each other. And what assumptions must hold to use DFS to solve problems in general. So I hope it helps.\n\nHopefully this solution helps. As a TLDR/summary:\n* DFS doesn\'t work bec...
0
A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns. The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph. The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n...
null
Python3 FT 90%, Basically SPFA to Address the Pitfalls of Naive DFS
cat-and-mouse
0
1
# Intro\n\nThis problem took me FOREVER to solve, but it really clarified a lot of things about how game problems, DFS, and SPFA relate to each other. And what assumptions must hold to use DFS to solve problems in general. So I hope it helps.\n\nHopefully this solution helps. As a TLDR/summary:\n* DFS doesn\'t work bec...
0
Given an array `arr` of 4 digits, find the latest 24-hour time that can be made using each digit **exactly once**. 24-hour times are formatted as `"HH:MM "`, where `HH` is between `00` and `23`, and `MM` is between `00` and `59`. The earliest 24-hour time is `00:00`, and the latest is `23:59`. Return _the latest 24-h...
null
[Python] Clean Solution with Comments | O(N^3)
cat-and-mouse
0
1
# Code\n```\nclass Solution:\n def catMouseGame(self, g: List[List[int]]) -> int:\n n = len(g)\n dq = deque()\n dp = defaultdict(int) # store who will win 1-mouse, 2-cat\n for t in [1,2]: # start from known states\n for k in range(n):\n dp[(0,k,t)] = 1\n ...
0
A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns. The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph. The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n...
null
[Python] Clean Solution with Comments | O(N^3)
cat-and-mouse
0
1
# Code\n```\nclass Solution:\n def catMouseGame(self, g: List[List[int]]) -> int:\n n = len(g)\n dq = deque()\n dp = defaultdict(int) # store who will win 1-mouse, 2-cat\n for t in [1,2]: # start from known states\n for k in range(n):\n dp[(0,k,t)] = 1\n ...
0
Given an array `arr` of 4 digits, find the latest 24-hour time that can be made using each digit **exactly once**. 24-hour times are formatted as `"HH:MM "`, where `HH` is between `00` and `23`, and `MM` is between `00` and `59`. The earliest 24-hour time is `00:00`, and the latest is `23:59`. Return _the latest 24-h...
null
Python3 solution
cat-and-mouse
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
A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns. The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph. The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n...
null
Python3 solution
cat-and-mouse
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 an array `arr` of 4 digits, find the latest 24-hour time that can be made using each digit **exactly once**. 24-hour times are formatted as `"HH:MM "`, where `HH` is between `00` and `23`, and `MM` is between `00` and `59`. The earliest 24-hour time is `00:00`, and the latest is `23:59`. Return _the latest 24-h...
null
Simple Explained Solution
cat-and-mouse
0
1
\n```\nclass Solution:\n def catMouseGame(self, graph: List[List[int]]) -> int:\n def getPreStates(m,c,t):\n ans = []\n if t == 1:\n for c2 in graph[c]:\n if c2 == 0:continue\n ans.append((m,c2,2))\n else:\n f...
0
A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns. The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph. The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n...
null
Simple Explained Solution
cat-and-mouse
0
1
\n```\nclass Solution:\n def catMouseGame(self, graph: List[List[int]]) -> int:\n def getPreStates(m,c,t):\n ans = []\n if t == 1:\n for c2 in graph[c]:\n if c2 == 0:continue\n ans.append((m,c2,2))\n else:\n f...
0
Given an array `arr` of 4 digits, find the latest 24-hour time that can be made using each digit **exactly once**. 24-hour times are formatted as `"HH:MM "`, where `HH` is between `00` and `23`, and `MM` is between `00` and `59`. The earliest 24-hour time is `00:00`, and the latest is `23:59`. Return _the latest 24-h...
null
PYTHON SOLUTION || MEMOIZATION || BOTTOM UP DP ||
cat-and-mouse
0
1
```\nclass Solution:\n def catMouseGame(self, graph: List[List[int]]) -> int:\n def getPreStates(m,c,t):\n ans = []\n if t == 1:\n for c2 in graph[c]:\n if c2 == 0:continue\n ans.append((m,c2,2))\n else:\n for...
1
A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns. The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph. The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n...
null
PYTHON SOLUTION || MEMOIZATION || BOTTOM UP DP ||
cat-and-mouse
0
1
```\nclass Solution:\n def catMouseGame(self, graph: List[List[int]]) -> int:\n def getPreStates(m,c,t):\n ans = []\n if t == 1:\n for c2 in graph[c]:\n if c2 == 0:continue\n ans.append((m,c2,2))\n else:\n for...
1
Given an array `arr` of 4 digits, find the latest 24-hour time that can be made using each digit **exactly once**. 24-hour times are formatted as `"HH:MM "`, where `HH` is between `00` and `23`, and `MM` is between `00` and `59`. The earliest 24-hour time is `00:00`, and the latest is `23:59`. Return _the latest 24-h...
null
Python || Easy solution || Faster than 99.22%
x-of-a-kind-in-a-deck-of-cards
0
1
\n\n# Code\n```\nclass Solution:\n def hasGroupsSizeX(self, deck: List[int]) -> bool:\n count = collections.Counter(deck)\n val = count.values()\n import math\n m = math.gcd(*val)\n if m >= 2:\n return True \n else:\n return False\n\n```
2
You are given an integer array `deck` where `deck[i]` represents the number written on the `ith` card. Partition the cards into **one or more groups** such that: * Each group has **exactly** `x` cards where `x > 1`, and * All the cards in one group have the same integer written on them. Return `true` _if such pa...
null
Python || Easy solution || Faster than 99.22%
x-of-a-kind-in-a-deck-of-cards
0
1
\n\n# Code\n```\nclass Solution:\n def hasGroupsSizeX(self, deck: List[int]) -> bool:\n count = collections.Counter(deck)\n val = count.values()\n import math\n m = math.gcd(*val)\n if m >= 2:\n return True \n else:\n return False\n\n```
2
You are given an integer array `deck`. There is a deck of cards where every card has a unique integer. The integer on the `ith` card is `deck[i]`. You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. You will do the following steps repeatedly until all cards...
null
[Python3] Greatest Common Divisor | O(n)
x-of-a-kind-in-a-deck-of-cards
0
1
# Approach\nTo cover all the cases involved in this problem we need to find a baseline count of the elements. This can be facilitated with a dictionary as represented by \'dict\' in the code.\n\nOnce we have the counts, we have to understand if making partitions, single even multiple of the same element, is actually po...
2
You are given an integer array `deck` where `deck[i]` represents the number written on the `ith` card. Partition the cards into **one or more groups** such that: * Each group has **exactly** `x` cards where `x > 1`, and * All the cards in one group have the same integer written on them. Return `true` _if such pa...
null
[Python3] Greatest Common Divisor | O(n)
x-of-a-kind-in-a-deck-of-cards
0
1
# Approach\nTo cover all the cases involved in this problem we need to find a baseline count of the elements. This can be facilitated with a dictionary as represented by \'dict\' in the code.\n\nOnce we have the counts, we have to understand if making partitions, single even multiple of the same element, is actually po...
2
You are given an integer array `deck`. There is a deck of cards where every card has a unique integer. The integer on the `ith` card is `deck[i]`. You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. You will do the following steps repeatedly until all cards...
null
python solution easy one
x-of-a-kind-in-a-deck-of-cards
0
1
```\nclass Solution:\n def hasGroupsSizeX(self, deck: List[int]) -> bool:\n \n \n f=defaultdict(int)\n \n for j in deck:\n f[j]+=1\n \n \n import math\n \n u=list(f.values())\n \n g=u[0]\n \n for j in rang...
7
You are given an integer array `deck` where `deck[i]` represents the number written on the `ith` card. Partition the cards into **one or more groups** such that: * Each group has **exactly** `x` cards where `x > 1`, and * All the cards in one group have the same integer written on them. Return `true` _if such pa...
null
python solution easy one
x-of-a-kind-in-a-deck-of-cards
0
1
```\nclass Solution:\n def hasGroupsSizeX(self, deck: List[int]) -> bool:\n \n \n f=defaultdict(int)\n \n for j in deck:\n f[j]+=1\n \n \n import math\n \n u=list(f.values())\n \n g=u[0]\n \n for j in rang...
7
You are given an integer array `deck`. There is a deck of cards where every card has a unique integer. The integer on the `ith` card is `deck[i]`. You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. You will do the following steps repeatedly until all cards...
null
Simple Solution using GCD and Hash Table
x-of-a-kind-in-a-deck-of-cards
0
1
\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n-> Find the occurences of all the elements .\n-> If GCD of those occurences is >1 then return 1, else return 0.\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n```\nfrom collections import Counter\ncla...
2
You are given an integer array `deck` where `deck[i]` represents the number written on the `ith` card. Partition the cards into **one or more groups** such that: * Each group has **exactly** `x` cards where `x > 1`, and * All the cards in one group have the same integer written on them. Return `true` _if such pa...
null
Simple Solution using GCD and Hash Table
x-of-a-kind-in-a-deck-of-cards
0
1
\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n-> Find the occurences of all the elements .\n-> If GCD of those occurences is >1 then return 1, else return 0.\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n```\nfrom collections import Counter\ncla...
2
You are given an integer array `deck`. There is a deck of cards where every card has a unique integer. The integer on the `ith` card is `deck[i]`. You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. You will do the following steps repeatedly until all cards...
null
Solution in Python 3 (beats ~99%) (one line)
x-of-a-kind-in-a-deck-of-cards
0
1
```\nfrom functools import reduce\nfrom math import gcd\nfrom collections import Counter\n\nclass Solution:\n def hasGroupsSizeX(self, deck: List[int]) -> bool:\n return reduce(gcd,Counter(deck).values()) != 1\n\t\t\n\t\t\n- Junaid Mansuri
18
You are given an integer array `deck` where `deck[i]` represents the number written on the `ith` card. Partition the cards into **one or more groups** such that: * Each group has **exactly** `x` cards where `x > 1`, and * All the cards in one group have the same integer written on them. Return `true` _if such pa...
null
Solution in Python 3 (beats ~99%) (one line)
x-of-a-kind-in-a-deck-of-cards
0
1
```\nfrom functools import reduce\nfrom math import gcd\nfrom collections import Counter\n\nclass Solution:\n def hasGroupsSizeX(self, deck: List[int]) -> bool:\n return reduce(gcd,Counter(deck).values()) != 1\n\t\t\n\t\t\n- Junaid Mansuri
18
You are given an integer array `deck`. There is a deck of cards where every card has a unique integer. The integer on the `ith` card is `deck[i]`. You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. You will do the following steps repeatedly until all cards...
null
Solution
x-of-a-kind-in-a-deck-of-cards
1
1
```C++ []\nclass Solution {\npublic:\n bool hasGroupsSizeX(vector<int>& deck) {\n map <int,int> mp;\n vector <int> v;\n if(deck.size()==1)\n return false;\n for (int i:deck)\n mp[i+1]++;\n for(auto it:mp){\n v.push_back(it.second);\n }\n ...
2
You are given an integer array `deck` where `deck[i]` represents the number written on the `ith` card. Partition the cards into **one or more groups** such that: * Each group has **exactly** `x` cards where `x > 1`, and * All the cards in one group have the same integer written on them. Return `true` _if such pa...
null
Solution
x-of-a-kind-in-a-deck-of-cards
1
1
```C++ []\nclass Solution {\npublic:\n bool hasGroupsSizeX(vector<int>& deck) {\n map <int,int> mp;\n vector <int> v;\n if(deck.size()==1)\n return false;\n for (int i:deck)\n mp[i+1]++;\n for(auto it:mp){\n v.push_back(it.second);\n }\n ...
2
You are given an integer array `deck`. There is a deck of cards where every card has a unique integer. The integer on the `ith` card is `deck[i]`. You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. You will do the following steps repeatedly until all cards...
null
[Python3] with least common divisor and reduce()
x-of-a-kind-in-a-deck-of-cards
0
1
# Code\n```\nfrom collections import Counter\nfrom functools import reduce\n\nclass Solution:\n def hasGroupsSizeX(self, deck: List[int]) -> bool:\n counts = set(Counter(deck).values())\n return reduce(self.least_common_divisor, counts) > 1\n \n def least_common_divisor(self, a, b):\n div ...
0
You are given an integer array `deck` where `deck[i]` represents the number written on the `ith` card. Partition the cards into **one or more groups** such that: * Each group has **exactly** `x` cards where `x > 1`, and * All the cards in one group have the same integer written on them. Return `true` _if such pa...
null
[Python3] with least common divisor and reduce()
x-of-a-kind-in-a-deck-of-cards
0
1
# Code\n```\nfrom collections import Counter\nfrom functools import reduce\n\nclass Solution:\n def hasGroupsSizeX(self, deck: List[int]) -> bool:\n counts = set(Counter(deck).values())\n return reduce(self.least_common_divisor, counts) > 1\n \n def least_common_divisor(self, a, b):\n div ...
0
You are given an integer array `deck`. There is a deck of cards where every card has a unique integer. The integer on the `ith` card is `deck[i]`. You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. You will do the following steps repeatedly until all cards...
null
Python one liner
x-of-a-kind-in-a-deck-of-cards
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
You are given an integer array `deck` where `deck[i]` represents the number written on the `ith` card. Partition the cards into **one or more groups** such that: * Each group has **exactly** `x` cards where `x > 1`, and * All the cards in one group have the same integer written on them. Return `true` _if such pa...
null
Python one liner
x-of-a-kind-in-a-deck-of-cards
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
You are given an integer array `deck`. There is a deck of cards where every card has a unique integer. The integer on the `ith` card is `deck[i]`. You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. You will do the following steps repeatedly until all cards...
null
x-of-a-kind-in-a-deck-of-cards
x-of-a-kind-in-a-deck-of-cards
0
1
# Code\n```\nclass Solution:\n def hasGroupsSizeX(self, deck: List[int]) -> bool:\n if len(deck)==1:\n return False\n d = {}\n for i in deck:\n if i not in d:\n d[i] = 1\n else:\n d[i] +=1\n t = sorted(d.items(), key = lambda ...
0
You are given an integer array `deck` where `deck[i]` represents the number written on the `ith` card. Partition the cards into **one or more groups** such that: * Each group has **exactly** `x` cards where `x > 1`, and * All the cards in one group have the same integer written on them. Return `true` _if such pa...
null
x-of-a-kind-in-a-deck-of-cards
x-of-a-kind-in-a-deck-of-cards
0
1
# Code\n```\nclass Solution:\n def hasGroupsSizeX(self, deck: List[int]) -> bool:\n if len(deck)==1:\n return False\n d = {}\n for i in deck:\n if i not in d:\n d[i] = 1\n else:\n d[i] +=1\n t = sorted(d.items(), key = lambda ...
0
You are given an integer array `deck`. There is a deck of cards where every card has a unique integer. The integer on the `ith` card is `deck[i]`. You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. You will do the following steps repeatedly until all cards...
null
Beats 93.55% of users with Python3
x-of-a-kind-in-a-deck-of-cards
0
1
# Code\n```\nclass Solution:\n def hasGroupsSizeX(self, deck: List[int]) -> bool:\n # straight forward logic\n # 115ms\n # Beats 93.55% of users with Python3\n if len(deck) == 1:\n return False\n \n import math\n from collections import Counter\n ret...
0
You are given an integer array `deck` where `deck[i]` represents the number written on the `ith` card. Partition the cards into **one or more groups** such that: * Each group has **exactly** `x` cards where `x > 1`, and * All the cards in one group have the same integer written on them. Return `true` _if such pa...
null
Beats 93.55% of users with Python3
x-of-a-kind-in-a-deck-of-cards
0
1
# Code\n```\nclass Solution:\n def hasGroupsSizeX(self, deck: List[int]) -> bool:\n # straight forward logic\n # 115ms\n # Beats 93.55% of users with Python3\n if len(deck) == 1:\n return False\n \n import math\n from collections import Counter\n ret...
0
You are given an integer array `deck`. There is a deck of cards where every card has a unique integer. The integer on the `ith` card is `deck[i]`. You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. You will do the following steps repeatedly until all cards...
null
Solution
partition-array-into-disjoint-intervals
1
1
```C++ []\nclass Solution {\npublic:\n int partitionDisjoint(vector<int>& a) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int minNum=a[0],maxNum=a[0];\n int ans=0;\n for(int i=1;i<a.size();i++){\n if(a[i]<minNum){\n ans=i;\n minNum=maxNu...
1
Given an integer array `nums`, partition it into two (contiguous) subarrays `left` and `right` so that: * Every element in `left` is less than or equal to every element in `right`. * `left` and `right` are non-empty. * `left` has the smallest possible size. Return _the length of_ `left` _after such a partitioni...
null
Solution
partition-array-into-disjoint-intervals
1
1
```C++ []\nclass Solution {\npublic:\n int partitionDisjoint(vector<int>& a) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int minNum=a[0],maxNum=a[0];\n int ans=0;\n for(int i=1;i<a.size();i++){\n if(a[i]<minNum){\n ans=i;\n minNum=maxNu...
1
For a binary tree **T**, we can define a **flip operation** as follows: choose any node, and swap the left and right child subtrees. A binary tree **X** is _flip equivalent_ to a binary tree **Y** if and only if we can make **X** equal to **Y** after some number of flip operations. Given the roots of two binary trees...
null
[PYTHON] Best solution yet! EXPLAINED with comments to make life easier. O(n) & O(1)
partition-array-into-disjoint-intervals
0
1
```\nclass Solution:\n def partitionDisjoint(self, nums: List[int]) -> int:\n """\n Intuition(logic) is to find two maximums.\n One maximum is for left array and other maximum is for right array.\n \n But the condition is that, the right maximum should be such that, \n no el...
9
Given an integer array `nums`, partition it into two (contiguous) subarrays `left` and `right` so that: * Every element in `left` is less than or equal to every element in `right`. * `left` and `right` are non-empty. * `left` has the smallest possible size. Return _the length of_ `left` _after such a partitioni...
null
[PYTHON] Best solution yet! EXPLAINED with comments to make life easier. O(n) & O(1)
partition-array-into-disjoint-intervals
0
1
```\nclass Solution:\n def partitionDisjoint(self, nums: List[int]) -> int:\n """\n Intuition(logic) is to find two maximums.\n One maximum is for left array and other maximum is for right array.\n \n But the condition is that, the right maximum should be such that, \n no el...
9
For a binary tree **T**, we can define a **flip operation** as follows: choose any node, and swap the left and right child subtrees. A binary tree **X** is _flip equivalent_ to a binary tree **Y** if and only if we can make **X** equal to **Y** after some number of flip operations. Given the roots of two binary trees...
null
Step By Step iteration & Explanation O(n) Space and time Comp
partition-array-into-disjoint-intervals
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBasic Idea was to observe the condition that min of left subarray is less than equal to max of right subarray .\nSo we have to compare left max with right min for every element .\n \n# Approach\n<!-- Describe your approach to solving the ...
0
Given an integer array `nums`, partition it into two (contiguous) subarrays `left` and `right` so that: * Every element in `left` is less than or equal to every element in `right`. * `left` and `right` are non-empty. * `left` has the smallest possible size. Return _the length of_ `left` _after such a partitioni...
null
Step By Step iteration & Explanation O(n) Space and time Comp
partition-array-into-disjoint-intervals
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBasic Idea was to observe the condition that min of left subarray is less than equal to max of right subarray .\nSo we have to compare left max with right min for every element .\n \n# Approach\n<!-- Describe your approach to solving the ...
0
For a binary tree **T**, we can define a **flip operation** as follows: choose any node, and swap the left and right child subtrees. A binary tree **X** is _flip equivalent_ to a binary tree **Y** if and only if we can make **X** equal to **Y** after some number of flip operations. Given the roots of two binary trees...
null
easy python approach
partition-array-into-disjoint-intervals
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 an integer array `nums`, partition it into two (contiguous) subarrays `left` and `right` so that: * Every element in `left` is less than or equal to every element in `right`. * `left` and `right` are non-empty. * `left` has the smallest possible size. Return _the length of_ `left` _after such a partitioni...
null
easy python approach
partition-array-into-disjoint-intervals
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
For a binary tree **T**, we can define a **flip operation** as follows: choose any node, and swap the left and right child subtrees. A binary tree **X** is _flip equivalent_ to a binary tree **Y** if and only if we can make **X** equal to **Y** after some number of flip operations. Given the roots of two binary trees...
null
python3 solution for partition of array
partition-array-into-disjoint-intervals
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 an integer array `nums`, partition it into two (contiguous) subarrays `left` and `right` so that: * Every element in `left` is less than or equal to every element in `right`. * `left` and `right` are non-empty. * `left` has the smallest possible size. Return _the length of_ `left` _after such a partitioni...
null
python3 solution for partition of array
partition-array-into-disjoint-intervals
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
For a binary tree **T**, we can define a **flip operation** as follows: choose any node, and swap the left and right child subtrees. A binary tree **X** is _flip equivalent_ to a binary tree **Y** if and only if we can make **X** equal to **Y** after some number of flip operations. Given the roots of two binary trees...
null
Intuitive 2-pointer O(N) solution
partition-array-into-disjoint-intervals
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 an integer array `nums`, partition it into two (contiguous) subarrays `left` and `right` so that: * Every element in `left` is less than or equal to every element in `right`. * `left` and `right` are non-empty. * `left` has the smallest possible size. Return _the length of_ `left` _after such a partitioni...
null
Intuitive 2-pointer O(N) solution
partition-array-into-disjoint-intervals
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
For a binary tree **T**, we can define a **flip operation** as follows: choose any node, and swap the left and right child subtrees. A binary tree **X** is _flip equivalent_ to a binary tree **Y** if and only if we can make **X** equal to **Y** after some number of flip operations. Given the roots of two binary trees...
null
Solution
word-subsets
1
1
```C++ []\nclass Solution {\npublic:\n Solution() {\n ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);\n }\n vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {\n int maxWord2[26]={0};\n for(auto x:words2){\n int temp[26]={0};\n for(auto xx:...
1
You are given two string arrays `words1` and `words2`. A string `b` is a **subset** of string `a` if every letter in `b` occurs in `a` including multiplicity. * For example, `"wrr "` is a subset of `"warrior "` but is not a subset of `"world "`. A string `a` from `words1` is **universal** if for every string `b` i...
null
Solution
word-subsets
1
1
```C++ []\nclass Solution {\npublic:\n Solution() {\n ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);\n }\n vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {\n int maxWord2[26]={0};\n for(auto x:words2){\n int temp[26]={0};\n for(auto xx:...
1
You are given an integer array of unique positive integers `nums`. Consider the following graph: * There are `nums.length` nodes, labeled `nums[0]` to `nums[nums.length - 1]`, * There is an undirected edge between `nums[i]` and `nums[j]` if `nums[i]` and `nums[j]` share a common factor greater than `1`. Return _t...
null
Solution Using Counter in Python
word-subsets
0
1
In this the order of characters in words2 does not matter what only matters is the character and its max count in all the words, so we create a dictionary and update the count for each character in words2.\n\nExample: words1 = ["acaac","cccbb","aacbb","caacc","bcbbb"]\nwords2 = *["c","cc","b"]*\n\nso our dictonary fo...
32
You are given two string arrays `words1` and `words2`. A string `b` is a **subset** of string `a` if every letter in `b` occurs in `a` including multiplicity. * For example, `"wrr "` is a subset of `"warrior "` but is not a subset of `"world "`. A string `a` from `words1` is **universal** if for every string `b` i...
null
Solution Using Counter in Python
word-subsets
0
1
In this the order of characters in words2 does not matter what only matters is the character and its max count in all the words, so we create a dictionary and update the count for each character in words2.\n\nExample: words1 = ["acaac","cccbb","aacbb","caacc","bcbbb"]\nwords2 = *["c","cc","b"]*\n\nso our dictonary fo...
32
You are given an integer array of unique positive integers `nums`. Consider the following graph: * There are `nums.length` nodes, labeled `nums[0]` to `nums[nums.length - 1]`, * There is an undirected edge between `nums[i]` and `nums[j]` if `nums[i]` and `nums[j]` share a common factor greater than `1`. Return _t...
null
Python | SImple and Easy | Using dictionaries | Count
word-subsets
0
1
```\nclass Solution:\n def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:\n ans = set(words1)\n letters = {}\n for i in words2:\n for j in i:\n count = i.count(j)\n if j not in letters or count > letters[j]:\n let...
15
You are given two string arrays `words1` and `words2`. A string `b` is a **subset** of string `a` if every letter in `b` occurs in `a` including multiplicity. * For example, `"wrr "` is a subset of `"warrior "` but is not a subset of `"world "`. A string `a` from `words1` is **universal** if for every string `b` i...
null
Python | SImple and Easy | Using dictionaries | Count
word-subsets
0
1
```\nclass Solution:\n def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:\n ans = set(words1)\n letters = {}\n for i in words2:\n for j in i:\n count = i.count(j)\n if j not in letters or count > letters[j]:\n let...
15
You are given an integer array of unique positive integers `nums`. Consider the following graph: * There are `nums.length` nodes, labeled `nums[0]` to `nums[nums.length - 1]`, * There is an undirected edge between `nums[i]` and `nums[j]` if `nums[i]` and `nums[j]` share a common factor greater than `1`. Return _t...
null
Python3 || 12 lines, dict & counter w/ explanation || T/M: 84%/73%
word-subsets
0
1
```\nclass Solution: # Suppose for example:\n # words1 = [\'food\', \'coffee\', \'foofy\']\n # words2 = [\'foo\', \'off\']\n # \n # Here\'s the plan:\n # 1) Construct a d...
10
You are given two string arrays `words1` and `words2`. A string `b` is a **subset** of string `a` if every letter in `b` occurs in `a` including multiplicity. * For example, `"wrr "` is a subset of `"warrior "` but is not a subset of `"world "`. A string `a` from `words1` is **universal** if for every string `b` i...
null
Python3 || 12 lines, dict & counter w/ explanation || T/M: 84%/73%
word-subsets
0
1
```\nclass Solution: # Suppose for example:\n # words1 = [\'food\', \'coffee\', \'foofy\']\n # words2 = [\'foo\', \'off\']\n # \n # Here\'s the plan:\n # 1) Construct a d...
10
You are given an integer array of unique positive integers `nums`. Consider the following graph: * There are `nums.length` nodes, labeled `nums[0]` to `nums[nums.length - 1]`, * There is an undirected edge between `nums[i]` and `nums[j]` if `nums[i]` and `nums[j]` share a common factor greater than `1`. Return _t...
null
Python two-liner
word-subsets
0
1
```\nclass Solution:\n def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:\n w2 = reduce(operator.or_, map(Counter, words2))\n return [w1 for w1 in words1 if Counter(w1) >= w2]\n```
8
You are given two string arrays `words1` and `words2`. A string `b` is a **subset** of string `a` if every letter in `b` occurs in `a` including multiplicity. * For example, `"wrr "` is a subset of `"warrior "` but is not a subset of `"world "`. A string `a` from `words1` is **universal** if for every string `b` i...
null
Python two-liner
word-subsets
0
1
```\nclass Solution:\n def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:\n w2 = reduce(operator.or_, map(Counter, words2))\n return [w1 for w1 in words1 if Counter(w1) >= w2]\n```
8
You are given an integer array of unique positive integers `nums`. Consider the following graph: * There are `nums.length` nodes, labeled `nums[0]` to `nums[nums.length - 1]`, * There is an undirected edge between `nums[i]` and `nums[j]` if `nums[i]` and `nums[j]` share a common factor greater than `1`. Return _t...
null
python || counter
word-subsets
0
1
```\nclass Solution:\n def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:\n res = []\n word2Counter = Counter()\n \n for word in words2:\n word2Counter |= Counter(word)\n \n for word in words1:\n tempCounter = Counter(word)\n ...
1
You are given two string arrays `words1` and `words2`. A string `b` is a **subset** of string `a` if every letter in `b` occurs in `a` including multiplicity. * For example, `"wrr "` is a subset of `"warrior "` but is not a subset of `"world "`. A string `a` from `words1` is **universal** if for every string `b` i...
null
python || counter
word-subsets
0
1
```\nclass Solution:\n def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:\n res = []\n word2Counter = Counter()\n \n for word in words2:\n word2Counter |= Counter(word)\n \n for word in words1:\n tempCounter = Counter(word)\n ...
1
You are given an integer array of unique positive integers `nums`. Consider the following graph: * There are `nums.length` nodes, labeled `nums[0]` to `nums[nums.length - 1]`, * There is an undirected edge between `nums[i]` and `nums[j]` if `nums[i]` and `nums[j]` share a common factor greater than `1`. Return _t...
null
Solution
reverse-only-letters
1
1
```C++ []\nclass Solution {\npublic:\n string reverseOnlyLetters(string str) {\n int s=0;\n int e=str.length()-1;\n while(s<e){\n if(((str[s]>=\'A\' && str[s]<=\'Z\') || str[s]>=\'a\' && str[s]<=\'z\') && ((str[e]>=\'A\' && str[e]<=\'Z\') || str[e]>=\'a\' && str[e]<=\'z\') ){\n ...
2
Given a string `s`, reverse the string according to the following rules: * All the characters that are not English letters remain in the same position. * All the English letters (lowercase or uppercase) should be reversed. Return `s` _after reversing it_. **Example 1:** **Input:** s = "ab-cd" **Output:** "dc-ba...
null
Solution
reverse-only-letters
1
1
```C++ []\nclass Solution {\npublic:\n string reverseOnlyLetters(string str) {\n int s=0;\n int e=str.length()-1;\n while(s<e){\n if(((str[s]>=\'A\' && str[s]<=\'Z\') || str[s]>=\'a\' && str[s]<=\'z\') && ((str[e]>=\'A\' && str[e]<=\'Z\') || str[e]>=\'a\' && str[e]<=\'z\') ){\n ...
2
In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different `order`. The `order` of the alphabet is some permutation of lowercase letters. Given a sequence of `words` written in the alien language, and the `order` of the alphabet, return `true` if and only if the given `wor...
This problem is exactly like reversing a normal string except that there are certain characters that we have to simply skip. That should be easy enough to do if you know how to reverse a string using the two-pointer approach.
python3 code beats 97%
reverse-only-letters
0
1
# Intuition\nThe method first reverses the input string s and stores it in a variable u. It then iterates through each character of the reversed string u and checks if it is an alphabet using the isalpha() method. If it is, it appends the character to a list called l.\n\nNext, it iterates through each character of the ...
2
Given a string `s`, reverse the string according to the following rules: * All the characters that are not English letters remain in the same position. * All the English letters (lowercase or uppercase) should be reversed. Return `s` _after reversing it_. **Example 1:** **Input:** s = "ab-cd" **Output:** "dc-ba...
null
python3 code beats 97%
reverse-only-letters
0
1
# Intuition\nThe method first reverses the input string s and stores it in a variable u. It then iterates through each character of the reversed string u and checks if it is an alphabet using the isalpha() method. If it is, it appends the character to a list called l.\n\nNext, it iterates through each character of the ...
2
In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different `order`. The `order` of the alphabet is some permutation of lowercase letters. Given a sequence of `words` written in the alien language, and the `order` of the alphabet, return `true` if and only if the given `wor...
This problem is exactly like reversing a normal string except that there are certain characters that we have to simply skip. That should be easy enough to do if you know how to reverse a string using the two-pointer approach.
Python code for reversing only letters (TC&SC: O(n))
reverse-only-letters
0
1
\n\n# Approach\nHere\'s the step-by-step approach taken by the function:\n\nInitialize an empty list l1 to store the alphabetic characters from the input string s.\n\nConvert the input string s into a list of characters, denoted as l.\n\nIterate through the characters of s to identify and store the alphabetic character...
2
Given a string `s`, reverse the string according to the following rules: * All the characters that are not English letters remain in the same position. * All the English letters (lowercase or uppercase) should be reversed. Return `s` _after reversing it_. **Example 1:** **Input:** s = "ab-cd" **Output:** "dc-ba...
null
Python code for reversing only letters (TC&SC: O(n))
reverse-only-letters
0
1
\n\n# Approach\nHere\'s the step-by-step approach taken by the function:\n\nInitialize an empty list l1 to store the alphabetic characters from the input string s.\n\nConvert the input string s into a list of characters, denoted as l.\n\nIterate through the characters of s to identify and store the alphabetic character...
2
In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different `order`. The `order` of the alphabet is some permutation of lowercase letters. Given a sequence of `words` written in the alien language, and the `order` of the alphabet, return `true` if and only if the given `wor...
This problem is exactly like reversing a normal string except that there are certain characters that we have to simply skip. That should be easy enough to do if you know how to reverse a string using the two-pointer approach.
🔥 Beats 100% 💥 O(n) 🙌 Python 🌟 Two Pointer 📢 Easiest Approach 💡
reverse-only-letters
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe simply convert the string to a list. Then, need to find if there are characters at both ends. If they are then we simple swap them by swapping their index. \n\nLastly, we convet the lis to a string again and return.\n# Approach\n<!-- D...
2
Given a string `s`, reverse the string according to the following rules: * All the characters that are not English letters remain in the same position. * All the English letters (lowercase or uppercase) should be reversed. Return `s` _after reversing it_. **Example 1:** **Input:** s = "ab-cd" **Output:** "dc-ba...
null
🔥 Beats 100% 💥 O(n) 🙌 Python 🌟 Two Pointer 📢 Easiest Approach 💡
reverse-only-letters
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe simply convert the string to a list. Then, need to find if there are characters at both ends. If they are then we simple swap them by swapping their index. \n\nLastly, we convet the lis to a string again and return.\n# Approach\n<!-- D...
2
In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different `order`. The `order` of the alphabet is some permutation of lowercase letters. Given a sequence of `words` written in the alien language, and the `order` of the alphabet, return `true` if and only if the given `wor...
This problem is exactly like reversing a normal string except that there are certain characters that we have to simply skip. That should be easy enough to do if you know how to reverse a string using the two-pointer approach.
Easy and Simple | Most Memory efficient more than 95%
reverse-only-letters
0
1
\n\n# Code\n```\nclass Solution:\n def reverseOnlyLetters(self, s: str) -> str:\n alphabets = [i for i in s if i.isalpha()][-1::-1]\n for i in range(len(s)):\n if not(s[i].isalpha()):\n alphabets.insert(i,s[i])\n alphabets = \'\'.join(alphabets)\n return alphabet...
8
Given a string `s`, reverse the string according to the following rules: * All the characters that are not English letters remain in the same position. * All the English letters (lowercase or uppercase) should be reversed. Return `s` _after reversing it_. **Example 1:** **Input:** s = "ab-cd" **Output:** "dc-ba...
null
Easy and Simple | Most Memory efficient more than 95%
reverse-only-letters
0
1
\n\n# Code\n```\nclass Solution:\n def reverseOnlyLetters(self, s: str) -> str:\n alphabets = [i for i in s if i.isalpha()][-1::-1]\n for i in range(len(s)):\n if not(s[i].isalpha()):\n alphabets.insert(i,s[i])\n alphabets = \'\'.join(alphabets)\n return alphabet...
8
In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different `order`. The `order` of the alphabet is some permutation of lowercase letters. Given a sequence of `words` written in the alien language, and the `order` of the alphabet, return `true` if and only if the given `wor...
This problem is exactly like reversing a normal string except that there are certain characters that we have to simply skip. That should be easy enough to do if you know how to reverse a string using the two-pointer approach.