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
Solution
grid-illumination
1
1
```C++ []\nclass Solution {\npublic:\n vector<int> gridIllumination(int n, vector<vector<int>>& l, vector<vector<int>>& q) {\n unordered_map<int,int>lx,ly,ld,rd;\n vector<int>a;\n vector<vector<int>>dir;\n unordered_set<long>lamp;\n for(auto &i:l){\n long s=(long)i[0]*n+...
1
There is a 2D `grid` of size `n x n` where each cell of this grid has a lamp that is initially **turned off**. You are given a 2D array of lamp positions `lamps`, where `lamps[i] = [rowi, coli]` indicates that the lamp at `grid[rowi][coli]` is **turned on**. Even if the same lamp is listed more than once, it is turned...
null
Solution
grid-illumination
1
1
```C++ []\nclass Solution {\npublic:\n vector<int> gridIllumination(int n, vector<vector<int>>& l, vector<vector<int>>& q) {\n unordered_map<int,int>lx,ly,ld,rd;\n vector<int>a;\n vector<vector<int>>dir;\n unordered_set<long>lamp;\n for(auto &i:l){\n long s=(long)i[0]*n+...
1
Given an integer array `arr`, partition the array into (contiguous) subarrays of length **at most** `k`. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return _the largest sum of the given array after partitioning. Test cases are generated so that the answer fi...
null
Python3 solution | Hashmap | Explained
grid-illumination
0
1
\'\'\'\n1. The idea is to store every lamp light in 4 hashmaps: for row, column, p diag, s diag (every direction of light) and store the lamps position in a set (cuz we need to access them quickly and also, they may repeat themselves)\n2. Check for light iterating queries (if there is light then we have to check for la...
1
There is a 2D `grid` of size `n x n` where each cell of this grid has a lamp that is initially **turned off**. You are given a 2D array of lamp positions `lamps`, where `lamps[i] = [rowi, coli]` indicates that the lamp at `grid[rowi][coli]` is **turned on**. Even if the same lamp is listed more than once, it is turned...
null
Python3 solution | Hashmap | Explained
grid-illumination
0
1
\'\'\'\n1. The idea is to store every lamp light in 4 hashmaps: for row, column, p diag, s diag (every direction of light) and store the lamps position in a set (cuz we need to access them quickly and also, they may repeat themselves)\n2. Check for light iterating queries (if there is light then we have to check for la...
1
Given an integer array `arr`, partition the array into (contiguous) subarrays of length **at most** `k`. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return _the largest sum of the given array after partitioning. Test cases are generated so that the answer fi...
null
Python Easy to Understand O(L+Q) Solution
grid-illumination
0
1
# Code\n``` python3 \nclass Solution:\n def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:\n on = set()\n onv = Counter() # number of turned-on-lights in | direction at given index\n onh = Counter() # number of turned-on-lights in - direction at ...
0
There is a 2D `grid` of size `n x n` where each cell of this grid has a lamp that is initially **turned off**. You are given a 2D array of lamp positions `lamps`, where `lamps[i] = [rowi, coli]` indicates that the lamp at `grid[rowi][coli]` is **turned on**. Even if the same lamp is listed more than once, it is turned...
null
Python Easy to Understand O(L+Q) Solution
grid-illumination
0
1
# Code\n``` python3 \nclass Solution:\n def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:\n on = set()\n onv = Counter() # number of turned-on-lights in | direction at given index\n onh = Counter() # number of turned-on-lights in - direction at ...
0
Given an integer array `arr`, partition the array into (contiguous) subarrays of length **at most** `k`. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return _the largest sum of the given array after partitioning. Test cases are generated so that the answer fi...
null
Python 4 Hash Maps, O(n)
grid-illumination
0
1
# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution:\n def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:\n on = set()\n yOn = {}\n xOn = {}\n dy = {}\n dx = {}\n\n res = []\n\n ...
0
There is a 2D `grid` of size `n x n` where each cell of this grid has a lamp that is initially **turned off**. You are given a 2D array of lamp positions `lamps`, where `lamps[i] = [rowi, coli]` indicates that the lamp at `grid[rowi][coli]` is **turned on**. Even if the same lamp is listed more than once, it is turned...
null
Python 4 Hash Maps, O(n)
grid-illumination
0
1
# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution:\n def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:\n on = set()\n yOn = {}\n xOn = {}\n dy = {}\n dx = {}\n\n res = []\n\n ...
0
Given an integer array `arr`, partition the array into (contiguous) subarrays of length **at most** `k`. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return _the largest sum of the given array after partitioning. Test cases are generated so that the answer fi...
null
Easy to understand, step wise explanation😸
grid-illumination
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nInitial thought to update the grid on each operation but then it was taking too long as each iteration we need to visit row, col and 2 diagonals. So ditched the idea of updating grid. then used hashmap to store the lamp position in each r...
0
There is a 2D `grid` of size `n x n` where each cell of this grid has a lamp that is initially **turned off**. You are given a 2D array of lamp positions `lamps`, where `lamps[i] = [rowi, coli]` indicates that the lamp at `grid[rowi][coli]` is **turned on**. Even if the same lamp is listed more than once, it is turned...
null
Easy to understand, step wise explanation😸
grid-illumination
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nInitial thought to update the grid on each operation but then it was taking too long as each iteration we need to visit row, col and 2 diagonals. So ditched the idea of updating grid. then used hashmap to store the lamp position in each r...
0
Given an integer array `arr`, partition the array into (contiguous) subarrays of length **at most** `k`. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return _the largest sum of the given array after partitioning. Test cases are generated so that the answer fi...
null
Python3||O(Queries*8)
grid-illumination
0
1
# Intuition\n- We create hashMaps for rows, cols, diag and anti diagonal and update the count as we come across each lamp\n- And we then move on to the queries, in it we try removing the lamps that are neighboring the current query point.\n\n# Approach\n- Intuition pretty much covers the approach\n\n# Complexity\n- Tim...
0
There is a 2D `grid` of size `n x n` where each cell of this grid has a lamp that is initially **turned off**. You are given a 2D array of lamp positions `lamps`, where `lamps[i] = [rowi, coli]` indicates that the lamp at `grid[rowi][coli]` is **turned on**. Even if the same lamp is listed more than once, it is turned...
null
Python3||O(Queries*8)
grid-illumination
0
1
# Intuition\n- We create hashMaps for rows, cols, diag and anti diagonal and update the count as we come across each lamp\n- And we then move on to the queries, in it we try removing the lamps that are neighboring the current query point.\n\n# Approach\n- Intuition pretty much covers the approach\n\n# Complexity\n- Tim...
0
Given an integer array `arr`, partition the array into (contiguous) subarrays of length **at most** `k`. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return _the largest sum of the given array after partitioning. Test cases are generated so that the answer fi...
null
[Python] Easy hashmaps
grid-illumination
0
1
\n# Code\n```\nclass Solution:\n def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:\n rows = defaultdict(int)\n cols = defaultdict(int)\n d1 = defaultdict(int)\n d2 = defaultdict(int)\n baseLights = set()\n for r, c in lamps:\n...
0
There is a 2D `grid` of size `n x n` where each cell of this grid has a lamp that is initially **turned off**. You are given a 2D array of lamp positions `lamps`, where `lamps[i] = [rowi, coli]` indicates that the lamp at `grid[rowi][coli]` is **turned on**. Even if the same lamp is listed more than once, it is turned...
null
[Python] Easy hashmaps
grid-illumination
0
1
\n# Code\n```\nclass Solution:\n def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:\n rows = defaultdict(int)\n cols = defaultdict(int)\n d1 = defaultdict(int)\n d2 = defaultdict(int)\n baseLights = set()\n for r, c in lamps:\n...
0
Given an integer array `arr`, partition the array into (contiguous) subarrays of length **at most** `k`. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return _the largest sum of the given array after partitioning. Test cases are generated so that the answer fi...
null
Python Solution
grid-illumination
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 2D `grid` of size `n x n` where each cell of this grid has a lamp that is initially **turned off**. You are given a 2D array of lamp positions `lamps`, where `lamps[i] = [rowi, coli]` indicates that the lamp at `grid[rowi][coli]` is **turned on**. Even if the same lamp is listed more than once, it is turned...
null
Python Solution
grid-illumination
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
Given an integer array `arr`, partition the array into (contiguous) subarrays of length **at most** `k`. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return _the largest sum of the given array after partitioning. Test cases are generated so that the answer fi...
null
SOLID Principles Practice | Commented and Explained | Set and Dictionaries
grid-illumination
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI wanted to solve this in as object oriented a process as possible and try to really focus on my solid principles, so I decided to work this a bit of a long way around. The goal of this was to increase software engineering capability, not...
0
There is a 2D `grid` of size `n x n` where each cell of this grid has a lamp that is initially **turned off**. You are given a 2D array of lamp positions `lamps`, where `lamps[i] = [rowi, coli]` indicates that the lamp at `grid[rowi][coli]` is **turned on**. Even if the same lamp is listed more than once, it is turned...
null
SOLID Principles Practice | Commented and Explained | Set and Dictionaries
grid-illumination
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI wanted to solve this in as object oriented a process as possible and try to really focus on my solid principles, so I decided to work this a bit of a long way around. The goal of this was to increase software engineering capability, not...
0
Given an integer array `arr`, partition the array into (contiguous) subarrays of length **at most** `k`. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return _the largest sum of the given array after partitioning. Test cases are generated so that the answer fi...
null
Python3: Hashmap O(N)
grid-illumination
0
1
# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(L + Q), L = lamps.length(); Q = queries.length()$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(L + Q)$$\n\n# Code\n```\nclass Solution:\n def gridIllumination(self, n: int, lamps: Lis...
0
There is a 2D `grid` of size `n x n` where each cell of this grid has a lamp that is initially **turned off**. You are given a 2D array of lamp positions `lamps`, where `lamps[i] = [rowi, coli]` indicates that the lamp at `grid[rowi][coli]` is **turned on**. Even if the same lamp is listed more than once, it is turned...
null
Python3: Hashmap O(N)
grid-illumination
0
1
# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(L + Q), L = lamps.length(); Q = queries.length()$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(L + Q)$$\n\n# Code\n```\nclass Solution:\n def gridIllumination(self, n: int, lamps: Lis...
0
Given an integer array `arr`, partition the array into (contiguous) subarrays of length **at most** `k`. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return _the largest sum of the given array after partitioning. Test cases are generated so that the answer fi...
null
Solution
find-common-characters
1
1
```C++ []\nclass Solution {\npublic:\n vector<string> commonChars(vector<string>& words) {\n vector<string> res;\n \n sort(words.begin(), words.end());\n \n for (char c : words[0]) {\n bool common = true;\n \n for (int i = 1; i < words.size(); i++) ...
450
Given a string array `words`, return _an array of all characters that show up in all strings within the_ `words` _(including duplicates)_. You may return the answer in **any order**. **Example 1:** **Input:** words = \["bella","label","roller"\] **Output:** \["e","l","l"\] **Example 2:** **Input:** words = \["cool"...
null
Solution
find-common-characters
1
1
```C++ []\nclass Solution {\npublic:\n vector<string> commonChars(vector<string>& words) {\n vector<string> res;\n \n sort(words.begin(), words.end());\n \n for (char c : words[0]) {\n bool common = true;\n \n for (int i = 1; i < words.size(); i++) ...
450
Given a string `s`, consider all _duplicated substrings_: (contiguous) substrings of s that occur 2 or more times. The occurrences may overlap. Return **any** duplicated substring that has the longest possible length. If `s` does not have a duplicated substring, the answer is `" "`. **Example 1:** **Input:** s = "ba...
null
For Beginners & more simplified :-)
find-common-characters
0
1
\n\n# Code\n```\nclass Solution:\n def commonChars(self, words: List[str]) -> List[str]:\n res = Counter(words[0])\n for i in words:\n res &= Counter(i)\n return list(res.elements())\n \n```\n\n# Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# ...
4
Given a string array `words`, return _an array of all characters that show up in all strings within the_ `words` _(including duplicates)_. You may return the answer in **any order**. **Example 1:** **Input:** words = \["bella","label","roller"\] **Output:** \["e","l","l"\] **Example 2:** **Input:** words = \["cool"...
null
For Beginners & more simplified :-)
find-common-characters
0
1
\n\n# Code\n```\nclass Solution:\n def commonChars(self, words: List[str]) -> List[str]:\n res = Counter(words[0])\n for i in words:\n res &= Counter(i)\n return list(res.elements())\n \n```\n\n# Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# ...
4
Given a string `s`, consider all _duplicated substrings_: (contiguous) substrings of s that occur 2 or more times. The occurrences may overlap. Return **any** duplicated substring that has the longest possible length. If `s` does not have a duplicated substring, the answer is `" "`. **Example 1:** **Input:** s = "ba...
null
C++ O(n) | O(1), two vectors
find-common-characters
0
1
**Python 3**\nThe `&` operation for counters results in minumum counters for each element. \n```python\nclass Solution:\n def commonChars(self, words: List[str]) -> List[str]:\n return reduce(lambda a, b: a & b, map(Counter, words)).elements()\n```\n**C++**\nFor each string, we count characters in ```cnt1```....
262
Given a string array `words`, return _an array of all characters that show up in all strings within the_ `words` _(including duplicates)_. You may return the answer in **any order**. **Example 1:** **Input:** words = \["bella","label","roller"\] **Output:** \["e","l","l"\] **Example 2:** **Input:** words = \["cool"...
null
C++ O(n) | O(1), two vectors
find-common-characters
0
1
**Python 3**\nThe `&` operation for counters results in minumum counters for each element. \n```python\nclass Solution:\n def commonChars(self, words: List[str]) -> List[str]:\n return reduce(lambda a, b: a & b, map(Counter, words)).elements()\n```\n**C++**\nFor each string, we count characters in ```cnt1```....
262
Given a string `s`, consider all _duplicated substrings_: (contiguous) substrings of s that occur 2 or more times. The occurrences may overlap. Return **any** duplicated substring that has the longest possible length. If `s` does not have a duplicated substring, the answer is `" "`. **Example 1:** **Input:** s = "ba...
null
✅ [PYTHON] Nested for-in loops [Beats 100.00%]
find-common-characters
0
1
# Intuition\nI decided to take the shortest word ("min_word") and compare it to the others\n\n# Approach\nI loop through the shortest word, then loop through the remaining words inside. \nIf a character is not in the word, I delete it from "min_word".\nElse, I delete it from the comparable word.\n\n# Code\n```\nclass S...
2
Given a string array `words`, return _an array of all characters that show up in all strings within the_ `words` _(including duplicates)_. You may return the answer in **any order**. **Example 1:** **Input:** words = \["bella","label","roller"\] **Output:** \["e","l","l"\] **Example 2:** **Input:** words = \["cool"...
null
✅ [PYTHON] Nested for-in loops [Beats 100.00%]
find-common-characters
0
1
# Intuition\nI decided to take the shortest word ("min_word") and compare it to the others\n\n# Approach\nI loop through the shortest word, then loop through the remaining words inside. \nIf a character is not in the word, I delete it from "min_word".\nElse, I delete it from the comparable word.\n\n# Code\n```\nclass S...
2
Given a string `s`, consider all _duplicated substrings_: (contiguous) substrings of s that occur 2 or more times. The occurrences may overlap. Return **any** duplicated substring that has the longest possible length. If `s` does not have a duplicated substring, the answer is `" "`. **Example 1:** **Input:** s = "ba...
null
Python solution with collections
find-common-characters
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
Given a string array `words`, return _an array of all characters that show up in all strings within the_ `words` _(including duplicates)_. You may return the answer in **any order**. **Example 1:** **Input:** words = \["bella","label","roller"\] **Output:** \["e","l","l"\] **Example 2:** **Input:** words = \["cool"...
null
Python solution with collections
find-common-characters
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
Given a string `s`, consider all _duplicated substrings_: (contiguous) substrings of s that occur 2 or more times. The occurrences may overlap. Return **any** duplicated substring that has the longest possible length. If `s` does not have a duplicated substring, the answer is `" "`. **Example 1:** **Input:** s = "ba...
null
Nice & short collections.Counter - 65 ms - 16.4 MB
find-common-characters
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI recalled a nice tool fit for the job - the Counter class from the collections built-in module. Aside from other solutions - mine is more readable, though slightly less optimized: redundunt 1-st element conversion and Counter-ization of ...
2
Given a string array `words`, return _an array of all characters that show up in all strings within the_ `words` _(including duplicates)_. You may return the answer in **any order**. **Example 1:** **Input:** words = \["bella","label","roller"\] **Output:** \["e","l","l"\] **Example 2:** **Input:** words = \["cool"...
null
Nice & short collections.Counter - 65 ms - 16.4 MB
find-common-characters
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI recalled a nice tool fit for the job - the Counter class from the collections built-in module. Aside from other solutions - mine is more readable, though slightly less optimized: redundunt 1-st element conversion and Counter-ization of ...
2
Given a string `s`, consider all _duplicated substrings_: (contiguous) substrings of s that occur 2 or more times. The occurrences may overlap. Return **any** duplicated substring that has the longest possible length. If `s` does not have a duplicated substring, the answer is `" "`. **Example 1:** **Input:** s = "ba...
null
Python - Readable/No shortcuts - WITH EXPLANATION
find-common-characters
0
1
# Approach\nThe idea of this approach is to first start with the counts of every character in the first word and store these `character : character count` pairs in `res_counts`. Then we iterate through every other word in the words list. For each word, we use its own `char_counts` hash map to store `character : charact...
2
Given a string array `words`, return _an array of all characters that show up in all strings within the_ `words` _(including duplicates)_. You may return the answer in **any order**. **Example 1:** **Input:** words = \["bella","label","roller"\] **Output:** \["e","l","l"\] **Example 2:** **Input:** words = \["cool"...
null
Python - Readable/No shortcuts - WITH EXPLANATION
find-common-characters
0
1
# Approach\nThe idea of this approach is to first start with the counts of every character in the first word and store these `character : character count` pairs in `res_counts`. Then we iterate through every other word in the words list. For each word, we use its own `char_counts` hash map to store `character : charact...
2
Given a string `s`, consider all _duplicated substrings_: (contiguous) substrings of s that occur 2 or more times. The occurrences may overlap. Return **any** duplicated substring that has the longest possible length. If `s` does not have a duplicated substring, the answer is `" "`. **Example 1:** **Input:** s = "ba...
null
My Solution using hash table -o(n) time and o (1) space
find-common-characters
0
1
\n\n# Code\n```\nclass Solution:\n def commonChars(self, words: List[str]) -> List[str]:\n return self.sol1(words)\n\n def sol1(self, words):\n # INIT\n\n # ALGO\n\n # --Build a freq graph of letters\n # --Each letter has an array mapped to it containing the frequency of cha...
3
Given a string array `words`, return _an array of all characters that show up in all strings within the_ `words` _(including duplicates)_. You may return the answer in **any order**. **Example 1:** **Input:** words = \["bella","label","roller"\] **Output:** \["e","l","l"\] **Example 2:** **Input:** words = \["cool"...
null
My Solution using hash table -o(n) time and o (1) space
find-common-characters
0
1
\n\n# Code\n```\nclass Solution:\n def commonChars(self, words: List[str]) -> List[str]:\n return self.sol1(words)\n\n def sol1(self, words):\n # INIT\n\n # ALGO\n\n # --Build a freq graph of letters\n # --Each letter has an array mapped to it containing the frequency of cha...
3
Given a string `s`, consider all _duplicated substrings_: (contiguous) substrings of s that occur 2 or more times. The occurrences may overlap. Return **any** duplicated substring that has the longest possible length. If `s` does not have a duplicated substring, the answer is `" "`. **Example 1:** **Input:** s = "ba...
null
two dictionaries python
find-common-characters
0
1
we create 2 dictionaries \ndictionary d1 stores the value of the minimum occurrence of a letter in a word \ndictionary d2 stores the count of letters of the current word\nwe put the minimum value of a letter after each word \nthe return list will contain the elements in d1 d1[letter] number of times .\n```\nclass So...
1
Given a string array `words`, return _an array of all characters that show up in all strings within the_ `words` _(including duplicates)_. You may return the answer in **any order**. **Example 1:** **Input:** words = \["bella","label","roller"\] **Output:** \["e","l","l"\] **Example 2:** **Input:** words = \["cool"...
null
two dictionaries python
find-common-characters
0
1
we create 2 dictionaries \ndictionary d1 stores the value of the minimum occurrence of a letter in a word \ndictionary d2 stores the count of letters of the current word\nwe put the minimum value of a letter after each word \nthe return list will contain the elements in d1 d1[letter] number of times .\n```\nclass So...
1
Given a string `s`, consider all _duplicated substrings_: (contiguous) substrings of s that occur 2 or more times. The occurrences may overlap. Return **any** duplicated substring that has the longest possible length. If `s` does not have a duplicated substring, the answer is `" "`. **Example 1:** **Input:** s = "ba...
null
Solution
check-if-word-is-valid-after-substitutions
1
1
```C++ []\nclass Solution {\npublic:\n bool isValid(string s) {\n\n if(s.size()%3!=0) return false;\n stack<char> st;\n\n for(char c:s){\n if(st.empty()) st.push(c);\n else if(c==\'c\'){\n char b=st.top();st.pop();\n if(st.empty()) return false...
1
Given a string `s`, determine if it is **valid**. A string `s` is **valid** if, starting with an empty string `t = " "`, you can **transform** `t` **into** `s` after performing the following operation **any number of times**: * Insert string `"abc "` into any position in `t`. More formally, `t` becomes `tleft + "ab...
null
[Easy]Simple Stack And String Replace Approaches
check-if-word-is-valid-after-substitutions
0
1
### Without Stack Approach\n```\nclass Solution:\n def isValid(self, s: str) -> bool:\n incomplete = True\n \n while incomplete:\n if \'abc\' in s:\n s= s.replace(\'abc\',\'\')\n else:\n incomplete = False\n \n return s == \'\'\n`...
2
Given a string `s`, determine if it is **valid**. A string `s` is **valid** if, starting with an empty string `t = " "`, you can **transform** `t` **into** `s` after performing the following operation **any number of times**: * Insert string `"abc "` into any position in `t`. More formally, `t` becomes `tleft + "ab...
null
[Python3] Good enough
check-if-word-is-valid-after-substitutions
0
1
``` Python3 []\nclass Solution:\n def isValid(self, s: str) -> bool:\n while s:\n for i in range(len(s)-2):\n if s[i:i+3]==\'abc\':\n s = s[:i]+s[i+3:]\n break\n else:\n return False\n \n return True\n```
0
Given a string `s`, determine if it is **valid**. A string `s` is **valid** if, starting with an empty string `t = " "`, you can **transform** `t` **into** `s` after performing the following operation **any number of times**: * Insert string `"abc "` into any position in `t`. More formally, `t` becomes `tleft + "ab...
null
Simple python3 solution | Stack + Greedy
check-if-word-is-valid-after-substitutions
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``` python3 []\nclass Solution:\n def isValid(self, s: str) -> bool:\n stack = []\n for elem in s:\n ...
0
Given a string `s`, determine if it is **valid**. A string `s` is **valid** if, starting with an empty string `t = " "`, you can **transform** `t` **into** `s` after performing the following operation **any number of times**: * Insert string `"abc "` into any position in `t`. More formally, `t` becomes `tleft + "ab...
null
Solution
max-consecutive-ones-iii
1
1
```C++ []\nclass Solution {\npublic:\n int longestOnes(vector<int>& nums, int k) {\n int i=0,j=0;\n while(j<nums.size()){\n if(nums[j]==0){\n k--;\n }\n if(k<0){\n if(nums[i]==0){\n k++;\n }\n ...
480
Given a binary array `nums` and an integer `k`, return _the maximum number of consecutive_ `1`_'s in the array if you can flip at most_ `k` `0`'s. **Example 1:** **Input:** nums = \[1,1,1,0,0,0,1,1,1,1,0\], k = 2 **Output:** 6 **Explanation:** \[1,1,1,0,0,**1**,1,1,1,1,**1**\] Bolded numbers were flipped from 0 to 1....
null
Solution
max-consecutive-ones-iii
1
1
```C++ []\nclass Solution {\npublic:\n int longestOnes(vector<int>& nums, int k) {\n int i=0,j=0;\n while(j<nums.size()){\n if(nums[j]==0){\n k--;\n }\n if(k<0){\n if(nums[i]==0){\n k++;\n }\n ...
480
You are given an array of integers `stones` where `stones[i]` is the weight of the `ith` stone. We are playing a game with the stones. On each turn, we choose the **heaviest two stones** and smash them together. Suppose the heaviest two stones have weights `x` and `y` with `x <= y`. The result of this smash is: * I...
One thing's for sure, we will only flip a zero if it extends an existing window of 1s. Otherwise, there's no point in doing it, right? Think Sliding Window! Since we know this problem can be solved using the sliding window construct, we might as well focus in that direction for hints. Basically, in a given window, we c...
Easy Python3 Solution using Sliding Window
max-consecutive-ones-iii
0
1
\n# Approach\n<!-- Describe your approach to solving the problem. -->\n Used Sliding Window Technique\n\n# Code\n```\nclass Solution:\n def longestOnes(self, nums: List[int], k: int) -> int:\n\n start=0\n end=0\n for i in range(len(nums)):\n if(nums[i]==0):\n k-=1\n ...
0
Given a binary array `nums` and an integer `k`, return _the maximum number of consecutive_ `1`_'s in the array if you can flip at most_ `k` `0`'s. **Example 1:** **Input:** nums = \[1,1,1,0,0,0,1,1,1,1,0\], k = 2 **Output:** 6 **Explanation:** \[1,1,1,0,0,**1**,1,1,1,1,**1**\] Bolded numbers were flipped from 0 to 1....
null
Easy Python3 Solution using Sliding Window
max-consecutive-ones-iii
0
1
\n# Approach\n<!-- Describe your approach to solving the problem. -->\n Used Sliding Window Technique\n\n# Code\n```\nclass Solution:\n def longestOnes(self, nums: List[int], k: int) -> int:\n\n start=0\n end=0\n for i in range(len(nums)):\n if(nums[i]==0):\n k-=1\n ...
0
You are given an array of integers `stones` where `stones[i]` is the weight of the `ith` stone. We are playing a game with the stones. On each turn, we choose the **heaviest two stones** and smash them together. Suppose the heaviest two stones have weights `x` and `y` with `x <= y`. The result of this smash is: * I...
One thing's for sure, we will only flip a zero if it extends an existing window of 1s. Otherwise, there's no point in doing it, right? Think Sliding Window! Since we know this problem can be solved using the sliding window construct, we might as well focus in that direction for hints. Basically, in a given window, we c...
Python3 sliding window with clear example - explains why the soln works
max-consecutive-ones-iii
0
1
The solution was confusing for me.\nHere let me try to explain it more clearly with the example below. Hope it helps.\n![image](https://assets.leetcode.com/users/images/b0204f0b-b267-4442-935a-5e99d2f9ed28_1593898279.6907446.png)\n\nHere is what the implementation looks like. I used explicit checks for K to make it cle...
351
Given a binary array `nums` and an integer `k`, return _the maximum number of consecutive_ `1`_'s in the array if you can flip at most_ `k` `0`'s. **Example 1:** **Input:** nums = \[1,1,1,0,0,0,1,1,1,1,0\], k = 2 **Output:** 6 **Explanation:** \[1,1,1,0,0,**1**,1,1,1,1,**1**\] Bolded numbers were flipped from 0 to 1....
null
Python3 sliding window with clear example - explains why the soln works
max-consecutive-ones-iii
0
1
The solution was confusing for me.\nHere let me try to explain it more clearly with the example below. Hope it helps.\n![image](https://assets.leetcode.com/users/images/b0204f0b-b267-4442-935a-5e99d2f9ed28_1593898279.6907446.png)\n\nHere is what the implementation looks like. I used explicit checks for K to make it cle...
351
You are given an array of integers `stones` where `stones[i]` is the weight of the `ith` stone. We are playing a game with the stones. On each turn, we choose the **heaviest two stones** and smash them together. Suppose the heaviest two stones have weights `x` and `y` with `x <= y`. The result of this smash is: * I...
One thing's for sure, we will only flip a zero if it extends an existing window of 1s. Otherwise, there's no point in doing it, right? Think Sliding Window! Since we know this problem can be solved using the sliding window construct, we might as well focus in that direction for hints. Basically, in a given window, we c...
Python || Sliding Windows
max-consecutive-ones-iii
0
1
# Approach\ni is a pointer that marks the beginning of the current subarray.\n\nmaxi is a variable that stores the length of the longest subarray with at most k replacements of 0s.\n\nj is another pointer that moves through the list to the right.\n\nzero is a counter that keeps track of the number of 0s in the current ...
4
Given a binary array `nums` and an integer `k`, return _the maximum number of consecutive_ `1`_'s in the array if you can flip at most_ `k` `0`'s. **Example 1:** **Input:** nums = \[1,1,1,0,0,0,1,1,1,1,0\], k = 2 **Output:** 6 **Explanation:** \[1,1,1,0,0,**1**,1,1,1,1,**1**\] Bolded numbers were flipped from 0 to 1....
null
Python || Sliding Windows
max-consecutive-ones-iii
0
1
# Approach\ni is a pointer that marks the beginning of the current subarray.\n\nmaxi is a variable that stores the length of the longest subarray with at most k replacements of 0s.\n\nj is another pointer that moves through the list to the right.\n\nzero is a counter that keeps track of the number of 0s in the current ...
4
You are given an array of integers `stones` where `stones[i]` is the weight of the `ith` stone. We are playing a game with the stones. On each turn, we choose the **heaviest two stones** and smash them together. Suppose the heaviest two stones have weights `x` and `y` with `x <= y`. The result of this smash is: * I...
One thing's for sure, we will only flip a zero if it extends an existing window of 1s. Otherwise, there's no point in doing it, right? Think Sliding Window! Since we know this problem can be solved using the sliding window construct, we might as well focus in that direction for hints. Basically, in a given window, we c...
✔️ 5 Ways to Solving Max Consecutive Ones III 🪔
max-consecutive-ones-iii
0
1
![68747470733a2f2f656d6f6a69732e736c61636b6d6f6a69732e636f6d2f656d6f6a69732f696d616765732f313539373630393836382f31303039362f6c6170746f705f706172726f742e6769663f31353937363039383638.gif](https://assets.leetcode.com/users/images/c81f3ac8-0206-484b-b8d2-8144db70506c_1697731422.7840035.gif)\nThis is cool right?~~~\n# Solut...
3
Given a binary array `nums` and an integer `k`, return _the maximum number of consecutive_ `1`_'s in the array if you can flip at most_ `k` `0`'s. **Example 1:** **Input:** nums = \[1,1,1,0,0,0,1,1,1,1,0\], k = 2 **Output:** 6 **Explanation:** \[1,1,1,0,0,**1**,1,1,1,1,**1**\] Bolded numbers were flipped from 0 to 1....
null
✔️ 5 Ways to Solving Max Consecutive Ones III 🪔
max-consecutive-ones-iii
0
1
![68747470733a2f2f656d6f6a69732e736c61636b6d6f6a69732e636f6d2f656d6f6a69732f696d616765732f313539373630393836382f31303039362f6c6170746f705f706172726f742e6769663f31353937363039383638.gif](https://assets.leetcode.com/users/images/c81f3ac8-0206-484b-b8d2-8144db70506c_1697731422.7840035.gif)\nThis is cool right?~~~\n# Solut...
3
You are given an array of integers `stones` where `stones[i]` is the weight of the `ith` stone. We are playing a game with the stones. On each turn, we choose the **heaviest two stones** and smash them together. Suppose the heaviest two stones have weights `x` and `y` with `x <= y`. The result of this smash is: * I...
One thing's for sure, we will only flip a zero if it extends an existing window of 1s. Otherwise, there's no point in doing it, right? Think Sliding Window! Since we know this problem can be solved using the sliding window construct, we might as well focus in that direction for hints. Basically, in a given window, we c...
Standard Sliding Window Template
max-consecutive-ones-iii
0
1
# Intuition\nSliding Window template for questions like [424. Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/description/)\n\n# Approach\nKeep increasing the window till count of `zeros > k`, remove left character from window till `zeros <= k`.\n\nP.S. `ri...
1
Given a binary array `nums` and an integer `k`, return _the maximum number of consecutive_ `1`_'s in the array if you can flip at most_ `k` `0`'s. **Example 1:** **Input:** nums = \[1,1,1,0,0,0,1,1,1,1,0\], k = 2 **Output:** 6 **Explanation:** \[1,1,1,0,0,**1**,1,1,1,1,**1**\] Bolded numbers were flipped from 0 to 1....
null
Standard Sliding Window Template
max-consecutive-ones-iii
0
1
# Intuition\nSliding Window template for questions like [424. Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/description/)\n\n# Approach\nKeep increasing the window till count of `zeros > k`, remove left character from window till `zeros <= k`.\n\nP.S. `ri...
1
You are given an array of integers `stones` where `stones[i]` is the weight of the `ith` stone. We are playing a game with the stones. On each turn, we choose the **heaviest two stones** and smash them together. Suppose the heaviest two stones have weights `x` and `y` with `x <= y`. The result of this smash is: * I...
One thing's for sure, we will only flip a zero if it extends an existing window of 1s. Otherwise, there's no point in doing it, right? Think Sliding Window! Since we know this problem can be solved using the sliding window construct, we might as well focus in that direction for hints. Basically, in a given window, we c...
Easiest solution
max-consecutive-ones-iii
0
1
# Intuition\nWe use 2 pointers left and right to keep track of sliding window.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\no(1)\n\n# Code\n```\nclass Solution:\n def longestOnes(self, nums: List[int], k: int) -> int:\n left=0\n count_zero=0\n max_array=0\n\n for righ...
7
Given a binary array `nums` and an integer `k`, return _the maximum number of consecutive_ `1`_'s in the array if you can flip at most_ `k` `0`'s. **Example 1:** **Input:** nums = \[1,1,1,0,0,0,1,1,1,1,0\], k = 2 **Output:** 6 **Explanation:** \[1,1,1,0,0,**1**,1,1,1,1,**1**\] Bolded numbers were flipped from 0 to 1....
null
Easiest solution
max-consecutive-ones-iii
0
1
# Intuition\nWe use 2 pointers left and right to keep track of sliding window.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\no(1)\n\n# Code\n```\nclass Solution:\n def longestOnes(self, nums: List[int], k: int) -> int:\n left=0\n count_zero=0\n max_array=0\n\n for righ...
7
You are given an array of integers `stones` where `stones[i]` is the weight of the `ith` stone. We are playing a game with the stones. On each turn, we choose the **heaviest two stones** and smash them together. Suppose the heaviest two stones have weights `x` and `y` with `x <= y`. The result of this smash is: * I...
One thing's for sure, we will only flip a zero if it extends an existing window of 1s. Otherwise, there's no point in doing it, right? Think Sliding Window! Since we know this problem can be solved using the sliding window construct, we might as well focus in that direction for hints. Basically, in a given window, we c...
[Python 3] Sliding window + bonus (similar question) || beats 97%
max-consecutive-ones-iii
0
1
```python3 []\nclass Solution:\n def longestOnes(self, nums: List[int], k: int) -> int:\n zeros, l = 0, 0\n for r, n in enumerate(nums):\n zeros += n == 0\n if zeros > k:\n zeros -= nums[l] == 0\n l += 1\n return r - l + 1\n\n```\n\n[485. Max C...
15
Given a binary array `nums` and an integer `k`, return _the maximum number of consecutive_ `1`_'s in the array if you can flip at most_ `k` `0`'s. **Example 1:** **Input:** nums = \[1,1,1,0,0,0,1,1,1,1,0\], k = 2 **Output:** 6 **Explanation:** \[1,1,1,0,0,**1**,1,1,1,1,**1**\] Bolded numbers were flipped from 0 to 1....
null
[Python 3] Sliding window + bonus (similar question) || beats 97%
max-consecutive-ones-iii
0
1
```python3 []\nclass Solution:\n def longestOnes(self, nums: List[int], k: int) -> int:\n zeros, l = 0, 0\n for r, n in enumerate(nums):\n zeros += n == 0\n if zeros > k:\n zeros -= nums[l] == 0\n l += 1\n return r - l + 1\n\n```\n\n[485. Max C...
15
You are given an array of integers `stones` where `stones[i]` is the weight of the `ith` stone. We are playing a game with the stones. On each turn, we choose the **heaviest two stones** and smash them together. Suppose the heaviest two stones have weights `x` and `y` with `x <= y`. The result of this smash is: * I...
One thing's for sure, we will only flip a zero if it extends an existing window of 1s. Otherwise, there's no point in doing it, right? Think Sliding Window! Since we know this problem can be solved using the sliding window construct, we might as well focus in that direction for hints. Basically, in a given window, we c...
Sliding Window Problems
max-consecutive-ones-iii
0
1
# Sliding Window Logic\n```\nclass Solution:\n def longestOnes(self, nums: List[int], k: int) -> int:\n res=[0]*2\n ans=left=0\n for right in range(len(nums)):\n res[nums[right]]+=1\n ans=max(ans,res[nums[right]])\n if (right-left+1)-ans>k:\n res[n...
5
Given a binary array `nums` and an integer `k`, return _the maximum number of consecutive_ `1`_'s in the array if you can flip at most_ `k` `0`'s. **Example 1:** **Input:** nums = \[1,1,1,0,0,0,1,1,1,1,0\], k = 2 **Output:** 6 **Explanation:** \[1,1,1,0,0,**1**,1,1,1,1,**1**\] Bolded numbers were flipped from 0 to 1....
null
Sliding Window Problems
max-consecutive-ones-iii
0
1
# Sliding Window Logic\n```\nclass Solution:\n def longestOnes(self, nums: List[int], k: int) -> int:\n res=[0]*2\n ans=left=0\n for right in range(len(nums)):\n res[nums[right]]+=1\n ans=max(ans,res[nums[right]])\n if (right-left+1)-ans>k:\n res[n...
5
You are given an array of integers `stones` where `stones[i]` is the weight of the `ith` stone. We are playing a game with the stones. On each turn, we choose the **heaviest two stones** and smash them together. Suppose the heaviest two stones have weights `x` and `y` with `x <= y`. The result of this smash is: * I...
One thing's for sure, we will only flip a zero if it extends an existing window of 1s. Otherwise, there's no point in doing it, right? Think Sliding Window! Since we know this problem can be solved using the sliding window construct, we might as well focus in that direction for hints. Basically, in a given window, we c...
Binary Search + Sliding Window
max-consecutive-ones-iii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nIn this problem we know the highest and the lowest possible answers which are `0` or `len(nums)`\n\nTherefore, there may be a possible solution for this problem using binary search\n\n# Approach\n<!-- Describe your approach to solving t...
2
Given a binary array `nums` and an integer `k`, return _the maximum number of consecutive_ `1`_'s in the array if you can flip at most_ `k` `0`'s. **Example 1:** **Input:** nums = \[1,1,1,0,0,0,1,1,1,1,0\], k = 2 **Output:** 6 **Explanation:** \[1,1,1,0,0,**1**,1,1,1,1,**1**\] Bolded numbers were flipped from 0 to 1....
null
Binary Search + Sliding Window
max-consecutive-ones-iii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nIn this problem we know the highest and the lowest possible answers which are `0` or `len(nums)`\n\nTherefore, there may be a possible solution for this problem using binary search\n\n# Approach\n<!-- Describe your approach to solving t...
2
You are given an array of integers `stones` where `stones[i]` is the weight of the `ith` stone. We are playing a game with the stones. On each turn, we choose the **heaviest two stones** and smash them together. Suppose the heaviest two stones have weights `x` and `y` with `x <= y`. The result of this smash is: * I...
One thing's for sure, we will only flip a zero if it extends an existing window of 1s. Otherwise, there's no point in doing it, right? Think Sliding Window! Since we know this problem can be solved using the sliding window construct, we might as well focus in that direction for hints. Basically, in a given window, we c...
💡Intuitive| ⏳0ms 🥇Beats 95% | ✅ Beginner's Friendly Explanation
max-consecutive-ones-iii
0
1
![Screenshot .png](https://assets.leetcode.com/users/images/0463dcf0-ba62-4951-9922-d741dae760ad_1700812397.684774.png)\n\n# Intuition\nThe dynamic sliding window approach efficiently explores a binary array to find the longest subarray with at most k zeros. Two pointers define a window that expands on encountering 1s ...
1
Given a binary array `nums` and an integer `k`, return _the maximum number of consecutive_ `1`_'s in the array if you can flip at most_ `k` `0`'s. **Example 1:** **Input:** nums = \[1,1,1,0,0,0,1,1,1,1,0\], k = 2 **Output:** 6 **Explanation:** \[1,1,1,0,0,**1**,1,1,1,1,**1**\] Bolded numbers were flipped from 0 to 1....
null
💡Intuitive| ⏳0ms 🥇Beats 95% | ✅ Beginner's Friendly Explanation
max-consecutive-ones-iii
0
1
![Screenshot .png](https://assets.leetcode.com/users/images/0463dcf0-ba62-4951-9922-d741dae760ad_1700812397.684774.png)\n\n# Intuition\nThe dynamic sliding window approach efficiently explores a binary array to find the longest subarray with at most k zeros. Two pointers define a window that expands on encountering 1s ...
1
You are given an array of integers `stones` where `stones[i]` is the weight of the `ith` stone. We are playing a game with the stones. On each turn, we choose the **heaviest two stones** and smash them together. Suppose the heaviest two stones have weights `x` and `y` with `x <= y`. The result of this smash is: * I...
One thing's for sure, we will only flip a zero if it extends an existing window of 1s. Otherwise, there's no point in doing it, right? Think Sliding Window! Since we know this problem can be solved using the sliding window construct, we might as well focus in that direction for hints. Basically, in a given window, we c...
Solution
maximize-sum-of-array-after-k-negations
1
1
```C++ []\nclass Solution {\npublic:\nlong long getSum(vector<int> negate, vector<int>positive)\n{\n long long sum =0;\n for(int i=0;i<negate.size();i++)\n {\n sum+=negate[i];\n }\n for(int i=0;i<positive.size();i++)\n {\n sum+=posi...
447
Given an integer array `nums` and an integer `k`, modify the array in the following way: * choose an index `i` and replace `nums[i]` with `-nums[i]`. You should apply this process exactly `k` times. You may choose the same index `i` multiple times. Return _the largest possible sum of the array after modifying it i...
null
Solution
maximize-sum-of-array-after-k-negations
1
1
```C++ []\nclass Solution {\npublic:\nlong long getSum(vector<int> negate, vector<int>positive)\n{\n long long sum =0;\n for(int i=0;i<negate.size();i++)\n {\n sum+=negate[i];\n }\n for(int i=0;i<positive.size();i++)\n {\n sum+=posi...
447
You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them. We repeatedly make **duplicate removals** on `s` until we no longer can. Return _the final string after all such duplicate removals have been made_...
null
Python 3 solution beats 98% - simple if else statements
maximize-sum-of-array-after-k-negations
0
1
This solution is easy but quite tasking to come up with all the edge cases. I did see shorter solutions using heaps which I dont fully understand yet but hope to learn how to apply it in the near future.\n# Code\n```\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[int], k: int) -> int: \n ...
1
Given an integer array `nums` and an integer `k`, modify the array in the following way: * choose an index `i` and replace `nums[i]` with `-nums[i]`. You should apply this process exactly `k` times. You may choose the same index `i` multiple times. Return _the largest possible sum of the array after modifying it i...
null
Python 3 solution beats 98% - simple if else statements
maximize-sum-of-array-after-k-negations
0
1
This solution is easy but quite tasking to come up with all the edge cases. I did see shorter solutions using heaps which I dont fully understand yet but hope to learn how to apply it in the near future.\n# Code\n```\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[int], k: int) -> int: \n ...
1
You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them. We repeatedly make **duplicate removals** on `s` until we no longer can. Return _the final string after all such duplicate removals have been made_...
null
🚀 Beats 100% | Java, C++, Python | Non queue solution
maximize-sum-of-array-after-k-negations
1
1
> \u26A0\uFE0F **Disclaimer**: The original solution was crafted in Java. Thus, when we mention "Beats 100%" it applies specifically to Java submissions. The performance may vary for other languages.\n\nDont forget to upvote if you like the content below. \uD83D\uDE43\n\n# Intuition\nGiven the problem, we should always...
2
Given an integer array `nums` and an integer `k`, modify the array in the following way: * choose an index `i` and replace `nums[i]` with `-nums[i]`. You should apply this process exactly `k` times. You may choose the same index `i` multiple times. Return _the largest possible sum of the array after modifying it i...
null
🚀 Beats 100% | Java, C++, Python | Non queue solution
maximize-sum-of-array-after-k-negations
1
1
> \u26A0\uFE0F **Disclaimer**: The original solution was crafted in Java. Thus, when we mention "Beats 100%" it applies specifically to Java submissions. The performance may vary for other languages.\n\nDont forget to upvote if you like the content below. \uD83D\uDE43\n\n# Intuition\nGiven the problem, we should always...
2
You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them. We repeatedly make **duplicate removals** on `s` until we no longer can. Return _the final string after all such duplicate removals have been made_...
null
1005. Simple solution | Beats 91%
maximize-sum-of-array-after-k-negations
0
1
# Code\n```\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:\n nums.sort()\n last_negative, last_ind = nums[0], 0\n for x, item in enumerate(nums):\n if k <= 0 or item == 0: break\n if item < 0:\n last_negative = -item\n...
2
Given an integer array `nums` and an integer `k`, modify the array in the following way: * choose an index `i` and replace `nums[i]` with `-nums[i]`. You should apply this process exactly `k` times. You may choose the same index `i` multiple times. Return _the largest possible sum of the array after modifying it i...
null
1005. Simple solution | Beats 91%
maximize-sum-of-array-after-k-negations
0
1
# Code\n```\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:\n nums.sort()\n last_negative, last_ind = nums[0], 0\n for x, item in enumerate(nums):\n if k <= 0 or item == 0: break\n if item < 0:\n last_negative = -item\n...
2
You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them. We repeatedly make **duplicate removals** on `s` until we no longer can. Return _the final string after all such duplicate removals have been made_...
null
🔥 [Python 3] Min Heap for negative values with comments, beats 80% 🥷🏼
maximize-sum-of-array-after-k-negations
0
1
```\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:\n # if k <= n(count of negative numbers )=> need make highest negative values positive (here using heap for this)\n # if k > n(count of negative numbers) and k-n is odd => need make all negative numbers positive...
5
Given an integer array `nums` and an integer `k`, modify the array in the following way: * choose an index `i` and replace `nums[i]` with `-nums[i]`. You should apply this process exactly `k` times. You may choose the same index `i` multiple times. Return _the largest possible sum of the array after modifying it i...
null
🔥 [Python 3] Min Heap for negative values with comments, beats 80% 🥷🏼
maximize-sum-of-array-after-k-negations
0
1
```\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:\n # if k <= n(count of negative numbers )=> need make highest negative values positive (here using heap for this)\n # if k > n(count of negative numbers) and k-n is odd => need make all negative numbers positive...
5
You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them. We repeatedly make **duplicate removals** on `s` until we no longer can. Return _the final string after all such duplicate removals have been made_...
null
Min Heap (O(n + k * log (n)) time / O(1) space)
maximize-sum-of-array-after-k-negations
0
1
Hi LeetCoders \uD83D\uDC4B\nHere is my simple and clean solution to this problem with use of min-heap.\n\n**Code:**\n```\nfrom heapq import heapify, heapreplace\n\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:\n heapify(nums)\n while k and nums[0] < 0:\n ...
28
Given an integer array `nums` and an integer `k`, modify the array in the following way: * choose an index `i` and replace `nums[i]` with `-nums[i]`. You should apply this process exactly `k` times. You may choose the same index `i` multiple times. Return _the largest possible sum of the array after modifying it i...
null
Min Heap (O(n + k * log (n)) time / O(1) space)
maximize-sum-of-array-after-k-negations
0
1
Hi LeetCoders \uD83D\uDC4B\nHere is my simple and clean solution to this problem with use of min-heap.\n\n**Code:**\n```\nfrom heapq import heapify, heapreplace\n\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:\n heapify(nums)\n while k and nums[0] < 0:\n ...
28
You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them. We repeatedly make **duplicate removals** on `s` until we no longer can. Return _the final string after all such duplicate removals have been made_...
null
Explanation of the whole idea
maximize-sum-of-array-after-k-negations
0
1
## Explanation\n```\nAssume nums = [8,-1,8,-3,2,-2], k = 2\n\nTo get the LARGEST SUM we will make positive -3 and -2 as they are the SMALLEST NEGATIVE\nNUMBERS. This finding process would be easier if we sort first:\n\n -3,-2,-1,2,8,8 \n\nIf k were 5, first we would turn all the negative numbers to positi...
2
Given an integer array `nums` and an integer `k`, modify the array in the following way: * choose an index `i` and replace `nums[i]` with `-nums[i]`. You should apply this process exactly `k` times. You may choose the same index `i` multiple times. Return _the largest possible sum of the array after modifying it i...
null
Explanation of the whole idea
maximize-sum-of-array-after-k-negations
0
1
## Explanation\n```\nAssume nums = [8,-1,8,-3,2,-2], k = 2\n\nTo get the LARGEST SUM we will make positive -3 and -2 as they are the SMALLEST NEGATIVE\nNUMBERS. This finding process would be easier if we sort first:\n\n -3,-2,-1,2,8,8 \n\nIf k were 5, first we would turn all the negative numbers to positi...
2
You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them. We repeatedly make **duplicate removals** on `s` until we no longer can. Return _the final string after all such duplicate removals have been made_...
null
WEEB EXPLAINS PYTHON SOLUTION
maximize-sum-of-array-after-k-negations
0
1
![image](https://assets.leetcode.com/users/images/deac20d6-f25b-4f9f-9a91-4f79bcb66724_1620122556.4046779.png)\n\n\t\n\tclass Solution:\n\t\tdef largestSumAfterKNegations(self, A: List[int], K: int) -> int:\n\t\t\tA.sort()\n\t\t\ti = 0\n\t\t\twhile i < len(A) and K>0:\n\t\t\t\tif A[i] < 0: # negative value\n\t\t\t\t\tA...
6
Given an integer array `nums` and an integer `k`, modify the array in the following way: * choose an index `i` and replace `nums[i]` with `-nums[i]`. You should apply this process exactly `k` times. You may choose the same index `i` multiple times. Return _the largest possible sum of the array after modifying it i...
null
WEEB EXPLAINS PYTHON SOLUTION
maximize-sum-of-array-after-k-negations
0
1
![image](https://assets.leetcode.com/users/images/deac20d6-f25b-4f9f-9a91-4f79bcb66724_1620122556.4046779.png)\n\n\t\n\tclass Solution:\n\t\tdef largestSumAfterKNegations(self, A: List[int], K: int) -> int:\n\t\t\tA.sort()\n\t\t\ti = 0\n\t\t\twhile i < len(A) and K>0:\n\t\t\t\tif A[i] < 0: # negative value\n\t\t\t\t\tA...
6
You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them. We repeatedly make **duplicate removals** on `s` until we no longer can. Return _the final string after all such duplicate removals have been made_...
null
Easy Python3 Intuition Approach
maximize-sum-of-array-after-k-negations
0
1
# Code1\n```\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:\n for i in range(k):\n n=nums.index(min(nums))\n nums[n]=-1*nums[n]\n return sum(nums)\n```\n\n# Code2\n\n```\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[i...
2
Given an integer array `nums` and an integer `k`, modify the array in the following way: * choose an index `i` and replace `nums[i]` with `-nums[i]`. You should apply this process exactly `k` times. You may choose the same index `i` multiple times. Return _the largest possible sum of the array after modifying it i...
null
Easy Python3 Intuition Approach
maximize-sum-of-array-after-k-negations
0
1
# Code1\n```\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:\n for i in range(k):\n n=nums.index(min(nums))\n nums[n]=-1*nums[n]\n return sum(nums)\n```\n\n# Code2\n\n```\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[i...
2
You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them. We repeatedly make **duplicate removals** on `s` until we no longer can. Return _the final string after all such duplicate removals have been made_...
null
Greedy approach - python
maximize-sum-of-array-after-k-negations
0
1
```\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:\n nums.sort(key=abs, reverse=True)\n for i in range(len(nums)):\n if nums[i] < 0 and k > 0:\n nums[i] *= -1\n k -= 1\n if k == 0:\n break\n ...
2
Given an integer array `nums` and an integer `k`, modify the array in the following way: * choose an index `i` and replace `nums[i]` with `-nums[i]`. You should apply this process exactly `k` times. You may choose the same index `i` multiple times. Return _the largest possible sum of the array after modifying it i...
null
Greedy approach - python
maximize-sum-of-array-after-k-negations
0
1
```\nclass Solution:\n def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:\n nums.sort(key=abs, reverse=True)\n for i in range(len(nums)):\n if nums[i] < 0 and k > 0:\n nums[i] *= -1\n k -= 1\n if k == 0:\n break\n ...
2
You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them. We repeatedly make **duplicate removals** on `s` until we no longer can. Return _the final string after all such duplicate removals have been made_...
null
[PYTHON 3] Min Heap | Few - Lines | Easy to Read Solution
maximize-sum-of-array-after-k-negations
0
1
```\nclass Solution:\n def largestSumAfterKNegations(self, min_heap: List[int], K: int) -> int:\n heapify(min_heap)\n while K > 0:\n heappush(min_heap , - (heappop(min_heap))) \n K -= 1\n return sum(min_heap)\n```
10
Given an integer array `nums` and an integer `k`, modify the array in the following way: * choose an index `i` and replace `nums[i]` with `-nums[i]`. You should apply this process exactly `k` times. You may choose the same index `i` multiple times. Return _the largest possible sum of the array after modifying it i...
null
[PYTHON 3] Min Heap | Few - Lines | Easy to Read Solution
maximize-sum-of-array-after-k-negations
0
1
```\nclass Solution:\n def largestSumAfterKNegations(self, min_heap: List[int], K: int) -> int:\n heapify(min_heap)\n while K > 0:\n heappush(min_heap , - (heappop(min_heap))) \n K -= 1\n return sum(min_heap)\n```
10
You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them. We repeatedly make **duplicate removals** on `s` until we no longer can. Return _the final string after all such duplicate removals have been made_...
null
Solution
clumsy-factorial
1
1
```C++ []\nclass Solution {\n public:\n int clumsy(int N) {\n if (N == 1)\n return 1;\n if (N == 2)\n return 2;\n if (N == 3)\n return 6;\n if (N == 4)\n return 7;\n if (N % 4 == 1)\n return N + 2;\n if (N % 4 == 2)\n return N + 2;\n if (N % 4 == 3)\n return N - ...
443
The **factorial** of a positive integer `n` is the product of all positive integers less than or equal to `n`. * For example, `factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1`. We make a **clumsy factorial** using the integers in decreasing order by swapping out the multiply operations for a fixed rotation o...
null
Solution
clumsy-factorial
1
1
```C++ []\nclass Solution {\n public:\n int clumsy(int N) {\n if (N == 1)\n return 1;\n if (N == 2)\n return 2;\n if (N == 3)\n return 6;\n if (N == 4)\n return 7;\n if (N % 4 == 1)\n return N + 2;\n if (N % 4 == 2)\n return N + 2;\n if (N % 4 == 3)\n return N - ...
443
You are given an array of `words` where each word consists of lowercase English letters. `wordA` is a **predecessor** of `wordB` if and only if we can insert **exactly one** letter anywhere in `wordA` **without changing the order of the other characters** to make it equal to `wordB`. * For example, `"abc "` is a **...
null
Simplest Solution with Python Conditional Statements
clumsy-factorial
0
1
\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nHere I\'m using the basic conditional statements.\nThe \'while loop\' along with some \'if\' conditions are used.\n<!-- Describe your approach to solving the problem. -->\n\n# Code\n```\nclass Solution:\n def clumsy(self, n: int) ->...
1
The **factorial** of a positive integer `n` is the product of all positive integers less than or equal to `n`. * For example, `factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1`. We make a **clumsy factorial** using the integers in decreasing order by swapping out the multiply operations for a fixed rotation o...
null
Simplest Solution with Python Conditional Statements
clumsy-factorial
0
1
\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nHere I\'m using the basic conditional statements.\nThe \'while loop\' along with some \'if\' conditions are used.\n<!-- Describe your approach to solving the problem. -->\n\n# Code\n```\nclass Solution:\n def clumsy(self, n: int) ->...
1
You are given an array of `words` where each word consists of lowercase English letters. `wordA` is a **predecessor** of `wordB` if and only if we can insert **exactly one** letter anywhere in `wordA` **without changing the order of the other characters** to make it equal to `wordB`. * For example, `"abc "` is a **...
null
O(n) Stack Python3 Solution
clumsy-factorial
0
1
```\nclass Solution:\n \n # O(n) time,\n # O(n) space,\n # Approach: stack, \n def clumsy(self, n: int) -> int:\n stack = [n]\n \n turn = 0\n # multiplicaiton and division\n for num in range(n-1, 0, -1):\n if turn % 4 == 0:\n num1 = stack.pop()...
1
The **factorial** of a positive integer `n` is the product of all positive integers less than or equal to `n`. * For example, `factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1`. We make a **clumsy factorial** using the integers in decreasing order by swapping out the multiply operations for a fixed rotation o...
null
O(n) Stack Python3 Solution
clumsy-factorial
0
1
```\nclass Solution:\n \n # O(n) time,\n # O(n) space,\n # Approach: stack, \n def clumsy(self, n: int) -> int:\n stack = [n]\n \n turn = 0\n # multiplicaiton and division\n for num in range(n-1, 0, -1):\n if turn % 4 == 0:\n num1 = stack.pop()...
1
You are given an array of `words` where each word consists of lowercase English letters. `wordA` is a **predecessor** of `wordB` if and only if we can insert **exactly one** letter anywhere in `wordA` **without changing the order of the other characters** to make it equal to `wordB`. * For example, `"abc "` is a **...
null
Clumsy factorial Python3
clumsy-factorial
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. -->\nIve used stack and implemented it in a for loop and used mod 4 to find the operation to be used in any particular number of the input range.\n# Complexity\n- Time comp...
0
The **factorial** of a positive integer `n` is the product of all positive integers less than or equal to `n`. * For example, `factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1`. We make a **clumsy factorial** using the integers in decreasing order by swapping out the multiply operations for a fixed rotation o...
null
Clumsy factorial Python3
clumsy-factorial
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. -->\nIve used stack and implemented it in a for loop and used mod 4 to find the operation to be used in any particular number of the input range.\n# Complexity\n- Time comp...
0
You are given an array of `words` where each word consists of lowercase English letters. `wordA` is a **predecessor** of `wordB` if and only if we can insert **exactly one** letter anywhere in `wordA` **without changing the order of the other characters** to make it equal to `wordB`. * For example, `"abc "` is a **...
null
Two Lines with eval()
clumsy-factorial
0
1
using an infinite iterator (```cycle```)\n```\nclass Solution:\n def clumsy(self, n: int) -> int:\n ops = cycle( [ "*", "//", "+", "-" ] )\n return eval("".join(str(i) + next(ops) for i in range(n, 1, -1)) + "1")```
0
The **factorial** of a positive integer `n` is the product of all positive integers less than or equal to `n`. * For example, `factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1`. We make a **clumsy factorial** using the integers in decreasing order by swapping out the multiply operations for a fixed rotation o...
null
Two Lines with eval()
clumsy-factorial
0
1
using an infinite iterator (```cycle```)\n```\nclass Solution:\n def clumsy(self, n: int) -> int:\n ops = cycle( [ "*", "//", "+", "-" ] )\n return eval("".join(str(i) + next(ops) for i in range(n, 1, -1)) + "1")```
0
You are given an array of `words` where each word consists of lowercase English letters. `wordA` is a **predecessor** of `wordB` if and only if we can insert **exactly one** letter anywhere in `wordA` **without changing the order of the other characters** to make it equal to `wordB`. * For example, `"abc "` is a **...
null
Reslove this in clumsy way
clumsy-factorial
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe first 4 numbers are positive, then the following numbers are regular (-n*(n-1)/(n-2)+(n-3)), and last will reamin less than 4 numbers.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nMany IFs, even nest IFs. It\'...
0
The **factorial** of a positive integer `n` is the product of all positive integers less than or equal to `n`. * For example, `factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1`. We make a **clumsy factorial** using the integers in decreasing order by swapping out the multiply operations for a fixed rotation o...
null
Reslove this in clumsy way
clumsy-factorial
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe first 4 numbers are positive, then the following numbers are regular (-n*(n-1)/(n-2)+(n-3)), and last will reamin less than 4 numbers.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nMany IFs, even nest IFs. It\'...
0
You are given an array of `words` where each word consists of lowercase English letters. `wordA` is a **predecessor** of `wordB` if and only if we can insert **exactly one** letter anywhere in `wordA` **without changing the order of the other characters** to make it equal to `wordB`. * For example, `"abc "` is a **...
null
Solution
minimum-domino-rotations-for-equal-row
1
1
```C++ []\nclass Solution {\npublic:\n int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {\n int flips = 0;\n\n const int num = tops.size();\n\n int top = tops[0];\n int bottom = bottoms[0];\n\n int symmetricals = (top == bottom) ? 1 : 0;\n\n for (int i = 1; i ...
1
In a row of dominoes, `tops[i]` and `bottoms[i]` represent the top and bottom halves of the `ith` domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.) We may rotate the `ith` domino, so that `tops[i]` and `bottoms[i]` swap values. Return the minimum number of rotations so that all...
null
Solution
minimum-domino-rotations-for-equal-row
1
1
```C++ []\nclass Solution {\npublic:\n int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {\n int flips = 0;\n\n const int num = tops.size();\n\n int top = tops[0];\n int bottom = bottoms[0];\n\n int symmetricals = (top == bottom) ? 1 : 0;\n\n for (int i = 1; i ...
1
You are given an array of integers `stones` where `stones[i]` is the weight of the `ith` stone. We are playing a game with the stones. On each turn, we choose any two stones and smash them together. Suppose the stones have weights `x` and `y` with `x <= y`. The result of this smash is: * If `x == y`, both stones ar...
null