Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: @lru_cache(None) def cut(left, right): # if not subcuts: # return 0 # elif (left, right) in dp: # return dp[(left, right)] res = right - left min_subproblem = float('inf') for c in cuts: if left < c < right: min_subproblem = min ( min_subproblem, cut(left, c) + cut(c, right)) if min_subproblem == float('inf'): return 0 res += min_subproblem # dp[(left, right)] = res return res # dp = {} return cut(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: @lru_cache(None) def min_cost(left_end=0, right_end=n): if any(left_end < cut < right_end for cut in cuts): cost = right_end - left_end nxt_costs = min( min_cost(left_end, cut) + min_cost(cut, right_end) for cut in cuts if left_end < cut < right_end ) return nxt_costs + cost return 0 return min_cost()
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: @lru_cache(None) def dp(l, r): if any(l < c < r for c in cuts): ans = r - l + min([dp(l, c) + dp(c, r) for c in cuts if l < c < r], default=0) return ans return 0 return dp(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: @lru_cache(None) def min_cost(left_end=0, right_end=n): if any(left_end < cut < right_end for cut in cuts): cost = right_end - left_end nxt_costs = min( min_cost(left_end, cut) + min_cost(cut, right_end) for cut in cuts if left_end < cut < right_end ) return nxt_costs + cost return 0 return min_cost()
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
from functools import lru_cache class Solution: def minCost(self, n: int, cuts: List[int]) -> int: cuts.sort() @lru_cache(maxsize=None) def dfs(l, r): m = 0xffffffff for c in cuts: if l < c < r: m = min(m, dfs(l, c) + dfs(c, r)) return 0 if m == 0xffffffff else m + r - l return dfs(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
from functools import lru_cache class Solution: def minCost(self, n: int, cuts: List[int]) -> int: lim = len(cuts) @lru_cache(None) def r(i,j): tmp = [] for k in cuts: if k>i and k<j: tmp.append((j-i)+r(i,k)+r(k,j)) if tmp==[]:return 0 return min(tmp) return r(0,n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
from itertools import permutations from functools import lru_cache class Solution: def minCost(self, n: int, cuts: List[int]) -> int: def bin_search(t,d): if t > cuts[-1]: return float('inf') if d == '+' else len(cuts)-1 if t < cuts[0]: return float('-inf') if d == '-' else 0 l,r=0,len(cuts)-1 while l <= r: m = (l+r)//2 if cuts[m] > t: r = m - 1 elif cuts[m] < t: l = m + 1 else: return m+1 if d == '+' else m-1 return l if d == '+' else r @lru_cache(None) def helper(st,end): # curr_cuts = [c for c in cuts if st < c < end] l = bin_search(st,'+') r = bin_search(end,'-') # print(st,end,[cuts[i] for i in range(l,r+1)]) n = max(r - l + 1,0) if n == 1: return end-st if n == 0: return 0 min_cost = float('inf') for i in range(l,r+1): min_cost = min(min_cost,end-st + helper(st,cuts[i]) + helper(cuts[i],end)) return min_cost cuts.sort() return helper(0,n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
from itertools import permutations from functools import lru_cache class Solution: def minCost(self, n: int, cuts: List[int]) -> int: @lru_cache(None) def helper(st,end): curr_cuts = [c for c in cuts if st < c < end] n = len(curr_cuts) if n == 1: return end-st if n == 0: return 0 min_cost = float('inf') for cut in curr_cuts: min_cost = min(min_cost,end-st + helper(st,cut) + helper(cut,end)) return min_cost cuts.sort() return helper(0,n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
from functools import lru_cache class Solution: def minCost(self, n: int, cuts: List[int]) -> int: @lru_cache(None) def minCost_r(k,l): if l-k <= 1: return 0 mc = float('inf') for c in cuts: if c <= k or c >= l: continue mc = min(mc, minCost_r(k,c) + minCost_r(c,l) + (l-k)) return mc if mc != float('inf') else 0 return minCost_r(0,n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: @lru_cache(None) def solve(l, r): best = n * n found = False for c in cuts: if c <= l or c >= r: continue found = True best = min(best, r - l + solve(l, c) + solve(c, r)) if not found: return 0 return best return solve(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
import functools class Solution: @functools.lru_cache(None) def dp(self, i, j): # print(f\"{i}..{j}\") if j - i < 2: # print(f\"{i}..{j} -> {0}\") return 0 options = [j - i + self.dp(i,k) + self.dp(k,j) for k in self.cuts if i < k and k < j] if len(options) == 0: # print(f\"{i}..{j} -> {0}\") return 0 result = min(options) # print(f\"{i}..{j} -> {result}\") return result def minCost(self, n: int, cuts) -> int: self.cuts = cuts return self.dp(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
from typing import List import functools class Solution(object): def minCost(self, n, cuts): @functools.lru_cache(None) def get_cuts(l,r): return [c for c in cuts if c>l and c<r] @functools.lru_cache(None) def solve(l, r): mycuts= get_cuts(l,r) if len(mycuts) == 1 : return r-l if len(mycuts) == 0 : return 0 minn = 1e9 for pos in mycuts: left_cost = solve(l, pos) right_cost = solve(pos, r) minn = min(minn, left_cost+right_cost + r-l ) return minn cuts = sorted(cuts) return solve(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: dp={} position={} cuts.sort() for i in range(len(cuts)): position[cuts[i ]]=i def f(s,e): if (s,e) in dp: return dp[(s,e)] else: res=float('inf') i=0 if s<cuts[0]: i=0 else: i=position[s]+1 # while i<len(cuts) and cuts[i]<=s: # i+=1 # print(\"i\"+str(i)) j=len(cuts)-1 if e>cuts[len(cuts)-1]: j=len(cuts)-1 else: j=position[e]-1 # while cuts[j]>=e and j>=0: # j-=1 # print(j) if i>j: dp[(s,e)]=0 return 0 for k in range(i,j+1): # dp[(s,e)]=dp[(s,cuts[k])]+dp[(cuts[k],e )] +s-e res=min(res,f(s,cuts[k])+f(cuts[k],e)+e-s) dp[(s,e)]=res return dp[(s,e)] return f(0,n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
from collections import defaultdict class Solution: def minCost(self, n: int, cuts: List[int]) -> int: def dfs(start, end): if (start, end) in cache: return cache[(start, end)] min_val = float('inf') cut_found = False for c in cuts: if c > start and c < end: # Important!!! check the boundary condition left_val = dfs(start, c) right_val = dfs(c, end) min_val = min(min_val, left_val + right_val) cut_found = True if not cut_found: # If no cut is found we know that the stick cannot be split more cache[(start, end)] = 0 else: cache[(start, end)] = end - start + min_val return cache[(start, end)] cache = defaultdict(int) return dfs(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: cache = dict() def recurse(start, end): key = (start, end) if key in cache: return cache[key] if start >= end: return 0 minCost = float('inf') len = end - start for cut in cuts: if cut > start and cut < end: first = recurse(start, cut) second = recurse(cut, end) minCost = min(minCost, len + first + second) if minCost == float('inf'): minCost = 0 cache[key] = minCost return minCost cuts.sort() ans = recurse(0, n) return ans
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: if cuts==None or len(cuts)==0: return 0 dp = defaultdict(int) def dfs(start, end): if (start, end) in dp: return dp[(start, end)] min_val = float('inf') cut_found = False for c in cuts: if c > start and c < end: # Important!!! check the boundary condition left_val = dfs(start, c) right_val = dfs(c, end) min_val = min(min_val, left_val + right_val) cut_found = True if not cut_found: # If no cut is found we know that the stick cannot be split more dp[(start, end)] = 0 else: dp[(start, end)] = end - start + min_val return dp[(start, end)] return dfs(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: min_costs = {} cut_set = set(cuts) def find_min_cost(start: int, end: int): if (start, end) in min_costs: return min_costs[(start, end)] cuts_in = [] for i in cut_set: if i > start and i < end: cuts_in.append(i) if len(cuts_in) == 0: min_costs[(start, end)] = 0 return 0 if len(cuts_in) == 1: min_costs[(start, end)] = end - start return end - start result = len(cuts_in) * (end - start) for cut in cuts_in: subresult = end - start subresult += find_min_cost(start, cut) subresult += find_min_cost(cut, end) if subresult < result: result = subresult min_costs[(start, end)] = result return result return find_min_cost(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: def dfs(start, end): if (start, end) in cache: return cache[(start, end)] min_val = float('inf') cut_found = False for c in cuts: if c > start and c < end: # Important!!! check the boundary condition left_val = dfs(start, c) # print ('Left', left_val, 'C', c) right_val = dfs(c, end) # print ('Right', right_val, 'C', c) min_val = min(min_val, left_val + right_val) # print (\"Left and Right\", left_val, right_val) # print (\"MIN VAL:n \", min_val) cut_found = True if not cut_found: # If no cut is found we know that the stick cannot be split more cache[(start, end)] = 0 else: cache[(start, end)] = end - start + min_val return cache[(start, end)] cache = defaultdict(int) return dfs(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
from collections import defaultdict class Solution: def minCost(self, n: int, cuts: List[int]) -> int: def dfs(start, end): if (start, end) in cache: return cache[(start, end)] min_val = float('inf') cut_found = False for c in cuts: if c > start and c < end: # Important!!! check the boundary condition left_val = dfs(start, c) right_val = dfs(c, end) min_val = min(min_val, left_val + right_val) cut_found = True if not cut_found: # If no cut is found we know that the stick cannot be split more cache[(start, end)] = 0 else: cache[(start, end)] = end - start + min_val return cache[(start, end)] cache = defaultdict(int) return dfs(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: return self.dp(0, n, cuts, dict()) def dp(self, i, j, cuts, memo): if (i, j) not in memo: minV = float('inf') for k in cuts: if k > i and k < j: # Valid cutting point l, r = self.dp(i, k, cuts, memo), self.dp(k, j, cuts, memo) minV = min(minV, l+r) if minV != float('inf'): memo[(i, j)] = minV+j-i else: memo[(i, j)] = 0 return memo[(i, j)]
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: visited = {} # map (start, end) -> min cost def minCostHelper(start, end): # check if cached if (start, end) in visited: return visited[(start, end)] # if not, calculate min_cost = float('inf') for c in cuts: if c <= start or c >= end: continue cost = (end-start) + minCostHelper(start, c) + minCostHelper(c, end) min_cost = min(min_cost, cost) if min_cost == float('inf'): min_cost = 0 visited[(start, end)] = min_cost return min_cost return minCostHelper(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: if cuts==None or len(cuts)==0: return 0 N = len(cuts) if N==1: return n dp = defaultdict(int) def dfs(start, end): if (start, end) in dp: return dp[(start, end)] min_val = float('inf') cut_found = False for c in cuts: if c > start and c < end: # Important!!! check the boundary condition left_val = dfs(start, c) right_val = dfs(c, end) min_val = min(min_val, left_val + right_val) cut_found = True if not cut_found: # If no cut is found we know that the stick cannot be split more dp[(start, end)] = 0 else: dp[(start, end)] = end - start + min_val return dp[(start, end)] return dfs(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
import sys class Solution: def rec(self,l,h,cuts,dp): if h in dp[l]: return dp[l][h] ans=sys.maxsize for i in cuts: if l<i and i<h: if i in dp[l]: k1=dp[l][i] else: k1=self.rec(l,i,cuts,dp) if h in dp[i]: k2=dp[i][h] else: k2=self.rec(i,h,cuts,dp) ans=min(ans,h-l+k1+k2) if ans==sys.maxsize: ans=0 dp[l][h]=ans return dp[l][h] def minCost(self, n: int, cuts: List[int]) -> int: dp={} for i in range(n+1): dp[i]={} return self.rec(0,n,cuts,dp)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: return self.dp(0, n, cuts, dict()) def dp(self, i, j, cuts, memo): if (i, j) not in memo: minV = float('inf') for k in cuts: if k > i and k < j: # Valid cutting point l, r = self.dp(i, k, cuts, memo), self.dp(k, j, cuts, memo) minV = min(minV, l+r) if minV != float('inf'): memo[(i, j)] = minV+j-i else: memo[(i, j)] = 0 return memo[(i, j)]
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: mem = collections.defaultdict() def dp(i, j): if (i, j) in mem: return mem[(i,j)] ans = float('inf') can_cut = False for c in cuts: if i < c < j: ans = min(ans, dp(i, c) + dp(c, j) + j - i) can_cut = True if not can_cut: ans = 0 mem[(i,j)] = ans return ans ans = dp(0, n) # print(mem) return ans
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: visited = {} # map (start, end) -> min cost def minCostHelper(start, end): # check if cached if (start, end) in visited: return visited[(start, end)] # if not, calculate min_cost = float('inf') for c in cuts: if c <= start or c >= end: continue cost = (end-start) + minCostHelper(start, c) + minCostHelper(c, end) min_cost = min(min_cost, cost) # no cuts between start and end if min_cost == float('inf'): min_cost = 0 visited[(start, end)] = min_cost return min_cost return minCostHelper(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: dp = {} return self.recur(0,n, dp, cuts) def recur(self,l, r, dp, cuts): if (l,r) in dp: return dp[(l,r)] cost = r - l res = float('inf') for c in cuts: if c > l and c < r: res = min(res, self.recur(l,c,dp,cuts) + self.recur(c,r,dp,cuts) + cost) if res == float('inf'): dp[(l,r)] = 0 else: dp[(l,r)] = res return dp[(l,r)]
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: dp = {} return self.recur(0,n, dp, cuts) def recur(self,l, r, dp, cuts): if (l,r) in dp: return dp[(l,r)] cost = r - l res = float('inf') for c in cuts: if c > l and c < r: res = min(res, self.recur(l,c,dp,cuts) + self.recur(c,r,dp,cuts) + cost) if res == float('inf'): res = 0 dp[(l,r)] = res return dp[(l,r)]
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: dp = {} def dfs(i,j): if(i>j): return 0 if((i,j) in dp): return dp[(i,j)] ans = float('inf') iscut = False; for cut in cuts: if(i<cut and j>cut): temp = dfs(i,cut) + dfs(cut,j) ans = min(temp, ans) iscut = True if(not iscut): dp[(i,j)] = 0; else: dp[(i,j)] = ans + j-i; return dp[(i,j)] return dfs(0,n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
import sys class Solution: def __init__(self): self.dp = {} def minCost(self, A: int, B: List[int]) -> int: self.set1 = set(B) def helper(st,end): if (st,end) in self.dp: return self.dp[(st,end)] if end-st ==1: return 0 t = sys.maxsize for i in B: if i>st and i<end: t = min(t,end-st+helper(st,i)+helper(i,end)) self.dp[st,end] = (t if t != sys.maxsize else 0) return self.dp[(st,end)] helper(0,A) return self.dp[0,A]
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: cuts.append(0) cuts.append(n) cuts.sort() neighbor = dict() for i, cut in enumerate(cuts): if i == 0: neighbor[cut] = [None, cuts[i + 1]] elif i == len(cuts) - 1: neighbor[cut] = [cuts[i - 1], None] else: neighbor[cut] = [cuts[i - 1], cuts[i + 1]] cache = dict() # left, right are exclusive def search(left: int, right: int) -> int: if (left, right) in cache: return cache[(left, right)] min_cost = None for cut in cuts: if cut <= left or cut >= right: continue leftnei, rightnei = neighbor[cut] # neighbor[leftnei][1] = rightnei # neighbor[rightnei][0] = leftnei cost = right - left cost += search(left, cut) cost += search(cut, right) min_cost = cost if min_cost is None else min(min_cost, cost) # neighbor[leftnei][1] = cut # neighbor[rightnei][0] = cut if min_cost is None: min_cost = 0 cache[(left, right)] = min_cost return min_cost ans = search(0, n) # print(cache) return ans
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: dp={} position={} # for i in range(len(cuts)): # position[cuts[i ]]=i cuts.sort() def f(s,e): if (s,e) in dp: return dp[(s,e)] else: res=float('inf') i=0 while i<len(cuts) and cuts[i]<=s: i+=1 # print(\"i\"+str(i)) j=len(cuts)-1 while cuts[j]>=e and j>=0: j-=1 # print(j) if i>j: dp[(s,e)]=0 return 0 for k in range(i,j+1): # dp[(s,e)]=dp[(s,cuts[k])]+dp[(cuts[k],e )] +s-e res=min(res,f(s,cuts[k])+f(cuts[k],e)+e-s) dp[(s,e)]=res return dp[(s,e)] return f(0,n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: self.memo = {} cuts = set(cuts) def dp(i, j): if (i, j) in self.memo: return self.memo[(i, j)] min_value = None for cut in cuts: if (cut > i) and (cut < j): if min_value is None: min_value = dp(i, cut) + dp(cut, j) + (j - i) else: min_value = min(min_value, dp(i, cut) + dp(cut, j) + (j-i)) if min_value is None: min_value = 0 self.memo[(i, j)] = min_value return min_value return dp(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: @lru_cache(None) def dfs(i,j): if i==j: return 0 tot = float('inf') for k in range(len(cuts)): if i<cuts[k]<j: tot = min(tot, dfs(i, cuts[k])+dfs(cuts[k],j)+j-i) if tot == float('inf'): return 0 return tot return dfs(0,n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: dp = {} def minCostij(i, j): if (i,j) in dp: return dp[(i,j)] if i + 1 == j or i == j: dp[(i,j)] = 0 else: minCosts = [minCostij(i,k) + minCostij(k,j) for k in cuts if (k < j and k > i)] if minCosts: dp[(i,j)] = min(minCosts) + (j-i) else: dp[(i,j)] = 0 return dp[(i,j)] return minCostij(0,n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
from collections import defaultdict class Solution: def minCost(self, n: int, cuts: List[int]) -> int: def dfs(start, end): if (start, end) in cache: return cache[(start, end)] min_val = float('inf') cut_found = False for c in cuts: if c > start and c < end: # Important!!! check the boundary condition left_val = dfs(start, c) right_val = dfs(c, end) min_val = min(min_val, left_val + right_val) cut_found = True if not cut_found: # If no cut is found we know that the stick cannot be split more cache[(start, end)] = 0 else: cache[(start, end)] = end - start + min_val return cache[(start, end)] cache = defaultdict() return dfs(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: self.memo = {} # self.memo[(i, j)]表示把i~j切成最小需要最少cost self.helper(0, n, cuts) return self.memo[(0, n)] def helper(self, left, right, candidates): if left >= right: return 0 if (left, right) in self.memo: return self.memo[(left, right)] res = sys.maxsize for k in candidates: if k <= left or k >= right: continue left_cost = self.helper(left, k, candidates) right_cost = self.helper(k, right, candidates) cost = right - left res = min(res, left_cost + right_cost + cost) if res == sys.maxsize: res = 0 self.memo[(left, right)] = res return res
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: # cut.append(0) # cuts.append(n) # cuts.sort() return self.dfs(cuts, 0, n, dict()) def dfs(self, cuts, i, j, memo): if j-i <= 1: return 0 if (i, j) not in memo: memo[(i, j)] = float('inf') for c in cuts: if c > i and c < j: memo[(i, j)] = min(memo[(i, j)], j-i+self.dfs(cuts, i, c, memo)+self.dfs(cuts, c, j, memo)) if memo[(i, j)] == float('inf'): memo[(i, j)] = 0 return memo[(i, j)]
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: return self.cut(0, n, set(cuts), {}) def cut(self, i, j, cuts, dp): v = float('inf') if (i, j) in dp: return dp[(i, j)] for x in cuts: if i < x and j > x: v = min(v, j - i + self.cut(i, x, cuts, dp) + self.cut(x, j, cuts, dp)) dp[(i, j)] = v if v != float('inf') else 0 return dp[(i, j)]
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
from collections import defaultdict class Solution: def minCost(self, n: int, cuts: List[int]) -> int: cache = defaultdict(int) def dfs(start, end): if (start, end) in cache: return cache[(start, end)] min_val = float('inf') cut_found = False for c in cuts: if c > start and c < end: left_val = dfs(start, c) right_val = dfs(c, end) min_val = min(min_val, left_val + right_val) cut_found = True if not cut_found: cache[(start, end)] = 0 else: cache[(start, end)] = end - start + min_val return cache[(start, end)] return dfs(0, n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: memo = {} def dp(start,end): if (start,end) in memo: return memo[(start,end)] ans = sys.maxsize canCut = False for cut in cuts: if start<cut<end: canCut = True ans = min(ans, dp(start,cut) + dp(cut,end) + (end-start)) if not canCut: return 0 memo[(start,end)] = ans return ans return dp(0,n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def helper(self,B,s,e,d): if len(B)==0: d[(s,e)]=0 return 0 if (s,e) in d: return d[(s,e)] cost=e-s m=2**31-1 for i in range(len(B)): m=min(m,self.helper(B[:i],s,B[i],d)+self.helper(B[i+1:],B[i],e,d)) d[(s,e)]=cost+m return cost+m def minCost(self, n: int, cuts: List[int]) -> int: cuts.sort() d={} k=self.helper(cuts,0,n,d) return k
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
from math import inf class Solution: def minCost(self, n: int, cuts: List[int]) -> int: def recurse(l,r): # if r<=l: # return inf if self.dp.get((l,r),-1)!=-1: return self.dp[(l,r)] mnCut=inf flag=1 for i in range(len(cuts)): if l<cuts[i]<r: mnCut=min(mnCut,recurse(l,cuts[i])+recurse(cuts[i],r)) flag=0 if flag==0: self.dp[(l,r)]=mnCut+r-l else: self.dp[(l,r)]=0 return self.dp[(l,r)] self.dp={} cuts.sort() mn=recurse(0,n) # print(self.dp) return mn
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: def dp(s,e): if (s,e) in memo: return memo[(s,e)] ans=2**31 # print(ans) canCut=False for cut in cuts: # canCut=True if s<cut<e: canCut=True a1=dp(s,cut)+dp(cut,e)+e-s # print(a1) ans=min(ans,a1) if not canCut: # print(\"in\") return 0 memo[(s,e)]=ans return ans memo={} return dp(0,n) # return count
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Node: def __init__(self, idx, l, r): self.idx = idx self.l = l self.r = r self.left_node = self self.right_node = self class Solution: def minCost(self, n: int, cuts: List[int]) -> int: solved = dict() def cost(l, r): if l == r: return 0 if (l, r) in solved: return solved[(l, r)] min_cost = float('inf') for c in cuts: if c > l and c < r: min_cost = min(min_cost, r - l + cost(l, c) + cost(c, r)) if min_cost == float('inf'): return 0 solved[(l, r)] = min_cost return min_cost return cost(0, n) ''' cost(0, l - 1) -> min(cost(0, r) + cost(r, l - 1)) for r in cuts '''
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: return self.dp(cuts, 0, n, dict()) def dp(self, cuts, a, b, memo): if (a, b) not in memo: memo[(a, b)], minV = float('inf'),float('inf') for j, c in enumerate(cuts): if c > a and c < b: l, r = self.dp(cuts, a,c,memo), self.dp(cuts, c,b,memo) memo[(a, b)] = min(memo[(a, b)], b-a) minV = min(minV, l+r) if memo[(a, b)] == float('inf'): memo[(a, b)] = 0 else: memo[(a, b)] += minV return memo[(a, b)]
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: # cuts.append(0) # cuts.append(n) # cuts.sort() return self.dp(cuts, 0, n, dict()) def dp(self, cuts, a, b, memo): if (a, b) not in memo: memo[(a, b)], minV = float('inf'),float('inf') for j, c in enumerate(cuts): if c > a and c < b: l, r = self.dp(cuts, a,c,memo), self.dp(cuts, c,b,memo) memo[(a, b)] = min(memo[(a, b)], b-a) minV = min(minV, l+r) if memo[(a, b)] == float('inf'): memo[(a, b)] = 0 else: memo[(a, b)] += minV return memo[(a, b)]
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: # cuts = sorted(cuts) # options = [] # cuts = [0]+cuts+[n] # N = len(cuts) # for i in range(1, N): # options.append([cuts[i-1], cuts[i]]) # val = 0 # while len(options) != 1: # short_i = 0 # min_wid = math.inf # for i in range(len(options)-1): # a = options[i] # b = options[i+1] # if (a[1]-a[0]) + (b[1]-b[0]) < min_wid: # short_i = i # min_wid = (a[1]-a[0]) + (b[1]-b[0]) # val += min_wid # options[short_i][1] = options[short_i+1][1] # del options[short_i+1] # return val cuts = sorted(cuts) memo = {} t_cuts = [0]+cuts+[n] N = len(t_cuts) for i in range(1, N): memo[(t_cuts[i-1], t_cuts[i])] = 0 def dp(s, e): # print((s,e), memo[(s,e)] if (s,e) in memo else False) if (s, e) in memo: return memo[(s,e)] filtered_cuts = [cut for cut in cuts if cut > s and cut < e] if len(filtered_cuts) == 0: return 0 ans = math.inf for cut in filtered_cuts: ans = min(ans, (e-s) + dp(s, cut) + dp(cut, e)) memo[(s,e)] = ans return memo[(s,e)] ans = dp(0, n) # print(memo) return ans
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: cuts.sort() def helper(cuts,memo,l,r): res=float('inf') key=(l,r) if key in memo.keys(): return memo[key] for i in range(len(cuts)): if cuts[i]<=l or cuts[i]>=r: continue cost=r-l res=min(helper(cuts,memo,l,cuts[i])+cost+helper(cuts,memo,cuts[i],r),res) if res==float('inf'): res=0 memo[key]=res return res c=collections.defaultdict(int) return helper(cuts,c,0,n)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: def dp(i, j, memo): if (i, j) not in memo: if j-i <= 1: return 0 memo[(i, j)] = float('inf') for c in cuts: if c > i and c < j: memo[(i, j)] = min(memo[(i, j)], j-i+dp(i, c, memo)+dp(c, j, memo)) return memo[(i, j)] if memo[(i, j)] != float('inf') else 0 return dp(0, n, dict())
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: # for total length, find the location closest to center and cut it # remove that location from cuts and move on to the parts # sort the cuts array, easy to find mid that way # cuts.sort() # def cut(start, end, cuts, cuts_start, cuts_end): # print(start, end, cuts_start, cuts_end) # if end <= start or cuts_start > cuts_end: # return 0 # mid = (start + end) // 2 # loc = bisect.bisect_left(cuts, mid, cuts_start, cuts_end) # print('loc', loc) # if loc >= len(cuts): # return 0 # left = cut(start, cuts[loc], cuts, cuts_start, loc - 1) # right = cut(cuts[loc], end, cuts, loc + 1, cuts_end) # print('price',(end - start), left , right) # v = (end - start) + left + right # return v # print(cuts) # return cut(0, n, cuts, 0, len(cuts)) # TLE # def top_down(low, high, cuts): # possible_cuts = [c for c in cuts if low < c < high] # # print(possible_cuts) # if not possible_cuts: # return 0 # ans = float('inf') # for mid in possible_cuts: # ans = min(ans, top_down(low, mid, cuts) + top_down(mid, high, cuts)) # return ans + high - low # return top_down(0, n, cuts) dp = {} def top_down(low, high, cuts): if (low, high) in dp: return dp[(low, high)] possible_cuts = [c for c in cuts if low < c < high] # print(possible_cuts) if not possible_cuts: return 0 ans = float('inf') for mid in possible_cuts: ans = min(ans, top_down(low, mid, cuts) + top_down(mid, high, cuts)) dp[(low, high)] = ans + high - low return dp[(low, high)] return top_down(0, n, cuts)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: def dfs(i, j, memo): if (i, j) not in memo: if j-i <= 1: return 0 memo[(i, j)] = float('inf') for c in cuts: if c > i and c < j: memo[(i, j)] = min(memo[(i, j)], j-i+dfs(i, c, memo)+dfs(c, j, memo)) return memo[(i, j)] if memo[(i, j)] != float('inf') else 0 return dfs(0, n, dict())
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def cal(self, i, j, cuts): if i in self.dp: if j in self.dp[i]: return self.dp[i][j] ans = float('inf') cost = j - i found = False for ind, c in enumerate(cuts): if i < c and c < j: t = cuts[:] del t[ind] ans = min(ans, cost + self.cal(i, c, t) + self.cal(c, j, t)) found = True if not found: ans = 0 if i in self.dp: self.dp[i][j] = ans else: self.dp[i] = {j: ans} return ans def minCost(self, n: int, cuts: List[int]) -> int: self.dp = {} return self.cal(0, n, sorted(cuts))
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: # cut.append(0) # cuts.append(n) # cuts.sort() return self.dfs(cuts, 0, n, dict()) def dfs(self, cuts, i, j, memo): if j-i <= 1: return 0 if (i, j) not in memo: memo[(i, j)] = float('inf') for c in cuts: if c > i and c < j: memo[(i, j)] = min(memo[(i, j)], j-i+self.dfs(cuts, i, c, memo)+self.dfs(cuts, c, j, memo)) return memo[(i, j)] if memo[(i, j)] != float('inf') else 0
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
import itertools import sys from functools import lru_cache from math import comb from typing import List ''' 给你一个整数数组 cuts ,其中 cuts[i] 表示你需要将棍子切开的位置。定义切割成本是要切割的棍子长度,求最小的切割成本,即找到一个最小成本的切割顺序 n = 7, cuts = [1,3,4,5] 0 1| 2 3| 4| 5| 6 7 dfs: a b c d dp[start][end]= min(dp[start][cut] + dp[cut][end]) 斜向上扫描。 ''' class Solution1: def dfs(self, start, end, path, cuts): if self.dp[start][end] != sys.maxsize: return self.dp[start][end] min_cut = sys.maxsize flag = False nums = cuts[self.cuts_map[start]:self.cuts_map[end]] for cut in nums: if cut not in path and start < cut and cut < end: flag = True path.add(cut) min_cut = min(min_cut, end - start + self.dfs(start, cut, path, cuts) + self.dfs(cut, end, path, cuts)) path.remove(cut) # 如果没切割点则返回0 if not flag: min_cut = 0 self.dp[start][end] = min_cut return min_cut def minCost(self, n: int, cuts: List[int]) -> int: self.dp = [[sys.maxsize for _ in range(n + 1)] for _ in range(n + 1)] cuts = sorted(cuts) self.cuts_map = {} for i, cut in enumerate(cuts): self.cuts_map[cut] = i self.cuts_map[0] = 0 self.cuts_map[n] = n return self.dfs(0, n, set(), cuts) class Solution: def dfs(self, start, end, path, cuts): if self.dp[self.cuts_map[start]][self.cuts_map[end]] != sys.maxsize: return self.dp[self.cuts_map[start]][self.cuts_map[end]] min_cut = sys.maxsize flag = False nums = cuts[self.cuts_map[start]:self.cuts_map[end]] for cut in nums: if cut not in path and start < cut and cut < end: flag = True path.add(cut) min_cut = min(min_cut, end - start + self.dfs(start, cut, path, cuts) + self.dfs(cut, end, path, cuts)) path.remove(cut) # 如果没切割点则返回0 if not flag: min_cut = 0 self.dp[self.cuts_map[start]][self.cuts_map[end]] = min_cut return min_cut def minCost(self, n: int, cuts: List[int]) -> int: self.dp = [[sys.maxsize for _ in range(len(cuts) + 2)] for _ in range(len(cuts) + 2)] # 内存太大,需要优化 cuts.extend([0, n]) cuts = sorted(cuts) self.cuts_map = {} for i, cut in enumerate(cuts): self.cuts_map[cut] = i return self.dfs(0, n, set(), cuts)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def cal(self, i, j, cuts): if i in self.dp: if j in self.dp[i]: return self.dp[i][j] ans = float('inf') cost = j - i found = False for ind, c in enumerate(cuts): if i < c and c < j: t = cuts[:] del t[ind] ans = min(ans, cost + self.cal(i, c, t) + self.cal(c, j, t)) found = True if not found: ans = 0 if i in self.dp: self.dp[i][j] = ans else: self.dp[i] = {j: ans} return ans def minCost(self, n: int, cuts: List[int]) -> int: self.dp = {} return self.cal(0, n, cuts)
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. You should perform the cuts in order, you can change the order of the cuts as you wish. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. Return the minimum total cost of the cuts.   Example 1: Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2: Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.   Constraints: 2 <= n <= 10^6 1 <= cuts.length <= min(n - 1, 100) 1 <= cuts[i] <= n - 1 All the integers in cuts array are distinct.
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: def top_down(i, j, memo): if (i, j) not in memo: # If length of current stick is less than or equal to one, we can't cut any further so cost will be zero. if j-i <= 1: return 0 memo[(i, j)] = float('inf') for c in cuts: # If c is not in the range of (i, j) exclusive, cut cannot be performed. if c > i and c < j: memo[(i, j)] = min(memo[(i, j)], j-i+top_down(i, c, memo)+top_down(c, j, memo)) # if no valid cutting position, return 0 otherwise return optmal solution. return memo[(i, j)] if memo[(i, j)] != float('inf') else 0 return top_down(0, n, dict())
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: mod = 10**9+7 odd_presum_cnt = 0 par = 0 for a in arr: par ^= a & 1 if par: odd_presum_cnt += 1 return odd_presum_cnt * (len(arr)+1 - odd_presum_cnt)%mod
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: res = odd = even = 0 for x in arr: even += 1 if x % 2 != 0: even,odd = odd,even res = (res + odd) % (10 ** 9 + 7) return res
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: np = [[0 for i in range(len(arr))] for j in range(2)] # 0 is even sub array # 1 is odd sub array np[0][0] = 1 if arr[0] % 2 == 0 else 0 np[1][0] = 1 if arr[0] % 2 == 1 else 0 res = np[1][0] for i in range(1, len(arr)): if arr[i] % 2 == 0: np[0][i] = (1 + np[0][i - 1]) % 1000000007 np[1][i] = np[1][i - 1] % 1000000007 else: np[0][i] = np[1][i - 1] % 1000000007 np[1][i] = (1 + np[0][i - 1]) % 1000000007 #print(np) res += np[1][i] return res % 1000000007
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: s=0 odd={} ok=[] even={} ek=[] for i in range(len(arr)): s+=arr[i] if(s%2==0): even[i]=s ek.append(i) else: odd[i]=s ok.append(i) j=0 c=0 for i in ok: while(j<len(ek) and ek[j]<i): j+=1 c+=j j=0 for i in ek: while(j<len(ok) and ok[j]<i): j+=1 c+=j #0:1,1:3,4:15,5:21 #2:6, 3:10, 6:28 return (c+len(ok))%(10**9 + 7)
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: mod = 10**9 + 7 odd_even_count = [[0,0] for _ in range(len(arr)+1)] prefix_odd_sum = [-1 for _ in range(len(arr))] cur = 0 odd_count = 0 even_count = 0 for idx in range(len(arr)): cur += arr[idx] prefix_odd_sum[idx] = cur % 2 if cur % 2 == 1: odd_count += 1 else: even_count += 1 odd_even_count[idx+1] = (odd_count, even_count) ans = 0 for idx in range(len(arr)): is_odd = prefix_odd_sum[idx] ## odd: add 1 + prefix even count if is_odd: ans += 1 + odd_even_count[idx][1] ## even: add prefix odd count else: ans += odd_even_count[idx][0] return ans % mod
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, A: List[int]) -> int: n = len(A) mod = 10**9+7 ans = 0 p, ctr = [0]*(n+1), Counter([0]) for i in range(n): p[i] = p[i-1]+A[i] s = p[i]%2 ans = ans+ctr[1-s] ans = ans%mod ctr[s] += 1 return ans
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: dp = [0 for i in range(len(arr))] # dp[i] is the number of subarrays that end at i with (odd sum, even sum) dp[0] = (1,0) if arr[0] % 2 == 1 else (0,1) for i in range(1,len(dp)): if arr[i] % 2 == 0: oddCount = dp[i-1][0] evenCount = dp[i-1][1] + 1 else: oddCount = dp[i-1][1] + 1 evenCount = dp[i-1][0] dp[i] = (oddCount,evenCount) # print(dp) return sum([elem[0] for elem in dp]) % (10**9 + 7)
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: MOD = 10**9 + 7 res = 0 curr_sum = 0 even_count = 1 odd_count = 0 for num in arr: curr_sum += num curr_sum %=2 if curr_sum == 1: # odd res += even_count odd_count += 1 else: res += odd_count even_count += 1 return res % MOD
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: dp = [[0, 0] for _ in range(len(arr)+1)] for i, num in enumerate(arr): if num % 2 == 0: dp[i+1][0] = dp[i][0] dp[i+1][1] = dp[i][1] + 1 else: dp[i+1][0] = dp[i][1] + 1 dp[i+1][1] = dp[i][0] return sum(dp[i][0] for i in range(len(dp))) % (10**9+7)
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: mod = 10**9 + 7 dic = collections.Counter() dic[0] += 1 pre = ans = 0 for i,x in enumerate(arr): pre += x pre %= 2 ans = (ans + dic[pre^1]) % mod dic[pre] += 1 return ans
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: if len(arr)<1: return None if len(arr)==1: if arr[0]%2!=0: return 1 mod=10**9 + 7 flag=False for i in arr: if i%2!=0: flag=True break if flag==False: return 0 even,odd=0,0 ret=0 for i in arr: if i%2!=0: ret+=even+1 odd,even=even+1,odd else: ret+=odd odd,even=odd,even+1 return ret%mod
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: N = len(arr) dp = [0] * N dp[0] = arr[0] & 1 for i in range(1, N): if arr[i] & 1: dp[i] = i - dp[i - 1] + 1 else: dp[i] = dp[i - 1] return sum(dp) % (10**9 + 7)
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: ans = 0 even = 0 odd = 0 for v in arr: if v % 2 == 0: even, odd = (even + 1) % 10000000007, odd else: even, odd = odd, (even + 1) % 10000000007 ans = (ans + odd) % 10000000007 return ans % 1000000007
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: kMod = int(1e9 + 7) n = len(arr) # dp[i][0] is how many sub-arrays ends with arr[i] have even sum # dp[i][1] is how many sub-arrays ends with arr[i] have odd sum dp = [[0]*2 for _ in range(n)] ans = 0 if arr[0]%2 == 0: dp[0][0] = 1 else: dp[0][1] = 1 ans += dp[0][1] for i in range(1, n): if (arr[i] % 2 != 0): dp[i][0] = dp[i-1][1] dp[i][1] = dp[i-1][0]+ 1 else: dp[i][0] = dp[i-1][0]+1 dp[i][1] = dp[i-1][1] ans += dp[i][1] return ans % kMod
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: dp=[] for i in range(len(arr)+1): dp.append([0,0]) ans=0 for i in range(1,len(arr)+1): if arr[i-1]%2==0: dp[i][0]=dp[i-1][0]+1 dp[i][1]=dp[i-1][1] else: dp[i][0]=dp[i-1][1] dp[i][1]=dp[i-1][0]+1 ans+=dp[i][1] return ans%(pow(10,9)+7)
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: n = len(arr) mod = 10**9+7 ans = 0 p = [0]*(n+1) counter = Counter([0]) for i in range(n): p[i] = p[i-1]+arr[i] if p[i]%2: ans += counter[0] else: ans += counter[1] counter[p[i]%2] += 1 return ans%mod
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: sum_even = 0 sum_odd = 0 out = 0 for i in range(len(arr)): if arr[i] %2 ==0: sum_even, sum_odd = sum_even+1, sum_odd else: sum_even, sum_odd = sum_odd , sum_even +1 out += sum_odd return out % (10**9 + 7)
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: sums = [0] sum = 0 for n in arr: sum += n sums.append(sum) odd_sum_count = [] even_sum_count = [] odd_sum = 0 even_sum = 0 for ss in sums: odd_sum += 1 if ss % 2 == 1 else 0 even_sum += 0 if ss % 2 == 1 else 1 odd_sum_count.append(odd_sum) even_sum_count.append(even_sum) ans = 0 for i in range(len(arr)): if sums[i+1] % 2 == 0: ans += odd_sum_count[i] else: ans += even_sum_count[i] ans = ans % (10 ** 9 + 7) return ans
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self,A) -> int: n = len(A) MOD = pow(10,9)+7 dp_even,dp_odd = [0],[0] if A[0]%2: dp_odd[0] += 1 else: dp_even[0] += 1 ans = dp_odd[-1] for i in range(1,n): if A[i]%2: dp_odd.append( (dp_even[i-1]+1)%MOD ) dp_even.append( dp_odd[i-1] ) else: dp_odd.append( dp_odd[i-1] ) dp_even.append( (dp_even[i-1]+1)%MOD ) ans += dp_odd[i] ans %= MOD #print(dp_even,dp_odd) return ans
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: memoOdd = arr[0] % 2 memoEven = -(arr[0] % 2 - 1) memoOddSum = memoOdd for i in range(1, len(arr)): memoOdd_temp = memoOdd memoEven_temp = memoEven memoOdd = memoOdd_temp * (-(arr[i] % 2 - 1)) + memoEven_temp * (arr[i] % 2) + arr[i] % 2 memoEven = memoOdd_temp * (arr[i] % 2) + memoEven_temp * (-(arr[i] % 2 - 1)) -(arr[i] % 2 - 1) memoOddSum += memoOdd return memoOddSum % 1000000007
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: # Make prefix sum, store only whether sum is odd or even # Iterate through prefix sum, check if current prefix sum is odd/even and count number of odd/even subarrays behind it def numOfSubarrays(self, arr: List[int]) -> int: countOdd, countEven, result = 0, 1, 0 modulo = 1000000007 prev = 0 for i in arr: prev = (prev + i) % 2 if prev == 0: countEven += 1 result += countOdd if result >= modulo: result %= modulo else: countOdd += 1 result += countEven if result >= modulo: result %= modulo return result
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: ans=0 evenCount=0 oddCount=0 for i in arr: if(i%2==0): ans+=oddCount oddCount, evenCount = oddCount, evenCount+1 else: ans+=evenCount+1 oddCount, evenCount = evenCount+1, oddCount return int(ans%(math.pow(10,9)+7))
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: n = len(arr) MOD = int(1e9 + 7) even, odd = [0]*(n+1), [0]*(n+1) for i, a in enumerate(arr): if a % 2 == 1: even[i] = odd[i-1] % MOD odd[i] = (even[i-1] + 1) % MOD else: even[i] = (even[i-1] + 1) % MOD odd[i] = odd[i-1] % MOD return sum(odd) % MOD
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: acc=[] temp=0 ones=0 for u in arr: temp+=u%2 if temp%2==1: ones+=1 L=len(arr) return ones*(L-ones+1)%(10**9+7)
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: kMod = int(1e9 + 7) n = len(arr) # dp[i][0] is how many sub-arrays ends with arr[i-1] have even sum # dp[i][1] is how many sub-arrays ends with arr[i-1] have odd sum dp = [[0]*2 for _ in range(n+1)] ans = 0 for i in range(1, n+1): if (arr[i-1] % 2 != 0): dp[i][0] = dp[i-1][1] dp[i][1] = dp[i-1][0]+ 1 else: dp[i][0] = dp[i-1][0]+1 dp[i][1] = dp[i-1][1] ans += dp[i][1] return ans % kMod
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: evenCount, oddCount = 1, 0 totalSum = numArrays = 0 for val in arr: totalSum += val numArrays += evenCount if totalSum%2 else oddCount evenCount += totalSum%2 == 0 oddCount += totalSum%2 == 1 return numArrays % ((10**9)+7)
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: mod = 10 ** 9 + 7 n = len(arr) dp = [[0] * 2 for _ in range(n)] if arr[n - 1] & 1: dp[n - 1][1] = 1 else: dp[n - 1][0] = 1 for i in range(n - 2, -1, -1): if arr[i] & 1: dp[i][1] = (dp[i + 1][0] + 1) % mod dp[i][0] = dp[i + 1][1] else: dp[i][1] = dp[i + 1][1] dp[i][0] = (dp[i + 1][0] + 1) % mod ans = 0 for i in range(n): ans += dp[i][1] return ans % mod
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: odd, even = 0, 1 s = res = 0 for n in arr: s += n if s % 2 == 0: res += odd even += 1 else: res += even odd += 1 res %= 1000000007 return res
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: o = [] score = 0 tmp = 0 pa,no = 0,0 check = [] for el in arr: tmp += el if tmp % 2 ==0: pa += 1 check.append(0) else: no += 1 check.append(1) o.append((pa, no)) score = 0 for i in range(len(arr)): # print(arr[i], score, 'b') if arr[i] % 2 ==1: score += 1 if check[i-1] == 0 or i==0: score += o[-1][1] - o[i][1] else: score += o[-1][0] - o[i][0] # print(o) # print(arr[i], score, 'a') mod = 10**9 + 7 return score % mod
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: odd = 0 even = 1 res = 0 sum = 0 for i in arr: sum += i if sum%2 == 0: even += 1 res = (res+odd)%1_000_000_007 else: odd += 1 res = (res+even)%1_000_000_007 return res
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: #dp[i]:the number of odd subarrays ended with arr[i] n = len(arr) dp = [0]*n dp[0] = arr[0]%2 for i in range(1, n): if arr[i]%2 == 0: dp[i] = dp[i-1] else: dp[i] = i-dp[i-1]+arr[i]%2 return sum(dp)% (1000000000 + 7)
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOddEvenSubarrays(self, arr:List[int], start:int) -> (int, int, int): if len(arr) == 0: return (0, 0, 0) if len(arr) == 1 or start == len(arr)-1: return (0, 1, 0) if arr[start] % 2 == 0 else (1, 0, 0) (odd, even, oldOdd) = self.numOddEvenSubarrays(arr, start+1) #print(f'{arr[start:], odd, even, oldOdd}') indOdd = (odd + oldOdd) % (10 ** 9 + 7) return (odd, even + 1, indOdd) if arr[start] % 2 == 0 else (even + 1, odd, indOdd) def numOfSubarrays(self, arr: List[int]) -> int: (odd, even, oldOdd) = self.numOddEvenSubarrays(arr, 0) #print(f'{arr, odd, even, oldOdd}') total = (odd + oldOdd) % (10 ** 9 + 7) return total
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, coll: List[int]) -> int: n = len(coll) m = 10**9 + 7 acc = 0 evens = odds = 0 for i, x in enumerate(coll): if x & 1: evens, odds = odds, evens + 1 else: evens += 1 acc += odds acc %= m return acc
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: res = odd = even = 0 for x in arr: even += 1 if x % 2 != 0: odd,even = even,odd res = (res + odd) % (10 ** 9 + 7) return res
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: prefix_sum = 0 number_odd = 0 number_even = 0 total = 0 for i in arr: prefix_sum += i if prefix_sum % 2 == 1: number_odd += 1 total += 1 total += number_even else: total += number_odd number_even += 1 return int(total % (1e9 + 7))
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: n = len(arr) d = {} d[0] = 1 sm = 0 even = 0 for i in range(n): sm += arr[i] sm %= 2 if sm < 0: sm += 2 if sm in d: even += d[sm] if sm not in d: d[sm] = 0 d[sm]+=1 return (n*(n+1)//2-even) % (10**9+7)
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: res, s, prev_odd, prev_even = 0, 0, 0, 1 for v in arr: s = (s + v) % 2 if s == 1: res += prev_even prev_odd += 1 else: res += prev_odd prev_even += 1 return res % (10**9 + 7)
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: if len(arr) == 0: return 0 result = 0 num_odd = 0 Cum = [0] for i in range(len(arr)): Cum.append(Cum[-1] + arr[i]) if Cum[-1] % 2 != 0: num_odd += 1 #print(num_odd) return (len(arr) + 1 - num_odd) * num_odd % (10**9+7)
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, coll: List[int]) -> int: n = len(coll) m = 10**9 + 7 dp = [(0, 0) for _ in range(n+1)] for i, x in enumerate(coll): if x & 1: dp[i + 1] = (dp[i][1], dp[i][0] + 1) else: dp[i + 1] = (dp[i][0] + 1, dp[i][1]) return sum(odds for evens, odds in dp) % m
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: s = 0 evens, odds = 0, 0 cnt = 0 for num in arr: s += num if s % 2 == 1: cnt += (evens+1) odds += 1 else: cnt += odds evens += 1 cnt = cnt % (10**9+7) return cnt
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: res = 0 odds = 0 even = 0 for i, c in enumerate(arr): if c & 1: odds, even = even + 1, odds else: even += 1 res = (res + odds) % 1000000007 return res % 1000000007
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: if not arr: return 0 n_even = 0 n_odd = 0 res = 0 for x in arr: if x % 2 == 0: n_even, n_odd = n_even + 1, n_odd else: n_even, n_odd = n_odd, n_even + 1 res += n_odd return res % (10**9+7)
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: even = [0] * len(arr) odd = [0] * len(arr) for i in range(len(arr)): if i == 0 and arr[i] % 2 == 0: even[i] = 1 elif i == 0: odd[i] = 1 elif arr[i] % 2 == 0: even[i] = even[i - 1] + 1 odd[i] = odd[i - 1] else: even[i] = odd[i - 1] odd[i] = even[i - 1] + 1 mod = 10 ** 9 + 7 ans = 0 for i in range(len(arr)): ans = (ans + odd[i]) % mod return ans