inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def maxLength(self, arr) -> int:
maxLen = 0
arr = sorted(arr, key=lambda x: len(x), reverse=True)
self.compatible = {}
for i in range(len(arr)):
currString = arr[i]
if self.noDupes(currString):
if currString not in self.compatible:
self.compatible[currString] = []
for j in range(i+1, len(arr)):
if self.noDupes(arr[j]) and self.uniqueChars(currString, arr[j]):
self.compatible[currString].append(arr[j])
for key in self.compatible:
if len(self.compatible[key]) == 0:
length = len(key)
else:
length = self.findLength(key, key, 0)
maxLen = max(maxLen, length)
return maxLen
def uniqueChars(self, s1, s2):
for c in s2:
if c in s1:
return False
return True
def noDupes(self, string):
counts = {}
for c in string:
counts[c] = counts.get(c, 0) + 1
if counts[c] > 1:
return False
return True
def findLength(self, currString, totalString, maxLen):
for string in self.compatible[currString]:
if self.uniqueChars(totalString, string):
maxLen = max(maxLen, self.findLength(string, totalString + string, len(totalString) + len(string)))
return maxLen
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def __init__(self):
self._maxLength = 0
def maxLength(self, arr: List[str]) -> int:
self.uniqueLengths(arr, [])
return self._maxLength
def uniqueLengths(self, arr, concat):
result = ''.join(concat)
if not self.hasUniqueChars(result):
return
self._maxLength = max(self._maxLength, len(result))
for i in range(len(arr)):
concat.append(arr[i])
self.uniqueLengths(arr[i+1:], concat)
concat.pop()
def hasUniqueChars(self, concat):
counter = Counter(concat)
return all([x == 1 for x in counter.values()])
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
from collections import Counter
class Solution:
def maxLength(self, arr: List[str]) -> int:
max_str = 0
for i in range(len(arr)):
if self.is_unique(arr[i]) and len(arr[i]) > max_str:
max_str = len(arr[i])
concat = [arr[i]]
for j in range(i+1, len(arr)):
for x in concat:
c = x + arr[j]
if self.is_unique(c):
if len(c) > max_str:
max_str = len(c)
concat.append(c)
return max_str
def is_unique(self, str):
for v in Counter(str).values():
if v > 1:
return False
return True
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def is_unique(self, s):
return len(set(s)) == len(s)
def maxLength(self, arr: List[str]) -> int:
dct= {}
maxVal = 0
for i in range(0, len(arr)):
if self.is_unique(arr[i]):
v = frozenset(arr[i])
dct[v] = len(v)
maxVal = max(maxVal, len(v))
for j in range(i+1, len(arr)):
t = arr[i] + arr[j]
val = frozenset(t)
if val not in dct.keys():
if self.is_unique(t):
# print(t)
arr.append(t)
dct[val] = len(val)
maxVal = max(maxVal, len(val))
print(dct)
return maxVal
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def __init__(self):
self._maxLength = 0
def maxLength(self, arr: List[str]) -> int:
self.uniqueLengths(arr, [])
return self._maxLength
def uniqueLengths(self, arr, concat):
if not self.hasUniqueChars(''.join(concat)):
return
self._maxLength = max(self._maxLength, len(''.join(concat)))
for i in range(len(arr)):
concat.append(arr[i])
self.uniqueLengths( arr[i+1:], concat)
concat.pop()
def hasUniqueChars(self, concat):
counter = Counter(concat)
return all([x == 1 for x in counter.values()])
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution(object):
def maxLength(self, A):
for i in range(len(A) - 1, -1, -1):
if len(set(A[i])) != len(A[i]):
A.pop(i)
N = len(A)
B = []
for word in A:
ct = [0] * 26
for letter in word:
ct[ord(letter) - ord('a')] += 1
B.append(ct)
self.ans = 0
count = [0] * 26
def search(i):
if i == N:
cand = sum(count)
if cand > self.ans: self.ans = cand
return
for letter, ct in enumerate(B[i]):
if ct and count[letter]:
search(i+1)
break
else:
search(i+1)
for letter, ct in enumerate(B[i]):
if ct:
count[letter] += 1
search(i+1)
for letter, ct in enumerate(B[i]):
if ct:
count[letter] -= 1
search(0)
return self.ans
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def maxLength(self, arr: List[str]) -> int:
d = {}
def helper(banned,start):
if start == len(arr):
return len(banned)
k = tuple(sorted(str(banned)+str(start)))
if k in d:
return d[k]
string = arr[start]
new_set = set()
for i in string:
if i in banned or i in new_set:
return helper(banned,start+1)
new_set.add(i)
new_banned = banned.union(new_set)
val = max(helper(new_banned,start+1),helper(banned,start+1))
d[k] = val
return val
initial_banned = set()
res = helper(initial_banned,0)
return res
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def maxLength(self, arr: List[str]) -> int:
newarr = []
for w in arr:
if len(set(w)) == len(w):
newarr.append(w)
arr = newarr
n = len(arr)
res = 0
str_masks = [None] * n
for i in range(n):
wd = arr[i]
mask = 0
for ch in wd:
ch_pos = ord(ch) - ord('a')
mask |= 1 << ch_pos
str_masks[i] = mask
def dfs(idx, mask, curlen):
nonlocal res
if idx == n:
res = max(res, curlen)
return
# use arr[idx]
if mask & str_masks[idx] == 0:
# can use arr[idx]
dfs(idx + 1, mask | str_masks[idx], curlen + len(arr[idx]))
# not use arr[idx]
dfs(idx + 1, mask, curlen)
dfs(0, 0, 0)
return res
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
from collections import defaultdict
class Solution:
def maxLength(self, arr: List[str]) -> int:
self.longest = 0
def valid(s):
d = defaultdict(int)
for char in s:
d[char] += 1
if d[char] == 2:
return False
return True
def backtrack(curr, ind):
if ind == len(arr):
if len(curr) > self.longest: self.longest = len(curr)
return
for i in range(ind, len(arr)):
temp = curr+arr[i]
if valid(temp):
backtrack(curr+arr[i], i + 1)
if len(curr) > self.longest: self.longest = len(curr)
return
for i in range(len(arr)):
if valid(arr[i]):
backtrack(arr[i], i)
return self.longest
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def maxLength(self, arr: List[str]) -> int:
newarr = []
for cur in arr:
newarr.append(collections.Counter(cur))
res = [0]
def helper(cur, index):
if index == len(arr):
res[0] = max(res[0], len(cur))
else:
if len(arr[index]) == len(newarr[index]):
if not newarr[index] & cur:
helper(newarr[index] + cur, index+1)
helper(cur, index+1)
helper(collections.Counter(''), 0)
return res[0]
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def maxLength(self, arr: List[str]) -> int:
newarr = []
for cur in arr:
newarr.append(collections.Counter(cur))
res = [0]
def helper(cur, index):
if index == len(arr):
res[0] = max(res[0], len(cur))
else:
if len(arr[index]) == len(newarr[index]) and not newarr[index] & cur:
helper(newarr[index] + cur, index+1)
helper(cur, index+1)
helper(collections.Counter(''), 0)
return res[0]
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
from collections import Counter
class Solution:
def back(self, arr, soln, solns):
# print(\"Arr {} | Soln {}\".format(arr, soln))
if not arr:
solns.append([len(soln),soln])
return solns
for i in range(len(arr)):
sa = set(arr[i])
sb = set(soln)
c = dict(Counter(arr[i]))
if max(c.values()) == 1 and len(sb.intersection(sa))==0:
solncopy = soln
solncopy+=arr[i]
solns = self.back(arr[i+1:],solncopy, solns)
solns.append([len(soln),soln])
return solns
def maxLength(self, arr: List[str]) -> int:
solns = []
for i in range(len(arr)):
soln = arr[i]
c = dict(Counter(arr[i]))
if max(c.values()) == 1:
solns = self.back(arr[i+1:],soln, solns)
# print(solns)
# for s in solns:
# print(s)
if not solns:
return 0
return max(solns, key=lambda x:x[0])[0]
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def maxLength(self, arr: List[str]) -> int:
letters_dict = {}
def dfs(letter_set, arr, i):
if i >= len(arr):
return 0
if (tuple(letter_set), i) not in letters_dict:
if len(set(arr[i]).intersection(letter_set)) == 0 and (len(set(arr[i])) == len(arr[i])):
letters_dict[(tuple(letter_set), i)] = max(
dfs(letter_set.union(set(arr[i])), arr, i+1)+len(set(arr[i])),
dfs(letter_set, arr, i+1))
else:
letters_dict[(tuple(letter_set), i)] = dfs(letter_set, arr, i+1)
return letters_dict[(tuple(letter_set), i)]
return dfs(set(), arr, 0)
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def maxLength(self, arr: List[str]) -> int:
arr_sets = [set(x) for x in arr]
def buildSet(bitlist):
output = set()
for i, bit in enumerate(bitlist):
if bit:
output = output.union(arr_sets[i])
return output
def recurse(bitlist, index):
if index == len(arr):
return len(buildSet(bitlist))
not_included = recurse(bitlist, index+1)
if len(arr_sets[index]) != len(arr[index]) or buildSet(bitlist).intersection(arr_sets[index]):
return not_included
else:
new_bitlist = bitlist[:]
new_bitlist[index] = 1
return max(not_included, recurse(new_bitlist, index+1))
return recurse([0 for _ in arr], 0)
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def maxLength(self, arr: List[str]) -> int:
self.res = 0
def walk(i, temp):
self.res = max(self.res, len(temp))
for j in range(i, len(arr)):
if all(v <= 1 for v in list(collections.Counter(temp+arr[j]).values())):
walk(j+1, temp+arr[j])
return
walk(0, '')
return self.res
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
import collections
class Solution:
def maxLength(self, arr: List[str]) -> int:
self.m = collections.defaultdict(list)
for ix_1 in range(len(arr)):
set_1 = set(list(arr[ix_1]))
for ix_2 in range(ix_1+1, len(arr)):
set_2 = set(list(arr[ix_2]))
if len(set_1) == len(arr[ix_1]) and len(set_2) == len(arr[ix_2]) and len(set_1.intersection(set_2)) == 0:
self.m[ix_1].append(ix_2)
#self.m[ix_2].append(ix_1)
self.ans = 0
for ix in range(len(arr)):
if len(set(list(arr[ix]))) == len(arr[ix]):
self.dfs(arr, [ix])
return self.ans
def dfs(self, arr, visiting):
curr = visiting[-1]
l = [len(arr[i]) for i in visiting]
self.ans = max(sum(l), self.ans)
if not self.m.get(curr):
return
for i in self.m.get(curr):
if i in visiting:
continue
possible = True
for j in visiting[:-1]:
if len(set(list(arr[i])).intersection(set(list(arr[j]))))>0:
possible = False
break
if possible:
visiting.append(i)
self.dfs(arr, visiting)
visiting.pop()
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def maxLength(self, arr: List[str]) -> int:
#array with all possible unique string combinations so far
possible_strings = ['']
maximum = 0
for i in range(len(arr)):
temp_len = len(possible_strings)
for j in range(temp_len):
#trying combination
x = arr[i] + possible_strings[j]
#string is unique
if (len(x)==len(set(x))):
#append to possible strings
possible_strings.append(x)
#set max
maximum = max(maximum,len(x))
return maximum
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def is_unique(self, s):
return len(set(s)) == len(s)
def maxLength(self, arr: List[str]) -> int:
dct= {}
maxVal = 0
for i in range(0, len(arr)):
if self.is_unique(arr[i]):
maxVal = max(maxVal, len(arr[i]))
for j in range(i+1, len(arr)):
t = arr[i] + arr[j]
if self.is_unique(t):
arr.append(t)
maxVal = max(maxVal, len(t))
# print(dct)
return maxVal
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def maxLength(self, arr: List[str]) -> int:
if len(arr)==1:
if len(set(arr[0]))==len(arr[0]):
return len(arr[0])
else:
return 0
new_arr = []
for s in arr:
if len(s) == len(set(s)):
new_arr.append((set(s), len(s)))
if not new_arr:
return 0
# print(new_arr)
ans = 0
def backtrack(ind, temp, temp_sum):
nonlocal ans
if temp_sum > ans:
ans = temp_sum
for i in range(ind, len(new_arr)):
if not (new_arr[i][0] & temp):
backtrack(i+1, temp | new_arr[i][0], temp_sum+new_arr[i][1])
else:
backtrack(i+1, temp, temp_sum)
backtrack(0, set(), 0)
return ans
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def is_unique(self, s):
return len(set(s)) == len(s)
def maxLength(self, arr: List[str]) -> int:
dct= {}
maxVal = 0
for i in range(0, len(arr)):
if self.is_unique(arr[i]):
maxVal = max(maxVal, len(arr[i]))
for j in range(i+1, len(arr)):
t = arr[i] + arr[j]
if self.is_unique(t):
# print(t)
arr.append(t)
maxVal = max(maxVal, len(t))
# print(dct)
return maxVal
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def is_unique(self, s):
return len(set(s)) == len(s)
def maxLength(self, arr: List[str]) -> int:
maxVal = 0
for i in range(0, len(arr)):
if self.is_unique(arr[i]):
maxVal = max(maxVal, len(arr[i]))
for j in range(i+1, len(arr)):
t = arr[i] + arr[j]
if self.is_unique(t):
arr.append(t)
maxVal = max(maxVal, len(t))
# print(dct)
return maxVal
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
from collections import Counter
class Solution:
def maxLength(self, arr: List[str]) -> int:
self.max_len = 0
for i, s in enumerate(arr):
self.bfs(Counter(s), arr, i)
return self.max_len
def bfs(self, counter, arr, _index):
if max(counter.values()) > 1: return
if len(counter) > self.max_len:
self.max_len = len(counter)
for i in range(_index + 1, len(arr)):
s = arr[i]
counter_copy = copy.copy(counter)
counter_copy.update(Counter(s))
self.bfs(counter_copy, arr, i)
|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters.
|
class Solution:
def maxLength(self, arr: List[str]) -> int:
res = 0
arr = list([x for x in arr if len(set(x))==len(x)])
arr = [set(x) for x in arr]
def dfs(n, s, l, cur):
nonlocal res
if n == l:
res = max(res, len(cur))
return
for i in range(s, len(arr)):
if len(cur.intersection(arr[i])) == 0:
dfs(n, i+1, l+1, cur.union(arr[i]))
# new_cur = cur + arr[i]
# if len(set(new_cur)) != len(new_cur):
# return
# dfs(n, i+1, l+1, new_cur)
for i in range(len(arr)+1):
dfs(i, 0, 0, set())
return res
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
sum_set = set()
sum_set.add(0)
temp = 0
count = 0
for num in nums:
temp += num
if temp - target in sum_set:
count += 1
sum_set.clear()
sum_set.add(0)
temp = 0
continue
sum_set.add(temp)
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
res=0
s=set()
s.add(0)
t=0
for x in nums:
t+=x
if t-target in s:
res+=1
s.clear()
s.add(t)
return res
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
n = len(nums)
cumsum = [0]*(n+1)
for i in range(n):
cumsum[i+1] = cumsum[i] + nums[i]
d = {0:0}
temp = []
for i in range(n):
# print(i, cumsum[i+1]-target)
if cumsum[i+1]-target in d:
temp.append((d[cumsum[i+1]-target], i+1))
# print(d[cumsum[i+1]-target], i+1)
d[cumsum[i+1]] = i+1
l = sorted(temp, key=lambda x:(x[0],x[1]))
cur_s = -1
cur_e = -1
total = 0
for s,e in l:
if s >= cur_e:
total += 1
cur_s = s
cur_e = e
elif e < cur_e:
cur_s = s
cur_e = e
return total
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
ans, m, j, cur = 0, {0:-1}, -1, 0
for i, x in enumerate(nums):
cur += x
k = cur - target
if k in m and m[k] >= j:
ans += 1; j = i
m[cur] = i
return ans
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
dp = [0] * len(nums)
prev_sum = {0: -1}
num_nonover = 0
current_sum = 0
for (index, num) in enumerate(nums):
current_sum += num
if current_sum - target in prev_sum:
gain = dp[prev_sum[current_sum-target]] if prev_sum[current_sum-target] >= 0 else 0
num_nonover = max(num_nonover, gain + 1)
dp[index] = num_nonover
prev_sum[current_sum] = index
return num_nonover
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
curSum = 0
sumsSet = {0}
result = 0
for x in nums:
curSum += x
if (curSum - target) in sumsSet:
result += 1
curSum = 0
sumsSet.clear()
sumsSet.add(curSum)
return result
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
n = len(nums)
dp = [0] * (n+1)
dic = {}
dic[0] = 0
cur = 0
for i in range(1, n+1):
cur += nums[i-1]
if cur - target in dic:
dp[i] = 1 + dp[dic[cur - target]]
dp[i] = max(dp[i-1], dp[i])
dic[cur] = i
return dp[n]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
d = {}
d[0] = -1
N = len(nums)
if(N==0):
return 0
L = [0 for i in range(N)]
s = nums[0]
d[s] = 0
if(s==target):
L[0] = 1
for i in range(1,N):
s += nums[i]
L[i] = L[i-1]
if(s-target in d):
idx = d[s-target]
if(idx==-1):
L[i] = max(L[i],1)
else:
L[i] = max(L[i],L[idx]+1)
d[s] = i
return L[N-1]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums, target):
ans = 0
cur = 0
tmp = set([0])
for n in nums:
cur += n
if cur - target in tmp:
ans += 1
cur = 0
tmp = set([0])
tmp.add(cur)
return ans
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
P = [0]
for e in nums:
P.append(P[-1]+e)
seen = set()
ans = 0
for psum in P:
if psum - target in seen:
seen = {psum}
ans += 1
else:
seen.add(psum)
return ans
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
pre = [0]
for n in nums:
pre.append(pre[-1] + n)
dp = {0: 0}
counter = [0] * len(pre)
for i in range(1, len(pre)):
if pre[i] - target in dp:
idx = dp[pre[i] - target]
counter[i] = max(counter[i-1], counter[idx] + 1)
else:
counter[i] = counter[i-1]
dp[pre[i]] = i
return counter[-1]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
prefixsum = {}
prefixsum[0] = True
prev = 0
res = 0
for num in nums:
curr = prev + num
if curr - target in prefixsum:
res += 1
prefixsum = {}
prefixsum[0] = True
prev = 0
else:
prefixsum[curr] = True
prev = curr
print(res)
return res
'''
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
prefixsum = {}
prefixsum[0] = 0
res = 0
last = -1
prev = 0
for idx, num in enumerate(nums):
curr = prev + num
prefixsum[curr] = idx
if curr - target in prefixsum:
if prefixsum[curr - target] >= last:
res += 1
last = prefixsum[curr]
prev = curr
print(prefixsum)
print(res)
return res
'''
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
### good explanation in https://www.youtube.com/watch?v=EW521e8c8lk
### video starts 10:40
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
prefixsum = {}
prefixsum[0] = True
prev = 0
res = 0
for num in nums:
curr = prev + num
if curr - target in prefixsum:
res += 1
prefixsum = {}
prefixsum[0] = True
prev = 0
else:
prefixsum[curr] = True
prev = curr
print(res)
return res
'''
It seems that I have exact the same idea as the following post
https://leetcode.com/problems/maximum-number-of-non-overlapping-subarrays-with-sum-equals-target/discuss/780882/Java-14-lines-Greedy-PrefixSum-with-line-by-line-explanation-easy-to-understand
why does the following code now working????
### The following code does not pass submission .....
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
prefixsum = {}
prefixsum[0] = -1
res = 0
last = -1
prev = 0
for idx, num in enumerate(nums):
curr = prev + num
prefixsum[curr] = idx
if curr - target in prefixsum:
if prefixsum[curr - target] >= last:
res += 1
last = prefixsum[curr]
prev = curr
print(prefixsum)
print(res)
return res
'''
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
n = len(nums)
dp = [0]*(n+1)
dict = {}
dict[0] = 0
curr = 0
for i in range(1,n+1):
curr += nums[i-1]
miss = curr - target
if miss in dict:
dp[i]= 1 + dp[dict[miss]]
dp[i] = max(dp[i-1],dp[i])
dict[curr]=i
return dp[n]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
res = 0
csum = [0]
for n in nums:
csum.append(csum[-1]+n)
# print (csum[1:])
# print ([c-target for c in csum[1:]])
seen = set([])
for c in csum[:]:
# print (c, c-target, seen)
if c-target in seen:
res += 1
seen.clear()
seen.add(c)
return res
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
csum = {}
csum[0] = 0
dp = [0 for i in range(len(nums)+1)]
sum,best = (0,0)
for i,v in enumerate(nums):
sum += v
x = sum - target
dp[i+1] = dp[i]
if x in csum:
dp[i+1] = max(dp[i+1], dp[csum[x]] + 1)
best = max(best, dp[i+1])
csum[sum] = i + 1
return best
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
count = 0
sums = set()
sums.add(0)
tot = 0
for num in nums:
tot += num
if (tot - target) in sums:
count += 1
sums=set()
sums.add(tot)
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
lookup = {0 : -1}
running_sum = 0
count = 0
for i in range(len(nums)):
running_sum += nums[i]
if running_sum - target in lookup:
count += 1
lookup = {} #reset the map
lookup[running_sum] = i
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
dp = [0] + [-1] * len(nums)
s = 0
dic = {}
dic[0] = 0
res = 0
for i in range(len(nums)):
s += nums[i]
if s - target in dic:
dp[i+1] = max(dp[dic[s-target]] + 1, dp[i])
else:
dp[i+1] = dp[i]
dic[s] = i + 1
print(dp)
return dp[-1]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
for i in range(1, len(nums)):
nums[i] += nums[i - 1]
res = 0
seen = {}
used_i = 0
nums = [0] + nums
for i, num in enumerate(nums):
if num - target in seen and seen[num - target] >= used_i:
res += 1
used_i = i
seen[num] = i
return res
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
prefix_sum = count = 0
seen = {0}
for num in nums:
prefix_sum += num
if prefix_sum - target in seen:
prefix_sum = 0
count += 1
seen = set([0])
seen.add(prefix_sum)
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
dic = {0:-1}
acc = 0
last = -1
res = 0
for i, n in enumerate(nums):
acc += n
if acc - target in dic and dic[acc-target] >= last:
res += 1
last = i
dic[acc] = i
return res
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
m = {0: 0}
res = 0
sum = 0
for x in nums:
sum += x
if sum - target in m:
res = max(res, m[sum - target] + 1)
m[sum] = res
return res
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
cache = set([0])
cur, res = 0, 0
for i in range(len(nums)):
cur += nums[i]
if cur - target in cache:
res += 1
cache.clear()
cache.add(cur)
return res
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
count = 0
total = 0
pastSums = set()
for n in nums:
total += n
if total == target or total - target in pastSums:
total = 0
count += 1
pastSums.clear()
else:
pastSums.add(total)
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
prefix_sum = count = 0
prev_subarray_end = -1
seen = {0 : -1}
for i, num in enumerate(nums):
prefix_sum += num
if seen.get(prefix_sum - target, -2) >= prev_subarray_end:
count += 1
prev_subarray_end = i
seen[prefix_sum] = i
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
dp = [0] + list(accumulate(nums))
len_dp = len(dp)
answer = 0
lo = 0
lo_sum = 0
for hi in range(1, len_dp):
if dp[hi] < lo_sum:
lo_sum = dp[hi]
if dp[hi] - lo_sum >= target:
if dp[hi] - target in dp[lo:hi]:
answer += 1
lo = hi
lo_sum = dp[hi]
return answer
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
curr = 0
ans = 0
seen = set()
for num in nums:
seen.add(curr)
curr += num
want = curr - target
if want in seen:
ans += 1
seen = set()
curr = 0
return ans
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
psum = [0]
for v in nums:
psum.append(v+psum[-1])
def helper(lo, target):
d = {psum[lo-1]}
ans = 0
for i in range(lo, len(psum)):
if psum[i] - target in d:
ans = 1 + helper(i+1, target)
break
d.add(psum[i])
return ans
return helper(1, target)
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
cum_map = {
0: -1,
}
intervals = [-1] * len(nums)
cur = 0
for i, x in enumerate(nums):
cur += x
rest = cur - target
boundary = cum_map.get(rest)
if boundary is not None:
intervals[i] = boundary + 1
cum_map[cur] = i
res = 0
s = [0] * len(nums)
for i in range(len(nums)):
if intervals[i] == i:
s[i] = s[i-1] + 1
continue
s[i] = s[i-1]
if intervals[i] == 0:
s[i] = max(s[i-1], 1)
elif intervals[i] > 0:
s[i] = max(s[i-1], s[intervals[i] - 1] + 1)
return s[-1]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
from collections import defaultdict
from bisect import *
class Solution:
def maxNonOverlapping(self, lst: List[int], k: int) -> int:
n = len(lst)
first = 0
last = 0
total = lst[0]
dct = defaultdict(list)
dp = [0 for i in range(n+1)]
for i in range(1,n+1):
dp[i] = dp[i-1]+lst[i-1]
dct[dp[i]].append(i)
intervals = [ ]
for i in range(n+1):
# for temp in dct[k+dp[i]]:
# if(temp>i):
# intervals.append((i,temp))
left = bisect_right(dct[k+dp[i]],i)
if(left!=len(dct[k+dp[i]])):
intervals.append((i,dct[k+dp[i]][left]))
intervals.sort(key = lambda x:x[1])
pre = -float('inf')
ans = 0
index = 0
print(intervals)
while(index<len(intervals)):
if(intervals[index][0]>=pre):
ans+=1
pre = intervals[index][1]
index+=1
return ans
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
pref = {}
pref[0] = -1
ans = 0
presum = 0
j = -1
for i in range(len(nums)):
presum += nums[i]
c = presum - target
if c in pref and pref[c] >= j:
ans += 1
j = i
pref[presum] = j
return ans
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
N = len(nums)
prefix_sum = [0 for _ in range(N)]
prefix_sum[0] = nums[0]
for i in range(1, N):
prefix_sum[i] = nums[i] + prefix_sum[i-1]
prev_prefix = {}
last_index = -1
subarrays = 0
for i in range(N):
prefix = prefix_sum[i]
to_look = prefix - target
if to_look in prev_prefix and prev_prefix[to_look] >= last_index:
last_index = i
subarrays += 1
elif nums[i] == target:
subarrays += 1
last_index = i
elif prefix == target and last_index == -1:
subarrays += 1
last_index = i
prev_prefix[prefix] = i
return subarrays
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
n = len(nums)
dp = [0] * (n+1)
dic = {}
dic[0] = 0
curr = 0
for i in range(1, n+1):
curr += nums[i-1]
if curr - target in dic:
dp[i] = dp[dic[curr-target]] + 1
dp[i] = max(dp[i-1], dp[i])
dic[curr] = i
return dp[-1]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums, target) -> int:
hashMap = {0: -1} #totalSum -> inclusive ending index
totalSum = 0
answer = []
count = 0
for i in range(len(nums)):
totalSum += nums[i]
if totalSum - target in hashMap:
answer.append([hashMap[totalSum-target] + 1, i])
hashMap[totalSum] = i
ending = None
for i in answer:
if ending is None or i[0] > ending:
count += 1
ending = i[1]
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
D = {0:-1}
cumsum = 0
idx = -1
res = 0
for i, x in enumerate(nums):
cumsum += x
if cumsum - target in D and D[cumsum-target] >= idx:
res += 1
idx = i
D[cumsum] = i
return res
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
s = set()
s.add(0)
ans = 0
prefsum = 0
for num in nums:
prefsum += num
if prefsum - target in s:
ans += 1
s = set()
s.add(prefsum)
return ans
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
dp = [0] * (len(nums)+1)
acc = [0] * (len(nums)+1)
sumDict = {0:0}
for k, ele in enumerate(nums):
dk = k+1
acc[dk] = acc[k] + ele
if acc[dk]-target in sumDict:
dp[dk] = max(dp[k], dp[sumDict[acc[dk]-target]]+1)
else:
dp[dk] = dp[k]
sumDict[acc[dk]] = dk
return dp[len(nums)]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
from collections import defaultdict
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
cum_sum=[0]
for i in range(len(nums)): cum_sum.append(cum_sum[-1]+nums[i])
mem=defaultdict(list)
for i in range(len(nums)+1):
mem[cum_sum[i]].append(i)
def binSearch(index):
arr=mem[cum_sum[index]-target]
right=len(arr)-1
left=0
best=None
while left<=right:
mid=(left+right)//2
if arr[mid]<index:
best=arr[mid]
left=mid+1
else:
right=mid-1
return best
dp=[0 for i in range(len(nums)+1)]
for i in range(1,len(nums)+1):
ind=binSearch(i)
s=0
if ind!=None:
s=1+dp[ind]
dp[i]=max(dp[i-1],s)
return dp[-1]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
N = len(nums)
prefix_sum = [0 for _ in range(N)]
prefix_sum[0] = nums[0]
for i in range(1, N):
prefix_sum[i] = nums[i] + prefix_sum[i-1]
# TODO: there is a bug here..not sure what the hell it is though!!!!
prev_prefix = {}
last_index = -1
subarrays = 0
for i in range(N):
prefix = prefix_sum[i]
to_look = prefix - target
if to_look in prev_prefix and prev_prefix[to_look] >= last_index:
last_index = i
subarrays += 1
elif nums[i] == target:
subarrays += 1
last_index = i
elif prefix == target and last_index == -1:
subarrays += 1
last_index = i
prev_prefix[prefix] = i
return subarrays
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
prefix_sum = 0
seen = {0}
res = 0
for n in nums:
prefix_sum += n
if prefix_sum - target in seen:
seen = {prefix_sum}
res += 1
seen.add(prefix_sum)
return res
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
# Taking a walk through the array, if we can ever make the target, we might as well because we'd like the border for the last subarray to be as far left as possible for the next. To see if we can make the target, we just take the total sum so far and see if we ever had the right previous total sum to chop off. We also check that the chop point does not overlap another subarray end point we used.
#maintain a list of end indices
hmap = collections.defaultdict(int)
rsum = 0
res = 0
last = -1
hmap[0] = -1
for index,num in enumerate(nums):
rsum += num
csum = rsum - target
# if rsum == target:
# res += 1
if csum in hmap and hmap[csum] >= last:
res += 1
last = index
hmap[rsum] = index
return res
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
run_sum = 0
count = 0
book_keep = {0:1}
for num in nums:
run_sum += num
prev_sum = run_sum - target
if prev_sum in book_keep:
count += 1
run_sum = 0
book_keep.clear()
book_keep = {0:1}
else:
book_keep[run_sum] = 1
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
n = len(nums)
for i in range(1, n):
nums[i] += nums[i-1]
seen = {0: 0}
res = 0
for i, n in enumerate(nums):
if n-target in seen:
res += 1
seen = {n:0}
else:
seen[n] = i
return res
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
pre = 0
sums = [0]
for el in nums:
pre += el
sums.append(pre)
#print(\"---\")
# print(sums)
ans = 0
seen = {} # seen[v] = i iff sums[i] == v
for i, v in enumerate(sums):
#print(f\"seen: {seen}\")
if v-target in seen:
ans += 1
seen = {} # satisfies non-overlapping
seen[v] = i
#print(f\"seen: {seen}\")
return ans
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
n = len(nums)
hashmap = {0:0}
ans = 0
sums = 0
for i in range(n):
sums += nums[i]
if sums-target in hashmap:
ans = max(ans, hashmap[sums-target] + 1)
hashmap[sums] = ans
return ans
# n = len(nums)
# preSum = [0 for i in range(n+1)]
# hashmap = {0:0}
# lastend = 0
# ans = 0
# for i in range(1, n+1):
# preSum[i] = preSum[i-1] + nums[i-1]
# begin = hashmap.get(preSum[i]-target, -1)
# if begin >= 0 and begin >= lastend:
# lastend = i
# ans += 1
# pos = hashmap.get(preSum[i], 0)
# hashmap[preSum[i]] = max(pos, i)
# return ans
# n = len(nums)
# dp = [0 for i in range(n+1)]
# for i in range(n):
# tmpsum = 0
# for j in range(i, -1, -1):
# tmpsum += nums[j]
# if tmpsum == target:
# dp[i+1] = max(dp[i+1], dp[j] + 1)
# else:
# dp[i+1] = max(dp[i+1], dp[j])
# print(dp)
# return dp[n]
# n = len(nums)
# dp = [0 for i in range(n)]
# if target == nums[0]:
# dp[0] = 1
# for i in range(1, n):
# tmpsum = 0
# for j in range(i, -1, -1):
# tmpsum += nums[j]
# if tmpsum == target:
# if j > 0:
# dp[i] = max(dp[i], dp[j-1] + 1)
# else:
# dp[i] = max(dp[i], 1)
# break
# print(dp)
# return dp[n-1]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
n = len(nums)
prefix =[0] * (n + 1)
for i in range(n):
prefix[i + 1] = prefix[i] + nums[i]
seen = {0:0}
ans = 0
for i in range(1,n + 1):
current = prefix[i]
a = current - target
if a in seen:
ans += 1
seen = {}
seen[current] = 1
return ans
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
sums=set()
s=0
cnt=0
sums.add(0)
for n in nums:
s+=n
if s-target in sums:
cnt+=1
sums=set()
sums.add(s)
return cnt
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
h = {0: 1}
summ = count = 0
for i in range(0, len(nums)):
summ += nums[i]
if summ - target in h:
count += 1
h = {}
h[summ] = i
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
if not nums: return 0
seen = set([0])
out = 0
curr = 0
for i in nums:
curr += i
if curr - target in seen:
out += 1
seen = set([0])
curr = 0
else:
seen.add(curr)
return out
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
n = len(nums)
res = 0
cumsum = 0
cache = set([0])
for num in nums:
cumsum += num
if cumsum - target in cache:
res += 1
cache = set()
cache.add(cumsum)
return res
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
count = 0
curr_cnt = 0
dic = {0:-1}
for i in range(len(nums)):
curr_cnt += nums[i]
if curr_cnt - target in dic:
count += 1
dic = {}
dic[curr_cnt] = i
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
tot = 0
seen = set([0])
cur = 0
for n in nums:
cur += n
if cur - target in seen:
tot += 1
cur = 0
seen = set([0])
else:
seen.add(cur)
return tot
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
# n = len(nums)
# hashmap = {0:0}
# ans = 0
# sums = 0
# for i in range(n):
# sums += nums[i]
# if sums-target in hashmap:
# ans = max(ans, hashmap[sums-target] + 1)
# hashmap[sums] = ans
# return ans
n = len(nums)
preSum = [0 for i in range(n+1)]
hashmap = {0:0}
lastend = 0
ans = 0
for i in range(1, n+1):
preSum[i] = preSum[i-1] + nums[i-1]
begin = hashmap.get(preSum[i]-target, -1)
if begin >= 0 and begin >= lastend:
lastend = i
ans += 1
hashmap[preSum[i]] = i
return ans
# n = len(nums)
# dp = [0 for i in range(n+1)]
# for i in range(n):
# tmpsum = 0
# for j in range(i, -1, -1):
# tmpsum += nums[j]
# if tmpsum == target:
# dp[i+1] = max(dp[i+1], dp[j] + 1)
# else:
# dp[i+1] = max(dp[i+1], dp[j])
# print(dp)
# return dp[n]
# n = len(nums)
# dp = [0 for i in range(n)]
# if target == nums[0]:
# dp[0] = 1
# for i in range(1, n):
# tmpsum = 0
# for j in range(i, -1, -1):
# tmpsum += nums[j]
# if tmpsum == target:
# if j > 0:
# dp[i] = max(dp[i], dp[j-1] + 1)
# else:
# dp[i] = max(dp[i], 1)
# break
# print(dp)
# return dp[n-1]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
# TC: O(N), SC:O(N)
n = len(nums)
# prefix sum
for i in range(1, n):
nums[i] += nums[i - 1]
seen, dp = {0: -1}, [0] * n
for i in range(n):
x = nums[i] - target
if x in seen:
dp[i] = max(dp[i - 1], dp[seen[x]] + 1)
else:
dp[i] = dp[i - 1]
seen[nums[i]] = i
return dp[-1]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
from typing import List, Tuple
class Solution:
def maxNonOverlappingIntervals(self, intervals: List[Tuple[int, int]], sort=True) -> int:
if not intervals:
return 0
# Sort intervals by ending time
if sort:
intervals = sorted(intervals, key=lambda x: x[1])
ans = 1
cur_end = intervals[0][1]
for start, end in intervals[1:]:
if start <= cur_end:
continue
ans += 1
cur_end = end
return ans
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
# If we can get all subarrays that sum to target in order sorted by ending index,
# then we can simply apply activity selection to it.
# We can use prefix sums to get this^^. Note that because negative numbers are allowed,
# we might get multiple subarrays ending at i that sum to target. However, we can ignore
# all except the smallest one, as we want non-overlapping.
prefix_sum = {0: -1}
cumsum = 0
intervals = []
for i, x in enumerate(nums):
cumsum += x
if cumsum - target in prefix_sum:
intervals.append((prefix_sum[cumsum-target]+1, i))
prefix_sum[cumsum] = i # Don't bother with multiple indices, just use the the latest one
return self.maxNonOverlappingIntervals(intervals, False)
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
n = len(nums)
presum = 0
last_pos = {0: 0}
dp = [0] * (1 + n)
for i in range(1, 1 + n):
presum += nums[i - 1]
dp[i] = dp[i - 1]
if presum - target in last_pos:
pos = last_pos[presum - target]
dp[i] = max(dp[i], dp[pos] + 1)
last_pos[presum] = i
# print(dp)
return dp[n]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
dp=[0]*(1+len(nums))
mem={0:0}
cur=0
for i,v in enumerate(nums):
cur+=v
dp[i+1]=dp[i]
if cur-target in mem:
dp[i+1]=max(dp[i+1],dp[mem[cur-target]]+1)
mem[cur]=i+1
# print(dp)
return dp[-1]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
# idea: greedy. keep a check of cumulated sum, and as soon as we find a subarray whose sum equals target, we reset cumulated sum
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
cum_sum = 0
seen_cum_sum = set([0])
count = 0
for num in nums:
cum_sum += num
complement = cum_sum-target
if complement in seen_cum_sum:
count += 1
cum_sum = 0
seen_cum_sum = set([0])
seen_cum_sum.add(cum_sum)
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
prefixSum = {0:-1}
dp = [0] * len(nums)
currSum = 0
for i in range (len(nums)):
currSum = currSum + nums[i]
if currSum - target in prefixSum:
end = prefixSum[currSum - target]
dp[i] = max(1 + dp[end], dp[i-1])
elif i == 0:
dp[i] = 0
else:
dp[i] = dp[i-1]
prefixSum[currSum] = i
return dp[len(nums)-1]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
prefix = {0:-1}
cur = 0
res = 0
for i,v in enumerate(nums):
cur += v
if (cur - target) in prefix:
res += 1
prefix = {}
prefix[cur] = i
return res
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
'''
Get prefix sum: pref[]
Find the largest sorted list of indices index[] where
pref[index[1]] - pref[index[0]] = pref[index[3]] - pref[index[2]] = ... = target
'''
pref = [0] * (len(nums) + 1)
for i in range(len(nums)):
pref[i+1] = pref[i] + nums[i]
d = {}
ans = 0
#print(pref)
for i in range(len(pref)):
#print(pref[i], d)
if pref[i] - target in d:
ans += 1
#print(ans)
d.clear()
d[pref[i]] = i
return ans
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
d={0:-1}
add=0
count=0
i=0
while(i<=len(nums)-1):
add+=nums[i]
if (add-target) in d :
count+=1
d= {}
d[add]=i
i+=1
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
prefix = list(itertools.accumulate(nums))
def dfs(i):
s = {prefix[i - 1] if i > 0 else 0}
for j in range(i, len(prefix)):
if prefix[j] - target in s:
return 1 + dfs(j + 1)
s.add(prefix[j])
return 0
return dfs(0)
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
count, sums, curr = 0, {0}, 0
for num in nums:
curr += num
if curr - target in sums:
count += 1
sums.clear()
sums.add(curr)
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
n = len(nums)
prefix = [0] * (n + 1)
for i in range(n):
prefix[i + 1] = prefix[i] + nums[i]
ans = 0
seen = {0:0}
for i in range(1,n + 1):
curr = prefix[i]
a = curr - target
if a in seen:
ans += 1
seen = {}
seen[curr] = i
return ans
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
n = len(nums)
presum = [0]
for num in nums:
presum.append(presum[-1] + num)
last_pos = {0: 0}
dp = [0] * (1 + n)
for i in range(1, 1 + n):
dp[i] = dp[i - 1]
if presum[i] - target in last_pos:
pos = last_pos[presum[i] - target]
dp[i] = max(dp[i], dp[pos] + 1)
last_pos[presum[i]] = i
# print(dp)
return dp[n]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
preSum = [0]
for num in nums:
preSum.append(preSum[-1] + num)
n = len(nums)
dp = [0 for i in range(n + 1)]
temp = {0:0}
for i in range(1, n + 1):
dp[i] = dp[i - 1]
if preSum[i] - target in temp:
dp[i] = max(dp[i], dp[temp[preSum[i] - target]] + 1)
temp[preSum[i]] = i
return dp[n]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
N = len(nums)
rtv = 0
presum = dict()
presum[0] = -1
total = 0
for i,v in enumerate(nums):
total += v
ne = total - target
if ne in presum and presum[ne] < i:
rtv += 1
# print(\"{} {}\".format(presum[ne]+1, i))
presum = dict()
presum[0] = i
total = 0
else:
presum[total] = i
return rtv
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
ln = len(nums)
ret = 0
sums = {0 : 0}
dp = [0] * (ln+1)
s = 0
for i in range(ln):
s += nums[i]
if s - target in sums:
idx = sums[s-target]
dp[i+1] = 1 + dp[idx]
dp[i+1] = max(dp[i], dp[i+1])
sums[s] = i+1
return dp[ln]
pass
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
subarrays = []
summ = 0
lookup = set([0])
count = 0
for num in nums:
summ += num
if summ - target in lookup:
count += 1
summ = 0
lookup = set([0])
else:
lookup.add(summ)
return count
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
if len(nums) == 0:
return 0
h = {}
dp = []
s = nums[0]
if s == target:
dp.append(1)
else:
dp.append(0)
h[s] = 0
for i in range(1, len(nums)):
s = s + nums[i]
if s == target:
if 0 in h:
dp.append(max(dp[h[0]]+1, dp[-1]))
else:
dp.append(max(1, dp[-1]))
elif s-target in h:
dp.append(max(dp[h[s-target]]+1, dp[-1]))
else:
dp.append(dp[-1])
h[s] = i
return dp[-1]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
n = len(nums)
d = {0: -1}
cursum = 0
ans = 0
for i,n in enumerate(nums):
cursum += n
prev = cursum - target
if prev in d:
ans += 1
d = {}
d[cursum] = i
return ans
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
cur = 0
n = len(nums)
dp = [0]*(n+1)
dic = {}
dic[0]=0
for i in range(1,n+1):
cur+=nums[i-1]
if cur-target in dic:
dp[i] = 1+dp[dic[cur-target]]
dp[i] = max(dp[i],dp[i-1])
dic[cur]=i
return dp[n]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
n = len(nums)
prefix = [0] * (n + 1)
for i in range(n):
prefix[i + 1] = prefix[i] + nums[i]
seen = {0:0}
ans = 0
for i in range(1,n + 1):
cur = prefix[i]
a = cur - target
if a in seen:
ans += 1
seen = {}
seen[cur] = 1
return ans
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
dp = [0] * (len(nums) + 1)
mem = dict()
mem[0] = -1
cur = 0
for i in range(1, len(nums) + 1):
dp[i] = dp[i - 1]
cur += nums[i - 1]
t = cur - target
#print(i-1, cur, t, mem)
if t in mem:
dp[i] = max(dp[i], dp[mem[t]] + 1)
#print('!!', i, dp)
mem[cur] = i
return dp[-1]
|
Given an array nums and an integer target.
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Example 3:
Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10
Output: 3
Example 4:
Input: nums = [0,0,0], target = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
0 <= target <= 10^6
|
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
sm = 0
hsh = {}
dp = {}
dp[-1] = 0
ans = 0
for i in range(len(nums)):
dp[i] = dp[i-1]
sm += nums[i]
if sm - target in hsh:
dp[i] = max(dp[i], dp[hsh[sm-target]] + 1)
hsh[sm] = i
if sm == target:
dp[i] = max(dp[i], 1)
ans = max(ans, dp[i])
return ans
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.