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
91% Faster Solution
minimum-difficulty-of-a-job-schedule
0
1
```\nclass Solution:\n def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:\n jobCount = len(jobDifficulty) \n if jobCount < d:\n return -1\n\n @lru_cache(None)\n def topDown(jobIndex: int, remainDayCount: int) -> int:\n remainJobCount = jobCount - jo...
4
Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be **pseudo-palindromic** if at least one permutation of the node values in the path is a palindrome. _Return the number of **pseudo-palindromic** paths going from the root node to leaf nodes._ **Example 1:** **Input:*...
Use DP. Try to cut the array into d non-empty sub-arrays. Try all possible cuts for the array. Use dp[i][j] where DP states are i the index of the last cut and j the number of remaining cuts. Complexity is O(n * n * d).
python3 | dp | dfs | memoziation
minimum-difficulty-of-a-job-schedule
0
1
```\nclass Solution:\n def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:\n \n n = len(jobDifficulty)\n @lru_cache(None)\n def dfs(index, remaining_days):\n \n if remaining_days == 0:\n if index == n: return 0\n else: retu...
2
You want to schedule a list of jobs in `d` days. Jobs are dependent (i.e To work on the `ith` job, you have to finish all the jobs `j` where `0 <= j < i`). You have to finish **at least** one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the `d` days. The difficulty of a da...
For a fixed number of candies c, how can you check if each child can get c candies? Use binary search to find the maximum c as the answer.
python3 | dp | dfs | memoziation
minimum-difficulty-of-a-job-schedule
0
1
```\nclass Solution:\n def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:\n \n n = len(jobDifficulty)\n @lru_cache(None)\n def dfs(index, remaining_days):\n \n if remaining_days == 0:\n if index == n: return 0\n else: retu...
2
Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be **pseudo-palindromic** if at least one permutation of the node values in the path is a palindrome. _Return the number of **pseudo-palindromic** paths going from the root node to leaf nodes._ **Example 1:** **Input:*...
Use DP. Try to cut the array into d non-empty sub-arrays. Try all possible cuts for the array. Use dp[i][j] where DP states are i the index of the last cut and j the number of remaining cuts. Complexity is O(n * n * d).
EASY PYTHON SOLUTION USING DP
minimum-difficulty-of-a-job-schedule
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 want to schedule a list of jobs in `d` days. Jobs are dependent (i.e To work on the `ith` job, you have to finish all the jobs `j` where `0 <= j < i`). You have to finish **at least** one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the `d` days. The difficulty of a da...
For a fixed number of candies c, how can you check if each child can get c candies? Use binary search to find the maximum c as the answer.
EASY PYTHON SOLUTION USING DP
minimum-difficulty-of-a-job-schedule
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 a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be **pseudo-palindromic** if at least one permutation of the node values in the path is a palindrome. _Return the number of **pseudo-palindromic** paths going from the root node to leaf nodes._ **Example 1:** **Input:*...
Use DP. Try to cut the array into d non-empty sub-arrays. Try all possible cuts for the array. Use dp[i][j] where DP states are i the index of the last cut and j the number of remaining cuts. Complexity is O(n * n * d).
Python 3 Memoized and Tabulated Solution (Credits: Striver DP Series)
minimum-difficulty-of-a-job-schedule
0
1
**The Problem is Same as Partition Array For max Sum, So You can Refer To Striver\'s DP Series for the Solution of that problem.\nIn This You Just need to minimize the Sum instead of max and keep track of no of remaining days rest is just the same.**\n```\nclass Solution1: # Memoized Solution\n def minDifficulty(sel...
2
You want to schedule a list of jobs in `d` days. Jobs are dependent (i.e To work on the `ith` job, you have to finish all the jobs `j` where `0 <= j < i`). You have to finish **at least** one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the `d` days. The difficulty of a da...
For a fixed number of candies c, how can you check if each child can get c candies? Use binary search to find the maximum c as the answer.
Python 3 Memoized and Tabulated Solution (Credits: Striver DP Series)
minimum-difficulty-of-a-job-schedule
0
1
**The Problem is Same as Partition Array For max Sum, So You can Refer To Striver\'s DP Series for the Solution of that problem.\nIn This You Just need to minimize the Sum instead of max and keep track of no of remaining days rest is just the same.**\n```\nclass Solution1: # Memoized Solution\n def minDifficulty(sel...
2
Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be **pseudo-palindromic** if at least one permutation of the node values in the path is a palindrome. _Return the number of **pseudo-palindromic** paths going from the root node to leaf nodes._ **Example 1:** **Input:*...
Use DP. Try to cut the array into d non-empty sub-arrays. Try all possible cuts for the array. Use dp[i][j] where DP states are i the index of the last cut and j the number of remaining cuts. Complexity is O(n * n * d).
Python 3 || 1 line, w/ explanation || T/S: 98% / 94%
the-k-weakest-rows-in-a-matrix
0
1
Here\'s the plan:\n\n- We sort all `x`in `range(len(nums))` using the sum of the xth row as the key. (We could also sort on the rows themselves--`key = lambda x: mat[x]`-- but sorting on integer seems to be more efficient)\n\n- We return the initial`k`elements of this sorted range.\n```\nclass Solution:\n def kWeake...
6
You are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
Python 3 || 1 line, w/ explanation || T/S: 98% / 94%
the-k-weakest-rows-in-a-matrix
0
1
Here\'s the plan:\n\n- We sort all `x`in `range(len(nums))` using the sum of the xth row as the key. (We could also sort on the rows themselves--`key = lambda x: mat[x]`-- but sorting on integer seems to be more efficient)\n\n- We return the initial`k`elements of this sorted range.\n```\nclass Solution:\n def kWeake...
6
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
【Video】Beats 97.49% - Simple Heap solution - Python, JavaScript, Java, C++
the-k-weakest-rows-in-a-matrix
1
1
# Intuition\nUsing min heap with data like (soldiers, index). This Python solution beats 97%.\n\n![Screen Shot 2023-09-18 at 18.13.34.png](https://assets.leetcode.com/users/images/dc715f78-d301-49bd-8904-6d1fab84781a_1695028452.0074272.png)\n\n\n---\n\n# Solution Video\n\nhttps://youtu.be/mv8FvPlQxtM\n\nIn the video, t...
23
You are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
【Video】Beats 97.49% - Simple Heap solution - Python, JavaScript, Java, C++
the-k-weakest-rows-in-a-matrix
1
1
# Intuition\nUsing min heap with data like (soldiers, index). This Python solution beats 97%.\n\n![Screen Shot 2023-09-18 at 18.13.34.png](https://assets.leetcode.com/users/images/dc715f78-d301-49bd-8904-6d1fab84781a_1695028452.0074272.png)\n\n\n---\n\n# Solution Video\n\nhttps://youtu.be/mv8FvPlQxtM\n\nIn the video, t...
23
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
Binary Search, Sorting, Priority Queue + bonus 1 liner
the-k-weakest-rows-in-a-matrix
0
1
\n# Code\nPQ:\n```\nclass Solution {\npublic:\n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; \n vector<int> res;\n for(int i = 0; i < mat.size(); ++i) {\n pq.push({accumulate(begin(mat...
2
You are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
Binary Search, Sorting, Priority Queue + bonus 1 liner
the-k-weakest-rows-in-a-matrix
0
1
\n# Code\nPQ:\n```\nclass Solution {\npublic:\n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; \n vector<int> res;\n for(int i = 0; i < mat.size(); ++i) {\n pq.push({accumulate(begin(mat...
2
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
✅ 98.41% Sorting & Min-Heap & Binary Search
the-k-weakest-rows-in-a-matrix
1
1
# Comprehensive Guide to Solving "The K Weakest Rows in a Matrix"\n\n## Introduction & Problem Statement\n\nIn today\'s journey through the realm of matrices, we aim to identify the k weakest rows based on the number of soldiers. Each row in the matrix represents a combination of soldiers (1\'s) and civilians (0\'s). T...
78
You are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
✅ 98.41% Sorting & Min-Heap & Binary Search
the-k-weakest-rows-in-a-matrix
1
1
# Comprehensive Guide to Solving "The K Weakest Rows in a Matrix"\n\n## Introduction & Problem Statement\n\nIn today\'s journey through the realm of matrices, we aim to identify the k weakest rows based on the number of soldiers. Each row in the matrix represents a combination of soldiers (1\'s) and civilians (0\'s). T...
78
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
Python 3 || Brute
the-k-weakest-rows-in-a-matrix
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
You are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
Python 3 || Brute
the-k-weakest-rows-in-a-matrix
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
✅98.65%🔥Min-Heap & Sorting 🔥2 linear code🔥Py/C/C#/C++/Java
the-k-weakest-rows-in-a-matrix
1
1
# Problem\n**The problem statement describes a binary matrix where 1\'s represent soldiers and 0\'s represent civilians. The task is to find the k weakest rows in the matrix based on the following criteria:**\n\n###### **1.** A row i is weaker than a row j if the number of soldiers in row i is less than the number of s...
14
You are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
✅98.65%🔥Min-Heap & Sorting 🔥2 linear code🔥Py/C/C#/C++/Java
the-k-weakest-rows-in-a-matrix
1
1
# Problem\n**The problem statement describes a binary matrix where 1\'s represent soldiers and 0\'s represent civilians. The task is to find the k weakest rows in the matrix based on the following criteria:**\n\n###### **1.** A row i is weaker than a row j if the number of soldiers in row i is less than the number of s...
14
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
✔98.90% Beats C++ || Easy to understand 📈✨|| Commented Code Explanation || 3Beginner😉😎
the-k-weakest-rows-in-a-matrix
1
1
# Intuition\n- The code is designed to find the k weakest rows in a binary matrix. A "weakest" row is determined by the number of ones it contains. \n- The code calculates the number of ones in each row, then uses a set to store pairs of (count of ones, row index). \n- Finally, it extracts the k weakest rows from the s...
11
You are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
✔98.90% Beats C++ || Easy to understand 📈✨|| Commented Code Explanation || 3Beginner😉😎
the-k-weakest-rows-in-a-matrix
1
1
# Intuition\n- The code is designed to find the k weakest rows in a binary matrix. A "weakest" row is determined by the number of ones it contains. \n- The code calculates the number of ones in each row, then uses a set to store pairs of (count of ones, row index). \n- Finally, it extracts the k weakest rows from the s...
11
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
🔥🔥 Python 3 | Beats 91.08% Runtime and 70.8% Memory | Min Heap Solution 🔥🔥
the-k-weakest-rows-in-a-matrix
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:\nO(nlogn) Time\n\n- Space complexity:\nO(n) Memory\n\n# Code\n```\nclass Solution:\n def kWeakestRows(self, mat: List[List[int]],...
1
You are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
🔥🔥 Python 3 | Beats 91.08% Runtime and 70.8% Memory | Min Heap Solution 🔥🔥
the-k-weakest-rows-in-a-matrix
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:\nO(nlogn) Time\n\n- Space complexity:\nO(n) Memory\n\n# Code\n```\nclass Solution:\n def kWeakestRows(self, mat: List[List[int]],...
1
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
One row Solution Python
the-k-weakest-rows-in-a-matrix
0
1
\n# Complexity\n- Time complexity: $$O(n log (n))$$\n\n# Code\n```python []\nclass Solution:\n def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:\n return [\n x[0] for x in sorted(enumerate(map(sum, mat)), key=lambda x: x[::-1])\n ][:k]\n\n```
1
You are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
One row Solution Python
the-k-weakest-rows-in-a-matrix
0
1
\n# Complexity\n- Time complexity: $$O(n log (n))$$\n\n# Code\n```python []\nclass Solution:\n def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:\n return [\n x[0] for x in sorted(enumerate(map(sum, mat)), key=lambda x: x[::-1])\n ][:k]\n\n```
1
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
Python 1-liners. 2 solutions. Functional programming.
the-k-weakest-rows-in-a-matrix
0
1
# Approach 1: Sum and Sort\n1. Find the `soldiers` for each row `x` by summing over `mat[x]`.\n\n2. Sort the `soldiers` for each index from `0` to `m` and return the first `k` indices.\n\n# Complexity\n- Time complexity: $$O(m \\cdot n + m \\cdot log_2(m))$$\n\n- Space complexity: $$O(m)$$\n\nwhere,\n`m x n is the dime...
1
You are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
Python 1-liners. 2 solutions. Functional programming.
the-k-weakest-rows-in-a-matrix
0
1
# Approach 1: Sum and Sort\n1. Find the `soldiers` for each row `x` by summing over `mat[x]`.\n\n2. Sort the `soldiers` for each index from `0` to `m` and return the first `k` indices.\n\n# Complexity\n- Time complexity: $$O(m \\cdot n + m \\cdot log_2(m))$$\n\n- Space complexity: $$O(m)$$\n\nwhere,\n`m x n is the dime...
1
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
Python3 oneline 95%
the-k-weakest-rows-in-a-matrix
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 are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
Python3 oneline 95%
the-k-weakest-rows-in-a-matrix
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 are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
[Python3] Easy to understand | Modified bucket sort approach | Linear Time
the-k-weakest-rows-in-a-matrix
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(m x n)\n\n- Space complexity:\nO( max(m, n) )\n\n# Code\n```\nclass Solution:\n def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:\n counts = collections.defaultdict(int)\n\n # Cre...
1
You are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
[Python3] Easy to understand | Modified bucket sort approach | Linear Time
the-k-weakest-rows-in-a-matrix
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(m x n)\n\n- Space complexity:\nO( max(m, n) )\n\n# Code\n```\nclass Solution:\n def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:\n counts = collections.defaultdict(int)\n\n # Cre...
1
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
Python3 Solution
the-k-weakest-rows-in-a-matrix
0
1
\n```\nclass Solution:\n def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:\n ans=[]\n n=len(mat)\n for i in range(n):\n ans.append([mat[i].count(1),i])\n\n ans.sort() \n return [i[1] for i in ans[:k]]\n```
1
You are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
Python3 Solution
the-k-weakest-rows-in-a-matrix
0
1
\n```\nclass Solution:\n def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:\n ans=[]\n n=len(mat)\n for i in range(n):\n ans.append([mat[i].count(1),i])\n\n ans.sort() \n return [i[1] for i in ans[:k]]\n```
1
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
Simple Python Solution || Beats 99.07%📶 || HashMap✅
the-k-weakest-rows-in-a-matrix
0
1
# Intuition\nBuilding a **hash table** for number of ones in each row.\n\n# Approach\n1. Decalre a Hash Table with key as row index and value of each key as no of ones in each row.\n\n2. Sorting the hash table with key as values.\n\n3. Returning the top k elements from the sorted list.\n# Complexity\n- Time complexity:...
1
You are given an `m x n` binary matrix `mat` of `1`'s (representing soldiers) and `0`'s (representing civilians). The soldiers are positioned **in front** of the civilians. That is, all the `1`'s will appear to the **left** of all the `0`'s in each row. A row `i` is **weaker** than a row `j` if one of the following is...
null
Simple Python Solution || Beats 99.07%📶 || HashMap✅
the-k-weakest-rows-in-a-matrix
0
1
# Intuition\nBuilding a **hash table** for number of ones in each row.\n\n# Approach\n1. Decalre a Hash Table with key as row index and value of each key as no of ones in each row.\n\n2. Sorting the hash table with key as values.\n\n3. Returning the top k elements from the sorted list.\n# Complexity\n- Time complexity:...
1
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell. You have two robots that can collect cherries for you: * **Robot #1** is located at the **top-left corner** `(0, 0)`, and * **Robot #2** is...
Sort the matrix row indexes by the number of soldiers and then row indexes.
👾C#, Java, Python3,JavaScript Solution
reduce-array-size-to-the-half
1
1
See all the code :**\u2B50[https://zyrastory.com/en/coding-en/leetcode-en/leetcode-1338-reduce-array-size-to-the-half-solution-and-explanation-en/](https://zyrastory.com/en/coding-en/leetcode-en/leetcode-1338-reduce-array-size-to-the-half-solution-and-explanation-en/)\u2B50**\n\nExample :\n\n**C# Solution - Dictionary ...
3
You are given an integer array `arr`. You can choose a set of integers and remove all the occurrences of these integers in the array. Return _the minimum size of the set so that **at least** half of the integers of the array are removed_. **Example 1:** **Input:** arr = \[3,3,3,3,5,5,5,2,2,7\] **Output:** 2 **Explan...
null
👾C#, Java, Python3,JavaScript Solution
reduce-array-size-to-the-half
1
1
See all the code :**\u2B50[https://zyrastory.com/en/coding-en/leetcode-en/leetcode-1338-reduce-array-size-to-the-half-solution-and-explanation-en/](https://zyrastory.com/en/coding-en/leetcode-en/leetcode-1338-reduce-array-size-to-the-half-solution-and-explanation-en/)\u2B50**\n\nExample :\n\n**C# Solution - Dictionary ...
3
Given the array of integers `nums`, you will choose two different indices `i` and `j` of that array. _Return the maximum value of_ `(nums[i]-1)*(nums[j]-1)`. **Example 1:** **Input:** nums = \[3,4,5,2\] **Output:** 12 **Explanation:** If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum v...
Count the frequency of each integer in the array. Start with an empty set, add to the set the integer with the maximum frequency. Keep Adding the integer with the max frequency until you remove at least half of the integers.
Python Easy Solution
reduce-array-size-to-the-half
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
You are given an integer array `arr`. You can choose a set of integers and remove all the occurrences of these integers in the array. Return _the minimum size of the set so that **at least** half of the integers of the array are removed_. **Example 1:** **Input:** arr = \[3,3,3,3,5,5,5,2,2,7\] **Output:** 2 **Explan...
null
Python Easy Solution
reduce-array-size-to-the-half
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
Given the array of integers `nums`, you will choose two different indices `i` and `j` of that array. _Return the maximum value of_ `(nums[i]-1)*(nums[j]-1)`. **Example 1:** **Input:** nums = \[3,4,5,2\] **Output:** 12 **Explanation:** If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum v...
Count the frequency of each integer in the array. Start with an empty set, add to the set the integer with the maximum frequency. Keep Adding the integer with the max frequency until you remove at least half of the integers.
[Python] - Hashmap and Heap - O(N) but optimal
reduce-array-size-to-the-half
0
1
My thought process regarding the solution was the following:\n\n1) We need some informations about the count of numbers \n2) We need to somehow sort the count of number as we need to subtract numbers with the highest count first to reach an optimal solution\n3) We might not need all of the counts, in the best case just...
2
You are given an integer array `arr`. You can choose a set of integers and remove all the occurrences of these integers in the array. Return _the minimum size of the set so that **at least** half of the integers of the array are removed_. **Example 1:** **Input:** arr = \[3,3,3,3,5,5,5,2,2,7\] **Output:** 2 **Explan...
null
[Python] - Hashmap and Heap - O(N) but optimal
reduce-array-size-to-the-half
0
1
My thought process regarding the solution was the following:\n\n1) We need some informations about the count of numbers \n2) We need to somehow sort the count of number as we need to subtract numbers with the highest count first to reach an optimal solution\n3) We might not need all of the counts, in the best case just...
2
Given the array of integers `nums`, you will choose two different indices `i` and `j` of that array. _Return the maximum value of_ `(nums[i]-1)*(nums[j]-1)`. **Example 1:** **Input:** nums = \[3,4,5,2\] **Output:** 12 **Explanation:** If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum v...
Count the frequency of each integer in the array. Start with an empty set, add to the set the integer with the maximum frequency. Keep Adding the integer with the max frequency until you remove at least half of the integers.
👾C#, Java, Python3,JavaScript Solution
reduce-array-size-to-the-half
1
1
See all the code :**\u2B50[https://zyrastory.com/en/coding-en/leetcode-en/leetcode-1338-reduce-array-size-to-the-half-solution-and-explanation-en/](https://zyrastory.com/en/coding-en/leetcode-en/leetcode-1338-reduce-array-size-to-the-half-solution-and-explanation-en/)\u2B50**\n\nExample :\n\n**C# Solution - Dictionary ...
2
You are given an integer array `arr`. You can choose a set of integers and remove all the occurrences of these integers in the array. Return _the minimum size of the set so that **at least** half of the integers of the array are removed_. **Example 1:** **Input:** arr = \[3,3,3,3,5,5,5,2,2,7\] **Output:** 2 **Explan...
null
👾C#, Java, Python3,JavaScript Solution
reduce-array-size-to-the-half
1
1
See all the code :**\u2B50[https://zyrastory.com/en/coding-en/leetcode-en/leetcode-1338-reduce-array-size-to-the-half-solution-and-explanation-en/](https://zyrastory.com/en/coding-en/leetcode-en/leetcode-1338-reduce-array-size-to-the-half-solution-and-explanation-en/)\u2B50**\n\nExample :\n\n**C# Solution - Dictionary ...
2
Given the array of integers `nums`, you will choose two different indices `i` and `j` of that array. _Return the maximum value of_ `(nums[i]-1)*(nums[j]-1)`. **Example 1:** **Input:** nums = \[3,4,5,2\] **Output:** 12 **Explanation:** If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum v...
Count the frequency of each integer in the array. Start with an empty set, add to the set the integer with the maximum frequency. Keep Adding the integer with the max frequency until you remove at least half of the integers.
recursion does the job
maximum-product-of-splitted-binary-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
6
Given the `root` of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized. Return _the maximum product of the sums of the two subtrees_. Since the answer may be too large, return it **modulo** `109 + 7`. **Note** that you need to max...
null
recursion does the job
maximum-product-of-splitted-binary-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
6
You are given a rectangular cake of size `h x w` and two arrays of integers `horizontalCuts` and `verticalCuts` where: * `horizontalCuts[i]` is the distance from the top of the rectangular cake to the `ith` horizontal cut and similarly, and * `verticalCuts[j]` is the distance from the left of the rectangular cake ...
If we know the sum of a subtree, the answer is max( (total_sum - subtree_sum) * subtree_sum) in each node.
Clean and Easy and Fast Python3 Code 🌴
maximum-product-of-splitted-binary-tree
0
1
> Python3 Code\n# Code\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def maxProduct(self, root: Optional[TreeNode]) -> int:\n def dfs(...
1
Given the `root` of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized. Return _the maximum product of the sums of the two subtrees_. Since the answer may be too large, return it **modulo** `109 + 7`. **Note** that you need to max...
null
Clean and Easy and Fast Python3 Code 🌴
maximum-product-of-splitted-binary-tree
0
1
> Python3 Code\n# Code\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def maxProduct(self, root: Optional[TreeNode]) -> int:\n def dfs(...
1
You are given a rectangular cake of size `h x w` and two arrays of integers `horizontalCuts` and `verticalCuts` where: * `horizontalCuts[i]` is the distance from the top of the rectangular cake to the `ith` horizontal cut and similarly, and * `verticalCuts[j]` is the distance from the left of the rectangular cake ...
If we know the sum of a subtree, the answer is max( (total_sum - subtree_sum) * subtree_sum) in each node.
Python: 85% Faster | O(n) Time complexity | Smallest difference from half of sum | DFS
maximum-product-of-splitted-binary-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBreak the edge above node where current sum and half of sum have smallest difference.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nDFS\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here...
1
Given the `root` of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized. Return _the maximum product of the sums of the two subtrees_. Since the answer may be too large, return it **modulo** `109 + 7`. **Note** that you need to max...
null
Python: 85% Faster | O(n) Time complexity | Smallest difference from half of sum | DFS
maximum-product-of-splitted-binary-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBreak the edge above node where current sum and half of sum have smallest difference.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nDFS\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here...
1
You are given a rectangular cake of size `h x w` and two arrays of integers `horizontalCuts` and `verticalCuts` where: * `horizontalCuts[i]` is the distance from the top of the rectangular cake to the `ith` horizontal cut and similarly, and * `verticalCuts[j]` is the distance from the left of the rectangular cake ...
If we know the sum of a subtree, the answer is max( (total_sum - subtree_sum) * subtree_sum) in each node.
Backtracking to find sub tree sum
maximum-product-of-splitted-binary-tree
0
1
Find sub tree sums of all nodes and then the find max product.\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def maxProduct(self, root: Optio...
1
Given the `root` of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized. Return _the maximum product of the sums of the two subtrees_. Since the answer may be too large, return it **modulo** `109 + 7`. **Note** that you need to max...
null
Backtracking to find sub tree sum
maximum-product-of-splitted-binary-tree
0
1
Find sub tree sums of all nodes and then the find max product.\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def maxProduct(self, root: Optio...
1
You are given a rectangular cake of size `h x w` and two arrays of integers `horizontalCuts` and `verticalCuts` where: * `horizontalCuts[i]` is the distance from the top of the rectangular cake to the `ith` horizontal cut and similarly, and * `verticalCuts[j]` is the distance from the left of the rectangular cake ...
If we know the sum of a subtree, the answer is max( (total_sum - subtree_sum) * subtree_sum) in each node.
Easy, Clean, Recursive Python3 Solution🔥🐍
maximum-product-of-splitted-binary-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can see in the question that we would need to find two such subtrees of the given subtree such that the product of their separate sums is maximum.\nTo do so, we need to know the sum of each subtree. That is waht we are doing in this so...
2
Given the `root` of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized. Return _the maximum product of the sums of the two subtrees_. Since the answer may be too large, return it **modulo** `109 + 7`. **Note** that you need to max...
null
Easy, Clean, Recursive Python3 Solution🔥🐍
maximum-product-of-splitted-binary-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can see in the question that we would need to find two such subtrees of the given subtree such that the product of their separate sums is maximum.\nTo do so, we need to know the sum of each subtree. That is waht we are doing in this so...
2
You are given a rectangular cake of size `h x w` and two arrays of integers `horizontalCuts` and `verticalCuts` where: * `horizontalCuts[i]` is the distance from the top of the rectangular cake to the `ith` horizontal cut and similarly, and * `verticalCuts[j]` is the distance from the left of the rectangular cake ...
If we know the sum of a subtree, the answer is max( (total_sum - subtree_sum) * subtree_sum) in each node.
Python | Visitor Pattern
maximum-product-of-splitted-binary-tree
0
1
# Intuition\n\nI assume that you already know the solution: compute all subtree sums and find the best product of `(tree_sum - subtree_sum) * subtree_sum`. The purpose of this post is to discuss the implementation.\n\n# Approach\n\nYour initial approach may look something like this:\n\n```py\nclass Solution:\n def m...
1
Given the `root` of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized. Return _the maximum product of the sums of the two subtrees_. Since the answer may be too large, return it **modulo** `109 + 7`. **Note** that you need to max...
null
Python | Visitor Pattern
maximum-product-of-splitted-binary-tree
0
1
# Intuition\n\nI assume that you already know the solution: compute all subtree sums and find the best product of `(tree_sum - subtree_sum) * subtree_sum`. The purpose of this post is to discuss the implementation.\n\n# Approach\n\nYour initial approach may look something like this:\n\n```py\nclass Solution:\n def m...
1
You are given a rectangular cake of size `h x w` and two arrays of integers `horizontalCuts` and `verticalCuts` where: * `horizontalCuts[i]` is the distance from the top of the rectangular cake to the `ith` horizontal cut and similarly, and * `verticalCuts[j]` is the distance from the left of the rectangular cake ...
If we know the sum of a subtree, the answer is max( (total_sum - subtree_sum) * subtree_sum) in each node.
Intuitive Topological Sort || Python || O(n*d)
jump-game-v
0
1
# Intuition\nThe idea is we construct a topological order (using monotonic stack) and take the length of the longest path.\n\n\n# Complexity\n- Time complexity:\nO(n*d)\n\n- Space complexity:\nO(n*d)\n\n# Code\n```\nclass Solution:\n def maxJumps(self, arr: List[int], d: int) -> int:\n \n\n \n g...
2
Given an array of integers `arr` and an integer `d`. In one step you can jump from index `i` to index: * `i + x` where: `i + x < arr.length` and `0 < x <= d`. * `i - x` where: `i - x >= 0` and `0 < x <= d`. In addition, you can only jump from index `i` to index `j` if `arr[i] > arr[j]` and `arr[i] > arr[k]` for a...
null
Intuitive Topological Sort || Python || O(n*d)
jump-game-v
0
1
# Intuition\nThe idea is we construct a topological order (using monotonic stack) and take the length of the longest path.\n\n\n# Complexity\n- Time complexity:\nO(n*d)\n\n- Space complexity:\nO(n*d)\n\n# Code\n```\nclass Solution:\n def maxJumps(self, arr: List[int], d: int) -> int:\n \n\n \n g...
2
There are `n` cities numbered from `0` to `n - 1` and `n - 1` roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow. Roads are represented by `connections` wh...
Use dynamic programming. dp[i] is max jumps you can do starting from index i. Answer is max(dp[i]). dp[i] = 1 + max (dp[j]) where j is all indices you can reach from i.
Easy, Explained Python Soln. time: O(n*d) space:O(n)
jump-game-v
0
1
My solution with Explanation...\nwe Can use even less space if we store dp = [0]*N and when its value is updated it will alway be greater than 0.\nthere by seen (set) can be removed. \nreplacing \nif indx in seen: -> if dp[indx] \nHope it makes Sense. \nfeel free to comment. Networking helps \n\n\n# space complexity...
1
Given an array of integers `arr` and an integer `d`. In one step you can jump from index `i` to index: * `i + x` where: `i + x < arr.length` and `0 < x <= d`. * `i - x` where: `i - x >= 0` and `0 < x <= d`. In addition, you can only jump from index `i` to index `j` if `arr[i] > arr[j]` and `arr[i] > arr[k]` for a...
null
Easy, Explained Python Soln. time: O(n*d) space:O(n)
jump-game-v
0
1
My solution with Explanation...\nwe Can use even less space if we store dp = [0]*N and when its value is updated it will alway be greater than 0.\nthere by seen (set) can be removed. \nreplacing \nif indx in seen: -> if dp[indx] \nHope it makes Sense. \nfeel free to comment. Networking helps \n\n\n# space complexity...
1
There are `n` cities numbered from `0` to `n - 1` and `n - 1` roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow. Roads are represented by `connections` wh...
Use dynamic programming. dp[i] is max jumps you can do starting from index i. Answer is max(dp[i]). dp[i] = 1 + max (dp[j]) where j is all indices you can reach from i.
✔ Python3 Solution | DP | O(nd)
jump-game-v
0
1
# Complexity\n- Time complexity: $$O(nd)$$\n- Space complexity: $$O(n)$$\n\n# Solution 1\n```\nclass Solution:\n def maxJumps(self, A, d):\n n = len(A)\n dp = [1] * n\n for i in sorted(range(n), key = lambda x: -A[x]):\n for x in range(i - 1, max(0, i - d) - 1, -1):\n i...
3
Given an array of integers `arr` and an integer `d`. In one step you can jump from index `i` to index: * `i + x` where: `i + x < arr.length` and `0 < x <= d`. * `i - x` where: `i - x >= 0` and `0 < x <= d`. In addition, you can only jump from index `i` to index `j` if `arr[i] > arr[j]` and `arr[i] > arr[k]` for a...
null
✔ Python3 Solution | DP | O(nd)
jump-game-v
0
1
# Complexity\n- Time complexity: $$O(nd)$$\n- Space complexity: $$O(n)$$\n\n# Solution 1\n```\nclass Solution:\n def maxJumps(self, A, d):\n n = len(A)\n dp = [1] * n\n for i in sorted(range(n), key = lambda x: -A[x]):\n for x in range(i - 1, max(0, i - d) - 1, -1):\n i...
3
There are `n` cities numbered from `0` to `n - 1` and `n - 1` roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow. Roads are represented by `connections` wh...
Use dynamic programming. dp[i] is max jumps you can do starting from index i. Answer is max(dp[i]). dp[i] = 1 + max (dp[j]) where j is all indices you can reach from i.
📌📌 Well-Coded and Easy Explanation || Use of Memoization 🐍
jump-game-v
0
1
## IDEA:\nFor each index i, run search starting from i left and right up to d steps. This way, we can easily detect if arr[j] is blocking further jumps.\n\n**So, we stop the search when we encounter j where arr[i] <= arr[j].**\n\nTo prevent re-computations, we need to memoise max jumps for every index in dp.\n\n****\n*...
6
Given an array of integers `arr` and an integer `d`. In one step you can jump from index `i` to index: * `i + x` where: `i + x < arr.length` and `0 < x <= d`. * `i - x` where: `i - x >= 0` and `0 < x <= d`. In addition, you can only jump from index `i` to index `j` if `arr[i] > arr[j]` and `arr[i] > arr[k]` for a...
null
📌📌 Well-Coded and Easy Explanation || Use of Memoization 🐍
jump-game-v
0
1
## IDEA:\nFor each index i, run search starting from i left and right up to d steps. This way, we can easily detect if arr[j] is blocking further jumps.\n\n**So, we stop the search when we encounter j where arr[i] <= arr[j].**\n\nTo prevent re-computations, we need to memoise max jumps for every index in dp.\n\n****\n*...
6
There are `n` cities numbered from `0` to `n - 1` and `n - 1` roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow. Roads are represented by `connections` wh...
Use dynamic programming. dp[i] is max jumps you can do starting from index i. Answer is max(dp[i]). dp[i] = 1 + max (dp[j]) where j is all indices you can reach from i.
Very Clear Python - DFS + Memoisation
jump-game-v
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe most nodes you can visit from a starting node is one more than the most you can reach from any other valid node. I.e., `f(x) = 1 + f(y)` for `y in [x-d, x+d]`.\n\nThe \'trick\' is to expand outwards from the starting node. As soon as ...
0
Given an array of integers `arr` and an integer `d`. In one step you can jump from index `i` to index: * `i + x` where: `i + x < arr.length` and `0 < x <= d`. * `i - x` where: `i - x >= 0` and `0 < x <= d`. In addition, you can only jump from index `i` to index `j` if `arr[i] > arr[j]` and `arr[i] > arr[k]` for a...
null
Very Clear Python - DFS + Memoisation
jump-game-v
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe most nodes you can visit from a starting node is one more than the most you can reach from any other valid node. I.e., `f(x) = 1 + f(y)` for `y in [x-d, x+d]`.\n\nThe \'trick\' is to expand outwards from the starting node. As soon as ...
0
There are `n` cities numbered from `0` to `n - 1` and `n - 1` roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow. Roads are represented by `connections` wh...
Use dynamic programming. dp[i] is max jumps you can do starting from index i. Answer is max(dp[i]). dp[i] = 1 + max (dp[j]) where j is all indices you can reach from i.
Very Simple Dp in py3
jump-game-v
0
1
\n\n# Code\n```\nclass Solution:\n def maxJumps(self, arr: List[int], d: int) -> int:\n n = len(arr)\n @cache\n def dp(i):\n res = 0\n for j in range(i-1,max(0,i-d)-1,-1):\n if arr[j]>=arr[i]:break\n res = max(dp(j),res)\n for j in r...
0
Given an array of integers `arr` and an integer `d`. In one step you can jump from index `i` to index: * `i + x` where: `i + x < arr.length` and `0 < x <= d`. * `i - x` where: `i - x >= 0` and `0 < x <= d`. In addition, you can only jump from index `i` to index `j` if `arr[i] > arr[j]` and `arr[i] > arr[k]` for a...
null
Very Simple Dp in py3
jump-game-v
0
1
\n\n# Code\n```\nclass Solution:\n def maxJumps(self, arr: List[int], d: int) -> int:\n n = len(arr)\n @cache\n def dp(i):\n res = 0\n for j in range(i-1,max(0,i-d)-1,-1):\n if arr[j]>=arr[i]:break\n res = max(dp(j),res)\n for j in r...
0
There are `n` cities numbered from `0` to `n - 1` and `n - 1` roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow. Roads are represented by `connections` wh...
Use dynamic programming. dp[i] is max jumps you can do starting from index i. Answer is max(dp[i]). dp[i] = 1 + max (dp[j]) where j is all indices you can reach from i.
✅ 🔥 Python3 || ⚡easy solution
jump-game-v
0
1
```\nclass Solution:\n def maxJumps(self, nums: List[int], d: int) -> int:\n N = len(nums)\n seen = set()\n dp = [1] * N\n\n def recursion(indx):\n if indx in seen:\n return dp[indx]\n if indx < 0 or indx >= N:\n return 0\n\n ...
0
Given an array of integers `arr` and an integer `d`. In one step you can jump from index `i` to index: * `i + x` where: `i + x < arr.length` and `0 < x <= d`. * `i - x` where: `i - x >= 0` and `0 < x <= d`. In addition, you can only jump from index `i` to index `j` if `arr[i] > arr[j]` and `arr[i] > arr[k]` for a...
null
✅ 🔥 Python3 || ⚡easy solution
jump-game-v
0
1
```\nclass Solution:\n def maxJumps(self, nums: List[int], d: int) -> int:\n N = len(nums)\n seen = set()\n dp = [1] * N\n\n def recursion(indx):\n if indx in seen:\n return dp[indx]\n if indx < 0 or indx >= N:\n return 0\n\n ...
0
There are `n` cities numbered from `0` to `n - 1` and `n - 1` roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow. Roads are represented by `connections` wh...
Use dynamic programming. dp[i] is max jumps you can do starting from index i. Answer is max(dp[i]). dp[i] = 1 + max (dp[j]) where j is all indices you can reach from i.
DP SIMPLE ALGORITHM
jump-game-v
0
1
\n# Code\n```\nclass Solution:\n global arr\n global d \n\n\n\n def maxJumps(self, arr: List[int], d: int) -> int:\n #attempt jumps from every index\n @cache\n def dfs(index):\n #left compute\n rv = 1\n for i in range(index-1, max(-1, index - d-1), -1):\n ...
0
Given an array of integers `arr` and an integer `d`. In one step you can jump from index `i` to index: * `i + x` where: `i + x < arr.length` and `0 < x <= d`. * `i - x` where: `i - x >= 0` and `0 < x <= d`. In addition, you can only jump from index `i` to index `j` if `arr[i] > arr[j]` and `arr[i] > arr[k]` for a...
null
DP SIMPLE ALGORITHM
jump-game-v
0
1
\n# Code\n```\nclass Solution:\n global arr\n global d \n\n\n\n def maxJumps(self, arr: List[int], d: int) -> int:\n #attempt jumps from every index\n @cache\n def dfs(index):\n #left compute\n rv = 1\n for i in range(index-1, max(-1, index - d-1), -1):\n ...
0
There are `n` cities numbered from `0` to `n - 1` and `n - 1` roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow. Roads are represented by `connections` wh...
Use dynamic programming. dp[i] is max jumps you can do starting from index i. Answer is max(dp[i]). dp[i] = 1 + max (dp[j]) where j is all indices you can reach from i.
Efficient Movie Analysis Approach
movie-rating
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to devide the problem into two separate tasks, each focusing on one aspect: finding the high-rated movie and finding the user who wrote the most reviews. \n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. ##...
0
Given an array `nums`. We define a running sum of an array as `runningSum[i] = sum(nums[0]...nums[i])`. Return the running sum of `nums`. **Example 1:** **Input:** nums = \[1,2,3,4\] **Output:** \[1,3,6,10\] **Explanation:** Running sum is obtained as follows: \[1, 1+2, 1+2+3, 1+2+3+4\]. **Example 2:** **Input:** ...
null
Python Elegant & Short | O(1) | Recursive / Iterative / Bit Manipulation
number-of-steps-to-reduce-a-number-to-zero
0
1
## Recursive solution\n\n```\nclass Solution:\n """\n Time: O(log(n))\n Memory: O(log(n))\n """\n\n def numberOfSteps(self, num: int) -> int:\n if num == 0:\n return 0\n return 1 + self.numberOfSteps(num - 1 if num & 1 else num >> 1)\n```\n\n## Iterative solution\n\n```\nclass ...
217
Given an integer `num`, return _the number of steps to reduce it to zero_. In one step, if the current number is even, you have to divide it by `2`, otherwise, you have to subtract `1` from it. **Example 1:** **Input:** num = 14 **Output:** 6 **Explanation:** Step 1) 14 is even; divide by 2 and obtain 7. Step 2) 7...
Check 8 directions around the King. Find the nearest queen in each direction.
Python Elegant & Short | O(1) | Recursive / Iterative / Bit Manipulation
number-of-steps-to-reduce-a-number-to-zero
0
1
## Recursive solution\n\n```\nclass Solution:\n """\n Time: O(log(n))\n Memory: O(log(n))\n """\n\n def numberOfSteps(self, num: int) -> int:\n if num == 0:\n return 0\n return 1 + self.numberOfSteps(num - 1 if num & 1 else num >> 1)\n```\n\n## Iterative solution\n\n```\nclass ...
217
Given a rectangular pizza represented as a `rows x cols` matrix containing the following characters: `'A'` (an apple) and `'.'` (empty cell) and given the integer `k`. You have to cut the pizza into `k` pieces using `k-1` cuts. For each cut you choose the direction: vertical or horizontal, then you choose a cut positi...
Simulate the process to get the final answer.
simple beginner friendly solutions
number-of-steps-to-reduce-a-number-to-zero
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\nusing while loop and recursion\n\n# Complexity\n- Time complexity: $$O(logN)$$\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 1\n```\nclass Solut...
1
Given an integer `num`, return _the number of steps to reduce it to zero_. In one step, if the current number is even, you have to divide it by `2`, otherwise, you have to subtract `1` from it. **Example 1:** **Input:** num = 14 **Output:** 6 **Explanation:** Step 1) 14 is even; divide by 2 and obtain 7. Step 2) 7...
Check 8 directions around the King. Find the nearest queen in each direction.
simple beginner friendly solutions
number-of-steps-to-reduce-a-number-to-zero
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\nusing while loop and recursion\n\n# Complexity\n- Time complexity: $$O(logN)$$\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 1\n```\nclass Solut...
1
Given a rectangular pizza represented as a `rows x cols` matrix containing the following characters: `'A'` (an apple) and `'.'` (empty cell) and given the integer `k`. You have to cut the pizza into `k` pieces using `k-1` cuts. For each cut you choose the direction: vertical or horizontal, then you choose a cut positi...
Simulate the process to get the final answer.
Beats 94.19% || Number of steps to reduce a number to zero
number-of-steps-to-reduce-a-number-to-zero
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
Given an integer `num`, return _the number of steps to reduce it to zero_. In one step, if the current number is even, you have to divide it by `2`, otherwise, you have to subtract `1` from it. **Example 1:** **Input:** num = 14 **Output:** 6 **Explanation:** Step 1) 14 is even; divide by 2 and obtain 7. Step 2) 7...
Check 8 directions around the King. Find the nearest queen in each direction.
Beats 94.19% || Number of steps to reduce a number to zero
number-of-steps-to-reduce-a-number-to-zero
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
Given a rectangular pizza represented as a `rows x cols` matrix containing the following characters: `'A'` (an apple) and `'.'` (empty cell) and given the integer `k`. You have to cut the pizza into `k` pieces using `k-1` cuts. For each cut you choose the direction: vertical or horizontal, then you choose a cut positi...
Simulate the process to get the final answer.
Python3 Easy Solution. Beats 97.53%. O(n)
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAt first, I used the sum() function for every subarray and divided it by k but then I realized, the time complexity would be better if we were just doing 2 computations per subarray so I created a running sum which can be easily calculate...
1
Given an array of integers `arr` and two integers `k` and `threshold`, return _the number of sub-arrays of size_ `k` _and average greater than or equal to_ `threshold`. **Example 1:** **Input:** arr = \[2,2,2,2,5,5,5,8\], k = 3, threshold = 4 **Output:** 3 **Explanation:** Sub-arrays \[2,5,5\],\[5,5,5\] and \[5,5,8\]...
Think on Dynamic Programming. DP(pos, last) which means we are at the position pos having as last the last character seen.
Easy | Python Solution | Prefix Sum
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
0
1
# Code\n```\nclass Solution:\n def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:\n\n currLen = 0\n currSum = 0\n ans = 0\n\n for i in range(0, len(arr)):\n\n if currLen == k:\n\n if currSum >= threshold:\n ans += 1\n\n ...
1
Given an array of integers `arr` and two integers `k` and `threshold`, return _the number of sub-arrays of size_ `k` _and average greater than or equal to_ `threshold`. **Example 1:** **Input:** arr = \[2,2,2,2,5,5,5,8\], k = 3, threshold = 4 **Output:** 3 **Explanation:** Sub-arrays \[2,5,5\],\[5,5,5\] and \[5,5,8\]...
Think on Dynamic Programming. DP(pos, last) which means we are at the position pos having as last the last character seen.
easy python 2 pointers solution. DO UPVOTE!!!
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
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 `arr` and two integers `k` and `threshold`, return _the number of sub-arrays of size_ `k` _and average greater than or equal to_ `threshold`. **Example 1:** **Input:** arr = \[2,2,2,2,5,5,5,8\], k = 3, threshold = 4 **Output:** 3 **Explanation:** Sub-arrays \[2,5,5\],\[5,5,5\] and \[5,5,8\]...
Think on Dynamic Programming. DP(pos, last) which means we are at the position pos having as last the last character seen.
Python Solution | Easy Approach ✅
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
0
1
```\ncount = 0\ncurrent_sum = sum(arr[:k]) # 1-st window\nif current_sum / k >= float(threshold): # check for 1-st window\n\tcount += 1\n\nfor i in range(len(arr) - k):\n\tcurrent_sum += (-1) * arr[i] + arr[i + k] # subtract 1-st element of current window & add the k-th\n\tif current_sum / k >= float(threshold):\n\t\...
2
Given an array of integers `arr` and two integers `k` and `threshold`, return _the number of sub-arrays of size_ `k` _and average greater than or equal to_ `threshold`. **Example 1:** **Input:** arr = \[2,2,2,2,5,5,5,8\], k = 3, threshold = 4 **Output:** 3 **Explanation:** Sub-arrays \[2,5,5\],\[5,5,5\] and \[5,5,8\]...
Think on Dynamic Programming. DP(pos, last) which means we are at the position pos having as last the last character seen.
🔥 [Python3] Easy, beats 99% 🔥
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
0
1
```\nclass Solution:\n def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:\n res, s = 0, 0\n threshold = threshold * k\n s = sum(arr[:k-1])\n\n for r in range(k-1, len(arr)):\n s += arr[r]\n if s >= threshold: res +=1\n s -= arr[r-k+1]...
5
Given an array of integers `arr` and two integers `k` and `threshold`, return _the number of sub-arrays of size_ `k` _and average greater than or equal to_ `threshold`. **Example 1:** **Input:** arr = \[2,2,2,2,5,5,5,8\], k = 3, threshold = 4 **Output:** 3 **Explanation:** Sub-arrays \[2,5,5\],\[5,5,5\] and \[5,5,8\]...
Think on Dynamic Programming. DP(pos, last) which means we are at the position pos having as last the last character seen.
[Python 3] Sliding window - Fixed size approach | O(N) Time | O(1) Space
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
0
1
# Intuition\nThe question asks us to do something in a subarray of size `k`. This is quite standard with **Sliding Window** - *fixed size* problems, where `k` is the fixed size of the window.\n\n### What is a Sliding Window of fixed size ?\n> The fixed-size sliding window technique is a computer science algorithm that ...
5
Given an array of integers `arr` and two integers `k` and `threshold`, return _the number of sub-arrays of size_ `k` _and average greater than or equal to_ `threshold`. **Example 1:** **Input:** arr = \[2,2,2,2,5,5,5,8\], k = 3, threshold = 4 **Output:** 3 **Explanation:** Sub-arrays \[2,5,5\],\[5,5,5\] and \[5,5,8\]...
Think on Dynamic Programming. DP(pos, last) which means we are at the position pos having as last the last character seen.
Python3 simple and easy solution || sliding window
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
0
1
# Code\n```\nclass Solution:\n def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:\n count = 0\n # avg >= threshold , avg = sum(subarray) / k >= threshold\n # thus, sum(subarray) >= k * threshold\n target = k * threshold \n\n curr = 0\n for i in range(k ...
2
Given an array of integers `arr` and two integers `k` and `threshold`, return _the number of sub-arrays of size_ `k` _and average greater than or equal to_ `threshold`. **Example 1:** **Input:** arr = \[2,2,2,2,5,5,5,8\], k = 3, threshold = 4 **Output:** 3 **Explanation:** Sub-arrays \[2,5,5\],\[5,5,5\] and \[5,5,8\]...
Think on Dynamic Programming. DP(pos, last) which means we are at the position pos having as last the last character seen.
Python || 91.78%Faster || Sliding Window || O(N) Solution
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
0
1
```\nclass Solution:\n def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:\n c,j,n=0,0,len(arr)\n s=sum(arr[:k])\n if s>=k*threshold:\n c=1\n for i in range(k,n):\n s+=arr[i]-arr[j]\n if s>=k*threshold:\n c+=1\n ...
2
Given an array of integers `arr` and two integers `k` and `threshold`, return _the number of sub-arrays of size_ `k` _and average greater than or equal to_ `threshold`. **Example 1:** **Input:** arr = \[2,2,2,2,5,5,5,8\], k = 3, threshold = 4 **Output:** 3 **Explanation:** Sub-arrays \[2,5,5\],\[5,5,5\] and \[5,5,8\]...
Think on Dynamic Programming. DP(pos, last) which means we are at the position pos having as last the last character seen.
Easiest Array Solution (Beats 90%)
angle-between-hands-of-a-clock
0
1
# Code\n```\nclass Solution:\n def angleClock(self, hour: int, minutes: int) -> float:\n hour = hour % 12\n poss = [abs((((hour * 5) + (5 * (minutes / 60))) * 6) - minutes * 6), abs((360 - (((hour * 5) + (5 * (minutes / 60))) * 6)) + (360 - minutes * 6)), abs(((((hour * 5) + (5 * (minutes / 60))) * 6))...
1
Given two numbers, `hour` and `minutes`, return _the smaller angle (in degrees) formed between the_ `hour` _and the_ `minute` _hand_. Answers within `10-5` of the actual value will be accepted as correct. **Example 1:** **Input:** hour = 12, minutes = 30 **Output:** 165 **Example 2:** **Input:** hour = 3, minutes ...
Keep track of the min and max frequencies. The number to be eliminated must have a frequency of 1, same as the others or the same +1.
Easiest Array Solution (Beats 90%)
angle-between-hands-of-a-clock
0
1
# Code\n```\nclass Solution:\n def angleClock(self, hour: int, minutes: int) -> float:\n hour = hour % 12\n poss = [abs((((hour * 5) + (5 * (minutes / 60))) * 6) - minutes * 6), abs((360 - (((hour * 5) + (5 * (minutes / 60))) * 6)) + (360 - minutes * 6)), abs(((((hour * 5) + (5 * (minutes / 60))) * 6))...
1
The **power** of the string is the maximum length of a non-empty substring that contains only one unique character. Given a string `s`, return _the **power** of_ `s`. **Example 1:** **Input:** s = "leetcode " **Output:** 2 **Explanation:** The substring "ee " is of length 2 with the character 'e' only. **Example ...
The tricky part is determining how the minute hand affects the position of the hour hand. Calculate the angles separately then find the difference.
[Java/Python/C++] Simple Math on Clock angles
angle-between-hands-of-a-clock
1
1
**Basic Unitary Method**\n(Credits - @rajcm)\n\n**Hour Hand**\nIn 12 hours Hour hand complete whole circle and cover 360\xB0\nSo, 1 hour = 360\xB0 / 12 = 30\xB0\n\nSince 1 hours = 30\xB0\nIn 1 minute, hours hand rotate -> 30\xB0 / 60 = 0.5\xB0\nSo total angle because of minutes by hour hand is `minutes/60 * 30` or `min...
170
Given two numbers, `hour` and `minutes`, return _the smaller angle (in degrees) formed between the_ `hour` _and the_ `minute` _hand_. Answers within `10-5` of the actual value will be accepted as correct. **Example 1:** **Input:** hour = 12, minutes = 30 **Output:** 165 **Example 2:** **Input:** hour = 3, minutes ...
Keep track of the min and max frequencies. The number to be eliminated must have a frequency of 1, same as the others or the same +1.
[Java/Python/C++] Simple Math on Clock angles
angle-between-hands-of-a-clock
1
1
**Basic Unitary Method**\n(Credits - @rajcm)\n\n**Hour Hand**\nIn 12 hours Hour hand complete whole circle and cover 360\xB0\nSo, 1 hour = 360\xB0 / 12 = 30\xB0\n\nSince 1 hours = 30\xB0\nIn 1 minute, hours hand rotate -> 30\xB0 / 60 = 0.5\xB0\nSo total angle because of minutes by hour hand is `minutes/60 * 30` or `min...
170
The **power** of the string is the maximum length of a non-empty substring that contains only one unique character. Given a string `s`, return _the **power** of_ `s`. **Example 1:** **Input:** s = "leetcode " **Output:** 2 **Explanation:** The substring "ee " is of length 2 with the character 'e' only. **Example ...
The tricky part is determining how the minute hand affects the position of the hour hand. Calculate the angles separately then find the difference.
Python - Easiest to understand! Comments - Clear and Concise
angle-between-hands-of-a-clock
0
1
**Solution**:\n```\nclass Solution:\n def angleClock(self, h, m):\n # Convert the hour hand to another minute hand\n m2 = (h%12 + m/60)*5\n \n # Calculate the difference between the two minute hands\n diff = abs(m-m2)\n \n # Convert the difference to an angle\n ...
2
Given two numbers, `hour` and `minutes`, return _the smaller angle (in degrees) formed between the_ `hour` _and the_ `minute` _hand_. Answers within `10-5` of the actual value will be accepted as correct. **Example 1:** **Input:** hour = 12, minutes = 30 **Output:** 165 **Example 2:** **Input:** hour = 3, minutes ...
Keep track of the min and max frequencies. The number to be eliminated must have a frequency of 1, same as the others or the same +1.
Python - Easiest to understand! Comments - Clear and Concise
angle-between-hands-of-a-clock
0
1
**Solution**:\n```\nclass Solution:\n def angleClock(self, h, m):\n # Convert the hour hand to another minute hand\n m2 = (h%12 + m/60)*5\n \n # Calculate the difference between the two minute hands\n diff = abs(m-m2)\n \n # Convert the difference to an angle\n ...
2
The **power** of the string is the maximum length of a non-empty substring that contains only one unique character. Given a string `s`, return _the **power** of_ `s`. **Example 1:** **Input:** s = "leetcode " **Output:** 2 **Explanation:** The substring "ee " is of length 2 with the character 'e' only. **Example ...
The tricky part is determining how the minute hand affects the position of the hour hand. Calculate the angles separately then find the difference.
Python one line solution based on aptitude formula
angle-between-hands-of-a-clock
0
1
```\nclass Solution:\n def angleClock(self, hour: int, minutes: int) -> float:\n return min(abs(30*hour-5.5*minutes),360-abs(30*hour-5.5*minutes))\n \n```
2
Given two numbers, `hour` and `minutes`, return _the smaller angle (in degrees) formed between the_ `hour` _and the_ `minute` _hand_. Answers within `10-5` of the actual value will be accepted as correct. **Example 1:** **Input:** hour = 12, minutes = 30 **Output:** 165 **Example 2:** **Input:** hour = 3, minutes ...
Keep track of the min and max frequencies. The number to be eliminated must have a frequency of 1, same as the others or the same +1.
Python one line solution based on aptitude formula
angle-between-hands-of-a-clock
0
1
```\nclass Solution:\n def angleClock(self, hour: int, minutes: int) -> float:\n return min(abs(30*hour-5.5*minutes),360-abs(30*hour-5.5*minutes))\n \n```
2
The **power** of the string is the maximum length of a non-empty substring that contains only one unique character. Given a string `s`, return _the **power** of_ `s`. **Example 1:** **Input:** s = "leetcode " **Output:** 2 **Explanation:** The substring "ee " is of length 2 with the character 'e' only. **Example ...
The tricky part is determining how the minute hand affects the position of the hour hand. Calculate the angles separately then find the difference.
Angle Between Hands of a Clock python solution
angle-between-hands-of-a-clock
0
1
def angleClock(self, hour: int, minutes: int) -> float:\n \n """\n As hour clock move by (360/12) is per hour + ((360/12)/60) per minutes\n """\n hours_degree = (360/12)*hour+(360/720)*minutes\n minute_degree = (360/60)*minutes\n res = abs(hours_degree-minute_degree)\n ...
1
Given two numbers, `hour` and `minutes`, return _the smaller angle (in degrees) formed between the_ `hour` _and the_ `minute` _hand_. Answers within `10-5` of the actual value will be accepted as correct. **Example 1:** **Input:** hour = 12, minutes = 30 **Output:** 165 **Example 2:** **Input:** hour = 3, minutes ...
Keep track of the min and max frequencies. The number to be eliminated must have a frequency of 1, same as the others or the same +1.
Angle Between Hands of a Clock python solution
angle-between-hands-of-a-clock
0
1
def angleClock(self, hour: int, minutes: int) -> float:\n \n """\n As hour clock move by (360/12) is per hour + ((360/12)/60) per minutes\n """\n hours_degree = (360/12)*hour+(360/720)*minutes\n minute_degree = (360/60)*minutes\n res = abs(hours_degree-minute_degree)\n ...
1
The **power** of the string is the maximum length of a non-empty substring that contains only one unique character. Given a string `s`, return _the **power** of_ `s`. **Example 1:** **Input:** s = "leetcode " **Output:** 2 **Explanation:** The substring "ee " is of length 2 with the character 'e' only. **Example ...
The tricky part is determining how the minute hand affects the position of the hour hand. Calculate the angles separately then find the difference.
Solution | Python
angle-between-hands-of-a-clock
0
1
# Intuition\n- This is a tricky problem, here any `division` of whole `whole` numbers will be evealuated to `floor` division. \n*In Python 2.x, the division of two integers returns an integer result (floor division) instead of a float.*\nMaybe the author wanted to pay respect or whatever.\nSo:\n - You can try `retur...
1
Given two numbers, `hour` and `minutes`, return _the smaller angle (in degrees) formed between the_ `hour` _and the_ `minute` _hand_. Answers within `10-5` of the actual value will be accepted as correct. **Example 1:** **Input:** hour = 12, minutes = 30 **Output:** 165 **Example 2:** **Input:** hour = 3, minutes ...
Keep track of the min and max frequencies. The number to be eliminated must have a frequency of 1, same as the others or the same +1.
Solution | Python
angle-between-hands-of-a-clock
0
1
# Intuition\n- This is a tricky problem, here any `division` of whole `whole` numbers will be evealuated to `floor` division. \n*In Python 2.x, the division of two integers returns an integer result (floor division) instead of a float.*\nMaybe the author wanted to pay respect or whatever.\nSo:\n - You can try `retur...
1
The **power** of the string is the maximum length of a non-empty substring that contains only one unique character. Given a string `s`, return _the **power** of_ `s`. **Example 1:** **Input:** s = "leetcode " **Output:** 2 **Explanation:** The substring "ee " is of length 2 with the character 'e' only. **Example ...
The tricky part is determining how the minute hand affects the position of the hour hand. Calculate the angles separately then find the difference.
📌📌Python3 || ⚡623 ms, faster than 98.86% of Python3
jump-game-iv
0
1
![image](https://assets.leetcode.com/users/images/d5e657e7-ac49-41ee-912e-91677f662469_1678002372.3302972.png)\n```\ndef minJumps(self, arr: List[int]) -> int:\n if len(arr) == 1: return 0\n dict = collections.defaultdict(list)\n for i , n in enumerate(arr): dict[n].append(i)\n\n N = len(arr...
2
Given an array of integers `arr`, you are initially positioned at the first index of the array. In one step you can jump from index `i` to index: * `i + 1` where: `i + 1 < arr.length`. * `i - 1` where: `i - 1 >= 0`. * `j` where: `arr[i] == arr[j]` and `i != j`. Return _the minimum number of steps_ to reach the...
Intuitively performing all shift operations is acceptable due to the constraints. You may notice that left shift cancels the right shift, so count the total left shift times (may be negative if the final result is right shift), and perform it once.
📌📌Python3 || ⚡623 ms, faster than 98.86% of Python3
jump-game-iv
0
1
![image](https://assets.leetcode.com/users/images/d5e657e7-ac49-41ee-912e-91677f662469_1678002372.3302972.png)\n```\ndef minJumps(self, arr: List[int]) -> int:\n if len(arr) == 1: return 0\n dict = collections.defaultdict(list)\n for i , n in enumerate(arr): dict[n].append(i)\n\n N = len(arr...
2
Given an integer `n`, return _a list of all **simplified** fractions between_ `0` _and_ `1` _(exclusive) such that the denominator is less-than-or-equal-to_ `n`. You can return the answer in **any order**. **Example 1:** **Input:** n = 2 **Output:** \[ "1/2 "\] **Explanation:** "1/2 " is the only unique fraction wit...
Build a graph of n nodes where nodes are the indices of the array and edges for node i are nodes i+1, i-1, j where arr[i] == arr[j]. Start bfs from node 0 and keep distance. The answer is the distance when you reach node n-1.