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
Intuitive Python Recursive Solution With Explanation
find-elements-in-a-contaminated-binary-tree
0
1
# Intuition\nWe can just depth first search the tree and recover the values.\n\n# Approach\nDepth first search the tree and recover the values. Add the values we have recovered to a set for $O(1)$ lookup with `find` method.\n\n# Complexity\n- Time complexity:\n$$O(n)$$, $n$ being the number the nodes in the tree. We de...
0
Given a binary tree with the following rules: 1. `root.val == 0` 2. If `treeNode.val == x` and `treeNode.left != null`, then `treeNode.left.val == 2 * x + 1` 3. If `treeNode.val == x` and `treeNode.right != null`, then `treeNode.right.val == 2 * x + 2` Now the binary tree is contaminated, which means all `treeNode...
There are two cases: a block of characters, or two blocks of characters between one different character. By keeping a run-length encoded version of the string, we can easily check these cases.
Easy python solution using BFS | Beats 84.70% in memory
find-elements-in-a-contaminated-binary-tree
0
1
# Intuition\nrecover the nodes of the tree using BFS and store it in a `list`. return `true` if target in list else `false`. \n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, righ...
0
Given a binary tree with the following rules: 1. `root.val == 0` 2. If `treeNode.val == x` and `treeNode.left != null`, then `treeNode.left.val == 2 * x + 1` 3. If `treeNode.val == x` and `treeNode.right != null`, then `treeNode.right.val == 2 * x + 2` Now the binary tree is contaminated, which means all `treeNode...
There are two cases: a block of characters, or two blocks of characters between one different character. By keeping a run-length encoded version of the string, we can easily check these cases.
[Python] Greedy, heap
greatest-sum-divisible-by-three
0
1
# Intuition\nIn order to maximize sum divisible by 3, we need to minimize sum of items, which give the same remainder as the whole array sum.\n\n# Approach\n1. Check whole array sum remainder.\n- If it is ``0`` then return whole array sum\n- Otherwise check the remainder - either ``1`` or ``2``\n2. Build two min-heaps ...
1
Given an integer array `nums`, return _the **maximum possible sum** of elements of the array such that it is divisible by three_. **Example 1:** **Input:** nums = \[3,6,5,1,8\] **Output:** 18 **Explanation:** Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). **Example 2:** **Input:** nums = \...
What's special about a majority element ? A majority element appears more than half the length of the array number of times. If we tried a random index of the array, what's the probability that this index has a majority element ? It's more than 50% if that array has a majority element. Try a random index for a proper n...
[Python] Greedy, heap
greatest-sum-divisible-by-three
0
1
# Intuition\nIn order to maximize sum divisible by 3, we need to minimize sum of items, which give the same remainder as the whole array sum.\n\n# Approach\n1. Check whole array sum remainder.\n- If it is ``0`` then return whole array sum\n- Otherwise check the remainder - either ``1`` or ``2``\n2. Build two min-heaps ...
1
There is a pizza with `3n` slices of varying size, you and your friends will take slices of pizza as follows: * You will pick **any** pizza slice. * Your friend Alice will pick the next slice in the anti-clockwise direction of your pick. * Your friend Bob will pick the next slice in the clockwise direction of yo...
Represent the state as DP[pos][mod]: maximum possible sum starting in the position "pos" in the array where the current sum modulo 3 is equal to mod.
Python3 Greedy Linear Time
greatest-sum-divisible-by-three
0
1
In this approach, the goal will be to sum up the entire list, and subtract only what needs to be subtracted to make the sum divisible by 3.\n\nThere are 3 situations regarding the sum:\n**It is divisible by 3:** We are done already, just return the sum\n**Leaves a remainder of 1:** Subtract the smallest number that lea...
2
Given an integer array `nums`, return _the **maximum possible sum** of elements of the array such that it is divisible by three_. **Example 1:** **Input:** nums = \[3,6,5,1,8\] **Output:** 18 **Explanation:** Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). **Example 2:** **Input:** nums = \...
What's special about a majority element ? A majority element appears more than half the length of the array number of times. If we tried a random index of the array, what's the probability that this index has a majority element ? It's more than 50% if that array has a majority element. Try a random index for a proper n...
Python3 Greedy Linear Time
greatest-sum-divisible-by-three
0
1
In this approach, the goal will be to sum up the entire list, and subtract only what needs to be subtracted to make the sum divisible by 3.\n\nThere are 3 situations regarding the sum:\n**It is divisible by 3:** We are done already, just return the sum\n**Leaves a remainder of 1:** Subtract the smallest number that lea...
2
There is a pizza with `3n` slices of varying size, you and your friends will take slices of pizza as follows: * You will pick **any** pizza slice. * Your friend Alice will pick the next slice in the anti-clockwise direction of your pick. * Your friend Bob will pick the next slice in the clockwise direction of yo...
Represent the state as DP[pos][mod]: maximum possible sum starting in the position "pos" in the array where the current sum modulo 3 is equal to mod.
Python Very Easy Solution
greatest-sum-divisible-by-three
0
1
# 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)$$ -->\n\n# Code\n```\nclass Solution:\n def maxSumDivThree(self, nums: List[int]) -> int:\n dp = [0] * 3\n for v in nums:\n a, b, c =...
1
Given an integer array `nums`, return _the **maximum possible sum** of elements of the array such that it is divisible by three_. **Example 1:** **Input:** nums = \[3,6,5,1,8\] **Output:** 18 **Explanation:** Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). **Example 2:** **Input:** nums = \...
What's special about a majority element ? A majority element appears more than half the length of the array number of times. If we tried a random index of the array, what's the probability that this index has a majority element ? It's more than 50% if that array has a majority element. Try a random index for a proper n...
Python Very Easy Solution
greatest-sum-divisible-by-three
0
1
# 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)$$ -->\n\n# Code\n```\nclass Solution:\n def maxSumDivThree(self, nums: List[int]) -> int:\n dp = [0] * 3\n for v in nums:\n a, b, c =...
1
There is a pizza with `3n` slices of varying size, you and your friends will take slices of pizza as follows: * You will pick **any** pizza slice. * Your friend Alice will pick the next slice in the anti-clockwise direction of your pick. * Your friend Bob will pick the next slice in the clockwise direction of yo...
Represent the state as DP[pos][mod]: maximum possible sum starting in the position "pos" in the array where the current sum modulo 3 is equal to mod.
[Python 3] 0-1 BFS clean code
minimum-moves-to-move-a-box-to-their-target-location
0
1
\tclass Solution:\n\t\tdef minPushBox(self, grid: List[List[str]]) -> int:\n\t\t\tn,m=len(grid),len(grid[0])\n\t\t\tfor i in range(n):\n\t\t\t\tfor j in range(m):\n\t\t\t\t\tif grid[i][j]==\'S\':\n\t\t\t\t\t\tplayer=[i,j]\n\t\t\t\t\telif grid[i][j]==\'T\':\n\t\t\t\t\t\ttarget=[i,j]\n\t\t\t\t\telif grid[i][j]==\'B\':\n\...
1
A storekeeper is a game in which the player pushes boxes around in a warehouse trying to get them to target locations. The game is represented by an `m x n` grid of characters `grid` where each element is a wall, floor, or box. Your task is to move the box `'B'` to the target position `'T'` under the following rules:...
Use dynamic programming. The states are how many dice are remaining, and what sum total you have rolled so far.
Beats 100/95 in speed and memory , simple code using Java
minimum-moves-to-move-a-box-to-their-target-location
0
1
\n# Code\n```\nclass Solution:\n def minPushBox(self, grid):\n free = set()\n \n for i in range(len(grid)):\n for j in range(len(grid[0])):\n if grid[i][j] == "#": continue\n if grid[i][j] == "S": sx,sy = i,j\n if grid[i][j] == "B": bx,by ...
0
A storekeeper is a game in which the player pushes boxes around in a warehouse trying to get them to target locations. The game is represented by an `m x n` grid of characters `grid` where each element is a wall, floor, or box. Your task is to move the box `'B'` to the target position `'T'` under the following rules:...
Use dynamic programming. The states are how many dice are remaining, and what sum total you have rolled so far.
Solution
minimum-moves-to-move-a-box-to-their-target-location
1
1
```C++ []\ntypedef pair<int, int> PII;\n\nstruct Node {\n PII b_pos;\n PII p_pos;\n int steps;\n Node(PII b_pos_, PII p_pos_, int step) : b_pos(b_pos_), p_pos(p_pos_), steps(step) {}\n};\nclass Solution {\npublic:\n int dx[4] = {0, 1, 0, -1};\n int dy[4] = {1, 0, -1, 0};\n int row = 0, col = 0;\n ...
0
A storekeeper is a game in which the player pushes boxes around in a warehouse trying to get them to target locations. The game is represented by an `m x n` grid of characters `grid` where each element is a wall, floor, or box. Your task is to move the box `'B'` to the target position `'T'` under the following rules:...
Use dynamic programming. The states are how many dice are remaining, and what sum total you have rolled so far.
Clean | Fast | Python Solution
minimum-moves-to-move-a-box-to-their-target-location
0
1
# Code\n```\nclass Solution:\n\tdef minPushBox(self, grid: List[List[str]]) -> int:\n\t\tn,m=len(grid),len(grid[0])\n\t\tfor i in range(n):\n\t\t\tfor j in range(m):\n\t\t\t\tif grid[i][j]==\'S\':\n\t\t\t\t\tplayer=[i,j]\n\t\t\t\telif grid[i][j]==\'T\':\n\t\t\t\t\ttarget=[i,j]\n\t\t\t\telif grid[i][j]==\'B\':\n\t\t\t\t...
0
A storekeeper is a game in which the player pushes boxes around in a warehouse trying to get them to target locations. The game is represented by an `m x n` grid of characters `grid` where each element is a wall, floor, or box. Your task is to move the box `'B'` to the target position `'T'` under the following rules:...
Use dynamic programming. The states are how many dice are remaining, and what sum total you have rolled so far.
Python (Simple Dijkstra's algorithm)
minimum-moves-to-move-a-box-to-their-target-location
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 storekeeper is a game in which the player pushes boxes around in a warehouse trying to get them to target locations. The game is represented by an `m x n` grid of characters `grid` where each element is a wall, floor, or box. Your task is to move the box `'B'` to the target position `'T'` under the following rules:...
Use dynamic programming. The states are how many dice are remaining, and what sum total you have rolled so far.
Python Simple BFS Solution, Faster than 90%
minimum-moves-to-move-a-box-to-their-target-location
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Check whether the box can be shifted to the new position(up, down, left, right)\n2. For it to be shifted to the new position the person has to be in a corresponding position.\n3. So, we check if the person can travel from his old position to his co...
0
A storekeeper is a game in which the player pushes boxes around in a warehouse trying to get them to target locations. The game is represented by an `m x n` grid of characters `grid` where each element is a wall, floor, or box. Your task is to move the box `'B'` to the target position `'T'` under the following rules:...
Use dynamic programming. The states are how many dice are remaining, and what sum total you have rolled so far.
python bfs + dfs
minimum-moves-to-move-a-box-to-their-target-location
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 storekeeper is a game in which the player pushes boxes around in a warehouse trying to get them to target locations. The game is represented by an `m x n` grid of characters `grid` where each element is a wall, floor, or box. Your task is to move the box `'B'` to the target position `'T'` under the following rules:...
Use dynamic programming. The states are how many dice are remaining, and what sum total you have rolled so far.
Simple approach by using abs()
minimum-time-visiting-all-points
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)$$ --...
7
On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return _the **minimum time** in seconds to visit all the points in the order given by_ `points`. You can move according to these rules: * In `1` second, you can either: * move vertically by one unit, * move horizontally...
null
Simple approach by using abs()
minimum-time-visiting-all-points
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)$$ --...
7
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
✅ One Line Solution
minimum-time-visiting-all-points
0
1
# Complexity\n- Time complexity: $$O(n)$$.\n\n- Space complexity: $$O(1)$$.\n\n# Code #1\n```\nclass Solution:\n def minTimeToVisitAllPoints(self, p: List[List[int]]) -> int:\n return sum(max(abs(x1-x2), abs(y1-y2)) for (x1, y1), (x2, y2) in zip(p, p[1:]))\n```\n\n# Code #2\n```\nclass Solution:\n def minT...
3
On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return _the **minimum time** in seconds to visit all the points in the order given by_ `points`. You can move according to these rules: * In `1` second, you can either: * move vertically by one unit, * move horizontally...
null
✅ One Line Solution
minimum-time-visiting-all-points
0
1
# Complexity\n- Time complexity: $$O(n)$$.\n\n- Space complexity: $$O(1)$$.\n\n# Code #1\n```\nclass Solution:\n def minTimeToVisitAllPoints(self, p: List[List[int]]) -> int:\n return sum(max(abs(x1-x2), abs(y1-y2)) for (x1, y1), (x2, y2) in zip(p, p[1:]))\n```\n\n# Code #2\n```\nclass Solution:\n def minT...
3
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
✅☑[C++/Java/Python/JavaScript] || Easy Solution || EXPLAINED🔥
minimum-time-visiting-all-points
1
1
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n1. **minTimeToVisitAllPoints Function:**\n\n - This function calculates the minimum time required to visit all points given a vector of points.\n1. **Total Time Calculation:**\n\n - It initializes the `ans` variable to keep t...
5
On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return _the **minimum time** in seconds to visit all the points in the order given by_ `points`. You can move according to these rules: * In `1` second, you can either: * move vertically by one unit, * move horizontally...
null
✅☑[C++/Java/Python/JavaScript] || Easy Solution || EXPLAINED🔥
minimum-time-visiting-all-points
1
1
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n1. **minTimeToVisitAllPoints Function:**\n\n - This function calculates the minimum time required to visit all points given a vector of points.\n1. **Total Time Calculation:**\n\n - It initializes the `ans` variable to keep t...
5
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
1️⃣Liner.py
minimum-time-visiting-all-points
0
1
\n\n# Code\n```py\nclass Solution:\n def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:\n return sum([max(abs(points[i-1][0]-points[i][0]),abs(points[i-1][1]-points[i][1])) for i in range(1,len(points))])\n```\n![](https://assets.leetcode.com/users/images/634b6054-f32e-4031-9f4b-149bd724734b_1...
4
On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return _the **minimum time** in seconds to visit all the points in the order given by_ `points`. You can move according to these rules: * In `1` second, you can either: * move vertically by one unit, * move horizontally...
null
1️⃣Liner.py
minimum-time-visiting-all-points
0
1
\n\n# Code\n```py\nclass Solution:\n def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:\n return sum([max(abs(points[i-1][0]-points[i][0]),abs(points[i-1][1]-points[i][1])) for i in range(1,len(points))])\n```\n![](https://assets.leetcode.com/users/images/634b6054-f32e-4031-9f4b-149bd724734b_1...
4
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
Beats 83% | Python 3
minimum-time-visiting-all-points
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
On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return _the **minimum time** in seconds to visit all the points in the order given by_ `points`. You can move according to these rules: * In `1` second, you can either: * move vertically by one unit, * move horizontally...
null
Beats 83% | Python 3
minimum-time-visiting-all-points
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
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
Python | One Liner | O(n) / O(1)
minimum-time-visiting-all-points
0
1
# Code\n```\nclass Solution:\n def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:\n return sum(max(abs(points[i][0] - points[i-1][0]), abs(points[i][1] - points[i-1][1])) for i in range(1, len(points)))\n\n\n```
1
On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return _the **minimum time** in seconds to visit all the points in the order given by_ `points`. You can move according to these rules: * In `1` second, you can either: * move vertically by one unit, * move horizontally...
null
Python | One Liner | O(n) / O(1)
minimum-time-visiting-all-points
0
1
# Code\n```\nclass Solution:\n def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:\n return sum(max(abs(points[i][0] - points[i-1][0]), abs(points[i][1] - points[i-1][1])) for i in range(1, len(points)))\n\n\n```
1
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
Easy to read solution. TC = On, SC = O1
minimum-time-visiting-all-points
1
1
# Intuition\nWhen solving this problem, it\'s essential to make the most of diagonal movement. \n\n# Approach\nFor example, when moving from point [1,1] to [3,4], First calculate the differences in both `x` and `y` coordinates: `dx = 3 - 1 = 2` and `dy = 4 - 1 = 3`. You can move diagonally for the minimum of these two ...
1
On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return _the **minimum time** in seconds to visit all the points in the order given by_ `points`. You can move according to these rules: * In `1` second, you can either: * move vertically by one unit, * move horizontally...
null
Easy to read solution. TC = On, SC = O1
minimum-time-visiting-all-points
1
1
# Intuition\nWhen solving this problem, it\'s essential to make the most of diagonal movement. \n\n# Approach\nFor example, when moving from point [1,1] to [3,4], First calculate the differences in both `x` and `y` coordinates: `dx = 3 - 1 = 2` and `dy = 4 - 1 = 3`. You can move diagonally for the minimum of these two ...
1
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
【Video】Give me 5 minutes - How we think about a solution
minimum-time-visiting-all-points
1
1
# Intuition\nWe can move diagonally.\n\n---\n\n# Solution Video\n\nhttps://youtu.be/3i7JVvaKX5M\n\n\u25A0 Timeline of the video\n\n`0:05` Think about distance of two points with a simple example\n`1:37` Demonstrate how it works\n`3:31` Make sure important points\n`5:09` Coding\n`6:26` Time Complexity and Space Complexi...
17
On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return _the **minimum time** in seconds to visit all the points in the order given by_ `points`. You can move according to these rules: * In `1` second, you can either: * move vertically by one unit, * move horizontally...
null
【Video】Give me 5 minutes - How we think about a solution
minimum-time-visiting-all-points
1
1
# Intuition\nWe can move diagonally.\n\n---\n\n# Solution Video\n\nhttps://youtu.be/3i7JVvaKX5M\n\n\u25A0 Timeline of the video\n\n`0:05` Think about distance of two points with a simple example\n`1:37` Demonstrate how it works\n`3:31` Make sure important points\n`5:09` Coding\n`6:26` Time Complexity and Space Complexi...
17
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
Easy python solution for beginners
minimum-time-visiting-all-points
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
On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return _the **minimum time** in seconds to visit all the points in the order given by_ `points`. You can move according to these rules: * In `1` second, you can either: * move vertically by one unit, * move horizontally...
null
Easy python solution for beginners
minimum-time-visiting-all-points
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
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
🔥🔥🔥BEATS 100% CODERS🔥🔥🔥by PRODONiK✅✅✅
minimum-time-visiting-all-points
1
1
# Intuition\nThe problem involves calculating the minimum time to visit all points in a 2D array (`points`). The initial intuition might involve considering the distances between consecutive points and determining the time required to travel from one point to another.\n\n# Approach\nThe code uses a straightforward appr...
1
On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return _the **minimum time** in seconds to visit all the points in the order given by_ `points`. You can move according to these rules: * In `1` second, you can either: * move vertically by one unit, * move horizontally...
null
🔥🔥🔥BEATS 100% CODERS🔥🔥🔥by PRODONiK✅✅✅
minimum-time-visiting-all-points
1
1
# Intuition\nThe problem involves calculating the minimum time to visit all points in a 2D array (`points`). The initial intuition might involve considering the distances between consecutive points and determining the time required to travel from one point to another.\n\n# Approach\nThe code uses a straightforward appr...
1
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
Python, multiple liners but better readability and explain
minimum-time-visiting-all-points
0
1
# Intuition\n\nConsidering two points with the distances x_dist and y_dist between them, the travel time is calculated as the sum of the minimum of x_dist and y_dist, along with the absolute difference between x_dist and y_dist.\n\n# Code\n```python\nclass Solution:\n def minTimeToVisitAllPoints(self, points: List[L...
1
On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return _the **minimum time** in seconds to visit all the points in the order given by_ `points`. You can move according to these rules: * In `1` second, you can either: * move vertically by one unit, * move horizontally...
null
Python, multiple liners but better readability and explain
minimum-time-visiting-all-points
0
1
# Intuition\n\nConsidering two points with the distances x_dist and y_dist between them, the travel time is calculated as the sum of the minimum of x_dist and y_dist, along with the absolute difference between x_dist and y_dist.\n\n# Code\n```python\nclass Solution:\n def minTimeToVisitAllPoints(self, points: List[L...
1
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
Best and Simple Solution
minimum-time-visiting-all-points
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBest and Fast code using simple Python and basic Knowledge\n\n\n![Screenshot 2023-12-03 084537.png](https://assets.leetcode.com/users/images/589ab83a-08ec-47c7-8ffa-0039a584d477_1701573392.201185.png)\n\n\n# Approach\n<!-- Describe your a...
1
On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return _the **minimum time** in seconds to visit all the points in the order given by_ `points`. You can move according to these rules: * In `1` second, you can either: * move vertically by one unit, * move horizontally...
null
Best and Simple Solution
minimum-time-visiting-all-points
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBest and Fast code using simple Python and basic Knowledge\n\n\n![Screenshot 2023-12-03 084537.png](https://assets.leetcode.com/users/images/589ab83a-08ec-47c7-8ffa-0039a584d477_1701573392.201185.png)\n\n\n# Approach\n<!-- Describe your a...
1
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
[Python 3] Check rows and cols sum || beats 98% || 401ms 🥷🏼
count-servers-that-communicate
0
1
```python3 []\nclass Solution:\n def countServers(self, grid: List[List[int]]) -> int:\n rows = list(map(sum, grid))\n cols = list(map(sum, zip(*grid)))\n \n res = 0\n for i, j in product(range(len(grid)), range(len(grid[0]))):\n if grid[i][j] == 1 and (rows[i] > 1 or co...
4
You are given a map of a server center, represented as a `m * n` integer matrix `grid`, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column. Return the number of servers that communicate with any o...
Convert the linked list into an array. While you can find a non-empty subarray with sum = 0, erase it. Convert the array into a linked list.
[Python 3] Check rows and cols sum || beats 98% || 401ms 🥷🏼
count-servers-that-communicate
0
1
```python3 []\nclass Solution:\n def countServers(self, grid: List[List[int]]) -> int:\n rows = list(map(sum, grid))\n cols = list(map(sum, zip(*grid)))\n \n res = 0\n for i, j in product(range(len(grid)), range(len(grid[0]))):\n if grid[i][j] == 1 and (rows[i] > 1 or co...
4
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another. Implement the `UndergroundSystem` class: * `void checkIn(int id, string stationName, int t)` * A customer w...
Store number of computer in each row and column. Count all servers that are not isolated.
Python, simple, fast with full explanation
count-servers-that-communicate
0
1
```\nclass Solution:\n def countServers(self, grid: List[List[int]]) -> int:\n rows = len(grid)\n cols = len(grid[0])\n \n # Check the input: size of the grid. If it is 0 then exit\n if not (cols and rows):\n return 0\n\n connected = 0 # store num...
44
You are given a map of a server center, represented as a `m * n` integer matrix `grid`, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column. Return the number of servers that communicate with any o...
Convert the linked list into an array. While you can find a non-empty subarray with sum = 0, erase it. Convert the array into a linked list.
Python, simple, fast with full explanation
count-servers-that-communicate
0
1
```\nclass Solution:\n def countServers(self, grid: List[List[int]]) -> int:\n rows = len(grid)\n cols = len(grid[0])\n \n # Check the input: size of the grid. If it is 0 then exit\n if not (cols and rows):\n return 0\n\n connected = 0 # store num...
44
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another. Implement the `UndergroundSystem` class: * `void checkIn(int id, string stationName, int t)` * A customer w...
Store number of computer in each row and column. Count all servers that are not isolated.
85% TC and 78% SC easy python solution
count-servers-that-communicate
0
1
```\ndef countServers(self, grid: List[List[int]]) -> int:\n\tm, n = len(grid), len(grid[0])\n\trows = [0] * m\n\tcols = [0] * n\n\tfor i in range(m):\n\t\tfor j in range(n):\n\t\t\tif(grid[i][j]):\n\t\t\t\trows[i] += 1\n\t\t\t\tcols[j] += 1\n\tans = 0\n\tfor i in range(m):\n\t\tif(rows[i] > 1):\n\t\t\tans += rows[i]\n...
1
You are given a map of a server center, represented as a `m * n` integer matrix `grid`, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column. Return the number of servers that communicate with any o...
Convert the linked list into an array. While you can find a non-empty subarray with sum = 0, erase it. Convert the array into a linked list.
85% TC and 78% SC easy python solution
count-servers-that-communicate
0
1
```\ndef countServers(self, grid: List[List[int]]) -> int:\n\tm, n = len(grid), len(grid[0])\n\trows = [0] * m\n\tcols = [0] * n\n\tfor i in range(m):\n\t\tfor j in range(n):\n\t\t\tif(grid[i][j]):\n\t\t\t\trows[i] += 1\n\t\t\t\tcols[j] += 1\n\tans = 0\n\tfor i in range(m):\n\t\tif(rows[i] > 1):\n\t\t\tans += rows[i]\n...
1
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another. Implement the `UndergroundSystem` class: * `void checkIn(int id, string stationName, int t)` * A customer w...
Store number of computer in each row and column. Count all servers that are not isolated.
Python Union Find. Fast and easy to understand.
count-servers-that-communicate
0
1
# Code\n```\nclass Solution:\n def countServers(self, grid: List[List[int]]) -> int:\n m, n = len(grid), len(grid[0])\n uf = {}\n computer = []\n for i in range(m):\n for j in range(n):\n if grid[i][j]:\n # story the column and row in 1 dimensi...
2
You are given a map of a server center, represented as a `m * n` integer matrix `grid`, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column. Return the number of servers that communicate with any o...
Convert the linked list into an array. While you can find a non-empty subarray with sum = 0, erase it. Convert the array into a linked list.
Python Union Find. Fast and easy to understand.
count-servers-that-communicate
0
1
# Code\n```\nclass Solution:\n def countServers(self, grid: List[List[int]]) -> int:\n m, n = len(grid), len(grid[0])\n uf = {}\n computer = []\n for i in range(m):\n for j in range(n):\n if grid[i][j]:\n # story the column and row in 1 dimensi...
2
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another. Implement the `UndergroundSystem` class: * `void checkIn(int id, string stationName, int t)` * A customer w...
Store number of computer in each row and column. Count all servers that are not isolated.
C#, Go, Python | Two approaches
search-suggestions-system
0
1
# Code\n\n## Option 1\n``` csharp [one]\npublic class Solution {\n public IList<IList<string>> SuggestedProducts(string[] products, string searchWord) {\n var result = new List<IList<string>>();\n var previouse = products.OrderBy(it => it).ToList();\n for (var i = 0; i < searchWord.Length; i++)\...
2
You are given an array of strings `products` and a string `searchWord`. Design a system that suggests at most three product names from `products` after each character of `searchWord` is typed. Suggested products should have common prefix with `searchWord`. If there are more than three products with a common prefix ret...
null
C#, Go, Python | Two approaches
search-suggestions-system
0
1
# Code\n\n## Option 1\n``` csharp [one]\npublic class Solution {\n public IList<IList<string>> SuggestedProducts(string[] products, string searchWord) {\n var result = new List<IList<string>>();\n var previouse = products.OrderBy(it => it).ToList();\n for (var i = 0; i < searchWord.Length; i++)\...
2
Given the strings `s1` and `s2` of size `n` and the string `evil`, return _the number of **good** strings_. A **good** string has size `n`, it is alphabetically greater than or equal to `s1`, it is alphabetically smaller than or equal to `s2`, and it does not contain the string `evil` as a substring. Since the answer ...
Brute force is a good choice because length of the string is ≤ 1000. Binary search the answer. Use Trie data structure to store the best three matching. Traverse the Trie.
✅ 90.51% Binary Search Unleashed
search-suggestions-system
0
1
# **Intuition**\nThe problem revolves around finding product suggestions as we type each character of a search word. One of the immediate observations is that if the products list was sorted, our task of finding the lexicographically smallest products would become easier. Given a prefix from the search word, we would n...
4
You are given an array of strings `products` and a string `searchWord`. Design a system that suggests at most three product names from `products` after each character of `searchWord` is typed. Suggested products should have common prefix with `searchWord`. If there are more than three products with a common prefix ret...
null
✅ 90.51% Binary Search Unleashed
search-suggestions-system
0
1
# **Intuition**\nThe problem revolves around finding product suggestions as we type each character of a search word. One of the immediate observations is that if the products list was sorted, our task of finding the lexicographically smallest products would become easier. Given a prefix from the search word, we would n...
4
Given the strings `s1` and `s2` of size `n` and the string `evil`, return _the number of **good** strings_. A **good** string has size `n`, it is alphabetically greater than or equal to `s1`, it is alphabetically smaller than or equal to `s2`, and it does not contain the string `evil` as a substring. Since the answer ...
Brute force is a good choice because length of the string is ≤ 1000. Binary search the answer. Use Trie data structure to store the best three matching. Traverse the Trie.
Beats 96.8 % || Python Explained
search-suggestions-system
0
1
# Intuition\nSo, basically instead of searching the complete list again and again for each character rather trim ur products array.\nSort ur array lexiographically.\nKeep popping out elements from first whose first char doesnt match with to be searchedWord.\nSimilarly do it from back.\n`why? Because it can be so that u...
1
You are given an array of strings `products` and a string `searchWord`. Design a system that suggests at most three product names from `products` after each character of `searchWord` is typed. Suggested products should have common prefix with `searchWord`. If there are more than three products with a common prefix ret...
null
Beats 96.8 % || Python Explained
search-suggestions-system
0
1
# Intuition\nSo, basically instead of searching the complete list again and again for each character rather trim ur products array.\nSort ur array lexiographically.\nKeep popping out elements from first whose first char doesnt match with to be searchedWord.\nSimilarly do it from back.\n`why? Because it can be so that u...
1
Given the strings `s1` and `s2` of size `n` and the string `evil`, return _the number of **good** strings_. A **good** string has size `n`, it is alphabetically greater than or equal to `s1`, it is alphabetically smaller than or equal to `s2`, and it does not contain the string `evil` as a substring. Since the answer ...
Brute force is a good choice because length of the string is ≤ 1000. Binary search the answer. Use Trie data structure to store the best three matching. Traverse the Trie.
Python Easy 2 Approaches
search-suggestions-system
0
1
The solution that I have implemented is brute force. With each iteration, I will update the list of available words using `indices`.\n\n## **Example**\n```\n# sort the products\nproducts = ["mobile","moneypot","monitor","mouse","mousepad"] \nsearchWord = "mouse"\n```\n\nAfter each iteration, the contents of products w...
29
You are given an array of strings `products` and a string `searchWord`. Design a system that suggests at most three product names from `products` after each character of `searchWord` is typed. Suggested products should have common prefix with `searchWord`. If there are more than three products with a common prefix ret...
null
Python Easy 2 Approaches
search-suggestions-system
0
1
The solution that I have implemented is brute force. With each iteration, I will update the list of available words using `indices`.\n\n## **Example**\n```\n# sort the products\nproducts = ["mobile","moneypot","monitor","mouse","mousepad"] \nsearchWord = "mouse"\n```\n\nAfter each iteration, the contents of products w...
29
Given the strings `s1` and `s2` of size `n` and the string `evil`, return _the number of **good** strings_. A **good** string has size `n`, it is alphabetically greater than or equal to `s1`, it is alphabetically smaller than or equal to `s2`, and it does not contain the string `evil` as a substring. Since the answer ...
Brute force is a good choice because length of the string is ≤ 1000. Binary search the answer. Use Trie data structure to store the best three matching. Traverse the Trie.
Simple Easy Approach
search-suggestions-system
0
1
```\nclass Solution:\n def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:\n product = []\n for i in range(len(searchWord)):\n p = []\n for prod in products:\n if prod.startswith(searchWord[:i+1]):\n p.append(prod...
4
You are given an array of strings `products` and a string `searchWord`. Design a system that suggests at most three product names from `products` after each character of `searchWord` is typed. Suggested products should have common prefix with `searchWord`. If there are more than three products with a common prefix ret...
null
Simple Easy Approach
search-suggestions-system
0
1
```\nclass Solution:\n def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:\n product = []\n for i in range(len(searchWord)):\n p = []\n for prod in products:\n if prod.startswith(searchWord[:i+1]):\n p.append(prod...
4
Given the strings `s1` and `s2` of size `n` and the string `evil`, return _the number of **good** strings_. A **good** string has size `n`, it is alphabetically greater than or equal to `s1`, it is alphabetically smaller than or equal to `s2`, and it does not contain the string `evil` as a substring. Since the answer ...
Brute force is a good choice because length of the string is ≤ 1000. Binary search the answer. Use Trie data structure to store the best three matching. Traverse the Trie.
[Python] A simple approach without using Trie
search-suggestions-system
0
1
# Solution\n\nUse sort and list comprehension.\n\n```python\nclass Solution:\n def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:\n list_ = []\n products.sort()\n for i, c in enumerate(searchWord):\n products = [ p for p in products if len(p) > i and...
44
You are given an array of strings `products` and a string `searchWord`. Design a system that suggests at most three product names from `products` after each character of `searchWord` is typed. Suggested products should have common prefix with `searchWord`. If there are more than three products with a common prefix ret...
null
[Python] A simple approach without using Trie
search-suggestions-system
0
1
# Solution\n\nUse sort and list comprehension.\n\n```python\nclass Solution:\n def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:\n list_ = []\n products.sort()\n for i, c in enumerate(searchWord):\n products = [ p for p in products if len(p) > i and...
44
Given the strings `s1` and `s2` of size `n` and the string `evil`, return _the number of **good** strings_. A **good** string has size `n`, it is alphabetically greater than or equal to `s1`, it is alphabetically smaller than or equal to `s2`, and it does not contain the string `evil` as a substring. Since the answer ...
Brute force is a good choice because length of the string is ≤ 1000. Binary search the answer. Use Trie data structure to store the best three matching. Traverse the Trie.
Python 3 | Two Methods (Sort, Trie) | Explanation
search-suggestions-system
0
1
### Approach \\#1. Sort + Binary Search\n- Sort `A`\n- For each prefix, do binary search\n- Get 3 words from the index and check if it matches the prefix\n```\nclass Solution:\n def suggestedProducts(self, A: List[str], searchWord: str) -> List[List[str]]:\n A.sort()\n res, cur = [], \'\'\n for ...
30
You are given an array of strings `products` and a string `searchWord`. Design a system that suggests at most three product names from `products` after each character of `searchWord` is typed. Suggested products should have common prefix with `searchWord`. If there are more than three products with a common prefix ret...
null
Python 3 | Two Methods (Sort, Trie) | Explanation
search-suggestions-system
0
1
### Approach \\#1. Sort + Binary Search\n- Sort `A`\n- For each prefix, do binary search\n- Get 3 words from the index and check if it matches the prefix\n```\nclass Solution:\n def suggestedProducts(self, A: List[str], searchWord: str) -> List[List[str]]:\n A.sort()\n res, cur = [], \'\'\n for ...
30
Given the strings `s1` and `s2` of size `n` and the string `evil`, return _the number of **good** strings_. A **good** string has size `n`, it is alphabetically greater than or equal to `s1`, it is alphabetically smaller than or equal to `s2`, and it does not contain the string `evil` as a substring. Since the answer ...
Brute force is a good choice because length of the string is ≤ 1000. Binary search the answer. Use Trie data structure to store the best three matching. Traverse the Trie.
Python Trie Solution. Industry Level Code
search-suggestions-system
0
1
```\nclass TreeNode:\n def __init__(self):\n self.children = [None]*26\n self.words = []\nclass Trie:\n def __init__(self):\n self.root = TreeNode()\n def _getIndex(self,ch):\n return ord(ch)-ord(\'a\')\n def insert(self,word):\n cur_node = self.root\n for ch in wor...
11
You are given an array of strings `products` and a string `searchWord`. Design a system that suggests at most three product names from `products` after each character of `searchWord` is typed. Suggested products should have common prefix with `searchWord`. If there are more than three products with a common prefix ret...
null
Python Trie Solution. Industry Level Code
search-suggestions-system
0
1
```\nclass TreeNode:\n def __init__(self):\n self.children = [None]*26\n self.words = []\nclass Trie:\n def __init__(self):\n self.root = TreeNode()\n def _getIndex(self,ch):\n return ord(ch)-ord(\'a\')\n def insert(self,word):\n cur_node = self.root\n for ch in wor...
11
Given the strings `s1` and `s2` of size `n` and the string `evil`, return _the number of **good** strings_. A **good** string has size `n`, it is alphabetically greater than or equal to `s1`, it is alphabetically smaller than or equal to `s2`, and it does not contain the string `evil` as a substring. Since the answer ...
Brute force is a good choice because length of the string is ≤ 1000. Binary search the answer. Use Trie data structure to store the best three matching. Traverse the Trie.
【Video】Give me 10 minutes - How we think abou a solution - Python, JavaScript, Java, C++
number-of-ways-to-stay-in-the-same-place-after-some-steps
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\nWe only care about ...
36
You have a pointer at index `0` in an array of size `arrLen`. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time). Given two integers `steps` and `arrLen`, return the number of ways such that...
null
🚩📈Beats 99.70% |🔥Easy and Optimised O(sqrt(n)) Approach | 📈Explained in detail
number-of-ways-to-stay-in-the-same-place-after-some-steps
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea behind this solution is to recognize that as the number of steps increases, the number of ways to reach the starting position at index 0 forms a bell curve. This bell curve reaches its peak at the midpoint and then starts to decr...
3
You have a pointer at index `0` in an array of size `arrLen`. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time). Given two integers `steps` and `arrLen`, return the number of ways such that...
null
Beats 100%🔥Memory & Runtime🦄7 Lines🔮1D DP✅Python 3
number-of-ways-to-stay-in-the-same-place-after-some-steps
0
1
![image.png](https://assets.leetcode.com/users/images/9087a656-d2e6-42f6-91fe-dc304c5a41ef_1697349962.7212107.png)\n<!-- ![image.png](https://assets.leetcode.com/users/images/91bea382-d181-4c75-b6a5-ce2965576fe7_1697349959.2977636.png) -->\n![image.png](https://assets.leetcode.com/users/images/551677af-b756-455c-82ff-f...
3
You have a pointer at index `0` in an array of size `arrLen`. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time). Given two integers `steps` and `arrLen`, return the number of ways such that...
null
Dynamic Programming Staircase || Beginner Friendly || Easy To Understand || Python || Beats 100 % ||
number-of-ways-to-stay-in-the-same-place-after-some-steps
1
1
# BEATS 100%\n![image.png](https://assets.leetcode.com/users/images/6a39831c-e878-4e23-b404-9c38da061e96_1697344015.0906134.png)\n\n# Intuition\n\n\nImagine you are standing at the beginning of a long staircase. You want to reach the top, but you can only take one or two steps at a time. How many different ways can you...
29
You have a pointer at index `0` in an array of size `arrLen`. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time). Given two integers `steps` and `arrLen`, return the number of ways such that...
null
Beats 99% | Optimised Solution Using Dynamic Programming | Easy O(steps * maxPosition) Solution
number-of-ways-to-stay-in-the-same-place-after-some-steps
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is to count the number of ways to stay at index 0 in an array of length arrLen after taking steps steps. You can move one step to the left, one step to the right, or stay in the same place at each step. To solve this problem, ...
3
You have a pointer at index `0` in an array of size `arrLen`. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time). Given two integers `steps` and `arrLen`, return the number of ways such that...
null
✅☑[C++/C/Java/Python/JavaScript] || 3 Approaches || EXPLAINED🔥
number-of-ways-to-stay-in-the-same-place-after-some-steps
1
1
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### *Approach 1(Top Down DP)*\n\n1. **Class Definition:** The code defines a class called Solution.\n\n1. **Member Variables:**\n\n - **memo:** A 2D vector to store the memoization table for dynamic programming.\n - **MOD:...
2
You have a pointer at index `0` in an array of size `arrLen`. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time). Given two integers `steps` and `arrLen`, return the number of ways such that...
null
Python3 Solution
number-of-ways-to-stay-in-the-same-place-after-some-steps
0
1
\n\n```\nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n mod=10**9+7\n n=min(arrLen,steps//2+1)\n ways=[1]+[0]*(n-1)\n for step in range(steps):\n ways=[sum(ways[max(0,i-1):i+2])%mod for i in range(min(step+2,steps-step,n))]\n return ways[0] \n`...
2
You have a pointer at index `0` in an array of size `arrLen`. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time). Given two integers `steps` and `arrLen`, return the number of ways such that...
null
Python 36ms no caching :)
number-of-ways-to-stay-in-the-same-place-after-some-steps
0
1
This is very similar to all the other DP approaches here.\n\nThe technique with using a `prev` variable that starts at `0` is one I found from this nice solution: https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/solutions/4169508/beats-99-44-7-lines-1d-dp-python-3/\n\nHowever, the...
1
You have a pointer at index `0` in an array of size `arrLen`. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time). Given two integers `steps` and `arrLen`, return the number of ways such that...
null
Mastering Dynamic Programming: Counting Ways to Reach a Position in an Array
number-of-ways-to-stay-in-the-same-place-after-some-steps
1
1
# Counting Ways to Reach a Position in an Array\n\n## Approach 1: Recursion\n\n### Explanation\nIn this approach, we\'ll use a recursive function to explore all possible ways to reach a specific position in an array after a given number of steps. The key idea is to consider three options at each step: staying in the cu...
2
You have a pointer at index `0` in an array of size `arrLen`. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time). Given two integers `steps` and `arrLen`, return the number of ways such that...
null
🔥 Easy solution | Python 3 🔥|
number-of-ways-to-stay-in-the-same-place-after-some-steps
0
1
# Intuition\nNumber of Ways to Stay in the Same Place After Some Steps\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize constants and variables:\n\n 1. MOD is set to 10^9 + 7, which is used for taking the modulus of the result to avoid integer overflow.\n 1. Calcul...
1
You have a pointer at index `0` in an array of size `arrLen`. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time). Given two integers `steps` and `arrLen`, return the number of ways such that...
null
python
number-of-ways-to-stay-in-the-same-place-after-some-steps
0
1
```\nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n modulo = 1000000007\n max_len = min(arrLen, 1 + steps // 2)\n ways = [0] * (max_len + 1)\n ways[0] = 1\n for i in range(steps):\n left = 0\n for j in range(min(max_len, i + 2, steps - ...
1
You have a pointer at index `0` in an array of size `arrLen`. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time). Given two integers `steps` and `arrLen`, return the number of ways such that...
null
Easy Solution | Recursion-> Memoization-> Tabulation | Python Solution | Easy Explanation
number-of-ways-to-stay-in-the-same-place-after-some-steps
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this solution is to break down the problem into smaller subproblems. At each step, you can make three possible moves, so you explore all three options and recursively calculate the number of ways to reach the destinat...
1
You have a pointer at index `0` in an array of size `arrLen`. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time). Given two integers `steps` and `arrLen`, return the number of ways such that...
null
🔥 [Python 3] 2 solutions, beats 90% 🥷🏼
find-winner-on-a-tic-tac-toe-game
0
1
```python3 []\nclass Solution:\n def tictactoe(self, moves: List[List[int]]) -> str:\n winner = None\n matrix = [[0 for _ in range(3)] for _ in range(3)]\n\n # use 1 for the first player move, 5 \u2014 the second player\n # winner sum on row/col/diagonal == 3(1+1+1) or 15(5+5+5)\n ...
8
**Tic-tac-toe** is played by two players `A` and `B` on a `3 x 3` grid. The rules of Tic-Tac-Toe are: * Players take turns placing characters into empty squares `' '`. * The first player `A` always places `'X'` characters, while the second player `B` always places `'O'` characters. * `'X'` and `'O'` characters a...
Find the parent of each node. A valid tree must have nodes with only one parent and exactly one node with no parent.
🔥 [Python 3] 2 solutions, beats 90% 🥷🏼
find-winner-on-a-tic-tac-toe-game
0
1
```python3 []\nclass Solution:\n def tictactoe(self, moves: List[List[int]]) -> str:\n winner = None\n matrix = [[0 for _ in range(3)] for _ in range(3)]\n\n # use 1 for the first player move, 5 \u2014 the second player\n # winner sum on row/col/diagonal == 3(1+1+1) or 15(5+5+5)\n ...
8
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
[Java/Python/C++] 0ms, short and simple, all 8 ways to win in one array
find-winner-on-a-tic-tac-toe-game
0
1
There are 8 ways to win for each player:\n - 3 columns\n - 3 rows\n - 2 diagonals\n \nPlayers make moves one by one so all odd moves are for player A, even for B.\nNow we just need to track if we reach 3 in any line for any of the players.\nOne array keeps all ways to win for each player:\n - 0,1,2 - for rows\n - 3,4...
164
**Tic-tac-toe** is played by two players `A` and `B` on a `3 x 3` grid. The rules of Tic-Tac-Toe are: * Players take turns placing characters into empty squares `' '`. * The first player `A` always places `'X'` characters, while the second player `B` always places `'O'` characters. * `'X'` and `'O'` characters a...
Find the parent of each node. A valid tree must have nodes with only one parent and exactly one node with no parent.
[Java/Python/C++] 0ms, short and simple, all 8 ways to win in one array
find-winner-on-a-tic-tac-toe-game
0
1
There are 8 ways to win for each player:\n - 3 columns\n - 3 rows\n - 2 diagonals\n \nPlayers make moves one by one so all odd moves are for player A, even for B.\nNow we just need to track if we reach 3 in any line for any of the players.\nOne array keeps all ways to win for each player:\n - 0,1,2 - for rows\n - 3,4...
164
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
Python Short, Optimized and Interview Friendly Solution
find-winner-on-a-tic-tac-toe-game
0
1
* Instead of harcoding 3 all over the place. Solve for n * n grid and set n = 3 in the beginning. That way the code is scalable and there will just be a one line change if the interviewer asks your for a 4x4 grid.\n* Storing the rows and cols as array of size n + 2 variables for diagonals and reverse diagonal. This req...
111
**Tic-tac-toe** is played by two players `A` and `B` on a `3 x 3` grid. The rules of Tic-Tac-Toe are: * Players take turns placing characters into empty squares `' '`. * The first player `A` always places `'X'` characters, while the second player `B` always places `'O'` characters. * `'X'` and `'O'` characters a...
Find the parent of each node. A valid tree must have nodes with only one parent and exactly one node with no parent.
Python Short, Optimized and Interview Friendly Solution
find-winner-on-a-tic-tac-toe-game
0
1
* Instead of harcoding 3 all over the place. Solve for n * n grid and set n = 3 in the beginning. That way the code is scalable and there will just be a one line change if the interviewer asks your for a 4x4 grid.\n* Storing the rows and cols as array of size n + 2 variables for diagonals and reverse diagonal. This req...
111
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
Python/C++ solution beats 100% w printing Board
find-winner-on-a-tic-tac-toe-game
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo design a Tic Tac Toe game, you have to at first judge who wins. Use "012345678" to denote the positions in board.\n\nThere are exactly 8 lines to observe; \n$$\n\\{\n \\{0, 1, 2\\},\\{3, 4, 5\\},\\{6, 7, 8\\},\\\\\n ...
3
**Tic-tac-toe** is played by two players `A` and `B` on a `3 x 3` grid. The rules of Tic-Tac-Toe are: * Players take turns placing characters into empty squares `' '`. * The first player `A` always places `'X'` characters, while the second player `B` always places `'O'` characters. * `'X'` and `'O'` characters a...
Find the parent of each node. A valid tree must have nodes with only one parent and exactly one node with no parent.
Python/C++ solution beats 100% w printing Board
find-winner-on-a-tic-tac-toe-game
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo design a Tic Tac Toe game, you have to at first judge who wins. Use "012345678" to denote the positions in board.\n\nThere are exactly 8 lines to observe; \n$$\n\\{\n \\{0, 1, 2\\},\\{3, 4, 5\\},\\{6, 7, 8\\},\\\\\n ...
3
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
Python 3 solution with comments
find-winner-on-a-tic-tac-toe-game
0
1
```\nclass Solution:\n def tictactoe(self, moves: List[List[int]]) -> str:\n # keep track of the "net score" of each row/col/diagonal\n # player A adds 1 to the "net score" of each row/col/diagonal they play in,\n # player B subtracts 1\n # scores[0], scores[1] and scores[2] are for rows ...
19
**Tic-tac-toe** is played by two players `A` and `B` on a `3 x 3` grid. The rules of Tic-Tac-Toe are: * Players take turns placing characters into empty squares `' '`. * The first player `A` always places `'X'` characters, while the second player `B` always places `'O'` characters. * `'X'` and `'O'` characters a...
Find the parent of each node. A valid tree must have nodes with only one parent and exactly one node with no parent.
Python 3 solution with comments
find-winner-on-a-tic-tac-toe-game
0
1
```\nclass Solution:\n def tictactoe(self, moves: List[List[int]]) -> str:\n # keep track of the "net score" of each row/col/diagonal\n # player A adds 1 to the "net score" of each row/col/diagonal they play in,\n # player B subtracts 1\n # scores[0], scores[1] and scores[2] are for rows ...
19
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
Python3
find-winner-on-a-tic-tac-toe-game
0
1
\n# Code\n```\nclass Solution:\n def tictactoe(self, moves: List[List[int]]) -> str:\n\n board = [[\' \' for _ in range(3)] for _ in range(3)]\n player_moves = {\'A\': [], \'B\': []}\n win_combinations = [[[i, j] for j in range(3)] for i in range(3)] + \\\n [[[j, i] fo...
1
**Tic-tac-toe** is played by two players `A` and `B` on a `3 x 3` grid. The rules of Tic-Tac-Toe are: * Players take turns placing characters into empty squares `' '`. * The first player `A` always places `'X'` characters, while the second player `B` always places `'O'` characters. * `'X'` and `'O'` characters a...
Find the parent of each node. A valid tree must have nodes with only one parent and exactly one node with no parent.
Python3
find-winner-on-a-tic-tac-toe-game
0
1
\n# Code\n```\nclass Solution:\n def tictactoe(self, moves: List[List[int]]) -> str:\n\n board = [[\' \' for _ in range(3)] for _ in range(3)]\n player_moves = {\'A\': [], \'B\': []}\n win_combinations = [[[i, j] for j in range(3)] for i in range(3)] + \\\n [[[j, i] fo...
1
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
Python elegant and short, based on bitmasks
find-winner-on-a-tic-tac-toe-game
0
1
\n WIN_MASKS = {7, 56, 73, 84, 146, 273, 292, 448}\n\n def tictactoe(self, moves: List[List[int]]) -> str:\n cross_mask = zero_mask = 0\n\n for ind, (r, c) in enumerate(moves):\n if ind & 1:\n zero_mask |= 1 << 3 * r + c\n else:\n cross_mask |= 1 <...
3
**Tic-tac-toe** is played by two players `A` and `B` on a `3 x 3` grid. The rules of Tic-Tac-Toe are: * Players take turns placing characters into empty squares `' '`. * The first player `A` always places `'X'` characters, while the second player `B` always places `'O'` characters. * `'X'` and `'O'` characters a...
Find the parent of each node. A valid tree must have nodes with only one parent and exactly one node with no parent.
Python elegant and short, based on bitmasks
find-winner-on-a-tic-tac-toe-game
0
1
\n WIN_MASKS = {7, 56, 73, 84, 146, 273, 292, 448}\n\n def tictactoe(self, moves: List[List[int]]) -> str:\n cross_mask = zero_mask = 0\n\n for ind, (r, c) in enumerate(moves):\n if ind & 1:\n zero_mask |= 1 << 3 * r + c\n else:\n cross_mask |= 1 <...
3
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
FAST but long code || is there a shorter way? || Explained
find-winner-on-a-tic-tac-toe-game
0
1
# Intuition\nYou can just try all possibilities. I used ifs for the diagonals and nested loops for the rows and columns.\n\n# Approach\nThis is slow. This post must be one of my worst. Ok, I\'ll get started.\n- creating the grid\n - create an empty grid first\n - loop over `moves` starting from index `0` and skip...
1
**Tic-tac-toe** is played by two players `A` and `B` on a `3 x 3` grid. The rules of Tic-Tac-Toe are: * Players take turns placing characters into empty squares `' '`. * The first player `A` always places `'X'` characters, while the second player `B` always places `'O'` characters. * `'X'` and `'O'` characters a...
Find the parent of each node. A valid tree must have nodes with only one parent and exactly one node with no parent.
FAST but long code || is there a shorter way? || Explained
find-winner-on-a-tic-tac-toe-game
0
1
# Intuition\nYou can just try all possibilities. I used ifs for the diagonals and nested loops for the rows and columns.\n\n# Approach\nThis is slow. This post must be one of my worst. Ok, I\'ll get started.\n- creating the grid\n - create an empty grid first\n - loop over `moves` starting from index `0` and skip...
1
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
[Python3] memo scores
find-winner-on-a-tic-tac-toe-game
0
1
For each player, we use a list `score` of 8 elements with below meaning \n0-2 : number of characters placed at each row \n3-5 : number of characters placed at each column\n6 : number of characters placed at diagonal \n7 : number of characters placed at anti-diagonal\nto keep track of his/her winning status. \n\nHere, "...
16
**Tic-tac-toe** is played by two players `A` and `B` on a `3 x 3` grid. The rules of Tic-Tac-Toe are: * Players take turns placing characters into empty squares `' '`. * The first player `A` always places `'X'` characters, while the second player `B` always places `'O'` characters. * `'X'` and `'O'` characters a...
Find the parent of each node. A valid tree must have nodes with only one parent and exactly one node with no parent.
[Python3] memo scores
find-winner-on-a-tic-tac-toe-game
0
1
For each player, we use a list `score` of 8 elements with below meaning \n0-2 : number of characters placed at each row \n3-5 : number of characters placed at each column\n6 : number of characters placed at diagonal \n7 : number of characters placed at anti-diagonal\nto keep track of his/her winning status. \n\nHere, "...
16
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
Python - 30ms Solution - Easy
find-winner-on-a-tic-tac-toe-game
0
1
30ms code- Easy To understand...\n```\nclass Solution:\n def tictactoe(self, moves: List[List[int]]) -> str:\n if len(moves)<5:\n return"Pending"\n grid=[[0,0,0],[0,0,0],[0,0,0]]\n for i in range(len(moves)):\n if i%2:\n grid[moves[i][0]][moves[i][1]]= "B"\n ...
1
**Tic-tac-toe** is played by two players `A` and `B` on a `3 x 3` grid. The rules of Tic-Tac-Toe are: * Players take turns placing characters into empty squares `' '`. * The first player `A` always places `'X'` characters, while the second player `B` always places `'O'` characters. * `'X'` and `'O'` characters a...
Find the parent of each node. A valid tree must have nodes with only one parent and exactly one node with no parent.
Python - 30ms Solution - Easy
find-winner-on-a-tic-tac-toe-game
0
1
30ms code- Easy To understand...\n```\nclass Solution:\n def tictactoe(self, moves: List[List[int]]) -> str:\n if len(moves)<5:\n return"Pending"\n grid=[[0,0,0],[0,0,0],[0,0,0]]\n for i in range(len(moves)):\n if i%2:\n grid[moves[i][0]][moves[i][1]]= "B"\n ...
1
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
Alternative Solution Binary Search | Explained
number-of-burgers-with-no-waste-of-ingredients
0
1
# Intuition\nWell you can do this problem in many linear way but i tried to do this in binary search for a better understanding of binary Search algorithm.\nSo what are the left and right limits? Its the 0 and the max Jumbo burger we can create from given tomatoes(t).\nFind the remaining tomatoes left for making small ...
1
Given two integers `tomatoSlices` and `cheeseSlices`. The ingredients of different burgers are as follows: * **Jumbo Burger:** `4` tomato slices and `1` cheese slice. * **Small Burger:** `2` Tomato slices and `1` cheese slice. Return `[total_jumbo, total_small]` so that the number of remaining `tomatoSlices` equa...
Find the divisors of n+1 and n+2. To find the divisors of a number, you only need to iterate to the square root of that number.
Alternative Solution Binary Search | Explained
number-of-burgers-with-no-waste-of-ingredients
0
1
# Intuition\nWell you can do this problem in many linear way but i tried to do this in binary search for a better understanding of binary Search algorithm.\nSo what are the left and right limits? Its the 0 and the max Jumbo burger we can create from given tomatoes(t).\nFind the remaining tomatoes left for making small ...
1
You are given a circle represented as `(radius, xCenter, yCenter)` and an axis-aligned rectangle represented as `(x1, y1, x2, y2)`, where `(x1, y1)` are the coordinates of the bottom-left corner, and `(x2, y2)` are the coordinates of the top-right corner of the rectangle. Return `true` _if the circle and rectangle are...
Can we have an answer if the number of tomatoes is odd ? If we have answer will be there multiple answers or just one answer ? Let us define number of jumbo burgers as X and number of small burgers as Y We have to find an x and y in this equation 1. 4X + 2Y = tomato 2. X + Y = cheese
Pure Maths
number-of-burgers-with-no-waste-of-ingredients
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 two integers `tomatoSlices` and `cheeseSlices`. The ingredients of different burgers are as follows: * **Jumbo Burger:** `4` tomato slices and `1` cheese slice. * **Small Burger:** `2` Tomato slices and `1` cheese slice. Return `[total_jumbo, total_small]` so that the number of remaining `tomatoSlices` equa...
Find the divisors of n+1 and n+2. To find the divisors of a number, you only need to iterate to the square root of that number.
Pure Maths
number-of-burgers-with-no-waste-of-ingredients
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 a circle represented as `(radius, xCenter, yCenter)` and an axis-aligned rectangle represented as `(x1, y1, x2, y2)`, where `(x1, y1)` are the coordinates of the bottom-left corner, and `(x2, y2)` are the coordinates of the top-right corner of the rectangle. Return `true` _if the circle and rectangle are...
Can we have an answer if the number of tomatoes is odd ? If we have answer will be there multiple answers or just one answer ? Let us define number of jumbo burgers as X and number of small burgers as Y We have to find an x and y in this equation 1. 4X + 2Y = tomato 2. X + Y = cheese
Python Math
number-of-burgers-with-no-waste-of-ingredients
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\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O...
0
Given two integers `tomatoSlices` and `cheeseSlices`. The ingredients of different burgers are as follows: * **Jumbo Burger:** `4` tomato slices and `1` cheese slice. * **Small Burger:** `2` Tomato slices and `1` cheese slice. Return `[total_jumbo, total_small]` so that the number of remaining `tomatoSlices` equa...
Find the divisors of n+1 and n+2. To find the divisors of a number, you only need to iterate to the square root of that number.