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 O(N) bit manipulation - beats 100% | can-make-palindrome-from-substring | 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 string `s` and array `queries` where `queries[i] = [lefti, righti, ki]`. We may rearrange the substring `s[lefti...righti]` for each query and then choose up to `ki` of them to replace with any lowercase English letter.
If the substring is possible to be a palindrome string after the operations above, ... | Start at any node A and traverse the tree to find the furthest node from it, let's call it B. Having found the furthest node B, traverse the tree from B to find the furthest node from it, lets call it C. The distance between B and C is the tree diameter. |
Python O(N) bit manipulation - beats 100% | can-make-palindrome-from-substring | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Given an integer number `n`, return the difference between the product of its digits and the sum of its digits.
**Example 1:**
**Input:** n = 234
**Output:** 15
**Explanation:**
Product of digits = 2 \* 3 \* 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
**Example 2:**
**Input:** n = 4421
**Output:**... | Since we can rearrange the substring, all we care about is the frequency of each character in that substring. How to find the character frequencies efficiently ? As a preprocess, calculate the accumulate frequency of all characters for all prefixes of the string. How to check if a substring can be changed to a palindro... |
Python3 speed and space > 90% | can-make-palindrome-from-substring | 0 | 1 | # Intuition\nFor every query, we only need to know the number of every letter is even or odd in the substring to get minimum replacement needed. Because there is only 26 letters, we can use a int bitmask to store the parity of every letter in the string.\n# Approach\nUse another array mem with length len(s)+1 to store ... | 1 | You are given a string `s` and array `queries` where `queries[i] = [lefti, righti, ki]`. We may rearrange the substring `s[lefti...righti]` for each query and then choose up to `ki` of them to replace with any lowercase English letter.
If the substring is possible to be a palindrome string after the operations above, ... | Start at any node A and traverse the tree to find the furthest node from it, let's call it B. Having found the furthest node B, traverse the tree from B to find the furthest node from it, lets call it C. The distance between B and C is the tree diameter. |
Python3 speed and space > 90% | can-make-palindrome-from-substring | 0 | 1 | # Intuition\nFor every query, we only need to know the number of every letter is even or odd in the substring to get minimum replacement needed. Because there is only 26 letters, we can use a int bitmask to store the parity of every letter in the string.\n# Approach\nUse another array mem with length len(s)+1 to store ... | 1 | Given an integer number `n`, return the difference between the product of its digits and the sum of its digits.
**Example 1:**
**Input:** n = 234
**Output:** 15
**Explanation:**
Product of digits = 2 \* 3 \* 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
**Example 2:**
**Input:** n = 4421
**Output:**... | Since we can rearrange the substring, all we care about is the frequency of each character in that substring. How to find the character frequencies efficiently ? As a preprocess, calculate the accumulate frequency of all characters for all prefixes of the string. How to check if a substring can be changed to a palindro... |
Python3 + Bitmask / 10 lines / Beats 99.17% in Speed | can-make-palindrome-from-substring | 0 | 1 | # Code\n```\nclass Solution:\n def canMakePaliQueries(self, s: str, queries: List[List[int]]) -> List[bool]:\n masks = [0]\n for i, c in enumerate(s):\n masks.append((1 << (ord(s[i]) - ord(\'a\'))) ^ masks[-1])\n \n ans = []\n for (l, r, k) in queries:\n ans.a... | 0 | You are given a string `s` and array `queries` where `queries[i] = [lefti, righti, ki]`. We may rearrange the substring `s[lefti...righti]` for each query and then choose up to `ki` of them to replace with any lowercase English letter.
If the substring is possible to be a palindrome string after the operations above, ... | Start at any node A and traverse the tree to find the furthest node from it, let's call it B. Having found the furthest node B, traverse the tree from B to find the furthest node from it, lets call it C. The distance between B and C is the tree diameter. |
Python3 + Bitmask / 10 lines / Beats 99.17% in Speed | can-make-palindrome-from-substring | 0 | 1 | # Code\n```\nclass Solution:\n def canMakePaliQueries(self, s: str, queries: List[List[int]]) -> List[bool]:\n masks = [0]\n for i, c in enumerate(s):\n masks.append((1 << (ord(s[i]) - ord(\'a\'))) ^ masks[-1])\n \n ans = []\n for (l, r, k) in queries:\n ans.a... | 0 | Given an integer number `n`, return the difference between the product of its digits and the sum of its digits.
**Example 1:**
**Input:** n = 234
**Output:** 15
**Explanation:**
Product of digits = 2 \* 3 \* 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
**Example 2:**
**Input:** n = 4421
**Output:**... | Since we can rearrange the substring, all we care about is the frequency of each character in that substring. How to find the character frequencies efficiently ? As a preprocess, calculate the accumulate frequency of all characters for all prefixes of the string. How to check if a substring can be changed to a palindro... |
modified from another solution | can-make-palindrome-from-substring | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | You are given a string `s` and array `queries` where `queries[i] = [lefti, righti, ki]`. We may rearrange the substring `s[lefti...righti]` for each query and then choose up to `ki` of them to replace with any lowercase English letter.
If the substring is possible to be a palindrome string after the operations above, ... | Start at any node A and traverse the tree to find the furthest node from it, let's call it B. Having found the furthest node B, traverse the tree from B to find the furthest node from it, lets call it C. The distance between B and C is the tree diameter. |
modified from another solution | can-make-palindrome-from-substring | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given an integer number `n`, return the difference between the product of its digits and the sum of its digits.
**Example 1:**
**Input:** n = 234
**Output:** 15
**Explanation:**
Product of digits = 2 \* 3 \* 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
**Example 2:**
**Input:** n = 4421
**Output:**... | Since we can rearrange the substring, all we care about is the frequency of each character in that substring. How to find the character frequencies efficiently ? As a preprocess, calculate the accumulate frequency of all characters for all prefixes of the string. How to check if a substring can be changed to a palindro... |
100% Python with Bit mask | can-make-palindrome-from-substring | 0 | 1 | Check [my first submission](https://leetcode.com/problems/can-make-palindrome-from-substring/submissions/952532375/)\n\n\n# Intuition\n- focus on the not-paired characters\n\n# Approach\n- bit mask\n\n# Complexity\n- Time complexity: O(len(s)+len(queries))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Spa... | 0 | You are given a string `s` and array `queries` where `queries[i] = [lefti, righti, ki]`. We may rearrange the substring `s[lefti...righti]` for each query and then choose up to `ki` of them to replace with any lowercase English letter.
If the substring is possible to be a palindrome string after the operations above, ... | Start at any node A and traverse the tree to find the furthest node from it, let's call it B. Having found the furthest node B, traverse the tree from B to find the furthest node from it, lets call it C. The distance between B and C is the tree diameter. |
100% Python with Bit mask | can-make-palindrome-from-substring | 0 | 1 | Check [my first submission](https://leetcode.com/problems/can-make-palindrome-from-substring/submissions/952532375/)\n\n\n# Intuition\n- focus on the not-paired characters\n\n# Approach\n- bit mask\n\n# Complexity\n- Time complexity: O(len(s)+len(queries))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Spa... | 0 | Given an integer number `n`, return the difference between the product of its digits and the sum of its digits.
**Example 1:**
**Input:** n = 234
**Output:** 15
**Explanation:**
Product of digits = 2 \* 3 \* 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
**Example 2:**
**Input:** n = 4421
**Output:**... | Since we can rearrange the substring, all we care about is the frequency of each character in that substring. How to find the character frequencies efficiently ? As a preprocess, calculate the accumulate frequency of all characters for all prefixes of the string. How to check if a substring can be changed to a palindro... |
[Python] Trie/Bitmasking Solutions with Explanation | number-of-valid-words-for-each-puzzle | 0 | 1 | ### Approach 1: Trie\n\nThis approach is more intuitive for working with sets of words. Essentially, we can perform the following:\n\n- Generate a Trie for all the given words in the input. (If you don\'t know what a Trie is or how it works, I suggest starting [here](https://medium.com/basecs/trying-to-understand-tries... | 16 | With respect to a given `puzzle` string, a `word` is _valid_ if both the following conditions are satisfied:
* `word` contains the first letter of `puzzle`.
* For each letter in `word`, that letter is in `puzzle`.
* For example, if the puzzle is `"abcdefg "`, then valid words are `"faced "`, `"cabbage "`, an... | Can you reduce this problem to a classic problem? The problem is equivalent to finding any palindromic subsequence of length at least N-K where N is the length of the string. Try to find the longest palindromic subsequence. Use DP to do that. |
[Python] Trie/Bitmasking Solutions with Explanation | number-of-valid-words-for-each-puzzle | 0 | 1 | ### Approach 1: Trie\n\nThis approach is more intuitive for working with sets of words. Essentially, we can perform the following:\n\n- Generate a Trie for all the given words in the input. (If you don\'t know what a Trie is or how it works, I suggest starting [here](https://medium.com/basecs/trying-to-understand-tries... | 16 | There are `n` people that are split into some unknown number of groups. Each person is labeled with a **unique ID** from `0` to `n - 1`.
You are given an integer array `groupSizes`, where `groupSizes[i]` is the size of the group that person `i` is in. For example, if `groupSizes[1] = 3`, then person `1` must be in a g... | Exploit the fact that the length of the puzzle is only 7. Use bit-masks to represent the word and puzzle strings. For each puzzle, count the number of words whose bit-mask is a sub-mask of the puzzle's bit-mask. |
Python solution. beats 100% | number-of-valid-words-for-each-puzzle | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo find the number of valid words for each puzzle in the list, we can create a dictionary where the keys are masks representing the unique characters in each word and the values are the number of occurrences of words with those characters... | 1 | With respect to a given `puzzle` string, a `word` is _valid_ if both the following conditions are satisfied:
* `word` contains the first letter of `puzzle`.
* For each letter in `word`, that letter is in `puzzle`.
* For example, if the puzzle is `"abcdefg "`, then valid words are `"faced "`, `"cabbage "`, an... | Can you reduce this problem to a classic problem? The problem is equivalent to finding any palindromic subsequence of length at least N-K where N is the length of the string. Try to find the longest palindromic subsequence. Use DP to do that. |
Python solution. beats 100% | number-of-valid-words-for-each-puzzle | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo find the number of valid words for each puzzle in the list, we can create a dictionary where the keys are masks representing the unique characters in each word and the values are the number of occurrences of words with those characters... | 1 | There are `n` people that are split into some unknown number of groups. Each person is labeled with a **unique ID** from `0` to `n - 1`.
You are given an integer array `groupSizes`, where `groupSizes[i]` is the size of the group that person `i` is in. For example, if `groupSizes[1] = 3`, then person `1` must be in a g... | Exploit the fact that the length of the puzzle is only 7. Use bit-masks to represent the word and puzzle strings. For each puzzle, count the number of words whose bit-mask is a sub-mask of the puzzle's bit-mask. |
[Python] 26 Tries | number-of-valid-words-for-each-puzzle | 0 | 1 | **Approach:**\n\nMake an array (```trees```) of 26 Tries where ```trees[0]``` is a Trie that only \nhas words that contain the letter "a" and ```trees[25]``` is a Trie \nthat only has words that contain the letter "z". \n\n<details>\n\n<summary><b>Why?</b> (click to show)</summary>\n\n*This step is taken because the fi... | 5 | With respect to a given `puzzle` string, a `word` is _valid_ if both the following conditions are satisfied:
* `word` contains the first letter of `puzzle`.
* For each letter in `word`, that letter is in `puzzle`.
* For example, if the puzzle is `"abcdefg "`, then valid words are `"faced "`, `"cabbage "`, an... | Can you reduce this problem to a classic problem? The problem is equivalent to finding any palindromic subsequence of length at least N-K where N is the length of the string. Try to find the longest palindromic subsequence. Use DP to do that. |
[Python] 26 Tries | number-of-valid-words-for-each-puzzle | 0 | 1 | **Approach:**\n\nMake an array (```trees```) of 26 Tries where ```trees[0]``` is a Trie that only \nhas words that contain the letter "a" and ```trees[25]``` is a Trie \nthat only has words that contain the letter "z". \n\n<details>\n\n<summary><b>Why?</b> (click to show)</summary>\n\n*This step is taken because the fi... | 5 | There are `n` people that are split into some unknown number of groups. Each person is labeled with a **unique ID** from `0` to `n - 1`.
You are given an integer array `groupSizes`, where `groupSizes[i]` is the size of the group that person `i` is in. For example, if `groupSizes[1] = 3`, then person `1` must be in a g... | Exploit the fact that the length of the puzzle is only 7. Use bit-masks to represent the word and puzzle strings. For each puzzle, count the number of words whose bit-mask is a sub-mask of the puzzle's bit-mask. |
Python 3 FT 90%: TC O(W+P * min(2^L, W)), SC O(W): Trie with Ordered Entries | number-of-valid-words-for-each-puzzle | 0 | 1 | # Intuition\n\nThere\'s an easier bit masking solution. The idea is you encode words as 26 bit integers, with a 1 if there\'s at least one copy of each corresponding character, and 0 otherwise. Then you start with `puzzleEncoding` and do a "masked decrement" to zero to find all `wordEncoding`s with those bits, and add ... | 0 | With respect to a given `puzzle` string, a `word` is _valid_ if both the following conditions are satisfied:
* `word` contains the first letter of `puzzle`.
* For each letter in `word`, that letter is in `puzzle`.
* For example, if the puzzle is `"abcdefg "`, then valid words are `"faced "`, `"cabbage "`, an... | Can you reduce this problem to a classic problem? The problem is equivalent to finding any palindromic subsequence of length at least N-K where N is the length of the string. Try to find the longest palindromic subsequence. Use DP to do that. |
Python 3 FT 90%: TC O(W+P * min(2^L, W)), SC O(W): Trie with Ordered Entries | number-of-valid-words-for-each-puzzle | 0 | 1 | # Intuition\n\nThere\'s an easier bit masking solution. The idea is you encode words as 26 bit integers, with a 1 if there\'s at least one copy of each corresponding character, and 0 otherwise. Then you start with `puzzleEncoding` and do a "masked decrement" to zero to find all `wordEncoding`s with those bits, and add ... | 0 | There are `n` people that are split into some unknown number of groups. Each person is labeled with a **unique ID** from `0` to `n - 1`.
You are given an integer array `groupSizes`, where `groupSizes[i]` is the size of the group that person `i` is in. For example, if `groupSizes[1] = 3`, then person `1` must be in a g... | Exploit the fact that the length of the puzzle is only 7. Use bit-masks to represent the word and puzzle strings. For each puzzle, count the number of words whose bit-mask is a sub-mask of the puzzle's bit-mask. |
Python - 9 line tidy code using itertools and Counter. | number-of-valid-words-for-each-puzzle | 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 | With respect to a given `puzzle` string, a `word` is _valid_ if both the following conditions are satisfied:
* `word` contains the first letter of `puzzle`.
* For each letter in `word`, that letter is in `puzzle`.
* For example, if the puzzle is `"abcdefg "`, then valid words are `"faced "`, `"cabbage "`, an... | Can you reduce this problem to a classic problem? The problem is equivalent to finding any palindromic subsequence of length at least N-K where N is the length of the string. Try to find the longest palindromic subsequence. Use DP to do that. |
Python - 9 line tidy code using itertools and Counter. | number-of-valid-words-for-each-puzzle | 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 are `n` people that are split into some unknown number of groups. Each person is labeled with a **unique ID** from `0` to `n - 1`.
You are given an integer array `groupSizes`, where `groupSizes[i]` is the size of the group that person `i` is in. For example, if `groupSizes[1] = 3`, then person `1` must be in a g... | Exploit the fact that the length of the puzzle is only 7. Use bit-masks to represent the word and puzzle strings. For each puzzle, count the number of words whose bit-mask is a sub-mask of the puzzle's bit-mask. |
Python Solution using binary search | number-of-valid-words-for-each-puzzle | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(64*M log(N))\n- where M:- 10^4 and N:- 10^5\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<... | 0 | With respect to a given `puzzle` string, a `word` is _valid_ if both the following conditions are satisfied:
* `word` contains the first letter of `puzzle`.
* For each letter in `word`, that letter is in `puzzle`.
* For example, if the puzzle is `"abcdefg "`, then valid words are `"faced "`, `"cabbage "`, an... | Can you reduce this problem to a classic problem? The problem is equivalent to finding any palindromic subsequence of length at least N-K where N is the length of the string. Try to find the longest palindromic subsequence. Use DP to do that. |
Python Solution using binary search | number-of-valid-words-for-each-puzzle | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(64*M log(N))\n- where M:- 10^4 and N:- 10^5\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<... | 0 | There are `n` people that are split into some unknown number of groups. Each person is labeled with a **unique ID** from `0` to `n - 1`.
You are given an integer array `groupSizes`, where `groupSizes[i]` is the size of the group that person `i` is in. For example, if `groupSizes[1] = 3`, then person `1` must be in a g... | Exploit the fact that the length of the puzzle is only 7. Use bit-masks to represent the word and puzzle strings. For each puzzle, count the number of words whose bit-mask is a sub-mask of the puzzle's bit-mask. |
Without Bitmasking and Trie, Optimization || Python 3 | number-of-valid-words-for-each-puzzle | 0 | 1 | # Code\n```\nclass Solution:\n def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]:\n\n words = ["".join(sorted(set(w))) for w in words]\n ct = Counter(words)\n words = [set(w) for w in list(set(words))]\n\n dt = defaultdict(lambda: [])\n for ws in word... | 0 | With respect to a given `puzzle` string, a `word` is _valid_ if both the following conditions are satisfied:
* `word` contains the first letter of `puzzle`.
* For each letter in `word`, that letter is in `puzzle`.
* For example, if the puzzle is `"abcdefg "`, then valid words are `"faced "`, `"cabbage "`, an... | Can you reduce this problem to a classic problem? The problem is equivalent to finding any palindromic subsequence of length at least N-K where N is the length of the string. Try to find the longest palindromic subsequence. Use DP to do that. |
Without Bitmasking and Trie, Optimization || Python 3 | number-of-valid-words-for-each-puzzle | 0 | 1 | # Code\n```\nclass Solution:\n def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]:\n\n words = ["".join(sorted(set(w))) for w in words]\n ct = Counter(words)\n words = [set(w) for w in list(set(words))]\n\n dt = defaultdict(lambda: [])\n for ws in word... | 0 | There are `n` people that are split into some unknown number of groups. Each person is labeled with a **unique ID** from `0` to `n - 1`.
You are given an integer array `groupSizes`, where `groupSizes[i]` is the size of the group that person `i` is in. For example, if `groupSizes[1] = 3`, then person `1` must be in a g... | Exploit the fact that the length of the puzzle is only 7. Use bit-masks to represent the word and puzzle strings. For each puzzle, count the number of words whose bit-mask is a sub-mask of the puzzle's bit-mask. |
[Python3] Good enough | distance-between-bus-stops | 0 | 1 | ``` Python3 []\nclass Solution:\n def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n minn = min(start, destination)\n maxx = max(start, destination)\n return min(sum(distance[minn:maxx]), sum(distance[:minn] + distance[maxx:]))\n``` | 3 | A bus has `n` stops numbered from `0` to `n - 1` that form a circle. We know the distance between all pairs of neighboring stops where `distance[i]` is the distance between the stops number `i` and `(i + 1) % n`.
The bus goes along both directions i.e. clockwise and counterclockwise.
Return the shortest distance betw... | Sort the pickup and dropoff events by location, then process them in order. |
[Python3] Good enough | distance-between-bus-stops | 0 | 1 | ``` Python3 []\nclass Solution:\n def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n minn = min(start, destination)\n maxx = max(start, destination)\n return min(sum(distance[minn:maxx]), sum(distance[:minn] + distance[maxx:]))\n``` | 3 | Given an integer array **sorted** in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.
**Example 1:**
**Input:** arr = \[1,2,2,6,6,6,6,7,10\]
**Output:** 6
**Example 2:**
**Input:** arr = \[1,1\]
**Output:** 1
**Constraints:**
* `1 <= arr... | Find the distance between the two stops if the bus moved in clockwise or counterclockwise directions. |
Python distance difference | distance-between-bus-stops | 0 | 1 | \n# Code\n```\nclass Solution:\n def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n if start == destination:\n return 0\n elif start < destination:\n trip = sum(distance[start:destination])\n else:\n trip = sum(distance... | 2 | A bus has `n` stops numbered from `0` to `n - 1` that form a circle. We know the distance between all pairs of neighboring stops where `distance[i]` is the distance between the stops number `i` and `(i + 1) % n`.
The bus goes along both directions i.e. clockwise and counterclockwise.
Return the shortest distance betw... | Sort the pickup and dropoff events by location, then process them in order. |
Python distance difference | distance-between-bus-stops | 0 | 1 | \n# Code\n```\nclass Solution:\n def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n if start == destination:\n return 0\n elif start < destination:\n trip = sum(distance[start:destination])\n else:\n trip = sum(distance... | 2 | Given an integer array **sorted** in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.
**Example 1:**
**Input:** arr = \[1,2,2,6,6,6,6,7,10\]
**Output:** 6
**Example 2:**
**Input:** arr = \[1,1\]
**Output:** 1
**Constraints:**
* `1 <= arr... | Find the distance between the two stops if the bus moved in clockwise or counterclockwise directions. |
Python3 O(n) || O(1) # Runtime: 63ms 60.00% Memory: 14.9mb 93.48% | distance-between-bus-stops | 0 | 1 | ```\nclass Solution:\n def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n# O(n) || O(1)\n# Runtime: 63ms 60.00% Memory: 14.9mb 93.48%\n clockWiseDirection = 0\n totalSum = 0\n\n for idx, val in enumerate(distance):\n if start < desti... | 1 | A bus has `n` stops numbered from `0` to `n - 1` that form a circle. We know the distance between all pairs of neighboring stops where `distance[i]` is the distance between the stops number `i` and `(i + 1) % n`.
The bus goes along both directions i.e. clockwise and counterclockwise.
Return the shortest distance betw... | Sort the pickup and dropoff events by location, then process them in order. |
Python3 O(n) || O(1) # Runtime: 63ms 60.00% Memory: 14.9mb 93.48% | distance-between-bus-stops | 0 | 1 | ```\nclass Solution:\n def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n# O(n) || O(1)\n# Runtime: 63ms 60.00% Memory: 14.9mb 93.48%\n clockWiseDirection = 0\n totalSum = 0\n\n for idx, val in enumerate(distance):\n if start < desti... | 1 | Given an integer array **sorted** in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.
**Example 1:**
**Input:** arr = \[1,2,2,6,6,6,6,7,10\]
**Output:** 6
**Example 2:**
**Input:** arr = \[1,1\]
**Output:** 1
**Constraints:**
* `1 <= arr... | Find the distance between the two stops if the bus moved in clockwise or counterclockwise directions. |
My Solution :) | distance-between-bus-stops | 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(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution:\n def distanceBetweenBusStops(self, distance: List[int], start... | 0 | A bus has `n` stops numbered from `0` to `n - 1` that form a circle. We know the distance between all pairs of neighboring stops where `distance[i]` is the distance between the stops number `i` and `(i + 1) % n`.
The bus goes along both directions i.e. clockwise and counterclockwise.
Return the shortest distance betw... | Sort the pickup and dropoff events by location, then process them in order. |
My Solution :) | distance-between-bus-stops | 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(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution:\n def distanceBetweenBusStops(self, distance: List[int], start... | 0 | Given an integer array **sorted** in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.
**Example 1:**
**Input:** arr = \[1,2,2,6,6,6,6,7,10\]
**Output:** 6
**Example 2:**
**Input:** arr = \[1,1\]
**Output:** 1
**Constraints:**
* `1 <= arr... | Find the distance between the two stops if the bus moved in clockwise or counterclockwise directions. |
1 line python beats 97% | day-of-the-week | 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 date, return the corresponding day of the week for that date.
The input is given as three integers representing the `day`, `month` and `year` respectively.
Return the answer as one of the following values `{ "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "}`.
**Example 1:**... | Based on whether A[i-1] < A[i] < A[i+1], A[i-1] < A[i] > A[i+1], or A[i-1] > A[i] > A[i+1], we are either at the left side, peak, or right side of the mountain. We can binary search to find the peak.
After finding the peak, we can binary search two more times to find whether the value occurs on either side of the pea... |
1 line python beats 97% | day-of-the-week | 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 `n x n` integer matrix `grid`, return _the minimum sum of a **falling path with non-zero shifts**_.
A **falling path with non-zero shifts** is a choice of exactly one element from each row of `grid` such that no two elements chosen in adjacent rows are in the same column.
**Example 1:**
**Input:** arr = \[\... | Sum up the number of days for the years before the given year. Handle the case of a leap year. Find the number of days for each month of the given year. |
Python simple one line solution | day-of-the-week | 0 | 1 | **Python :**\n\n```\ndef dayOfTheWeek(self, day: int, month: int, year: int) -> str:\n\treturn date(year, month, day).strftime("%A")\n```\n\n**Like it ? please upvote !** | 21 | Given a date, return the corresponding day of the week for that date.
The input is given as three integers representing the `day`, `month` and `year` respectively.
Return the answer as one of the following values `{ "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "}`.
**Example 1:**... | Based on whether A[i-1] < A[i] < A[i+1], A[i-1] < A[i] > A[i+1], or A[i-1] > A[i] > A[i+1], we are either at the left side, peak, or right side of the mountain. We can binary search to find the peak.
After finding the peak, we can binary search two more times to find whether the value occurs on either side of the pea... |
Python simple one line solution | day-of-the-week | 0 | 1 | **Python :**\n\n```\ndef dayOfTheWeek(self, day: int, month: int, year: int) -> str:\n\treturn date(year, month, day).strftime("%A")\n```\n\n**Like it ? please upvote !** | 21 | Given an `n x n` integer matrix `grid`, return _the minimum sum of a **falling path with non-zero shifts**_.
A **falling path with non-zero shifts** is a choice of exactly one element from each row of `grid` such that no two elements chosen in adjacent rows are in the same column.
**Example 1:**
**Input:** arr = \[\... | Sum up the number of days for the years before the given year. Handle the case of a leap year. Find the number of days for each month of the given year. |
Solution in Python 3 (beats 100.0 %) (one line) | day-of-the-week | 0 | 1 | ```\nfrom datetime import date\n\nclass Solution:\n def dayOfTheWeek(self, d: int, m: int, y: int) -> str:\n \treturn date(y,m,d).strftime("%A")\n\t\t\n\t\t\n- Junaid Mansuri\n(LeetCode ID)@hotmail.com | 10 | Given a date, return the corresponding day of the week for that date.
The input is given as three integers representing the `day`, `month` and `year` respectively.
Return the answer as one of the following values `{ "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "}`.
**Example 1:**... | Based on whether A[i-1] < A[i] < A[i+1], A[i-1] < A[i] > A[i+1], or A[i-1] > A[i] > A[i+1], we are either at the left side, peak, or right side of the mountain. We can binary search to find the peak.
After finding the peak, we can binary search two more times to find whether the value occurs on either side of the pea... |
Solution in Python 3 (beats 100.0 %) (one line) | day-of-the-week | 0 | 1 | ```\nfrom datetime import date\n\nclass Solution:\n def dayOfTheWeek(self, d: int, m: int, y: int) -> str:\n \treturn date(y,m,d).strftime("%A")\n\t\t\n\t\t\n- Junaid Mansuri\n(LeetCode ID)@hotmail.com | 10 | Given an `n x n` integer matrix `grid`, return _the minimum sum of a **falling path with non-zero shifts**_.
A **falling path with non-zero shifts** is a choice of exactly one element from each row of `grid` such that no two elements chosen in adjacent rows are in the same column.
**Example 1:**
**Input:** arr = \[\... | Sum up the number of days for the years before the given year. Handle the case of a leap year. Find the number of days for each month of the given year. |
Python3 simple solution | day-of-the-week | 0 | 1 | ```\nclass Solution:\n def dayOfTheWeek(self, day: int, month: int, year: int) -> str:\n prev_year = year - 1\n days = prev_year * 365 + prev_year // 4 - prev_year // 100 + prev_year // 400\n days += sum([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][:month - 1])\n days += day\n\n ... | 7 | Given a date, return the corresponding day of the week for that date.
The input is given as three integers representing the `day`, `month` and `year` respectively.
Return the answer as one of the following values `{ "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "}`.
**Example 1:**... | Based on whether A[i-1] < A[i] < A[i+1], A[i-1] < A[i] > A[i+1], or A[i-1] > A[i] > A[i+1], we are either at the left side, peak, or right side of the mountain. We can binary search to find the peak.
After finding the peak, we can binary search two more times to find whether the value occurs on either side of the pea... |
Python3 simple solution | day-of-the-week | 0 | 1 | ```\nclass Solution:\n def dayOfTheWeek(self, day: int, month: int, year: int) -> str:\n prev_year = year - 1\n days = prev_year * 365 + prev_year // 4 - prev_year // 100 + prev_year // 400\n days += sum([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][:month - 1])\n days += day\n\n ... | 7 | Given an `n x n` integer matrix `grid`, return _the minimum sum of a **falling path with non-zero shifts**_.
A **falling path with non-zero shifts** is a choice of exactly one element from each row of `grid` such that no two elements chosen in adjacent rows are in the same column.
**Example 1:**
**Input:** arr = \[\... | Sum up the number of days for the years before the given year. Handle the case of a leap year. Find the number of days for each month of the given year. |
Python Simple Logic with easy Steps | day-of-the-week | 0 | 1 | # Intuition\nLet us consider the odd days in the given year. As the test cases renge is from 1970 to 2100 so we can choose a year before 1970 and which comes after leap year. So we can take 1969 which has 3 odd days. Then subtract the given year by 1969 then store it in diff_y which is difference of years. For leap ye... | 1 | Given a date, return the corresponding day of the week for that date.
The input is given as three integers representing the `day`, `month` and `year` respectively.
Return the answer as one of the following values `{ "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "}`.
**Example 1:**... | Based on whether A[i-1] < A[i] < A[i+1], A[i-1] < A[i] > A[i+1], or A[i-1] > A[i] > A[i+1], we are either at the left side, peak, or right side of the mountain. We can binary search to find the peak.
After finding the peak, we can binary search two more times to find whether the value occurs on either side of the pea... |
Python Simple Logic with easy Steps | day-of-the-week | 0 | 1 | # Intuition\nLet us consider the odd days in the given year. As the test cases renge is from 1970 to 2100 so we can choose a year before 1970 and which comes after leap year. So we can take 1969 which has 3 odd days. Then subtract the given year by 1969 then store it in diff_y which is difference of years. For leap ye... | 1 | Given an `n x n` integer matrix `grid`, return _the minimum sum of a **falling path with non-zero shifts**_.
A **falling path with non-zero shifts** is a choice of exactly one element from each row of `grid` such that no two elements chosen in adjacent rows are in the same column.
**Example 1:**
**Input:** arr = \[\... | Sum up the number of days for the years before the given year. Handle the case of a leap year. Find the number of days for each month of the given year. |
One line Python Solution | day-of-the-week | 0 | 1 | # Intuition:\nBy using the Python programming language and its built-in datetime module.\n\n# Approach\ncreates a datetime object using the provided values. The strftime method is then used to format the date as a string, specifying %A as the format code to retrieve the full weekday name. Finally, the function returns ... | 1 | Given a date, return the corresponding day of the week for that date.
The input is given as three integers representing the `day`, `month` and `year` respectively.
Return the answer as one of the following values `{ "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "}`.
**Example 1:**... | Based on whether A[i-1] < A[i] < A[i+1], A[i-1] < A[i] > A[i+1], or A[i-1] > A[i] > A[i+1], we are either at the left side, peak, or right side of the mountain. We can binary search to find the peak.
After finding the peak, we can binary search two more times to find whether the value occurs on either side of the pea... |
One line Python Solution | day-of-the-week | 0 | 1 | # Intuition:\nBy using the Python programming language and its built-in datetime module.\n\n# Approach\ncreates a datetime object using the provided values. The strftime method is then used to format the date as a string, specifying %A as the format code to retrieve the full weekday name. Finally, the function returns ... | 1 | Given an `n x n` integer matrix `grid`, return _the minimum sum of a **falling path with non-zero shifts**_.
A **falling path with non-zero shifts** is a choice of exactly one element from each row of `grid` such that no two elements chosen in adjacent rows are in the same column.
**Example 1:**
**Input:** arr = \[\... | Sum up the number of days for the years before the given year. Handle the case of a leap year. Find the number of days for each month of the given year. |
Easy Python3 Solution | day-of-the-week | 0 | 1 | ```\nfrom datetime import date\n\nclass Solution:\n def dayOfTheWeek(self, day: int, month: int, year: int) -> str:\n \treturn date(year,month ,day).strftime("%A")\n```\nInspired from code written by Junaid Mansuri | 5 | Given a date, return the corresponding day of the week for that date.
The input is given as three integers representing the `day`, `month` and `year` respectively.
Return the answer as one of the following values `{ "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "}`.
**Example 1:**... | Based on whether A[i-1] < A[i] < A[i+1], A[i-1] < A[i] > A[i+1], or A[i-1] > A[i] > A[i+1], we are either at the left side, peak, or right side of the mountain. We can binary search to find the peak.
After finding the peak, we can binary search two more times to find whether the value occurs on either side of the pea... |
Easy Python3 Solution | day-of-the-week | 0 | 1 | ```\nfrom datetime import date\n\nclass Solution:\n def dayOfTheWeek(self, day: int, month: int, year: int) -> str:\n \treturn date(year,month ,day).strftime("%A")\n```\nInspired from code written by Junaid Mansuri | 5 | Given an `n x n` integer matrix `grid`, return _the minimum sum of a **falling path with non-zero shifts**_.
A **falling path with non-zero shifts** is a choice of exactly one element from each row of `grid` such that no two elements chosen in adjacent rows are in the same column.
**Example 1:**
**Input:** arr = \[\... | Sum up the number of days for the years before the given year. Handle the case of a leap year. Find the number of days for each month of the given year. |
Simple & Easy Solution by Python 3 | day-of-the-week | 0 | 1 | ```\nclass Solution:\n def dayOfTheWeek(self, day: int, month: int, year: int) -> str:\n prev_year = year - 1\n days = prev_year * 365 + prev_year // 4 - prev_year // 100 + prev_year // 400\n days += sum([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][:month - 1])\n days += day\n\n ... | 9 | Given a date, return the corresponding day of the week for that date.
The input is given as three integers representing the `day`, `month` and `year` respectively.
Return the answer as one of the following values `{ "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "}`.
**Example 1:**... | Based on whether A[i-1] < A[i] < A[i+1], A[i-1] < A[i] > A[i+1], or A[i-1] > A[i] > A[i+1], we are either at the left side, peak, or right side of the mountain. We can binary search to find the peak.
After finding the peak, we can binary search two more times to find whether the value occurs on either side of the pea... |
Simple & Easy Solution by Python 3 | day-of-the-week | 0 | 1 | ```\nclass Solution:\n def dayOfTheWeek(self, day: int, month: int, year: int) -> str:\n prev_year = year - 1\n days = prev_year * 365 + prev_year // 4 - prev_year // 100 + prev_year // 400\n days += sum([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][:month - 1])\n days += day\n\n ... | 9 | Given an `n x n` integer matrix `grid`, return _the minimum sum of a **falling path with non-zero shifts**_.
A **falling path with non-zero shifts** is a choice of exactly one element from each row of `grid` such that no two elements chosen in adjacent rows are in the same column.
**Example 1:**
**Input:** arr = \[\... | Sum up the number of days for the years before the given year. Handle the case of a leap year. Find the number of days for each month of the given year. |
Simple python solution, ~10 lines, top 99% time & mem. Comments before every line. | maximum-subarray-sum-with-one-deletion | 0 | 1 | # Intuition\n\nA good way to build intuition for this problem is by first solving its simpler version: Maximum Subarray (problem 53 in LeetCode). This will provide an understanding of the basic update set.\n\n# Approach\n\nWhile the current num is non-negative, we simply add it. Otherwise, we required more elaborate up... | 2 | Given an array of integers, return the maximum sum for a **non-empty** subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maxim... | null |
Simple python solution, ~10 lines, top 99% time & mem. Comments before every line. | maximum-subarray-sum-with-one-deletion | 0 | 1 | # Intuition\n\nA good way to build intuition for this problem is by first solving its simpler version: Maximum Subarray (problem 53 in LeetCode). This will provide an understanding of the basic update set.\n\n# Approach\n\nWhile the current num is non-negative, we simply add it. Otherwise, we required more elaborate up... | 2 | Given an array `intervals` where `intervals[i] = [li, ri]` represent the interval `[li, ri)`, remove all intervals that are covered by another interval in the list.
The interval `[a, b)` is covered by the interval `[c, d)` if and only if `c <= a` and `b <= d`.
Return _the number of remaining intervals_.
**Example 1:... | How to solve this problem if no deletions are allowed ? Try deleting each element and find the maximum subarray sum to both sides of that element. To do that efficiently, use the idea of Kadane's algorithm. |
[Python] Kadane's Algorithm easy solution | maximum-subarray-sum-with-one-deletion | 0 | 1 | Keep two array\'s one will have prefix sum ending at that index from start and one will have prefix sum ending at that index from end, using kadane\'s algorithm. For each i these array\'s will denote maximum subarray ending at i-1 and maximum subarray starting at i+1 so when you add these two values it will denote maxi... | 13 | Given an array of integers, return the maximum sum for a **non-empty** subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maxim... | null |
[Python] Kadane's Algorithm easy solution | maximum-subarray-sum-with-one-deletion | 0 | 1 | Keep two array\'s one will have prefix sum ending at that index from start and one will have prefix sum ending at that index from end, using kadane\'s algorithm. For each i these array\'s will denote maximum subarray ending at i-1 and maximum subarray starting at i+1 so when you add these two values it will denote maxi... | 13 | Given an array `intervals` where `intervals[i] = [li, ri]` represent the interval `[li, ri)`, remove all intervals that are covered by another interval in the list.
The interval `[a, b)` is covered by the interval `[c, d)` if and only if `c <= a` and `b <= d`.
Return _the number of remaining intervals_.
**Example 1:... | How to solve this problem if no deletions are allowed ? Try deleting each element and find the maximum subarray sum to both sides of that element. To do that efficiently, use the idea of Kadane's algorithm. |
python3 | DP + memorize | maximum-subarray-sum-with-one-deletion | 0 | 1 | \n# Code\n```\nclass Solution:\n def maximumSum(self, arr: List[int]) -> int:\n @cache\n def dp(i,delete):\n if i==0: return arr[i]\n if delete==0: return max(dp(i-1,delete)+arr[i],arr[i])\n return max(dp(i-1,delete)+arr[i],dp(i-1,delete-1),arr[i])\n ans = -float... | 1 | Given an array of integers, return the maximum sum for a **non-empty** subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maxim... | null |
python3 | DP + memorize | maximum-subarray-sum-with-one-deletion | 0 | 1 | \n# Code\n```\nclass Solution:\n def maximumSum(self, arr: List[int]) -> int:\n @cache\n def dp(i,delete):\n if i==0: return arr[i]\n if delete==0: return max(dp(i-1,delete)+arr[i],arr[i])\n return max(dp(i-1,delete)+arr[i],dp(i-1,delete-1),arr[i])\n ans = -float... | 1 | Given an array `intervals` where `intervals[i] = [li, ri]` represent the interval `[li, ri)`, remove all intervals that are covered by another interval in the list.
The interval `[a, b)` is covered by the interval `[c, d)` if and only if `c <= a` and `b <= d`.
Return _the number of remaining intervals_.
**Example 1:... | How to solve this problem if no deletions are allowed ? Try deleting each element and find the maximum subarray sum to both sides of that element. To do that efficiently, use the idea of Kadane's algorithm. |
| Simple 5 line | Kadane's Algorithm | Beats 91%, 82% | | maximum-subarray-sum-with-one-deletion | 0 | 1 | # Intuition\nSimple solution using the idea of Kadane\'s Algorithm. This solution is built on top of [53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) question\'s solution using Kadane\'s Algorithm. \n \n---\n\n# Approach\nThe basic idea of Kadane\'s algorithm for solving Maximum Subarray problem ... | 0 | Given an array of integers, return the maximum sum for a **non-empty** subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maxim... | null |
| Simple 5 line | Kadane's Algorithm | Beats 91%, 82% | | maximum-subarray-sum-with-one-deletion | 0 | 1 | # Intuition\nSimple solution using the idea of Kadane\'s Algorithm. This solution is built on top of [53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) question\'s solution using Kadane\'s Algorithm. \n \n---\n\n# Approach\nThe basic idea of Kadane\'s algorithm for solving Maximum Subarray problem ... | 0 | Given an array `intervals` where `intervals[i] = [li, ri]` represent the interval `[li, ri)`, remove all intervals that are covered by another interval in the list.
The interval `[a, b)` is covered by the interval `[c, d)` if and only if `c <= a` and `b <= d`.
Return _the number of remaining intervals_.
**Example 1:... | How to solve this problem if no deletions are allowed ? Try deleting each element and find the maximum subarray sum to both sides of that element. To do that efficiently, use the idea of Kadane's algorithm. |
Python 3: TC O(N) SC O(1) DP | maximum-subarray-sum-with-one-deletion | 0 | 1 | # Intuition\n\n## Zero Deletion "DP"\n\nThe classic solution to this problem with 0 deletions is to track a window sum to the left that includes the current element.\n\nIf the sum is positive, see if it\'s the new best.\n\nIf the sum is 0 or negative, then the cumulative sum is 0.\n\n## One Deletion DP\n\nSo I thought ... | 0 | Given an array of integers, return the maximum sum for a **non-empty** subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maxim... | null |
Python 3: TC O(N) SC O(1) DP | maximum-subarray-sum-with-one-deletion | 0 | 1 | # Intuition\n\n## Zero Deletion "DP"\n\nThe classic solution to this problem with 0 deletions is to track a window sum to the left that includes the current element.\n\nIf the sum is positive, see if it\'s the new best.\n\nIf the sum is 0 or negative, then the cumulative sum is 0.\n\n## One Deletion DP\n\nSo I thought ... | 0 | Given an array `intervals` where `intervals[i] = [li, ri]` represent the interval `[li, ri)`, remove all intervals that are covered by another interval in the list.
The interval `[a, b)` is covered by the interval `[c, d)` if and only if `c <= a` and `b <= d`.
Return _the number of remaining intervals_.
**Example 1:... | How to solve this problem if no deletions are allowed ? Try deleting each element and find the maximum subarray sum to both sides of that element. To do that efficiently, use the idea of Kadane's algorithm. |
commented kadane solution. | maximum-subarray-sum-with-one-deletion | 0 | 1 | \n- Time complexity:\nO(N)\n- Space complexity:\nO(N)\n\n# Code\n```\nclass Solution:\n def maximumSum(self, arr: List[int]) -> int:\n if len(arr) == 1 : \n # if only one element, just return it \n return arr[0]\n\n l = len(arr)\n # kadane from left and right \n # left[i... | 0 | Given an array of integers, return the maximum sum for a **non-empty** subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maxim... | null |
commented kadane solution. | maximum-subarray-sum-with-one-deletion | 0 | 1 | \n- Time complexity:\nO(N)\n- Space complexity:\nO(N)\n\n# Code\n```\nclass Solution:\n def maximumSum(self, arr: List[int]) -> int:\n if len(arr) == 1 : \n # if only one element, just return it \n return arr[0]\n\n l = len(arr)\n # kadane from left and right \n # left[i... | 0 | Given an array `intervals` where `intervals[i] = [li, ri]` represent the interval `[li, ri)`, remove all intervals that are covered by another interval in the list.
The interval `[a, b)` is covered by the interval `[c, d)` if and only if `c <= a` and `b <= d`.
Return _the number of remaining intervals_.
**Example 1:... | How to solve this problem if no deletions are allowed ? Try deleting each element and find the maximum subarray sum to both sides of that element. To do that efficiently, use the idea of Kadane's algorithm. |
Python Solution | maximum-subarray-sum-with-one-deletion | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\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, ... | 0 | Given an array of integers, return the maximum sum for a **non-empty** subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maxim... | null |
Python Solution | maximum-subarray-sum-with-one-deletion | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\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, ... | 0 | Given an array `intervals` where `intervals[i] = [li, ri]` represent the interval `[li, ri)`, remove all intervals that are covered by another interval in the list.
The interval `[a, b)` is covered by the interval `[c, d)` if and only if `c <= a` and `b <= d`.
Return _the number of remaining intervals_.
**Example 1:... | How to solve this problem if no deletions are allowed ? Try deleting each element and find the maximum subarray sum to both sides of that element. To do that efficiently, use the idea of Kadane's algorithm. |
python3 Solution | make-array-strictly-increasing | 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 two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
python3 Solution | make-array-strictly-increasing | 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 `head` which is a reference node to a singly-linked list. The value of each node in the linked list is either `0` or `1`. The linked list holds the binary representation of a number.
Return the _decimal value_ of the number in the linked list.
The **most significant bit** is at the head of the linked list.
**E... | Use dynamic programming. The state would be the index in arr1 and the index of the previous element in arr2 after sorting it and removing duplicates. |
Fastest Solution Yet | make-array-strictly-increasing | 0 | 1 | # Approach\n\nThe code first imports the `bisect` module, which provides a number of functions for working with sorted lists. The `arr2` array is then sorted and the unique elements are extracted using the `set()` function. The lengths of the two arrays are then stored in the variables `n1` and `n2`.\n\nA dictionary `d... | 2 | Given two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
Fastest Solution Yet | make-array-strictly-increasing | 0 | 1 | # Approach\n\nThe code first imports the `bisect` module, which provides a number of functions for working with sorted lists. The `arr2` array is then sorted and the unique elements are extracted using the `set()` function. The lengths of the two arrays are then stored in the variables `n1` and `n2`.\n\nA dictionary `d... | 2 | Given `head` which is a reference node to a singly-linked list. The value of each node in the linked list is either `0` or `1`. The linked list holds the binary representation of a number.
Return the _decimal value_ of the number in the linked list.
The **most significant bit** is at the head of the linked list.
**E... | Use dynamic programming. The state would be the index in arr1 and the index of the previous element in arr2 after sorting it and removing duplicates. |
✅ 99% Clear code | Full Explanation | Similar idea from the hint | make-array-strictly-increasing | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo make `arr1` strictly increasing, we need to consider two options for each element in `arr1`:\n1. Keep the current value from `arr1`.\n2. Replace the current value from `arr1` with a value from `arr2`.\n\nThe goal is to find the minimum... | 1 | Given two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
✅ 99% Clear code | Full Explanation | Similar idea from the hint | make-array-strictly-increasing | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo make `arr1` strictly increasing, we need to consider two options for each element in `arr1`:\n1. Keep the current value from `arr1`.\n2. Replace the current value from `arr1` with a value from `arr2`.\n\nThe goal is to find the minimum... | 1 | Given `head` which is a reference node to a singly-linked list. The value of each node in the linked list is either `0` or `1`. The linked list holds the binary representation of a number.
Return the _decimal value_ of the number in the linked list.
The **most significant bit** is at the head of the linked list.
**E... | Use dynamic programming. The state would be the index in arr1 and the index of the previous element in arr2 after sorting it and removing duplicates. |
[Pyhton3] [3D DP] top down/ memoization | make-array-strictly-increasing | 0 | 1 | # Intuition\n3 dimensional dp, one dimension for arr1 pointer, one for arr2 pointer and one is used a flag for previous element if it\'s from arr1 or arr2\n\n# Approach\nTop down memoization approach, dp(a,b,c): a for indicating the index we traversed for arr1 and b for arr2 and c for flagging if the previous element i... | 1 | Given two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
[Pyhton3] [3D DP] top down/ memoization | make-array-strictly-increasing | 0 | 1 | # Intuition\n3 dimensional dp, one dimension for arr1 pointer, one for arr2 pointer and one is used a flag for previous element if it\'s from arr1 or arr2\n\n# Approach\nTop down memoization approach, dp(a,b,c): a for indicating the index we traversed for arr1 and b for arr2 and c for flagging if the previous element i... | 1 | Given `head` which is a reference node to a singly-linked list. The value of each node in the linked list is either `0` or `1`. The linked list holds the binary representation of a number.
Return the _decimal value_ of the number in the linked list.
The **most significant bit** is at the head of the linked list.
**E... | Use dynamic programming. The state would be the index in arr1 and the index of the previous element in arr2 after sorting it and removing duplicates. |
Code simpler than Editorial : With Explanation 😎✌🏼 | make-array-strictly-increasing | 0 | 1 | # Code\n```\nfrom typing import List\nimport bisect\n\nclass Solution:\n def makeArrayIncreasing(self, arr1: List[int], arr2: List[int]) -> int:\n # Sort arr2 in ascending order\n arr2.sort()\n\n n = len(arr1)\n INF = float(\'inf\')\n\n # Initialize a DP table with dimensions (n+1)... | 2 | Given two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
Code simpler than Editorial : With Explanation 😎✌🏼 | make-array-strictly-increasing | 0 | 1 | # Code\n```\nfrom typing import List\nimport bisect\n\nclass Solution:\n def makeArrayIncreasing(self, arr1: List[int], arr2: List[int]) -> int:\n # Sort arr2 in ascending order\n arr2.sort()\n\n n = len(arr1)\n INF = float(\'inf\')\n\n # Initialize a DP table with dimensions (n+1)... | 2 | Given `head` which is a reference node to a singly-linked list. The value of each node in the linked list is either `0` or `1`. The linked list holds the binary representation of a number.
Return the _decimal value_ of the number in the linked list.
The **most significant bit** is at the head of the linked list.
**E... | Use dynamic programming. The state would be the index in arr1 and the index of the previous element in arr2 after sorting it and removing duplicates. |
Concise Code w/ Explanation | make-array-strictly-increasing | 0 | 1 | # Intuition\nOur inputs are `arr1` and `arr2`. We can treat (`arr1[i:]`, `arr2`) as subproblems, for index `0 <= i <= len(arr1)`.\n\nFor each index `i`, we can choose to do one of the following:\n* Replace `arr1[i]` with the first value in `arr2` that is strictly greater than `arr1[i - 1]` (if such value in `arr2` exis... | 5 | Given two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
Concise Code w/ Explanation | make-array-strictly-increasing | 0 | 1 | # Intuition\nOur inputs are `arr1` and `arr2`. We can treat (`arr1[i:]`, `arr2`) as subproblems, for index `0 <= i <= len(arr1)`.\n\nFor each index `i`, we can choose to do one of the following:\n* Replace `arr1[i]` with the first value in `arr2` that is strictly greater than `arr1[i - 1]` (if such value in `arr2` exis... | 5 | Given `head` which is a reference node to a singly-linked list. The value of each node in the linked list is either `0` or `1`. The linked list holds the binary representation of a number.
Return the _decimal value_ of the number in the linked list.
The **most significant bit** is at the head of the linked list.
**E... | Use dynamic programming. The state would be the index in arr1 and the index of the previous element in arr2 after sorting it and removing duplicates. |
using Counter() | maximum-number-of-balloons | 0 | 1 | # Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution:\n def maxNumberOfBalloons(self, text: str) -> int:\n CountText = Counter(text)\n balloon = Counter("balloon")\n\n res = len(text)\n for c in balloon:\n res = min(res,CountText[c]//... | 1 | Given a string `text`, you want to use the characters of `text` to form as many instances of the word **"balloon "** as possible.
You can use each character in `text` **at most once**. Return the maximum number of instances that can be formed.
**Example 1:**
**Input:** text = "nlaebolko "
**Output:** 1
**Example 2... | Try to find the number of binary digits returned by the function. The pattern is to start counting from zero after determining the number of binary digits. |
using Counter() | maximum-number-of-balloons | 0 | 1 | # Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution:\n def maxNumberOfBalloons(self, text: str) -> int:\n CountText = Counter(text)\n balloon = Counter("balloon")\n\n res = len(text)\n for c in balloon:\n res = min(res,CountText[c]//... | 1 | Given a string `s`, return the maximum number of ocurrences of **any** substring under the following rules:
* The number of unique characters in the substring must be less than or equal to `maxLetters`.
* The substring size must be between `minSize` and `maxSize` inclusive.
**Example 1:**
**Input:** s = "aababc... | Count the frequency of letters in the given string. Find the letter than can make the minimum number of instances of the word "balloon". |
Python use Counter 2 lines (my first solution up plz :)) | maximum-number-of-balloons | 0 | 1 | # Approach\nUse Counter and don\'t worry about it\n\n# Complexity\n- Time complexity: O(n)\n\n# Code\n```\nfrom collections import Counter\n\nclass Solution:\n def maxNumberOfBalloons(self, text: str) -> int:\n cnt = Counter(text)\n return min([cnt[\'b\'], cnt[\'a\'], cnt[\'n\'], cnt[\'l\'] // 2, cnt[\... | 7 | Given a string `text`, you want to use the characters of `text` to form as many instances of the word **"balloon "** as possible.
You can use each character in `text` **at most once**. Return the maximum number of instances that can be formed.
**Example 1:**
**Input:** text = "nlaebolko "
**Output:** 1
**Example 2... | Try to find the number of binary digits returned by the function. The pattern is to start counting from zero after determining the number of binary digits. |
Python use Counter 2 lines (my first solution up plz :)) | maximum-number-of-balloons | 0 | 1 | # Approach\nUse Counter and don\'t worry about it\n\n# Complexity\n- Time complexity: O(n)\n\n# Code\n```\nfrom collections import Counter\n\nclass Solution:\n def maxNumberOfBalloons(self, text: str) -> int:\n cnt = Counter(text)\n return min([cnt[\'b\'], cnt[\'a\'], cnt[\'n\'], cnt[\'l\'] // 2, cnt[\... | 7 | Given a string `s`, return the maximum number of ocurrences of **any** substring under the following rules:
* The number of unique characters in the substring must be less than or equal to `maxLetters`.
* The substring size must be between `minSize` and `maxSize` inclusive.
**Example 1:**
**Input:** s = "aababc... | Count the frequency of letters in the given string. Find the letter than can make the minimum number of instances of the word "balloon". |
Easiest Python O(n) Solution O(1) space | maximum-number-of-balloons | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI saw multiple solutions implementing hash map here, which was also the first thing I thought about but came to think of an easier way to do this. The main thing here is to realise is that you can create the word "balloon" only as along a... | 13 | Given a string `text`, you want to use the characters of `text` to form as many instances of the word **"balloon "** as possible.
You can use each character in `text` **at most once**. Return the maximum number of instances that can be formed.
**Example 1:**
**Input:** text = "nlaebolko "
**Output:** 1
**Example 2... | Try to find the number of binary digits returned by the function. The pattern is to start counting from zero after determining the number of binary digits. |
Easiest Python O(n) Solution O(1) space | maximum-number-of-balloons | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI saw multiple solutions implementing hash map here, which was also the first thing I thought about but came to think of an easier way to do this. The main thing here is to realise is that you can create the word "balloon" only as along a... | 13 | Given a string `s`, return the maximum number of ocurrences of **any** substring under the following rules:
* The number of unique characters in the substring must be less than or equal to `maxLetters`.
* The substring size must be between `minSize` and `maxSize` inclusive.
**Example 1:**
**Input:** s = "aababc... | Count the frequency of letters in the given string. Find the letter than can make the minimum number of instances of the word "balloon". |
Python Solution using Counter() | maximum-number-of-balloons | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def maxNumberOfBalloons(self, text: str) -> int:\n ctrText = Counter(text)\n cntB, c... | 1 | Given a string `text`, you want to use the characters of `text` to form as many instances of the word **"balloon "** as possible.
You can use each character in `text` **at most once**. Return the maximum number of instances that can be formed.
**Example 1:**
**Input:** text = "nlaebolko "
**Output:** 1
**Example 2... | Try to find the number of binary digits returned by the function. The pattern is to start counting from zero after determining the number of binary digits. |
Python Solution using Counter() | maximum-number-of-balloons | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def maxNumberOfBalloons(self, text: str) -> int:\n ctrText = Counter(text)\n cntB, c... | 1 | Given a string `s`, return the maximum number of ocurrences of **any** substring under the following rules:
* The number of unique characters in the substring must be less than or equal to `maxLetters`.
* The substring size must be between `minSize` and `maxSize` inclusive.
**Example 1:**
**Input:** s = "aababc... | Count the frequency of letters in the given string. Find the letter than can make the minimum number of instances of the word "balloon". |
Runtime: 100% | Memory Usage: 100% | One-Line [Python3] | maximum-number-of-balloons | 0 | 1 | ```\nclass Solution:\n def maxNumberOfBalloons(self, text: str) -> int:\n return min(text.count(\'b\'), text.count(\'a\'), text.count(\'l\') // 2, text.count(\'o\') // 2, text.count(\'n\'))\n``` | 31 | Given a string `text`, you want to use the characters of `text` to form as many instances of the word **"balloon "** as possible.
You can use each character in `text` **at most once**. Return the maximum number of instances that can be formed.
**Example 1:**
**Input:** text = "nlaebolko "
**Output:** 1
**Example 2... | Try to find the number of binary digits returned by the function. The pattern is to start counting from zero after determining the number of binary digits. |
Runtime: 100% | Memory Usage: 100% | One-Line [Python3] | maximum-number-of-balloons | 0 | 1 | ```\nclass Solution:\n def maxNumberOfBalloons(self, text: str) -> int:\n return min(text.count(\'b\'), text.count(\'a\'), text.count(\'l\') // 2, text.count(\'o\') // 2, text.count(\'n\'))\n``` | 31 | Given a string `s`, return the maximum number of ocurrences of **any** substring under the following rules:
* The number of unique characters in the substring must be less than or equal to `maxLetters`.
* The substring size must be between `minSize` and `maxSize` inclusive.
**Example 1:**
**Input:** s = "aababc... | Count the frequency of letters in the given string. Find the letter than can make the minimum number of instances of the word "balloon". |
[Python3] 1-liner | maximum-number-of-balloons | 0 | 1 | ```\nclass Solution:\n def maxNumberOfBalloons(self, t: str) -> int:\n return min(t.count(\'b\'), t.count(\'a\'), t.count(\'l\') // 2, t.count(\'o\') // 2, t.count(\'n\'))\n```\n* For fun\n```\nclass Solution:\n def maxNumberOfBalloons(self, t: str) -> int:\n return min(t.count(c) // int(cnt) for c,... | 26 | Given a string `text`, you want to use the characters of `text` to form as many instances of the word **"balloon "** as possible.
You can use each character in `text` **at most once**. Return the maximum number of instances that can be formed.
**Example 1:**
**Input:** text = "nlaebolko "
**Output:** 1
**Example 2... | Try to find the number of binary digits returned by the function. The pattern is to start counting from zero after determining the number of binary digits. |
[Python3] 1-liner | maximum-number-of-balloons | 0 | 1 | ```\nclass Solution:\n def maxNumberOfBalloons(self, t: str) -> int:\n return min(t.count(\'b\'), t.count(\'a\'), t.count(\'l\') // 2, t.count(\'o\') // 2, t.count(\'n\'))\n```\n* For fun\n```\nclass Solution:\n def maxNumberOfBalloons(self, t: str) -> int:\n return min(t.count(c) // int(cnt) for c,... | 26 | Given a string `s`, return the maximum number of ocurrences of **any** substring under the following rules:
* The number of unique characters in the substring must be less than or equal to `maxLetters`.
* The substring size must be between `minSize` and `maxSize` inclusive.
**Example 1:**
**Input:** s = "aababc... | Count the frequency of letters in the given string. Find the letter than can make the minimum number of instances of the word "balloon". |
PYTHON SOL | RECURSION AND STACK SOL | DETAILED EXPLANATION WITH PICTRUE | | reverse-substrings-between-each-pair-of-parentheses | 0 | 1 | # PICTURE EXPLANATION\n\n\n\n\n\n\n# RECURSION BASED CODE\n```\nclass Solution:\n def reverseParentheses(self, s: str) -> str:\n def solve(string):\n n = len(string)\n word = ""\... | 6 | You are given a string `s` that consists of lower case English letters and brackets.
Reverse the strings in each pair of matching parentheses, starting from the innermost one.
Your result should **not** contain any brackets.
**Example 1:**
**Input:** s = "(abcd) "
**Output:** "dcba "
**Example 2:**
**Input:** s... | Try to model the problem as a graph problem. The given graph is a tree. The problem is reduced to finding the lowest common ancestor of two nodes in a tree. |
PYTHON SOL | RECURSION AND STACK SOL | DETAILED EXPLANATION WITH PICTRUE | | reverse-substrings-between-each-pair-of-parentheses | 0 | 1 | # PICTURE EXPLANATION\n\n\n\n\n\n\n# RECURSION BASED CODE\n```\nclass Solution:\n def reverseParentheses(self, s: str) -> str:\n def solve(string):\n n = len(string)\n word = ""\... | 6 | You have `n` boxes labeled from `0` to `n - 1`. You are given four arrays: `status`, `candies`, `keys`, and `containedBoxes` where:
* `status[i]` is `1` if the `ith` box is open and `0` if the `ith` box is closed,
* `candies[i]` is the number of candies in the `ith` box,
* `keys[i]` is a list of the labels of th... | Find all brackets in the string. Does the order of the reverse matter ? The order does not matter. |
[Python3] Kadane Algorithm | k-concatenation-maximum-sum | 0 | 1 | # Intuition\nLongest continuos sum of sub-array containing negative number -> Kadane algorithm\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe can iterate all over the arr `k` times and do kadane in it. It will work perfectly but not smart enough to pass all test cases. You will m... | 3 | Given an integer array `arr` and an integer `k`, modify the array by repeating it `k` times.
For example, if `arr = [1, 2]` and `k = 3` then the modified array will be `[1, 2, 1, 2, 1, 2]`.
Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be `0` and its sum in that cas... | Find all synonymous groups of words. Use union-find data structure. By backtracking, generate all possible statements. |
[Python3] Kadane Algorithm | k-concatenation-maximum-sum | 0 | 1 | # Intuition\nLongest continuos sum of sub-array containing negative number -> Kadane algorithm\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe can iterate all over the arr `k` times and do kadane in it. It will work perfectly but not smart enough to pass all test cases. You will m... | 3 | Given an array `arr`, replace every element in that array with the greatest element among the elements to its right, and replace the last element with `-1`.
After doing so, return the array.
**Example 1:**
**Input:** arr = \[17,18,5,4,6,1\]
**Output:** \[18,6,6,6,1,-1\]
**Explanation:**
- index 0 --> the greatest e... | How to solve the problem for k=1 ? Use Kadane's algorithm for k=1. What are the possible cases for the answer ? The answer is the maximum between, the answer for k=1, the sum of the whole array multiplied by k, or the maximum suffix sum plus the maximum prefix sum plus (k-2) multiplied by the whole array sum for k > 1. |
Python 3 || 2-7 lines, two versions, w/ example || T/M: 99% / 12% | k-concatenation-maximum-sum | 0 | 1 | \nSame algorithm used by most of the other posted solutions.\n```\nclass Solution:\n def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:\n\n if max(arr)<= 0 : return 0 # arr = [-5,4,7,-2]\n sm, ct, mx, = sum(arr), 0, 0 # sm = 4\n ... | 5 | Given an integer array `arr` and an integer `k`, modify the array by repeating it `k` times.
For example, if `arr = [1, 2]` and `k = 3` then the modified array will be `[1, 2, 1, 2, 1, 2]`.
Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be `0` and its sum in that cas... | Find all synonymous groups of words. Use union-find data structure. By backtracking, generate all possible statements. |
Python 3 || 2-7 lines, two versions, w/ example || T/M: 99% / 12% | k-concatenation-maximum-sum | 0 | 1 | \nSame algorithm used by most of the other posted solutions.\n```\nclass Solution:\n def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:\n\n if max(arr)<= 0 : return 0 # arr = [-5,4,7,-2]\n sm, ct, mx, = sum(arr), 0, 0 # sm = 4\n ... | 5 | Given an array `arr`, replace every element in that array with the greatest element among the elements to its right, and replace the last element with `-1`.
After doing so, return the array.
**Example 1:**
**Input:** arr = \[17,18,5,4,6,1\]
**Output:** \[18,6,6,6,1,-1\]
**Explanation:**
- index 0 --> the greatest e... | How to solve the problem for k=1 ? Use Kadane's algorithm for k=1. What are the possible cases for the answer ? The answer is the maximum between, the answer for k=1, the sum of the whole array multiplied by k, or the maximum suffix sum plus the maximum prefix sum plus (k-2) multiplied by the whole array sum for k > 1. |
A short and sweet Python solution with a not so short and not so sweet explanation | k-concatenation-maximum-sum | 0 | 1 | ```python\nfrom math import inf as oo\n\n\nclass Solution:\n def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:\n total = sum(arr)\n kadanes = self.maxSubArraySum(arr)\n if (k < 2): return kadanes%(10**9+7)\n if (total > 0): return (kadanes + (k-1)*total)%(10**9+7)\n st... | 26 | Given an integer array `arr` and an integer `k`, modify the array by repeating it `k` times.
For example, if `arr = [1, 2]` and `k = 3` then the modified array will be `[1, 2, 1, 2, 1, 2]`.
Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be `0` and its sum in that cas... | Find all synonymous groups of words. Use union-find data structure. By backtracking, generate all possible statements. |
A short and sweet Python solution with a not so short and not so sweet explanation | k-concatenation-maximum-sum | 0 | 1 | ```python\nfrom math import inf as oo\n\n\nclass Solution:\n def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:\n total = sum(arr)\n kadanes = self.maxSubArraySum(arr)\n if (k < 2): return kadanes%(10**9+7)\n if (total > 0): return (kadanes + (k-1)*total)%(10**9+7)\n st... | 26 | Given an array `arr`, replace every element in that array with the greatest element among the elements to its right, and replace the last element with `-1`.
After doing so, return the array.
**Example 1:**
**Input:** arr = \[17,18,5,4,6,1\]
**Output:** \[18,6,6,6,1,-1\]
**Explanation:**
- index 0 --> the greatest e... | How to solve the problem for k=1 ? Use Kadane's algorithm for k=1. What are the possible cases for the answer ? The answer is the maximum between, the answer for k=1, the sum of the whole array multiplied by k, or the maximum suffix sum plus the maximum prefix sum plus (k-2) multiplied by the whole array sum for k > 1. |
Python 1-Liner Solution | k-concatenation-maximum-sum | 0 | 1 | # Code\n```\n\nclass Solution:\n def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:\n return (max(list(accumulate(chain(*[arr]*(2-(k==1))),\n lambda x,y: max(0,x+y), initial = 0))) + (k-2)*sum(arr)*(sum(arr)>0)*(k>1))%1000000007\n``` | 1 | Given an integer array `arr` and an integer `k`, modify the array by repeating it `k` times.
For example, if `arr = [1, 2]` and `k = 3` then the modified array will be `[1, 2, 1, 2, 1, 2]`.
Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be `0` and its sum in that cas... | Find all synonymous groups of words. Use union-find data structure. By backtracking, generate all possible statements. |
Python 1-Liner Solution | k-concatenation-maximum-sum | 0 | 1 | # Code\n```\n\nclass Solution:\n def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:\n return (max(list(accumulate(chain(*[arr]*(2-(k==1))),\n lambda x,y: max(0,x+y), initial = 0))) + (k-2)*sum(arr)*(sum(arr)>0)*(k>1))%1000000007\n``` | 1 | Given an array `arr`, replace every element in that array with the greatest element among the elements to its right, and replace the last element with `-1`.
After doing so, return the array.
**Example 1:**
**Input:** arr = \[17,18,5,4,6,1\]
**Output:** \[18,6,6,6,1,-1\]
**Explanation:**
- index 0 --> the greatest e... | How to solve the problem for k=1 ? Use Kadane's algorithm for k=1. What are the possible cases for the answer ? The answer is the maximum between, the answer for k=1, the sum of the whole array multiplied by k, or the maximum suffix sum plus the maximum prefix sum plus (k-2) multiplied by the whole array sum for k > 1. |
python 3 || Kadane's Algorithim || O(n)/O(1) | k-concatenation-maximum-sum | 0 | 1 | ```\nclass Solution:\n def kConcatenationMaxSum(self, nums: List[int], k: int) -> int:\n def maxSum(k):\n res = cur = 0\n for _ in range(k):\n for num in nums:\n cur = max(cur + num, num)\n res = max(res, cur)\n \n ... | 0 | Given an integer array `arr` and an integer `k`, modify the array by repeating it `k` times.
For example, if `arr = [1, 2]` and `k = 3` then the modified array will be `[1, 2, 1, 2, 1, 2]`.
Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be `0` and its sum in that cas... | Find all synonymous groups of words. Use union-find data structure. By backtracking, generate all possible statements. |
python 3 || Kadane's Algorithim || O(n)/O(1) | k-concatenation-maximum-sum | 0 | 1 | ```\nclass Solution:\n def kConcatenationMaxSum(self, nums: List[int], k: int) -> int:\n def maxSum(k):\n res = cur = 0\n for _ in range(k):\n for num in nums:\n cur = max(cur + num, num)\n res = max(res, cur)\n \n ... | 0 | Given an array `arr`, replace every element in that array with the greatest element among the elements to its right, and replace the last element with `-1`.
After doing so, return the array.
**Example 1:**
**Input:** arr = \[17,18,5,4,6,1\]
**Output:** \[18,6,6,6,1,-1\]
**Explanation:**
- index 0 --> the greatest e... | How to solve the problem for k=1 ? Use Kadane's algorithm for k=1. What are the possible cases for the answer ? The answer is the maximum between, the answer for k=1, the sum of the whole array multiplied by k, or the maximum suffix sum plus the maximum prefix sum plus (k-2) multiplied by the whole array sum for k > 1. |
Consider all cases, with kadane for the basic case | k-concatenation-maximum-sum | 0 | 1 | # Code\n```\nclass Solution:\n def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:\n max_so_far = 0\n max_ending_here = 0\n maxpre = 0\n currpre = 0\n for a in arr:\n currpre += a\n maxpre = max(currpre,maxpre)\n max_ending_here += a\n ... | 0 | Given an integer array `arr` and an integer `k`, modify the array by repeating it `k` times.
For example, if `arr = [1, 2]` and `k = 3` then the modified array will be `[1, 2, 1, 2, 1, 2]`.
Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be `0` and its sum in that cas... | Find all synonymous groups of words. Use union-find data structure. By backtracking, generate all possible statements. |
Consider all cases, with kadane for the basic case | k-concatenation-maximum-sum | 0 | 1 | # Code\n```\nclass Solution:\n def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:\n max_so_far = 0\n max_ending_here = 0\n maxpre = 0\n currpre = 0\n for a in arr:\n currpre += a\n maxpre = max(currpre,maxpre)\n max_ending_here += a\n ... | 0 | Given an array `arr`, replace every element in that array with the greatest element among the elements to its right, and replace the last element with `-1`.
After doing so, return the array.
**Example 1:**
**Input:** arr = \[17,18,5,4,6,1\]
**Output:** \[18,6,6,6,1,-1\]
**Explanation:**
- index 0 --> the greatest e... | How to solve the problem for k=1 ? Use Kadane's algorithm for k=1. What are the possible cases for the answer ? The answer is the maximum between, the answer for k=1, the sum of the whole array multiplied by k, or the maximum suffix sum plus the maximum prefix sum plus (k-2) multiplied by the whole array sum for k > 1. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.