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
Python Hard
count-number-of-possible-root-nodes
0
1
```\nclass Solution:\n def rootCount(self, edges: List[List[int]], guesses: List[List[int]], k: int) -> int:\n lookup = defaultdict(list)\n\n N = len(edges) + 1\n\n for start, end in edges:\n lookup[start] += [end]\n lookup[end] += [start]\n\n\n guess = set()\n\n ...
0
Alice has an undirected tree with `n` nodes labeled from `0` to `n - 1`. The tree is represented as a 2D integer array `edges` of length `n - 1` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree. Alice wants Bob to find the root of the tree. She allows Bob to make seve...
null
Python Easy Solution!! Easy to understand.. Successfully Passed
pass-the-pillow
0
1
# Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\n3 conditons ==> \n1) when time is greater than n\n2) when time is lesser than n\n3) when time is equal to n\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here,...
1
There are `n` people standing in a line labeled from `1` to `n`. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the ...
null
Simple Solution || Python
pass-the-pillow
0
1
\n\n# Code\n```\nclass Solution:\n def passThePillow(self, n: int, time: int) -> int:\n position = 0\n move = -1\n while time > 0:\n if position == n - 1 or position == 0:\n move *= -1\n position += move\n time -=1 \n \n return po...
1
There are `n` people standing in a line labeled from `1` to `n`. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the ...
null
Easy Python Solution
pass-the-pillow
0
1
\n# Code\n```\nclass Solution:\n def passThePillow(self, n: int, time: int) -> int:\n person=1\n c=0\n while time>0:\n if c==0:\n person+=1\n if person==n:\n c=-1\n elif c==-1:\n person-=1\n if p...
5
There are `n` people standing in a line labeled from `1` to `n`. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the ...
null
Python3 33ms faster than 100%
pass-the-pillow
0
1
![image](https://assets.leetcode.com/users/images/b58c81f8-affa-4e7b-aabd-6c7bb2828d01_1678056634.9569702.png)\n\n\nAlongside the timer ,using a pointer to iterate from 1 to the n and if the pointer = n; n becomes negative and we iterate on the way back, and if time = 0 and the pointer is negative we multiply it by -1 ...
1
There are `n` people standing in a line labeled from `1` to `n`. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the ...
null
Beats 99,70% speed Easy Python solution
pass-the-pillow
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` people standing in a line labeled from `1` to `n`. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the ...
null
simple python solution
pass-the-pillow
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` people standing in a line labeled from `1` to `n`. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the ...
null
[Python3] math
pass-the-pillow
0
1
Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/e3093716659ec141e47ca013abcf405967592686) for solutions of weekly 335. \n\n**Intuition**\nIf the pillow is passed n-1 times, it will reached the end. After another n-1 times, it will reach the beginnig again. \nAs a result, there is a periodicity o...
2
There are `n` people standing in a line labeled from `1` to `n`. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the ...
null
Python 3 || BFS, queue || T/M: 94% / 86%
kth-largest-sum-in-a-binary-tree
0
1
```\nclass Solution:\n def kthLargestLevelSum(self, root: TreeNode, k: int) -> int:\n\n queue, ans = deque([root]), []\n\n while len(queue) > 0:\n count = 0\n\n for _ in range(len(queue)):\n node = queue.popleft()\n\n if node.left : queue.append(node....
3
You are given the `root` of a binary tree and a positive integer `k`. The **level sum** in the tree is the sum of the values of the nodes that are on the **same** level. Return _the_ `kth` _**largest** level sum in the tree (not necessarily distinct)_. If there are fewer than `k` levels in the tree, return `-1`. **N...
null
Kth Largest sum in a binary tree
kth-largest-sum-in-a-binary-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
You are given the `root` of a binary tree and a positive integer `k`. The **level sum** in the tree is the sum of the values of the nodes that are on the **same** level. Return _the_ `kth` _**largest** level sum in the tree (not necessarily distinct)_. If there are fewer than `k` levels in the tree, return `-1`. **N...
null
Python3 BFS Easy Understanding
kth-largest-sum-in-a-binary-tree
0
1
\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# ...
1
You are given the `root` of a binary tree and a positive integer `k`. The **level sum** in the tree is the sum of the values of the nodes that are on the **same** level. Return _the_ `kth` _**largest** level sum in the tree (not necessarily distinct)_. If there are fewer than `k` levels in the tree, return `-1`. **N...
null
python/java bfs + heap
kth-largest-sum-in-a-binary-tree
1
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 the `root` of a binary tree and a positive integer `k`. The **level sum** in the tree is the sum of the values of the nodes that are on the **same** level. Return _the_ `kth` _**largest** level sum in the tree (not necessarily distinct)_. If there are fewer than `k` levels in the tree, return `-1`. **N...
null
Simplest Python level-order
kth-largest-sum-in-a-binary-tree
0
1
\n# Code\n```\nclass Solution:\n def kthLargestLevelSum(self, root: Optional[TreeNode], k: int) -> int:\n a = []\n \n def dfs(node, h):\n if not node: return\n if h == len(a):\n a.append([])\n a[h].append(node.val)\n dfs(node.left, h+1)\...
3
You are given the `root` of a binary tree and a positive integer `k`. The **level sum** in the tree is the sum of the values of the nodes that are on the **same** level. Return _the_ `kth` _**largest** level sum in the tree (not necessarily distinct)_. If there are fewer than `k` levels in the tree, return `-1`. **N...
null
Level Traversal || Python || Explained
kth-largest-sum-in-a-binary-tree
0
1
# Intuition\nPerform Level Traversal and find the value of all nodes values in current level and store in array\nSort the array in reverse order. why?\n`We are asked to return kth largest`\nReturn the val present at k - 1 in array (as array is 0-indexed based)\n\n# Code\n```\n# Definition for a binary tree node.\n# cla...
1
You are given the `root` of a binary tree and a positive integer `k`. The **level sum** in the tree is the sum of the values of the nodes that are on the **same** level. Return _the_ `kth` _**largest** level sum in the tree (not necessarily distinct)_. If there are fewer than `k` levels in the tree, return `-1`. **N...
null
[Python3] dfs
kth-largest-sum-in-a-binary-tree
0
1
Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/e3093716659ec141e47ca013abcf405967592686) for solutions of weekly 335. \n\n**Intuition**\nWe can run either a BFS or DFS to collect sum of node values on each level. Here, I choose to do DFS. \nUpon collecting all such values, I sort the array to r...
7
You are given the `root` of a binary tree and a positive integer `k`. The **level sum** in the tree is the sum of the values of the nodes that are on the **same** level. Return _the_ `kth` _**largest** level sum in the tree (not necessarily distinct)_. If there are fewer than `k` levels in the tree, return `-1`. **N...
null
[Python] A Short Solution that can pass
split-the-array-to-make-coprime-products
0
1
> This approach is just for fun, don\'t take it too serious\n> <s>It passes with a time about `9000ms`</s>\n> Now it is strictly TLE\n> View other post for optimal solution\n\n\n# Intuition\n\n1. We want to check the product of left array is coprime with the product of right array.\n2. This requirement is equivalent to...
0
You are given a **0-indexed** integer array `nums` of length `n`. A **split** at an index `i` where `0 <= i <= n - 2` is called **valid** if the product of the first `i + 1` elements and the product of the remaining elements are coprime. * For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is val...
null
Prime factorization + merge intervals in Python with explanation
split-the-array-to-make-coprime-products
0
1
# Intuition\nFor each primes, if we know the first index (left) and last index (right) it can divide nums[index], then we know we cannot cut anywhere within [left, right) (can cut at index right, but cannot cut at left). \n\nThe problem then reduces to, we have a bunch of intervals, we cannot cut anywhere within the in...
1
You are given a **0-indexed** integer array `nums` of length `n`. A **split** at an index `i` where `0 <= i <= n - 2` is called **valid** if the product of the first `i + 1` elements and the product of the remaining elements are coprime. * For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is val...
null
It is a meduim question. Easy and Straightforward!
split-the-array-to-make-coprime-products
0
1
This solution is quite straightforward!\n\n1. Loop `i` from `0` to `n - 2`, `left = nums[i]`\n2. Loop `j` from` i + 1` to `n - 1` and check `gcd(left, nums[j]`. If `i` is the answer, return `i`\n3. Otherwise, If we find `gcd(num[i], nums[j]) > 1`, `i` and `j` should be put on one side for the splitting, then `i` should...
1
You are given a **0-indexed** integer array `nums` of length `n`. A **split** at an index `i` where `0 <= i <= n - 2` is called **valid** if the product of the first `i + 1` elements and the product of the remaining elements are coprime. * For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is val...
null
Python | Count Prime Factor
split-the-array-to-make-coprime-products
0
1
## For example [4,7,8,15,3,5]\nTotal Prime Count:\n2: 5\n3: 2\n5: 2\n7: 1\n\n### Spilt at index 1\n[4, 7] [8, 15, 3, 5]\nPrefix Prime Count:\n2: 2\n7: 1 (prepare to delet)\n\nSuffix (Total - Prefix) Prime Count:\n2: 3\n3: 2\n5: 2\n\nthen we delet prefix[7] because suffix don\'t any Prime 7 and perfix won\'t add any Pri...
1
You are given a **0-indexed** integer array `nums` of length `n`. A **split** at an index `i` where `0 <= i <= n - 2` is called **valid** if the product of the first `i + 1` elements and the product of the remaining elements are coprime. * For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is val...
null
[Python 3] Interval Merge
split-the-array-to-make-coprime-products
0
1
# Intuition\nTrack [leftmost occurence index, rightmost occurence index] of all prime factors for values in nums to obtain an array of intervals. No need to sort, the array should be sorted by left indeices naturally.\n\nBegin merging them. If we found at a certain position merged end < next interval start, we can spli...
1
You are given a **0-indexed** integer array `nums` of length `n`. A **split** at an index `i` where `0 <= i <= n - 2` is called **valid** if the product of the first `i + 1` elements and the product of the remaining elements are coprime. * For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is val...
null
Python || Beats 100 % || Do Dry Run With Pen Paper To Understand 🔥
split-the-array-to-make-coprime-products
0
1
# Code\n```\nclass Solution:\n def findValidSplit(self, nums: List[int]) -> int:\n n = len(nums)\n prod = nums[0]\n streak = 0\n prod2 = 1\n for i in range(1, n):\n num = nums[i]\n if gcd(prod, num) == 1:\n streak += 1\n prod2 *= ...
2
You are given a **0-indexed** integer array `nums` of length `n`. A **split** at an index `i` where `0 <= i <= n - 2` is called **valid** if the product of the first `i + 1` elements and the product of the remaining elements are coprime. * For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is val...
null
Simple and Unique approach in Python!!
split-the-array-to-make-coprime-products
0
1
# Intuition\n- Let us consider a factor ***P*** to divide both prefix and suffix product.\n- Since P divides prefix product at the present index, **p** will divide the prefix product at all **j > i**.\n- And for all **j > i** that **P** divides the suffix product, the gcd will be greater than or equal to **P**.\n- So j...
0
You are given a **0-indexed** integer array `nums` of length `n`. A **split** at an index `i` where `0 <= i <= n - 2` is called **valid** if the product of the first `i + 1` elements and the product of the remaining elements are coprime. * For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is val...
null
Record right-most index of every prime factor into dictionary
split-the-array-to-make-coprime-products
0
1
# Intuition\nIf arr[0]==2, and if no factor "2" in the rest of the array, then arr[0] is the split point. If arr[5] also has prime factor "2", then it is not possible to split at arr[0\\~4]. The split index must be >=5. Also if all prime factors in arr[0~5] doesn\'t exist after arr[5], then the split point is arr[5], o...
31
You are given a **0-indexed** integer array `nums` of length `n`. A **split** at an index `i` where `0 <= i <= n - 2` is called **valid** if the product of the first `i + 1` elements and the product of the remaining elements are coprime. * For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is val...
null
[Python3] check common factors
split-the-array-to-make-coprime-products
0
1
Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/e3093716659ec141e47ca013abcf405967592686) for solutions of weekly 335. \n\n**Intuition**\nHere, we cannot actually compute the products as they can become astronomically large. Rather, we can collect prime factors and check if at any index no commo...
11
You are given a **0-indexed** integer array `nums` of length `n`. A **split** at an index `i` where `0 <= i <= n - 2` is called **valid** if the product of the first `i + 1` elements and the product of the remaining elements are coprime. * For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is val...
null
Oct., Day 6 - factors of prefix sum and suffix sum
split-the-array-to-make-coprime-products
0
1
# Code\n```\nclass Solution:\n def findValidSplit(self, nums: List[int]) -> int:\n n = len(nums)\n def getFactors(num):\n res = []\n for f in range(2, int(sqrt(num))+1):\n if num%f == 0:\n res.append(f)\n while num%f == 0:\n ...
0
You are given a **0-indexed** integer array `nums` of length `n`. A **split** at an index `i` where `0 <= i <= n - 2` is called **valid** if the product of the first `i + 1` elements and the product of the remaining elements are coprime. * For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is val...
null
Python3 workable greedy approach
split-the-array-to-make-coprime-products
0
1
*Disclaimer: For what follows, `n = len(nums)`*\n\n# Complexity\n\n- **Time complexity**: Worst case `O(n ** 2)` (but somehow still passed).\n\n- **Space complexity**: If we regard the number of prime factors a number has is `O(1)` (as each number is bounded above by `10 ** 6`), then SC is `O(n)`.\n\n# Code\n```\nclass...
0
You are given a **0-indexed** integer array `nums` of length `n`. A **split** at an index `i` where `0 <= i <= n - 2` is called **valid** if the product of the first `i + 1` elements and the product of the remaining elements are coprime. * For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is val...
null
✔️ Python3 Solution | O(n * target) | 100% faster | DP
number-of-ways-to-earn-points
0
1
# Complexity\n- Time complexity: $$O(n * target)$$\n- Space complexity: $$O(target)$$\n\n# Code\n```\nclass Solution:\n def waysToReachTarget(self, T, A):\n dp = [1] + [0] * T\n mod = 10 ** 9 + 7\n for c, m in A:\n for i in range(m, T + 1):\n dp[i] += dp[i - m]\n ...
1
There is a test that has `n` types of questions. You are given an integer `target` and a **0-indexed** 2D integer array `types` where `types[i] = [counti, marksi]` indicates that there are `counti` questions of the `ith` type, and each one of them is worth `marksi` points. Return _the number of ways you can earn **exa...
null
Python | Simple Top Down DP
number-of-ways-to-earn-points
0
1
# Code\n```\nfrom functools import cache\nclass Solution:\n def waysToReachTarget(self, target: int, types: List[List[int]]) -> int:\n MOD = 10**9 + 7\n n = len(types)\n @cache\n def dfs(i, prev):\n if prev > target: return 0\n if prev == target: return 1\n ...
1
There is a test that has `n` types of questions. You are given an integer `target` and a **0-indexed** 2D integer array `types` where `types[i] = [counti, marksi]` indicates that there are `counti` questions of the `ith` type, and each one of them is worth `marksi` points. Return _the number of ways you can earn **exa...
null
[C++, Java, Python3] Short Top Down DP explained
number-of-ways-to-earn-points
1
1
# Intuition\nSince the constraints are small we can explore a DP approach. Start with the `target` and then subtract combinations of question `types` from it. Our function definiton of the recursive function will have 2 variables, (target, i). `target` signifies the remaining `target` and `i` siginifies the ith questio...
31
There is a test that has `n` types of questions. You are given an integer `target` and a **0-indexed** 2D integer array `types` where `types[i] = [counti, marksi]` indicates that there are `counti` questions of the `ith` type, and each one of them is worth `marksi` points. Return _the number of ways you can earn **exa...
null
python + dp explained
number-of-ways-to-earn-points
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI read some posts and had a hard time understanding some of the posted solutions. Then I figured out they are space optimized (if I am not understanding them incorrectly). I used a 2d dp here to kinda unfolded the things and to make it mo...
1
There is a test that has `n` types of questions. You are given an integer `target` and a **0-indexed** 2D integer array `types` where `types[i] = [counti, marksi]` indicates that there are `counti` questions of the `ith` type, and each one of them is worth `marksi` points. Return _the number of ways you can earn **exa...
null
Python memoization
number-of-ways-to-earn-points
0
1
```\nclass Solution:\n def waysToReachTarget(self, target: int, types: List[List[int]]) -> int:\n n = len(types)\n \n @lru_cache(None)\n def dfs(i, total):\n if total == target:\n return 1\n if i == n or total > target:\n return 0\n ...
2
There is a test that has `n` types of questions. You are given an integer `target` and a **0-indexed** 2D integer array `types` where `types[i] = [counti, marksi]` indicates that there are `counti` questions of the `ith` type, and each one of them is worth `marksi` points. Return _the number of ways you can earn **exa...
null
Simple Solution || Python
number-of-ways-to-earn-points
0
1
# Complexity\n- Time complexity:\n$$O(n * target * count)$$ \n\n\n\n- Space complexity:\n$$O(target)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def waysToReachTarget(self, target: int, types: List[List[int]]) -> int:\n mod = 10**9 + 7\n dp = [0]*(target ...
3
There is a test that has `n` types of questions. You are given an integer `target` and a **0-indexed** 2D integer array `types` where `types[i] = [counti, marksi]` indicates that there are `counti` questions of the `ith` type, and each one of them is worth `marksi` points. Return _the number of ways you can earn **exa...
null
[Python3] bottom-up dp
number-of-ways-to-earn-points
0
1
Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/e3093716659ec141e47ca013abcf405967592686) for solutions of weekly 335. \n\n**Intuition**\nDefine `dp[i][j]` as the number of ways to earn `i` points using questions starting from `j` forward. \n* The recurrence relationship is `dp[i][j] = dp[i-x*m]...
3
There is a test that has `n` types of questions. You are given an integer `target` and a **0-indexed** 2D integer array `types` where `types[i] = [counti, marksi]` indicates that there are `counti` questions of the `ith` type, and each one of them is worth `marksi` points. Return _the number of ways you can earn **exa...
null
Backtracking + Memoization = DP (😃)
number-of-ways-to-earn-points
0
1
\n\n# Code\n```\nclass Solution:\n def waysToReachTarget(self, target: int, arr: List[List[int]]) -> int:\n n = len(arr)\n mod = 10**9+7\n dp = {}\n def fun(ind,curr):\n if curr == target:\n return 1\n ans = 0\n if ind == n or curr>target:\n...
1
There is a test that has `n` types of questions. You are given an integer `target` and a **0-indexed** 2D integer array `types` where `types[i] = [counti, marksi]` indicates that there are `counti` questions of the `ith` type, and each one of them is worth `marksi` points. Return _the number of ways you can earn **exa...
null
Python || Recursion || Memoization
number-of-ways-to-earn-points
0
1
# Intuition\nThe idea is to consider 2 cases - one in which we include the current question and second if we don\'t include the current solution. \n\nIf we include the current solution, we can do it count times, and for each of these times we have to check for other ways in which we can reach the target. \n\nFor exampl...
0
There is a test that has `n` types of questions. You are given an integer `target` and a **0-indexed** 2D integer array `types` where `types[i] = [counti, marksi]` indicates that there are `counti` questions of the `ith` type, and each one of them is worth `marksi` points. Return _the number of ways you can earn **exa...
null
python dp top down
number-of-ways-to-earn-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)$$ --...
0
There is a test that has `n` types of questions. You are given an integer `target` and a **0-indexed** 2D integer array `types` where `types[i] = [counti, marksi]` indicates that there are `counti` questions of the `ith` type, and each one of them is worth `marksi` points. Return _the number of ways you can earn **exa...
null
python3 solution
number-of-ways-to-earn-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)$$ --...
0
There is a test that has `n` types of questions. You are given an integer `target` and a **0-indexed** 2D integer array `types` where `types[i] = [counti, marksi]` indicates that there are `counti` questions of the `ith` type, and each one of them is worth `marksi` points. Return _the number of ways you can earn **exa...
null
Easiest Python Solution
count-the-number-of-vowel-strings-in-range
0
1
# Code\n```\nclass Solution:\n def vowelStrings(self, words: List[str], left: int, right: int) -> int:\n v = [\'a\', \'e\', \'i\', \'o\', \'u\']\n ans = 0\n for i in words[left : right + 1]:\n if i[0] in v and i[-1] in v:\n ans += 1\n return ans\n```
1
You are given a **0-indexed** array of string `words` and two integers `left` and `right`. A string is called a **vowel string** if it starts with a vowel character and ends with a vowel character where vowel characters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. Return _the number of vowel strings_ `words[i]` _where_...
null
Simple python iterative approach
count-the-number-of-vowel-strings-in-range
0
1
# Intuition\nThis a simple string problem. We can solve it simple iterative approach.\n\n# Approach\nIterative approach.\n\n# Complexity\n- Time complexity: $$O(n)$$ - Linear Time\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$ - Constant Time\n<!-- Add your space complexity here,...
1
You are given a **0-indexed** array of string `words` and two integers `left` and `right`. A string is called a **vowel string** if it starts with a vowel character and ends with a vowel character where vowel characters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. Return _the number of vowel strings_ `words[i]` _where_...
null
Easy Solution Using For Loop in Python
count-the-number-of-vowel-strings-in-range
0
1
\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n# Code\n```\nclass Solution:\n def vowelStrings(self, words: List[str], left: int, right: int) -> int:\n d=["a","e","i","o","u"]\n c=0\n for i in range(left,right+1):\n if(words[i][0] i...
1
You are given a **0-indexed** array of string `words` and two integers `left` and `right`. A string is called a **vowel string** if it starts with a vowel character and ends with a vowel character where vowel characters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. Return _the number of vowel strings_ `words[i]` _where_...
null
Easy solution using python3
count-the-number-of-vowel-strings-in-range
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 **0-indexed** array of string `words` and two integers `left` and `right`. A string is called a **vowel string** if it starts with a vowel character and ends with a vowel character where vowel characters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. Return _the number of vowel strings_ `words[i]` _where_...
null
Python3 Very Simple Solution
count-the-number-of-vowel-strings-in-range
0
1
\n\n# Code\n```\nclass Solution:\n def vowelStrings(self, words: List[str], left: int, right: int) -> int:\n ans=0\n vol="aeiou"\n\n for i in range(left,right+1):\n if words[i][0] in vol and words[i][-1] in vol:\n ans+=1\n return ans \n \n```
1
You are given a **0-indexed** array of string `words` and two integers `left` and `right`. A string is called a **vowel string** if it starts with a vowel character and ends with a vowel character where vowel characters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. Return _the number of vowel strings_ `words[i]` _where_...
null
Simple python3 O(n) solution
count-the-number-of-vowel-strings-in-range
0
1
\n# Code\n```\nclass Solution:\n def vowelStrings(self, words: List[str], left: int, right: int) -> int:\n res = 0\n for i in range(left, right + 1):\n if words[i][0] in \'aeiou\' and words[i][-1] in \'aeiou\':\n res += 1\n return res\n```\n\n# Complexity\n- Time comple...
1
You are given a **0-indexed** array of string `words` and two integers `left` and `right`. A string is called a **vowel string** if it starts with a vowel character and ends with a vowel character where vowel characters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. Return _the number of vowel strings_ `words[i]` _where_...
null
Python3 Solution
count-the-number-of-vowel-strings-in-range
0
1
\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def vowelStrings(self, words: List[str], left: int, right: int) -> int:\n vowels=\'aeiouAEIOU\'\n...
2
You are given a **0-indexed** array of string `words` and two integers `left` and `right`. A string is called a **vowel string** if it starts with a vowel character and ends with a vowel character where vowel characters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. Return _the number of vowel strings_ `words[i]` _where_...
null
2586: Solution with step by step explanation
count-the-number-of-vowel-strings-in-range
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define a function vowelStrings that takes a list of strings words and two integers left and right as input, and returns an integer.\n2. Initialize a variable vowels to the string \'aeiouAEIOU\', which contains all the vow...
12
You are given a **0-indexed** array of string `words` and two integers `left` and `right`. A string is called a **vowel string** if it starts with a vowel character and ends with a vowel character where vowel characters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. Return _the number of vowel strings_ `words[i]` _where_...
null
Simple solution in Python3
count-the-number-of-vowel-strings-in-range
0
1
# Intuition\nHere we have:\n- `words` list, `left` and `right` integers\n- we should find all of the **vowel** strings for `[left, right]` interval from `words`\n\n**Vowel strings** have the starting and the ending character as **vowel**. \n\n# Approach\n1. define `check` function to check if a character is a **vowel**...
1
You are given a **0-indexed** array of string `words` and two integers `left` and `right`. A string is called a **vowel string** if it starts with a vowel character and ends with a vowel character where vowel characters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. Return _the number of vowel strings_ `words[i]` _where_...
null
Easy to Understand Python Solution || Beats 90% of other solutions
count-the-number-of-vowel-strings-in-range
0
1
Please Upvote if you like it.\n```\nclass Solution:\n def vowelStrings(self, words: List[str], left: int, right: int) -> int:\n a = ["a","e","i","o","u"]\n count = 0\n for i in range(left,right+1):\n temp= words[i]\n if temp[0] in a and temp[-1] in a:\n count...
1
You are given a **0-indexed** array of string `words` and two integers `left` and `right`. A string is called a **vowel string** if it starts with a vowel character and ends with a vowel character where vowel characters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. Return _the number of vowel strings_ `words[i]` _where_...
null
Python 1 line - Easy
rearrange-array-to-maximize-prefix-score
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe must first put the positive numbers then zeros then negative numbers to get the most positives numbers in the accumulation\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nfirst reverse sort the nums\nthen accumu...
1
You are given a **0-indexed** integer array `nums`. You can rearrange the elements of `nums` to **any order** (including the given order). Let `prefix` be the array containing the prefix sums of `nums` after rearranging it. In other words, `prefix[i]` is the sum of the elements from `0` to `i` in `nums` after rearrang...
null
[Python] Count the number of negative numbers and zeros; Explained.
rearrange-array-to-maximize-prefix-score
0
1
We can count the number of negative numbers and zeros in the list.\n\nIf the sum of all the positive numbers is larger than 0 (i.e., we have positive prefix sum), we can first append zeros to the arranged list starting with all the positive numbers. After that, we append the negative numbers from the smallest so that w...
1
You are given a **0-indexed** integer array `nums`. You can rearrange the elements of `nums` to **any order** (including the given order). Let `prefix` be the array containing the prefix sums of `nums` after rearranging it. In other words, `prefix[i]` is the sum of the elements from `0` to `i` in `nums` after rearrang...
null
Simple python3 O(nlog(n)) solution
rearrange-array-to-maximize-prefix-score
0
1
\n# Code\n```\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n nums.sort(reverse=True)\n s = 0\n for i, n in enumerate(nums):\n s += n\n if s <= 0:\n return i\n return len(nums)\n \n```\n\n# Complexity\n- Time complexity: O(nlog(...
1
You are given a **0-indexed** integer array `nums`. You can rearrange the elements of `nums` to **any order** (including the given order). Let `prefix` be the array containing the prefix sums of `nums` after rearranging it. In other words, `prefix[i]` is the sum of the elements from `0` to `i` in `nums` after rearrang...
null
Python simple solution
rearrange-array-to-maximize-prefix-score
0
1
# Code\n```\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n nums.sort(reverse=True)\n pre = 0\n output = 0\n for i in range(len(nums)):\n pre += nums[i]\n if pre > 0:\n output += 1\n else:\n break\n ...
1
You are given a **0-indexed** integer array `nums`. You can rearrange the elements of `nums` to **any order** (including the given order). Let `prefix` be the array containing the prefix sums of `nums` after rearranging it. In other words, `prefix[i]` is the sum of the elements from `0` to `i` in `nums` after rearrang...
null
Prefix Sum of Sorted Array
rearrange-array-to-maximize-prefix-score
0
1
**Python 3**\n```python\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n return sum(n > 0 for n in accumulate(sorted(nums, reverse=True)))\n```\n**C++**\n```cpp\nint maxScore(vector<int>& n) {\n sort(begin(n), end(n), greater<>());\n for (long long sum = 0, i = 0; i <= n.size(); sum += n...
11
You are given a **0-indexed** integer array `nums`. You can rearrange the elements of `nums` to **any order** (including the given order). Let `prefix` be the array containing the prefix sums of `nums` after rearranging it. In other words, `prefix[i]` is the sum of the elements from `0` to `i` in `nums` after rearrang...
null
Easy PYTHON3 solution || O(nlogn)
rearrange-array-to-maximize-prefix-score
0
1
Simple use the sort function or use the 2 pointer approach to bring all the positive elements of the array in the front and pushing all the negative elementsof the array to the back.\n\n```\ndef maxScore(self, nums: List[int]) -> int:\n \n nums.sort(reverse =True)\n \n prefix = [nums[0]]\n ...
1
You are given a **0-indexed** integer array `nums`. You can rearrange the elements of `nums` to **any order** (including the given order). Let `prefix` be the array containing the prefix sums of `nums` after rearranging it. In other words, `prefix[i]` is the sum of the elements from `0` to `i` in `nums` after rearrang...
null
2587: Solution with step by step explanation
rearrange-array-to-maximize-prefix-score
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define a function maxScore that takes a list of integers nums as input and returns an integer.\n2. Sort the input list nums in descending order.\n3. Initialize a list prefix_sums with a single element 0.\n4. Loop through ...
9
You are given a **0-indexed** integer array `nums`. You can rearrange the elements of `nums` to **any order** (including the given order). Let `prefix` be the array containing the prefix sums of `nums` after rearranging it. In other words, `prefix[i]` is the sum of the elements from `0` to `i` in `nums` after rearrang...
null
Python 3 || 2 lines, Counter and accumulate() || T/M: 975 ms / 36 MB
count-the-number-of-beautiful-subarrays
0
1
```\nclass Solution:\n def beautifulSubarrays(self, nums: List[int]) -> int:\n \n ctr = Counter(accumulate(nums, xor, initial = 0))\n\n return sum (n*(n-1) for n in ctr.values())//2\n\n```\n[https://leetcode.com/problems/count-the-number-of-beautiful-subarrays/submissions/913653762/](http://)\n\...
3
You are given a **0-indexed** integer array `nums`. In one operation, you can: * Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`. * Choose a non-negative integer `k` such that the `kth` bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`. * Subtract `2k...
null
2588: Solution with step by step explanation
count-the-number-of-beautiful-subarrays
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define the function beautifulSubarrays that takes a list of integers nums as input and returns an integer.\n2. Initialize a variable res to 0 to keep track of the number of beautiful subarrays.\n3. Get the length of the i...
9
You are given a **0-indexed** integer array `nums`. In one operation, you can: * Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`. * Choose a non-negative integer `k` such that the `kth` bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`. * Subtract `2k...
null
Simple python3 prefix dictionary solution
count-the-number-of-beautiful-subarrays
0
1
\n# Code\n```\nclass Solution:\n def beautifulSubarrays(self, nums: List[int]) -> int:\n prefixDict = defaultdict(list)\n prefixDict[0].append(-1)\n prefix = 0\n res = 0\n for i, num in enumerate(nums):\n p = 1\n while num > 0:\n if num % 2 == 1...
1
You are given a **0-indexed** integer array `nums`. In one operation, you can: * Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`. * Choose a non-negative integer `k` such that the `kth` bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`. * Subtract `2k...
null
Go/Python O(n) time | O(1) space
count-the-number-of-beautiful-subarrays
0
1
# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```golang []\nfunc beautifulSubarrays(nums []int) int64 {\n counter := make(map[int]int)\n counter[0] = 1\n answer ...
2
You are given a **0-indexed** integer array `nums`. In one operation, you can: * Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`. * Choose a non-negative integer `k` such that the `kth` bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`. * Subtract `2k...
null
Can someone explain why [0,0] returns 3?
count-the-number-of-beautiful-subarrays
0
1
Since the i,j should be different, isn\'t the answer 1 instead of 3?
3
You are given a **0-indexed** integer array `nums`. In one operation, you can: * Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`. * Choose a non-negative integer `k` such that the `kth` bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`. * Subtract `2k...
null
Count subarrays with zero XOR
count-the-number-of-beautiful-subarrays
0
1
\n\nBits in a beautiful subarray should appear even number of times. \n \nTherefore, XOR of elements of a beautiful subarray should be zero.\n \nCounting subarrays with a specific XOR in O(n) is a classic problem (you can find it by the name).\n\n**Python 3**\n```python\nclass Solution:\n def beautifulSubarray...
33
You are given a **0-indexed** integer array `nums`. In one operation, you can: * Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`. * Choose a non-negative integer `k` such that the `kth` bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`. * Subtract `2k...
null
Python | Easy to Understand | Beats 72% | O(n)
count-the-number-of-beautiful-subarrays
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAn array is beautiful if xor sum of the array is zero\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe store all the encountered xor sums in a hash map. Whenever we encounter a xor sum which is present in the map...
1
You are given a **0-indexed** integer array `nums`. In one operation, you can: * Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`. * Choose a non-negative integer `k` such that the `kth` bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`. * Subtract `2k...
null
[Python3] | Count Subarrays with Zero Xor
count-the-number-of-beautiful-subarrays
0
1
```\nclass Solution:\n def beautifulSubarrays(self, nums: List[int]) -> int:\n hmap = {0:1}\n x,ans = 0,0\n for i in nums:\n x ^= i\n if x in hmap:\n ans += hmap[x]\n hmap[x] += 1\n else:\n hmap[x] = 1\n return ...
1
You are given a **0-indexed** integer array `nums`. In one operation, you can: * Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`. * Choose a non-negative integer `k` such that the `kth` bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`. * Subtract `2k...
null
Beats 100%
count-the-number-of-beautiful-subarrays
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUsing Hashing\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nStoring previous xor\'s with their count in hash\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(NlogN) - ...
0
You are given a **0-indexed** integer array `nums`. In one operation, you can: * Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`. * Choose a non-negative integer `k` such that the `kth` bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`. * Subtract `2k...
null
Python Solution with Intuition - Hash Table (Beats 95.98% in Time and 90.63% in Memory)
count-the-number-of-beautiful-subarrays
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe first step is to realize that **beautiful subarrays actually means the XOR of all the elements are `0`**. In each operation, we find a `1` bit in two elements and cancel them out. This is exactly what `XOR` does. If the `XOR` of two o...
0
You are given a **0-indexed** integer array `nums`. In one operation, you can: * Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`. * Choose a non-negative integer `k` such that the `kth` bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`. * Subtract `2k...
null
Prefix Sum
count-the-number-of-beautiful-subarrays
0
1
# Complexity\n- Time complexity: $$O(n)$$.\n\n- Space complexity: $$O(n)$$.\n\n# Code\n```\nclass Solution:\n def beautifulSubarrays(self, nums: List[int]) -> int:\n prefToCnt = Counter({0: 1})\n result = 0\n summ = 0\n for num in nums:\n summ ^= num\n result += pref...
0
You are given a **0-indexed** integer array `nums`. In one operation, you can: * Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`. * Choose a non-negative integer `k` such that the `kth` bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`. * Subtract `2k...
null
Take advantage of small time range - Greedy pickup
minimum-time-to-complete-all-tasks
0
1
Small time range allows tracking of all available time slots that can be greedily selected via sorting by earliest end time. \n\n# Code\n```\nclass Solution:\n def findMinimumTime(self, tasks: List[List[int]]) -> int:\n tasks.sort(key = lambda x: x[1])\n tot = 0\n used = [0] * 2001\n for ...
3
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
Simple python3 O(N^2) greedy solution
minimum-time-to-complete-all-tasks
0
1
\n# Code\n```\nclass Solution:\n def findMinimumTime(self, tasks: List[List[int]]) -> int:\n runSet = set()\n tasks.sort(key = lambda x: [x[1], x[0], -x[2]])\n for i, v in enumerate(tasks):\n start, end, duration = v\n run = end\n while duration > 0:\n ...
1
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
GREEDY Python
minimum-time-to-complete-all-tasks
0
1
# Intuition\nRun earlier task as late as you can which benefits later tasks.\n\n# Approach\nGREEDY\n\n# Complexity\n- Time complexity: $O(NT)$\n\n\n\n\n# Code\n```\nclass Solution:\n def findMinimumTime(self, tasks: List[List[int]]) -> int:\n tasks.sort(key=lambda x:x[1])\n on=[0]*2001\n for s,e...
1
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
Simple Diagram Explanation
minimum-time-to-complete-all-tasks
1
1
# Idea\n- The range for `start` and `end` is $$1$$ to $$2000$$ which is sizable to use a number line in the form of a list to represent it. \n- We put all previously ran tasks times onto the number line to know when intersection can occur. \n- Sort tasks by `end` times to maximize possible collisions. \n- Each time a n...
59
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
Python3 👍||⚡Easy solution🔥|| Greedy + sort + set ||
minimum-time-to-complete-all-tasks
0
1
\n\n# Complexity\n- Time complexity:O(N^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def findMinimumTime(self, tasks: List[List[int]]) -> int:\n tasks = sorted(tasks,key=lambda x:x...
4
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
[C++|Java|Python3] sweep line
minimum-time-to-complete-all-tasks
1
1
Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/15456f4294fa2d2de5e056fc441f28872a0c2a71) for solutions of weekly 336. \n\n**C++**\n```\nclass Solution {\npublic:\n int findMinimumTime(vector<vector<int>>& tasks) {\n vector<int> line(2001); \n sort(tasks.begin(), tasks.end(), []...
7
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
Add Replace or Ignore (I didn't fully understand why but got AC ;)
minimum-time-to-complete-all-tasks
0
1
# Approach\nFor every time `t`, you can either select it, ignore it or replace a previous selected time with it.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n^3)\n\n- Space complexity: O(n^2)\n\n# Code\n```\nclass Solution:\n def findMinimumTime(self, tasks: List[Li...
1
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
Go/Python O(n*max(end)-min(start)) time | O(max(end)-min(start))
minimum-time-to-complete-all-tasks
0
1
# Complexity\n- Time complexity: $$O(n*max(end)-min(start))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(max(end)-min(start))$$ \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```golang []\nfunc findMinimumTime(tasks [][]int) int {\n sort.SliceStable(tasks, ...
1
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
[Python] 100%, simple greedy, what to learn from this
minimum-time-to-complete-all-tasks
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis problem was eye opening for me for several reasons.\n\nThis is an overlapping intervals problem. We are going to calculate the minimum number of seconds the computer needs to be turned on to complete all of the tasks.\n...
4
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
Python3 Greedy O(n * n) no Sort solution
minimum-time-to-complete-all-tasks
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIntuition is greedy.The time slot we say [x, x] needs to choose only if there is a task. If you don\'t start at time x, you won\'t make it on time. And since we know we need to turn on the machine at time x. Every task can use this time s...
2
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
Python Hard Line Sweep
minimum-time-to-complete-all-tasks
0
1
```\nclass Solution:\n def findMinimumTime(self, tasks: List[List[int]]) -> int:\n line = [0] * 2001\n\n tasks.sort(key = lambda x: x[1])\n\n for start, end, time in tasks:\n count = sum(line[x] for x in range(start, end+1))\n time = max(0, time - count)\n \n ...
0
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
Python Solution Easy to understand
minimum-time-to-complete-all-tasks
0
1
# Code\n```\nclass Solution:\n def findMinimumTime(self, tasks: List[List[int]]) -> int:\n tasks.sort(key=lambda x:[x[1],-x[2],x[1]])\n done=[]\n for a,b,c in tasks:\n for a1,b1 in done[::-1]:\n if(b1>=a and c!=0):\n c=max(c-(b1-max(a1,a)+1),0)\n ...
0
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
Simple solution in Python3 with explanation, faster than 93%
minimum-time-to-complete-all-tasks
0
1
The goal is to minimize the total number of working time slots. Thus, my approach is to maximize the overlap of all tasks. \nThe tasks are first sorted by their end timestamps. \nI try to allocate the time slots that are already on for each task. \nIf the already on time slots in its [start, end] window is less than i...
0
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
Python || greedy || O(n^2)
minimum-time-to-complete-all-tasks
0
1
1. Consider the example `tasks = [[1,3,2],[2,5,3],[5,6,2]]`.\nImagine we list all tasks on schedule, like following:\n![image](https://assets.leetcode.com/users/images/b4e4146f-ba5c-4ede-876a-70ffb2c64b2b_1680318353.9329975.png)\n1-1. As time increasing, at `time=3`, `tasks[0]` reaches its deadline. At this moment, we ...
0
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
Using SegmentTree
minimum-time-to-complete-all-tasks
0
1
\n\n# Code\n```\nclass SegmentTree:\n\n def __init__(self, arr, operator, operationIdentity) -> None:\n self.arrLength = len(arr)\n self.arr = arr\n self.modifiedNumberOfChildren = 2**(math.ceil(math.log(self.arrLength, 2)))\n self.segmentTreeLength = 2*self.modifiedNumberOfChildren - 1\n...
0
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
Python solution + explanation
minimum-time-to-complete-all-tasks
0
1
# Approach\n1. Sort the tasks by the end time.\n```\n tasks.sort(key = (lambda x: x[1]))\n```\n2. We want to use list "work" to store times when the computer is on.\nFor each of the tasks we first check if the computer has already been designed to be working during the appropriate time to execute the task. We de...
0
There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti,...
null
One By One Distribute || python3 || simple approach
distribute-money-to-maximum-children
0
1
\n```\nclass Solution:\n def distMoney(self, money: int, children: int) -> int: \n if money < children: return -1 \n ans = 0 \n for j in range(1,children+1):\n leftmoney = money - 8 # if current child gets 8$\n leftchildren = children - j\n if leftmoney >= ...
8
You are given an integer `money` denoting the amount of money (in dollars) that you have and another integer `children` denoting the number of children that you must distribute the money to. You have to distribute the money according to the following rules: * All money must be distributed. * Everyone must receive...
null
2591. Simple solution in Python
distribute-money-to-maximum-children
0
1
# Code\n```\nclass Solution:\n def distMoney(self, money: int, children: int) -> int:\n if children > money: return -1\n elif children == money: return 0\n count, rem = money//8, money%8\n while count >= 0:\n if count + rem >= children:\n if children == count + 1...
1
You are given an integer `money` denoting the amount of money (in dollars) that you have and another integer `children` denoting the number of children that you must distribute the money to. You have to distribute the money according to the following rules: * All money must be distributed. * Everyone must receive...
null
Python 3 || 5 lines, w/ explanation || T/M: 39 ms / 13.8 MB
distribute-money-to-maximum-children
0
1
```\nclass Solution:\n def distMoney(self, money: int, children: int) -> int:\n\n if money < children: return -1 # <-- More children than dollars\n\n n = 8 * children - money # <-- Enough dollars for all children \n # to g...
5
You are given an integer `money` denoting the amount of money (in dollars) that you have and another integer `children` denoting the number of children that you must distribute the money to. You have to distribute the money according to the following rules: * All money must be distributed. * Everyone must receive...
null
[Python 3] Modulo 7
distribute-money-to-maximum-children
0
1
This *easy* question is actually tricky.\n\n# Approach\nFirst, we need to check if it is possible to distribute the money:\n```\nif money<children: return -1\n```\nSince *everyone must receive at least 1 dollar*, we give 1 dollar to each person:\n```\nmoney -= children\n```\nTo *maximize the number of children who may ...
5
You are given an integer `money` denoting the amount of money (in dollars) that you have and another integer `children` denoting the number of children that you must distribute the money to. You have to distribute the money according to the following rules: * All money must be distributed. * Everyone must receive...
null
Python O(n) Just do what question says
distribute-money-to-maximum-children
0
1
\n```\nclass Solution:\n def distMoney(self, money: int, children: int) -> int:\n if 8*children==money:\n return children\n \n if money<children:\n return -1\n\n #Give every child 1 dollar\n arr=[1]*children\n money-=children \n\n #------ Try mak...
3
You are given an integer `money` denoting the amount of money (in dollars) that you have and another integer `children` denoting the number of children that you must distribute the money to. You have to distribute the money according to the following rules: * All money must be distributed. * Everyone must receive...
null
Simple approach for the most toughest easy question :)
distribute-money-to-maximum-children
0
1
# Intuition\nTry to distribute the money keeping the conditions in mind.\n\n# Approach\nThink of each child as an element in a list.Initially each child value is 1 now we try to calculate how many children can get an additional 7.Here \'c\' calculates the no.of children who can get 8$ each without keeping any condition...
2
You are given an integer `money` denoting the amount of money (in dollars) that you have and another integer `children` denoting the number of children that you must distribute the money to. You have to distribute the money according to the following rules: * All money must be distributed. * Everyone must receive...
null
O(1) Beats 98% Python
distribute-money-to-maximum-children
0
1
# Python \n```\nclass Solution:\n def distMoney(self, money: int, children: int) -> int:\n if children>money:\n return -1\n if money>8*children:\n return children-1\n money-=children\n if children-money//7==1 and money%7==3:\n return money//7-1\n re...
2
You are given an integer `money` denoting the amount of money (in dollars) that you have and another integer `children` denoting the number of children that you must distribute the money to. You have to distribute the money according to the following rules: * All money must be distributed. * Everyone must receive...
null
Python3 Case by Case Solution
distribute-money-to-maximum-children
0
1
\n\n# Code\n```\nclass Solution:\n def distMoney(self, money: int, children: int) -> int:\n # cannot give 1 to every child\n if money < children: return -1\n # have more than enough, all child have 8 except 1 child have more\n if money > 8 * children: return children - 1\n # give 1...
8
You are given an integer `money` denoting the amount of money (in dollars) that you have and another integer `children` denoting the number of children that you must distribute the money to. You have to distribute the money according to the following rules: * All money must be distributed. * Everyone must receive...
null
Python if-else
distribute-money-to-maximum-children
0
1
```\nclass Solution:\n def distMoney(self, money: int, children: int) -> int:\n res = (money-children)//7\n if children>money:\n return -1\n elif res==children-1 and (money-children)%7==3:\n return res-1\n elif res==children and (money-children)%7>0:\n ret...
3
You are given an integer `money` denoting the amount of money (in dollars) that you have and another integer `children` denoting the number of children that you must distribute the money to. You have to distribute the money according to the following rules: * All money must be distributed. * Everyone must receive...
null
Python Elegant & Short | Sorting | Two pointers
maximize-greatness-of-an-array
0
1
```\nclass Solution:\n def maximizeGreatness(self, nums: List[int]) -> int:\n nums.sort()\n i = 0\n\n for num in nums:\n if nums[i] < num:\n i += 1\n\n return i\n\n```
1
You are given a 0-indexed integer array `nums`. You are allowed to permute `nums` into a new array `perm` of your choosing. We define the **greatness** of `nums` be the number of indices `0 <= i < nums.length` for which `perm[i] > nums[i]`. Return _the **maximum** possible greatness you can achieve after permuting_ `...
null
Greedy || Count Duplicates || Sorting
maximize-greatness-of-an-array
1
1
# Intuition\nThe test case : $$[1,2,3,4]$$ gives an idea to solve this problem. It\'s answer is $$3$$, or something to do with **the sorted array** arrangement. If the array had no duplicate elements, the answer would always be= **the number of elements -1** .Simple logic being, the greatest number[$$4$$ in the above a...
1
You are given a 0-indexed integer array `nums`. You are allowed to permute `nums` into a new array `perm` of your choosing. We define the **greatness** of `nums` be the number of indices `0 <= i < nums.length` for which `perm[i] > nums[i]`. Return _the **maximum** possible greatness you can achieve after permuting_ `...
null
[Java/Python 3] Sort then 2 pointers.
maximize-greatness-of-an-array
1
1
Sort then start from index `0` to check how many pairs of `nums[ans] < nums[j] (num in the code)` we can have.\n\n```java\n public int maximizeGreatness(int[] nums) {\n Arrays.sort(nums);\n int ans = 0;\n for (int num : nums) {\n if (nums[ans] < num) {\n ++ans;\n ...
15
You are given a 0-indexed integer array `nums`. You are allowed to permute `nums` into a new array `perm` of your choosing. We define the **greatness** of `nums` be the number of indices `0 <= i < nums.length` for which `perm[i] > nums[i]`. Return _the **maximum** possible greatness you can achieve after permuting_ `...
null
python3 Solution
maximize-greatness-of-an-array
0
1
\n```\nclass Solution:\n def maximizeGreatness(self, nums: List[int]) -> int:\n nums.sort()\n ans=0\n for num in nums:\n if num>nums[ans]:\n ans+=1\n\n return ans \n```
4
You are given a 0-indexed integer array `nums`. You are allowed to permute `nums` into a new array `perm` of your choosing. We define the **greatness** of `nums` be the number of indices `0 <= i < nums.length` for which `perm[i] > nums[i]`. Return _the **maximum** possible greatness you can achieve after permuting_ `...
null
sorting || python3 || easiest solution
maximize-greatness-of-an-array
0
1
\n```\nclass Solution:\n def maximizeGreatness(self, nums: List[int]) -> int: \n nums.sort() \n count = 0\n j = i = 0 \n while j < len(nums):\n if nums[i] < nums[j]:\n count+=1 \n i+=1\n j+=1 \n return count\n \n \n...
4
You are given a 0-indexed integer array `nums`. You are allowed to permute `nums` into a new array `perm` of your choosing. We define the **greatness** of `nums` be the number of indices `0 <= i < nums.length` for which `perm[i] > nums[i]`. Return _the **maximum** possible greatness you can achieve after permuting_ `...
null
python3 Solution
find-score-of-an-array-after-marking-all-elements
0
1
\n```\nclass Solution:\n def findScore(self, nums: List[int]) -> int:\n look=[0]*(len(nums)+1)\n ans=0\n for a,i in sorted([a,i] for i,a in enumerate(nums)):\n if look[i]:\n continue\n\n ans+=a\n look[i]=look[i-1]=look[i+1]=1\n\n return ans ...
2
You are given an array `nums` consisting of positive integers. Starting with `score = 0`, apply the following algorithm: * Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index. * Add the value of the chosen integer to `score`. * Mark **the chosen...
null
✔️ Python3 Solution | Sorting
find-score-of-an-array-after-marking-all-elements
0
1
# Approach\nsort the array for `index`, and traverse to the index with the smallest element, check if you marked it or not in `dp`, if it is not marked mark its adjacent element and add its `value` to the `score`.\n\n# Complexity\n- Time complexity: $$O(nlogn)$$\n- Space complexity: $$O(n)$$\n\n# Code\n```Python\nclass...
1
You are given an array `nums` consisting of positive integers. Starting with `score = 0`, apply the following algorithm: * Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index. * Add the value of the chosen integer to `score`. * Mark **the chosen...
null
Python | Heap
find-score-of-an-array-after-marking-all-elements
0
1
# Code\n```\nfrom heapq import heapify, heappush, heappop\nclass Solution:\n def findScore(self, nums: List[int]) -> int:\n n = len(nums)\n visited = set()\n h = [(num, i) for i,num in enumerate(nums)]\n heapify(h)\n res = 0\n while h:\n num, i = heappop(h)\n ...
1
You are given an array `nums` consisting of positive integers. Starting with `score = 0`, apply the following algorithm: * Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index. * Add the value of the chosen integer to `score`. * Mark **the chosen...
null
[Java/Python 3] TreeSet.
find-score-of-an-array-after-marking-all-elements
1
1
**Method 1: Use only TreeSet/SortedSet.**\n\n```java\n public long findScore(int[] nums) {\n TreeSet<List<Integer>> valIndex = new TreeSet<>(\n Comparator.comparingLong(l -> l.get(0) * 1_000_001L + l.get(1))\n );\n for (int i = 0; i < nums.length; ++i) {\n valIndex.add(Arra...
8
You are given an array `nums` consisting of positive integers. Starting with `score = 0`, apply the following algorithm: * Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index. * Add the value of the chosen integer to `score`. * Mark **the chosen...
null
Heap || Easiest Solution || python3
find-score-of-an-array-after-marking-all-elements
0
1
\n```\nclass Solution:\n def findScore(self, nums: List[int]) -> int: \n heap = [(nums[i],i) for i in range(len(nums))] \n marked = {} \n score = 0\n heapq.heapify(heap) \n for i in range(len(nums)):\n mini,ind = heapq.heappop(heap) \n if ind not in marked:\n ...
3
You are given an array `nums` consisting of positive integers. Starting with `score = 0`, apply the following algorithm: * Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index. * Add the value of the chosen integer to `score`. * Mark **the chosen...
null