Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: d = {} for i in range(len(s)): if s[i] in d: d[s[i]] += 1 else: d[s[i]] = 1 odds = 0 for v in d.values(): if v % 2 == 1: odds += 1 return odds <= k and k <= len(s)
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: return sum(i &1 for i in collections.Counter(s).values()) <= k <= len(s)
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: d = {} for i in range(len(s)): if s[i] in d: d[s[i]] += 1 else: d[s[i]] = 1 odds = 0 for v in d.values(): if v % 2 == 1: odds += 1 return odds <= k and k <= len(s)
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: if len(s) < k: return False dict = {} for st in s: if st in dict: dict[st] += 1 else: dict[st] = 1 countOddCh = 0 for key, v in dict.items(): if v % 2 != 0: countOddCh += 1 if countOddCh > k: return False return True
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: if k > len(s): return False char_cnt = defaultdict(int) for char in s: char_cnt[char] += 1 odd_cnt = 0 for char, cnt in char_cnt.items(): if cnt % 2 == 1: odd_cnt += 1 if odd_cnt > k: return False return True
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: if k > len(s): return False cnt = Counter() for c in s: cnt[c] +=1 single_nums = 0 for key in cnt: if cnt[key] % 2 == 1: single_nums +=1 if k < single_nums: return False return True
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: odd = 0 counts = defaultdict(lambda: 0) for c in s: counts[c] += 1 if (counts[c]%2 == 0): odd -= 1 else: odd += 1 if (odd > k or k>len(s)): return False return True
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: odd = 0 counts = defaultdict(lambda: 0) for c in s: counts[c] += 1 if (counts[c]%2): odd += 1 else: odd -= 1 if (odd > k or k>len(s)): return False return True
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: cnt_map = collections.defaultdict(int) for c in s: cnt_map[c] += 1 n_odd = 0 for c in cnt_map: if cnt_map[c] % 2 == 1: n_odd += 1 if n_odd <= k <= len(s): return True else: return False
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: ''' Compute the frequency of each characters. Count the 奇数次的字母, each palindrome can consume at most one char with odd frequency. thus k must >= |odd|. ans = k <= len(s) and k >= odd ''' if k>len(s): return False freq=collections.defaultdict(int) for c in s: freq[c]+=1 odd=0 for c in freq: odd+=freq[c]&1 return odd<=k
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
from collections import Counter class Solution: def canConstruct(self, s: str, k: int) -> bool: if len(s) < k: return False counter = Counter(s) count = 0 for key in counter: if counter[key] % 2 == 1: count += 1 return count <= k
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: if len(s) < k: return False cnts = collections.defaultdict(int) for ch in s: cnts[ch] += 1 odds, evens = [], [] for ch in cnts: if cnts[ch] % 2 == 1: odds.append(ch) else: evens.append(ch) return len(odds) <= k class Solution1: def canConstruct(self, s: str, k: int) -> bool: cnts = collections.defaultdict(int) for ch in s: cnts[ch] += 1 odds, evens = [], [] for ch in cnts: if cnts[ch] % 2 == 1: odds.append(ch) else: evens.append(ch) if len(odds) > k: return False for i in range(len(odds)): k -= 1 ch = odds.pop() cnts[ch] -= 1 if cnts[ch] > 0: evens.append(ch) while k > len(evens): if not evens: return False ch = evens.pop() k -= 2 cnts[ch] -= 2 if cnts[ch] > 0: evens.append(ch) return True
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: if k > len(s): return False elif k == len(s): return True seen = {} for letter in s: seen[letter] = seen.get(letter, 0) + 1 singles = 0 for key in seen: value = seen[key] if value % 2 == 1: singles += 1 return singles <= k
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: return sum(i & 1 for i in collections.Counter(s).values()) <= k <= len(s)
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
from collections import Counter class Solution: def canConstruct(self, s: str, k: int) -> bool: counter = Counter(s) evens = 0 odds = 0 for key, value in counter.items(): if value % 2 == 0: evens += 1 else: odds += 1 return k >= odds and len(s) >= k
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: sCounter = collections.Counter(s) numberOfLettersWithOddCounts = len([count for count in list(sCounter.values()) if count % 2 != 0]) return not (len(s) < k or numberOfLettersWithOddCounts > k)
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Example 4: Input: s = "yzyzyzyzyzyzyzy", k = 2 Output: true Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome. Example 5: Input: s = "cr", k = 7 Output: false Explanation: We don't have enough characters in s to construct 7 palindromes.   Constraints: 1 <= s.length <= 10^5 All characters in s are lower-case English letters. 1 <= k <= 10^5
class Solution: def canConstruct(self, s: str, k: int) -> bool: return sum(i & 1 for i in collections.Counter(s).values()) <= k <= len(s)
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ if n==2:return 1 if n==3:return 2 res=1 while n>4: n=n-3 res*=3 return res*n
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ import math if n == 2 or n == 3: return n-1 else: if n % 3 == 1: m1 = 2 elif n % 3 == 2: m1 = 1 else: m1 = 0 m2 = (n-2*m1) // 3 return 2**m1 * 3**m2 # import math # if n == 2 or n == 3: # return n - 1 # else: # x = n/math.e # ans = 0 # for y in [math.floor(x), math.ceil(x)]: # x1 = int(n/y) # x2 = x1 + 1 # n2 = n - x1*y # n1 = y - n2 # ans = max(ans, (x1**n1) * (x2**n2)) # return int(ans)
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ memo = [0] * (n+1) memo[0] = 0 memo[1] = 1 for i in range(2, n+1): maxprod = 1 for j in range(1, i): maxprod = max(maxprod, max(j, memo[j]) * max((i-j), memo[i-j])) memo[i] = maxprod return memo[n]
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ res = [] for i in range(n+1): res.append(-1) for i in range(n+1): for j in range(i): res[j] = max(j, res[j]) res[i] = max(res[i], res[j]*(i-j)) return res[n]
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ dp = [0 for i in range(n+1)] dp[1] = 1 # for i in range(1, n+1): # for j in range(1, n+1): for i in range(2, n+1): for j in range(1, i): dp[i] = max(max(dp[j],j) * max(dp[i-j], i-j), dp[i]) return dp[-1]
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int https://leetcode.com/problems/integer-break/discuss/80689/A-simple-explanation-of-the-math-part-and-a-O(n)-solution use 3 as many as possible """ if n < 4: return n - 1 ans = 1 while n > 4: ans *= 3 n -= 3 ans *= n return ans
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ ans = [1] * (n+1) for i in range(2, n+1): ans[i] = ans[i-1] for j in range(1, 10): if i-j > 0 and ans[i-j] * j > ans[i]: ans[i] = ans[i-j] * j if i < n: ans[i] = max(ans[i], i) return ans[n]
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ ans = [1] * (n+1) for i in range(2, n+1): ans[i] = ans[i-1] for j in range(1, 10): if i-j > 0 and ans[i-j] * j > ans[i]: ans[i] = ans[i-j] * j if i < n: # 关键是这一步, 如果不等于和, 那么可以不拆分 ans[i] = max(ans[i], i) return ans[n]
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ memo = [0] * (59) memo[1] = 1 if n == 2: return 1 if n == 3: return 2 for i in range(2, n + 1): tmp = [] for j in range(1, i): memo[i] = max(i, memo[i], (memo[j] * memo[i - j])) return memo[n]
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ dp = [0 for _ in range(n+1)] dp[1] = 1 for i in range(1, n+1): for j in range(1, i): dp[i] = max(max(dp[j],j) * max(i-j, dp[i-j]), dp[i]) return dp[-1]
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ memo = [0] * (59) memo[1] = 1 for i in range(2, n + 1): tmp = [] for j in range(1, i): memo[i] = max((memo[i], max(memo[j], j) * max(i-j,memo[i - j]))) return memo[n]
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ if n <= 3: return n - 1 if n == 4: return 4 if n > 4: if n%3 ==0 : return 3 ** int(n/3) if n%3 !=0 and (n-2)%3 ==0: return (3 ** int((n-2)/3)) *2 else: return (3 ** int((n-2)/3)) * 4
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ if n==2: return 1; if n==3: return 2; prod = 1 while n > 4: prod *= 3 n-=3 prod *= n return prod
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not larger than 58. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ if n < 4: return n - 1 # Proof. # 1. Let n = a1 + a2 + ... + ak, product = a1 * a2 * ... * ak # - For each ai >= 4, we can always maximize the product by: # ai <= 2 * (ai - 2) # - For each aj >= 5, we can always maximize the product by: # aj <= 3 * (aj - 3) # # Conclusion 1: # - For n >= 4, the max of the product must be in the form of # 3^a * 2^b, s.t. 3a + 2b = n # # 2. To maximize the product = 3^a * 2^b s.t. 3a + 2b = n # - For each b >= 3, we can always maximize the product by: # 3^a * 2^b <= 3^(a+2) * 2^(b-3) s.t. 3(a+2) + 2(b-3) = n # # Conclusion 2: # - For n >= 4, the max of the product must be in the form of # 3^Q * 2^R, 0 <= R < 3 s.t. 3Q + 2R = n # i.e. # if n = 3Q + 0, the max of the product = 3^Q * 2^0 # if n = 3Q + 2, the max of the product = 3^Q * 2^1 # if n = 3Q + 2*2, the max of the product = 3^Q * 2^2 res = 0 if n % 3 == 0: # n = 3Q + 0, the max is 3^Q * 2^0 res = 3 ** (n // 3) elif n % 3 == 2: # n = 3Q + 2, the max is 3^Q * 2^1 res = 3 ** (n // 3) * 2 else: # n = 3Q + 4, the max is 3^Q * 2^2 res = 3 ** (n // 3 - 1) * 4 return res
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n = len(arr) if n<=1: return 0 l,r = n,-1 for i in range(1,n): if arr[i]<arr[i-1]: l = i break # monotonicially increasing if l == n: return 0 for j in range(n-2,-1,-1): if arr[j]>arr[j+1]: r = j break # you can delete all to the left of r (including r) # you can also delete all to the right of l (including l) ans = min(r+1,n-l) i = 0 # sliding window, find the rightmost i for each j # note at all time i must be less than l for j in range(r+1,n): while i<l and arr[i] <= arr[j]: i += 1 ans = min(ans,j-i) return ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n = len(arr) s = 0 e = n - 1 while s < (n - 1) and arr[s] <= arr[s + 1]: s+=1 if(s == n - 1): return 0 while e >= s and arr[e] >= arr[e - 1]: e-=1 result = min(n - 1 - s, e) i = 0 j = e while(i <= s and j < n): if arr[j] >= arr[i]: result = min(result, j - i - 1) i+=1 else: j+=1 return result
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: stack = [0] stack2 = [] total = 0 for i in range(1,len(arr)): if arr[i]>=arr[i-1]: stack.append(0) else: total+=1 stack2.append(i) stack.append(1) if total==0: return 0 print(stack2) #erase small def erase(i,j): print((i,j)) if (i>0 and j<len(arr)-1) and arr[i-1]>arr[j+1]: return min(erase(i-1,j),erase(i,j+1)) else: return j-i+1 if total==1: return erase(stack2[0],stack2[0]) return erase(stack2[0],stack2[-1]-1)
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: templ = [] tempr = [] flag = 0 for i in range(len(arr)-1): if(arr[i]>arr[i+1]): flag = 1 if(flag == 0): return(0) for i in range(1,len(arr)): if(arr[i]>=arr[i-1]): templ.append(arr[i-1]) else: templ.append(arr[i-1]) break for i in range(len(arr)-1,0,-1): if(arr[i]<arr[i-1]): tempr.append(arr[i]) break else: tempr.append(arr[i]) print((templ,tempr)) if(len(templ)==len(tempr)): if(len(templ) == len(arr) or len(templ) ==0): return(len(templ)) print((templ,tempr)) from bisect import bisect_right mmax = max(len(templ),len(tempr)) for i in range(len(tempr)): ind = bisect_right(templ,tempr[i]) print((i,ind)) mmax = max(mmax,ind+i+1) return(max(0,len(arr) - (mmax)))
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: if len(arr) < 2: return 0 for i1 in range(len(arr)-1): if arr[i1+1] >= arr[i1]: continue else: break #arr[0] - arr[i1] is increasing and arr[i1+1] < arr[i1] #if overall sorted if arr[-2] <= arr[-1] and i1 == len(arr)-2: return 0 for i2 in range(len(arr)-1,0,-1): if arr[i2]>= arr[i2-1]: continue else: break #arr[i2] - arr[len(arr)-1] is increasing and arr[i2-1] < arr[i2] print(i1,i2) remove = i2-1-(i1+1)+1 ans = min(remove+i1+1,remove+len(arr)-i2) #binary search to find positions of num in first part in the second part for i in range(i1,-1,-1): num = arr[i] l,r = i2,len(arr) while l < r: mid = (l+r)//2 if arr[mid] >= num: r = mid else: l = mid+1 # if found if l < len(arr): cur = remove + i1-i + l- i2 ans = min(cur,ans) return ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: stack = [[len(arr)-1, arr[-1]]] for i in range(len(arr)-2, -1, -1): if arr[i] <= arr[i+1]: stack.append([i, arr[i]]) else: break min_len = len(arr)-len(stack) for i in range(len(arr)): if i == 0 or arr[i] >= arr[i-1]: while len(stack) > 0 and (stack[-1][1] < arr[i] or stack[-1][0] <= i): stack.pop() if len(stack) > 0: min_len = min(min_len, stack[-1][0]-i-1) else: min_len = min(min_len, len(arr)-i-1) else: break return min_len
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n=len(arr) answer=n-1 A=arr if len(arr)==1: return 0 flag=False for i in range(1,n): if A[i]<A[i-1]: flag=True j=n-1 while(j>i and A[j]>=A[i-1]) and (j==n-1 or A[j]<=A[j+1]): j=j-1 answer=min(answer,j-i+1) break if flag==False: return 0 for i in range(n-2,-1,-1): if A[i]>A[i+1]: j=0 while(j<i and A[j]<=A[i+1]) and (j==0 or A[j]>=A[j-1]): j=j+1 answer=min(answer,i-j+1) break return answer
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: if len(arr) < 2: return 0 for i1 in range(len(arr)-1): if arr[i1+1] >= arr[i1]: continue else: break #arr[0] - arr[i1] is increasing and arr[i1+1] < arr[i1] #if overall sorted if arr[-2] <= arr[-1] and i1 == len(arr)-2: return 0 for i2 in range(len(arr)-1,0,-1): if arr[i2]>= arr[i2-1]: continue else: break #arr[i2] - arr[len(arr)-1] is increasing and arr[i2-1] < arr[i2] # print(i1,i2) remove = i2-1-(i1+1)+1 ans = min(remove+i1+1,remove+len(arr)-i2) #binary search to find positions of num in first part in the second part for i in range(i1,-1,-1): num = arr[i] l,r = i2,len(arr) while l < r: mid = (l+r)//2 if arr[mid] >= num: r = mid else: l = mid+1 # if found if l < len(arr): cur = remove + i1-i + l- i2 ans = min(cur,ans) return ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: l = 0 n = len(arr) while l<n-1 and arr[l]<=arr[l+1]: l+=1 # print(l) if l == n-1:return 0 # if l == 0: return len(arr)-1 r = len(arr)-1 while r>0 and arr[r]>=arr[r-1]: r-=1 left = n-(l+1) right = r # print(r) ans = min(left,right) i=0 j=r while i<=l and j<n: if arr[j]>=arr[i]: ans =min(ans,j-i-1) i+=1 else: j+=1 return ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: l,r = 0,len(arr) - 1 while l < r and arr[l + 1] >= arr[l]: l += 1 if l == len(arr) - 1: return 0 while r > 0 and arr[r - 1] <= arr[r]: r -= 1 toRemove = min(len(arr) - l - 1,r) for iL in range(l + 1): if arr[iL] <= arr[r]: toRemove = min(toRemove,r - iL - 1) elif r < len(arr) - 1: r += 1 else: break return toRemove
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
from sortedcontainers import SortedDict class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: if len(arr) <= 1: return 0 last = -float('inf') sl = SortedDict() for i, n in enumerate(arr): if n < last: break sl[n] = i last = n ans = len(arr) - i last = float('inf') for i in range(len(arr)-1, 0, -1): if arr[i] > last: break last = arr[i] sl_idx = sl.bisect_right(arr[i]) ans = min(ans, i - sl_idx) return max(0, ans)
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: # check best from left # check best from right left, right = 0, len(arr) - 1 while left < len(arr) - 1 and arr[left] <= arr[left + 1]: left += 1 if left == len(arr) - 1: return 0 while right > 0 and arr[right - 1] <= arr[right]: right -= 1 best = min(len(arr) - 1 - left, right) # only prefix or suffix parts # merging prefix and suffix # first checking all elements smaller than left to see which ones can be used before the right index draw a line going from (0,0) -> (3,2) and a line going form (7, 1) -> (10, 3) and notice which arrays you can make by drawing horizontal lines on y = 1 and y = 2. # Essentially we start from the beginning and we see which ones from the left can exist if right is where it was before. If we get to a left that is greater than right, we increment right by 1 to see if the next left < the next right. We keep on repeating this process until we cant (when the current left is greater than the current right and the current right is the last element). At that point, we just return the smallest we have seen until that time. for i in range(left + 1): if arr[i] <= arr[right]: best = min(best, right - i - 1) elif right < len(arr) - 1: right += 1 else: break # The following method fails for [1,2,3,10,0,7,8,9] # for i in range(left + 1): # if arr[i] <= arr[right]: # best = min(best, right - i - 1) # for i in range(len(arr) - 1, right - 1, -1): # print(arr[left], arr[i]) # if arr[left] <= arr[i]: # best = min(best, i - left - 1) return best
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: k = 0 while k+1 < len(arr) and arr[k+1] >= arr[k]: k += 1 if k == len(arr) - 1: return 0 l = len(arr) - 1 while l > 0 and arr[l - 1] <= arr[l]: l -= 1 ans = max(k + 1, len(arr) - l) for i in range(k + 1): while l < len(arr) and arr[i] > arr[l]: l += 1 ans = max(ans, i + 1 + len(arr) - l) return len(arr) - ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: l = len(arr) if l == 1: return 0 longest = 0 # three possibilities: # 1. head + tail, 2. head, 3. tail # head benchmark = 'a' for i in range(1, l): if arr[i-1] > arr[i]: head_longest = i benchmark = arr[i-1] break if benchmark == 'a': return 0 i = l - 2 if arr[i+1] >= benchmark: head_longest += 1 while arr[i] <= arr[i+1] and arr[i] >= benchmark: head_longest += 1 i -= 1 # tail for i in range(l-2, -1, -1): if arr[i] > arr[i+1]: tail_longest = l - i - 1 benchmark = arr[i+1] break i = 1 if arr[i-1] <= benchmark: tail_longest += 1 while arr[i-1] <= arr[i] and arr[i] <= benchmark: tail_longest += 1 i += 1 longest = max(head_longest, tail_longest) return l - longest
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
from bisect import bisect_left as bl ,bisect_right as br,bisect as bis,insort_left as il,insort_right as ir ,insort as isrt class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: maxarr=[1] n=len(arr) flag=0 for i in range(1,n): if (arr[i]>=arr[i-1]): maxarr.append(maxarr[-1]+1) else: flag=1 maxarr.append(1) if flag ==0: return 0 arr1=[arr[0]] arr2=[arr[-1]] i=1 while i<len(arr) and maxarr[i]>maxarr[i-1]: arr1.append(arr[i]) i+=1 i=len(arr)-2 while i >=0 and maxarr[i]<maxarr[i+1]: arr2.append(arr[i]) i-=1 print((arr1, arr2[::-1])) arr2= arr2[::-1] size = len(arr2) maxm = len(arr1) for i in arr2: index = br(arr1 , i) maxm = max(index +size ,maxm) size-=1 return len(arr)-maxm
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: # Find the longest monotonically non-decreasing prefix array in [0, i] n = len(arr) l = 1 while l < n and arr[l-1] <= arr[l]: l += 1 l -= 1 if l >= n - 2: return n - 1 - l # Find the longest monotonically non-decreasing suffix array in [j, n - 1] r = n - 2 while r >= 0 and arr[r] <= arr[r+1]: r -= 1 r += 1 if r <= 1: return r # Start off with min of either left or right side removed as the min_to_move min_to_remove = min(n - 1, n - 1 - (l + 1) + 1, r) # Try to merge [0..i] from the left into [r..n-1]. We start with assuming # entire right side [r..n-1] could be included and try to see if [0], [0, 1] and so on # from the left side could be merged in. Until we hit some i on the left side that is # too large for the current arr[r], retreat r back further to the right to try # against a bigger value. Keep trying until we run out of either room for i to expand # toward the right or for no more r left to retreat to the right. i = 0 while i < l + 1: if arr[i] <= arr[r]: min_to_remove = min(min_to_remove, r - i - 1) # we could definitely include arr[i] into the result i += 1 elif r < n - 1: # We cannot include arr[r] into the result but we could # include arr[i] into the result r += 1 i += 1 else: break return min_to_remove def findLengthOfShortestSubarray_bruteforce_bsearch(self, arr: List[int]) -> int: # Returns the smallest index `idx` such that arr[idx] >= val def bsearch(val, low, high): while low < high: mid = (low + high) // 2 if arr[mid] >= val: high = mid else: low = mid + 1 if val > arr[high]: return high + 1 return high # Find the longest monotonically non-decreasing prefix array in [0, i] n = len(arr) curr_max = float('-inf') i = 0 while i < n: if arr[i] < curr_max: break curr_max = arr[i] i += 1 i -= 1 if i >= n - 2: return n - 1 - i # Find the longest monotonically non-decreasing suffix array in [j, n - 1] curr_min = float('inf') j = n - 1 while j >= 0: if arr[j] > curr_min: break curr_min = arr[j] j -= 1 j += 1 if j <= 1: return j # For each value arr[k], in [0, i], find the `right_idx` into the suffix array in [j, n - 1] # such that arr[k] >= arr[right_idx]. We want to find the one that results in the longest # sequence [0..k] + [right_idx..n-1] min_to_remove = float('inf') for k in range(i, -1, -1): right_idx = bsearch(arr[k], j, n - 1) l = k + 1 + n - right_idx to_remove = n - l min_to_remove = min(min_to_remove, to_remove) # For each value arr[k], in [j, n-1], find the `left_idx` into the prefix array in [0, i] # such that arr[k] >= arr[left_idx - 1]. We want to find the one that results in the longest # sequence [0..left_idx-1] + [k..n-1] for k in range(j, n): left_idx = bsearch(arr[k], 0, i) l = left_idx + n - k to_remove = n - l min_to_remove = min(min_to_remove, to_remove) return min_to_remove
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n = len(arr) left, right = 0, n-1 while left < n-1 and arr[left] <= arr[left+1]: #non-decreasing, keep increment left+=1 while right>0 and arr[right] >= arr[right-1]: right-=1 num_to_remove = min(n-left-1, right) #general case: #from the left for curr in range(left+1): if arr[curr]<=arr[right]: num_to_remove = min(num_to_remove, max(0, right-curr-1)) else: # arr[curr]>arr[right] if right < n-1: right += 1 else: #last element break return num_to_remove
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: # determine right sorted subarray r = len(arr) - 1 while r >0 and arr[r] >= arr[r-1]: r -= 1 # determine left sorted subarray that can be merged with right ret = r # set ret to worst case, remove all left l = 0 for l in range(len(arr)): # if left subarry is not in order anymore, stop, can't do better if l>0 and arr[l] < arr[l-1]: break # if l==r if l == r: return 0 while r < len(arr) and arr[l] > arr[r] : r +=1 ret = min(ret, r-l-1) return ret
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n = len(arr) left, right = 1, 1 i = 1 while i < n and arr[i] >= arr[i - 1]: left += 1 i += 1 i = n - 2 while i >= 0 and arr[i] <= arr[i + 1]: right += 1 i -= 1 l1, l2 = right, left i = n - right while i < n and arr[i] < arr[left - 1]: i += 1 l1 -= 1 i = left - 1 while i >= 0 and arr[i] > arr[n - right]: i -= 1 l2 -= 1 return max(min(n - left - l1, n - right - l2), 0)
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: l = [] for x in arr: if not l or x >= l[-1]: l.append(x) else: break r = [] for i in range(len(arr)-1, -1, -1): if not r or arr[i] <= r[-1]: r.append(arr[i]) else: break r.reverse() if len(l) == len(arr): return 0 sol = max(len(l), len(r)) i, j = 0, 0 # print(l, r) while i < len(l) and j < len(r): if l[i] <= r[j]: i += 1 else: while j < len(r) and l[i] > r[j]: j += 1 # print(i, j) sol = max(sol, i+len(r)-j) return len(arr) - sol
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: ''' 1,2,3,10,4,2,3,5] 1 2 2 3 3 4 5 10 ''' def findLengthOfShortestSubarray(self, A: List[int]) -> int: N = len(A) left, right = 0, N - 1 while left + 1 < N and A[left] <= A[left + 1]: left += 1 if left == N - 1: return 0 while right > left and A[right - 1] <= A[right]: right -= 1 ans = min(N - left - 1, right) i, j = 0, right while i <= left and j < N: if A[j] >= A[i]: ans = min(ans, j - i - 1) i += 1 else: j += 1 return ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n = len(arr) if(n==1): return 0 l = 0 r = n - 1 while(r > 0): if(arr[r-1] > arr[r]): break r = r - 1 ans = r - l while(l < r): if(l > 0 and arr[l-1] > arr[l]): break while(r < n and arr[r] < arr[l]): r = r + 1 ans = min(ans, r - l - 1) l = l + 1 return ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
import bisect class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: a = 0 for i in range(len(arr)): if i == 0 or arr[i] >= arr[i - 1]: a += 1 else: break b = 0 for i in reversed(list(range(len(arr)))): if i == len(arr) -1 or arr[i] <= arr[i + 1]: b += 1 else: break if a == len(arr): return 0 al = arr[: a] bl = arr[len(arr) - b: ] ans = len(arr) - 1 for i in range(a): target = al[i] index = bisect.bisect_left(bl, target) if index == len(bl): ans = min(ans, len(arr) - i - 1) else: ans = min(ans, len(arr) - (i + 1) - (len(bl) - index)) ans = min(ans, len(arr) - len(bl)) return ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: arr = [-1e99]+arr+[1e99] left_p = 0 while left_p + 1 < len(arr) and arr[left_p] <= arr[left_p+1] : left_p += 1 right_p = len(arr)-1 while right_p-1 >= 0 and arr[right_p] >= arr[right_p-1] : right_p -= 1 right_p_old = right_p if left_p > right_p : return 0 while arr[right_p] < arr[left_p] : right_p += 1 to_ret = right_p - left_p - 1 while right_p >= right_p_old and left_p > 0 : left_p -= 1 while right_p-1 >= right_p_old and arr[right_p-1] >= arr[left_p] : right_p -= 1 to_ret = min(to_ret, right_p - left_p - 1 ) return to_ret # 10 2 99 12 13
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: # O(n) def findLengthOfShortestSubarray(self, arr: List[int]) -> int: # determine right sorted subarray r = len(arr) - 1 while r >0 and arr[r] >= arr[r-1]: r -= 1 # r == 0 means array is sorted if r == 0: return 0 # now determine left sorted subarray that can be merged with right ret = r # set ret to worst case, remove all left l = 0 for l in range(len(arr)): # if left subarry is not in order anymore, stop, can't do better if l>0 and arr[l] < arr[l-1]: break # if arr[l] is larger than the left edge of right sorted subarray, # push the edge to the right # why this work? Is it OK to push to the right? We won't consider # arr[i+1] and arr[r]. # It's OK! We don't have to consider arr[i+1] and arr[r]. # if arr[i] is larger than the left edge, arr[i+1] is larger too! # so it won't be the possible answer # it's possible that you push it to the end of right subarray # for example [9,10,11,5], keep pushing r to 4 # in this case, right subarray is empty []. while r < len(arr) and arr[l] > arr[r] : r +=1 ret = min(ret, r-l-1) return ret
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: N = len(arr) lo, hi = 0, N - 1 while lo + 1 < N and arr[lo] <= arr[lo + 1]: lo += 1 if lo >= N - 1: return 0 while hi - 1 >= 0 and arr[hi - 1] <= arr[hi]: hi -= 1 # [0..lo] [hi..N-1] res = min(N - lo - 1, hi) j = hi for i in range(lo + 1): while j < N and arr[j] < arr[i]: j += 1 res = min(res, j - i - 1) return res
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: l = res = 0 r = n = len(arr) increasing = [0] * n for i in range(1, n): if arr[i] >= arr[i-1]: increasing[i] = 1 + increasing[i-1] # print(arr) # print(increasing) while l < r: mid = (l+r) // 2 # print(mid) for i in range(n - mid + 1): isLeftInc = False isRightInc = False if i > 0 and increasing[i-1] == i-1 or i == 0: isLeftInc = True if (i < n-mid and increasing[n-1] >= n - i - mid - 1 and arr[i+mid] >= (0 if i < 1 else arr[i-1])) or i == n-mid : isRightInc = True # print(i, isLeftInc, isRightInc, i + mid, i-1) if isLeftInc and isRightInc: break if isLeftInc and isRightInc: res = mid r = mid else: l = mid+1 return res
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: i=0 while i<len(arr)-1 and arr[i]<=arr[i+1]: i+=1 if i==len(arr):return 0 j=len(arr)-1 while j and arr[j-1]<=arr[j]: j-=1 if j==-1:return 0 def lowerbound(left, right, target): while left < right: mid = left + (right - left) // 2 if arr[mid] == target: right = mid elif arr[mid] < target: left = mid + 1 else: right = mid return left N=len(arr) print((i,j)) out=min(N - (N - j), N - i -1) print(out) for k in range(i+1): hi=lowerbound(j,len(arr),arr[k]) out=min(out,hi-k-1) return max(0,out)
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: p = 1 while p < len(arr) and arr[p] >= arr[p - 1]: p += 1 first = p p = len(arr) - 1 while p >= 1 and arr[p] >= arr[p - 1]: p -= 1 second = p ans = min(len(arr) - first, second) i, j = 0, second # print(i, j) while i < first and j < len(arr): if arr[i] > arr[j]: ans = min(ans, j - i) j += 1 else: i += 1 ans = min(ans, j - i) return ans if ans > 0 else 0
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: if len(arr) <= 1: return 0 p1 = 0 p2 = len(arr) - 1 for i in range(1,len(arr)): if arr[i] >= arr[i-1]: p1 += 1 else: break for i in range(len(arr)-2, -1, -1): if arr[i] <= arr[i+1]: p2 = i else: break ans = min(len(arr) - p1 - 1 , p2) if p1 > p2: return ans p11, p22 = 0, p2 while p11 <= p1 and p22 < len(arr): if arr[p11] <= arr[p22]: ans = min(ans, p22 - p11 - 1) p11 += 1 else: p22 += 1 return ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
from functools import lru_cache class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: ''' 1 2 3 10 4 2 3 5 i j 1 2 3 ij ''' N = len(arr) i = 0 while i < N-1 and arr[i+1] >= arr[i]: i += 1 left = N - i - 1 j = N-1 while j > 0 and arr[j-1] <= arr[j]: j -= 1 right = j ret = N-1 @lru_cache(maxsize=None) def search(i, j): print(i, j) nonlocal ret if i == j: return 0 if arr[j] >= arr[i]: ret = min(ret, j-i-1) I = i while i < N-1 and arr[i+1] >= arr[i] and arr[i+1] <= arr[j]: i += 1 if i > I: search(i, j) i = I J = j while j > 0 and arr[j-1] <= arr[j] and arr[j-1] >= arr[i]: j -= 1 if j < J: search(i, j) search(0, N-1) return min(left, ret, right)
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: lastPos = len(arr)-1 stop = False idx = len(arr)-2 while idx > -1 and not stop: num = arr[idx] prev = arr[lastPos] if num <= prev: lastPos = idx else: stop = True idx -= 1 rightSub = lastPos minSub = rightSub idx = 0 stop = False valid = False while idx < rightSub and not stop: valid = False if idx == 0 or arr[idx] >= arr[idx-1]: num = arr[idx] while rightSub < len(arr) and not valid: numRight = arr[rightSub] if numRight < num: rightSub += 1 else: valid = True minSub = min(minSub, (rightSub - idx - 1)) idx += 1 else: stop = True idx -= 1 minSub = min(minSub, (rightSub - idx - 1)) return minSub
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n = len(arr) ans = float('inf') max_s, min_e = 0, n-1 curr = -1 for i, a in enumerate(arr): if a < curr: break curr = a max_s = i curr = float('inf') for i in range(n-1, -1, -1): if arr[i] > curr: break curr = arr[i] min_e = i # n - 1 - (max_s + 1) + 1 = n - 1 - max_ s # min_e - 1 - 0 + 1 = min_e ans = min(n - max_s - 1, min_e) for s in range(max_s+1): index, jump = n-1, n - 1 - min_e while jump >= 1: while index-jump >= min_e and arr[index-jump] >= arr[s]: index -= jump jump //= 2 if arr[index] >= arr[s]: ans = min(ans, index-1 - (s + 1) + 1) return max(ans, 0)
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: if arr == sorted(arr): return 0 for head in range(len(arr) - 1): if arr[head] > arr[head + 1]: break if head == len(arr): return 0 # already sorted for tail in range(len(arr) - 1, 0, -1): if arr[tail - 1] > arr[tail]: break lo = head + 1 hi = len(arr) output = float('inf') while lo >= 0: while hi - 1 >= tail and (lo == 0 or arr[lo - 1] <= arr[hi - 1]): hi -= 1 output = min(output, hi - lo) lo -= 1 return output
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n = len(arr) ans = float('inf') max_s, min_e = 0, n-1 curr = -1 for i, a in enumerate(arr): if a < curr: break curr = a max_s = i curr = float('inf') for i in range(n-1, -1, -1): if arr[i] > curr: break curr = arr[i] min_e = i # n - 1 - (max_s + 1) + 1 = n - 1 - max_ s # min_e - 1 - 0 + 1 = min_e ans = min(n - max_s - 1, min_e) for s in range(max_s+1): index, jump = n-1, n-1 while jump >= 1: while index-jump >= min_e and arr[index-jump] >= arr[s]: index -= jump jump //= 2 if index > s and arr[index] >= arr[s]: ans = min(ans, index-1 - (s + 1) + 1) return ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: def bsearch(val, low, high): while low < high: mid = (low + high) // 2 if arr[mid] >= val: high = mid else: low = mid + 1 if val > arr[high]: return high + 1 return high n = len(arr) curr_max = float('-inf') i = 0 while i < n: if arr[i] < curr_max: break curr_max = arr[i] i += 1 i -= 1 if i >= n - 2: return n - 1 - i curr_min = float('inf') j = n - 1 while j >= 0: if arr[j] > curr_min: break curr_min = arr[j] j -= 1 j += 1 if j <= 1: return j min_to_remove = float('inf') for k in range(i, -1, -1): right_idx = bsearch(arr[k], j, n - 1) l = k + 1 + n - right_idx to_remove = n - l min_to_remove = min(min_to_remove, to_remove) for k in range(j, n): left_idx = bsearch(arr[k], 0, i) l = left_idx + n - k to_remove = n - l min_to_remove = min(min_to_remove, to_remove) return min_to_remove
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: if arr == None or len(arr) <= 1: return 0 left ,right = 0, len(arr) - 1 while left +1 < len(arr) and arr[left] <= arr[left+1] : left += 1 if left == len(arr)-1: return 0 while right > 0 and arr[right-1] <= arr[right] : right -= 1 result = min(len(arr)-left-1, right) if arr[left] <= arr[right]: result = min(result, right-left-1) for i in range(left+1): if arr[i] <= arr[right] : result = min(result, right-i-1) elif right < len(arr)-1 : right += 1 return result
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def canBeMadeSortedByDeletingKElements(self, arr: List[int], k: int) -> bool: # k : 0 1 2 3 4 5 . . . # f(k): 0 0 0 0 1 1 1 1 1 1 1 1 n = len(arr) i = 1 while i < n and arr[i - 1] <= arr[i]: i += 1 j = 1 while j < n and arr[n - j - 1] <= arr[n - j]: j += 1 if i == n: # and j == n return True # i > k j # ----- # # # # # ------ # 3 4 5 1 1 2 3 4 # k = 2 if i + j + k < n: return False # k = 3 # 0 1 2 3 4 5 6 # k = 3 # 0 1 2 3 4 5 # i = 0: - - - # i = 1: - - - for start in range(0, n - k + 1): left = start right = n - left - k if left <= i and right <= j and (left == 0 or right == 0 or arr[left - 1] <= arr[n - right]): return True return False def findLengthOfShortestSubarray(self, arr: List[int]) -> int: L = 0 R = len(arr) - 1 answer = -1 while L <= R: M = (L + R) // 2 if self.canBeMadeSortedByDeletingKElements(arr, M): answer = M R = M - 1 else: L = M + 1 return answer
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: l = 0 r = len(arr)-1 while l < len(arr)-1 and arr[l+1] >= arr[l]: l +=1 if l == len(arr)-1: return 0 while r > 0 and arr[r-1] <= arr[r]: r -=1 res = min(len(arr)-l-1, r) for i in range(l+1): if arr[i] <= arr[r]: res = min(res, r-i-1) elif r < len(arr)-1: r +=1 else: break return res
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: if all([arr[i+1] >= arr[i] for i in range(len(arr)-1)]): return 0 start = 0 for i in range(1, len(arr)): if arr[i] >= arr[i-1]: start = i else: break end = len(arr) - 1 for i in range(len(arr)-1)[::-1]: if arr[i] <= arr[i+1]: end = i else: break lo = -1 res = min(len(arr) - start - 1, end) for i in range(end, len(arr)): while lo < start and arr[lo+1] <= arr[i]: lo += 1 res = min(res, i - lo - 1) return res
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
import bisect class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: s={} a=[(arr[0],0)] s[0]=(0,0) n=len(arr) for i in range(1,n): if s[i-1][1]==0: move=1 else: move=s[i-1][0]+1 index=bisect.bisect_right(a,(arr[i],i)) if index==0: nmove=i else: nmove=i-a[index-1][1]-1 if arr[i-1]<=arr[i]: nmove=min(nmove,s[i-1][1]) if nmove==0: bisect.insort_right(a,(arr[i],i)) s[i]=(move,nmove) #print(s) return min(s[n-1][0],s[n-1][1])
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: left = 0 while left < len(arr) - 1: if arr[left] <= arr[left + 1]: left += 1 else: break if left >= len(arr) - 1: return 0 right = len(arr) - 1 while right > 0: if arr[right] >= arr[right - 1]: right -= 1 else: break ll = 0 rr = right res = min(len(arr) - left - 1, right) while ll < left + 1 and rr < len(arr): if arr[ll] <= arr[rr]: res = min(res, rr - ll - 1) ll += 1 else: rr += 1 return res
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n = len(arr) left = [False] * n # all to left are increasing right = [False] * n # all to right are increasing left[0] = True right[n-1] = True for i in range(1, n): left[i] = left[i - 1] and (arr[i] >= arr[i-1]) for i in range(n-2, -1, -1): right[i] = right[i + 1] and (arr[i] <= arr[i + 1]) def isPossible(num): res = False for i in range(n): if i == 0 or left[i - 1]: if i + num >= n or (right[i + num] and (i == 0 or arr[i - 1] <= arr[i + num])): res = True break return res be = 0 en = n - 1 while be < en: mid = be + (en - be) // 2 if isPossible(mid): en = mid else: be = mid + 1 return be
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: right=[] left=[] if not arr: return 0 pre=arr[-1]+1 while arr and arr[-1]<=pre: pre=arr[-1] right.append(arr.pop()) pre=-1 while arr and arr[0]>=pre: pre=arr[0] left.append(arr.pop(0)) l=[i for i in left] r=[i for i in right] val=0 while l and r and l[-1]>r[-1]: l.pop() val+=1 out=val val=0 while right and left and right[-1]<left[-1]: right.pop() val+=1 out=min(out,val) return out+len(arr)
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: l, r = 0, len(arr) - 1 while l < r and arr[l+1] >= arr[l]: l += 1 if l == len(arr) - 1: return 0 # whole array is sorted while r > 0 and arr[r-1] <= arr[r]: r -= 1 toRemove = min(len(arr) - l - 1, r) for iL in range(l+1): if arr[iL] <= arr[r]: toRemove = min(toRemove, r - iL - 1) elif r < len(arr) - 1: r += 1 else: break return toRemove
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, nums: List[int]) -> int: # equal to max increasing subarray seqs = [0] last = nums[0] for i, num in enumerate(nums[1:]): if num < last: seqs.append(i+1) last = num if len(seqs) == 1: return 0 no_first_size = seqs[-1] has_first_size = len(nums) - seqs[1] for first_end in range(0, seqs[1]): first_size = len(nums) for i in range(seqs[-1], len(nums)): if nums[i] >= nums[first_end]: first_size = i - first_end - 1 break has_first_size = min(has_first_size, first_size) if first_size == len(nums): break return min(no_first_size, has_first_size)
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution(object): def findLengthOfShortestSubarray(self, arr): stack = [[len(arr)-1, arr[-1]]] for i in range(len(arr)-2, -1, -1): if arr[i] <= arr[i+1]: stack.append([i, arr[i]]) else: break min_len = len(arr)-len(stack) for i in range(len(arr)): if i == 0 or arr[i] >= arr[i-1]: while len(stack) > 0 and (stack[-1][1] < arr[i] or stack[-1][0] <= i): stack.pop() if len(stack) > 0: min_len = min(min_len, stack[-1][0]-i-1) else: min_len = min(min_len, len(arr)-i-1) else: break return min_len
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n = len(arr) before = n after = 0 for i in range(1, n): if arr[i] < arr[i - 1]: before = i - 1 break if before == n: return 0 for i in range(n - 1)[::-1]: if arr[i] > arr[i + 1]: after = i + 1 break l, r = 0, n while l < r: m = l + (r - l) // 2 ok = before >= n - 1 - m or after <= m for start in range(1, n): if start + m >= n: break else: left = before >= start - 1 right = after <= start + m ok |= left and right and arr[start - 1] <= arr[start + m] if ok: r = m else: l = m + 1 return l
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n = len(arr) if n == 1: return 0 last = n - 1 while last > 0 and arr[last - 1] <= arr[last]: last -= 1 ans = last for i in range(n): if i > 0 and arr[i] < arr[i - 1]: break while last < n and (last <= i or arr[last] < arr[i]): last += 1 ans = min(ans, last - i - 1) return ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, A: List[int]) -> int: N = len(A) if N == 1: return 0 # Find left_end and right start left_end, right_start = 0, N - 1 for i in range(1, N): if A[i] >= A[i-1]: left_end = i else: break for i in range(N-2, -1, -1): if A[i] <= A[i+1]: right_start = i else: break # Increasing array, no need to do anything print((left_end, right_start)) if left_end > right_start: return 0 ret = min(N - left_end - 1, right_start) # Two pointer j = right_start for i in range(left_end + 1): if j < N and A[i] <= A[j]: ret = min(ret, j - i - 1) else: while j < N and A[j] < A[i]: j += 1 ret = min(ret, j - i - 1) return ret
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
from bisect import bisect_right class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: for i in range(len(arr)-1): if arr[i+1]<arr[i]: u = i break else: return 0 for j in range(len(arr)-1,-1, -1): if arr[j]<arr[j-1]: v = j break arr1 = arr[:u+1] arr2 = arr[v:] print(arr1, arr2) ans = len(arr) for i in range(len(arr2)): ans = min(ans, len(arr) - (bisect_right(arr1, arr2[i]) + 1 + len(arr2)-i-1)) return min(ans, len(arr)-len(arr1), len(arr)-len(arr2))
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: def helper(i, j): if i < 0 or j >= len(arr): return 0 if arr[i] > arr[j]: return min(helper(i-1, j), helper(i, j+1)) + 1 return 0 intervals = [] start = 0 end = 1 while end < len(arr): if arr[end] >= arr[end-1]: end += 1 else: intervals.append((start, end)) start = end end = start + 1 if start != len(arr): intervals.append((start, end)) if len(intervals) < 2: return 0 result = intervals[-1][0] - intervals[0][1] return result + helper(intervals[0][1]-1, intervals[-1][0])
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: if arr == list(sorted(arr)): return 0 increasing_parts = [] curr = [] for num in arr: if not curr or curr[-1] <= num: curr.append(num) else: increasing_parts.append(curr) curr = [num] increasing_parts.append(curr) first = set(increasing_parts[0]) second = set(increasing_parts[-1]) first_pos = {} sec_pos = {} last_start = sum([len(x) for x in increasing_parts[:-1]]) for i, num in enumerate(increasing_parts[0]): first_pos[num] = i for i, num in enumerate(increasing_parts[-1]): sec_pos[num] = i + last_start best = 0 seq = list(sorted(increasing_parts[0] + increasing_parts[-1])) for i, num in enumerate(seq): if num in first: if i < len(seq) - 1 and seq[i + 1] in second: best = max(first_pos[num] + 1 + len(arr) - sec_pos[seq[i + 1]], best) return min([len(arr) - len(increasing_parts[0]), len(arr) - len(increasing_parts[-1]), len(arr) - best])
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: L1=[arr[0]] L2=[] for i in range(1,len(arr)): if arr[i]>=arr[i-1]: L1.append(arr[i]) else: break temp=[arr[-1]] for i in range(len(arr)-2,-1,-1): if arr[i]<=arr[i+1]: temp.append(arr[i]) else: break L2=temp[-1::-1] margin=len(arr)-(len(L1)+len(L2)) m=min(len(L1),len(L2)) MIN=m+margin LENGTH1=len(L1)-1 LENGTH2=len(L2)-1 # print(L1,L2) i=len(L1)-1 j=len(L2)-1 while i>=0 and j>=0: if L1[i]<=L2[j]: j-=1 a=LENGTH1-i b=j+1 # print(i,j,a+b) MIN=min(MIN,margin+a+b) else: i-=1 if j!=LENGTH2: j+=1 # print(i,j) # print() i=0 j=0 while i<len(L1) and j<len(L2): if L1[i]<=L2[j]: i+=1 a=LENGTH1-i+1 b=j # print(i,j,a+b) MIN=min(MIN,margin+a+b) else: j+=1 if i!=0: i-=1 # print(i,j) if MIN<0: MIN=0 return MIN
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: left = [float('inf') for _ in range(len(arr))] right = [-float('inf') for _ in range(len(arr))] last = -float('inf') for i,x in enumerate(arr): if x >= last: left[i] = x else: break last = x last = float('inf') _n = len(arr) - 1 right_k = 0 for i,x in enumerate(reversed(arr)): if x <= last: right[_n-i] = x else: right_k = i break last = x ans = float('inf') import bisect for i,x in enumerate(left): if x < float('inf'): j = bisect.bisect_left(right, x, i+1) ans = min(ans, j-i-1) else: break ans = min(ans, len(arr)-right_k) return ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: a = [float('-inf')] + arr + [float('inf')] n = len(a) nondecrleft = [0] * n nondecrleft[0] = 1 for i in range(1, n): if a[i] < a[i-1]: break nondecrleft[i] = 1 nondecrright = [0] * n nondecrright[n-1] = 1 for i in range(n-2, -1, -1): if a[i] > a[i+1]: break nondecrright[i] = 1 def f(l): # true if removing l elements can make arr sorted for i in range(1, n - l): if nondecrleft[i-1] and nondecrright[i+l] and a[i-1] <= a[i+l]: return True return False lo, hi = 0, n-2 while lo!= hi: p = (lo + hi) // 2 if not f(p): lo = p + 1 else: hi = p return lo
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
from collections import deque class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n=len(arr) arr.append(-1) decrease=[] for i in range(n-1): if arr[i]>arr[i+1]: decrease.append(i) if len(decrease)==0: return 0 start=decrease[0]+1 end=decrease[-1] def valid(start,end): if arr[start-1]<=arr[end+1]: return True return False res=n-1 for i in range(start,-1,-1): lo,hi=end,n-1 while lo<=hi: mid=(lo+hi)//2 if valid(i,mid): #hi+1 is valid hi=mid-1 else:#lo-1 is not valid lo=mid+1 lo=min(lo,n-1) # print(start,lo,i,lo-i+1) res=min(res,lo-i+1) return res
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: if not arr: return 0 arr = [-10000000] + arr i = 0 for j in range(i+1, len(arr)): if arr[j] >= arr[i]: i += 1 else: break firsti = i j = len(arr)-1 for i in range(j-1, -1, -1): if arr[j] >= arr[i]: j -=1 else: break lastj = j n = len(arr) res = n-1 for i1 in range(firsti+1): while lastj < n and arr[lastj] < arr[i1]: lastj += 1 print((i1, lastj)) res = min(res, lastj - i1 - 1) return max(res, 0)
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n = len(arr) l, r = 0, n-1 # left for i in range(1, n): if arr[i] >= arr[i-1]: l += 1 else: break # if l == n-1: return 0 # right for i in reversed(list(range(1, n))): if arr[i] >= arr[i-1]: r -= 1 else: break # left find right res = min(n-l, r) for i in reversed(list(range(1, l+1))): j = self.bsearch(arr, arr[i], r, n-1) if arr[j] < arr[i]: res = min(res, j-i) else: res = min(res, j-i-1) # right find left # [1,2,3,10,4,2,3,5] for i in range(r, n): j = self.bsearch(arr, arr[i], 0, l) if arr[j] > arr[i]: res = min(res, i-j) else: res = min(res, i-j-1) return max(0, res) def bsearch(self, A, target, s, e): while s < e: m = s + (e-s)//2 if A[m] < target: s = m+1 else: e = m return e
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: pointer1=-1 n=len(arr) pointer2=n-1 for i in range(n-2,0,-1): if arr[i]>arr[i+1]: break pointer2=i res=pointer2-pointer1-1 for pointer1 in range(n): if pointer1>0 and arr[pointer1]<arr[pointer1-1]: break while(pointer2<n and (pointer2<=pointer1 or arr[pointer2]<arr[pointer1])): pointer2+=1 res=min(res,pointer2-pointer1-1) return res
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: leftIncreasing, rightDecreasing = 1, 1 for left, right in zip(arr, arr[1:]): if left <= right: leftIncreasing += 1 else: break for right, left in zip(arr[::-1], arr[::-1][1:]): if left <= right: rightDecreasing += 1 else: break if max(leftIncreasing, rightDecreasing) == len(arr): return 0 else: left_candidate, right_candidate = 0, len(arr) - rightDecreasing max_crop_length = max(leftIncreasing, rightDecreasing) if max_crop_length == leftIncreasing: crop_start, crop_end = leftIncreasing, len(arr) - 1 else: crop_start, crop_end = 0, len(arr) - rightDecreasing - 1 while left_candidate < leftIncreasing and right_candidate < len(arr): # print(left_candidate, leftIncreasing, right_candidate, rightDecreasing, crop_start, crop_end) if arr[left_candidate] > arr[right_candidate]: while right_candidate < len(arr): if arr[left_candidate] > arr[right_candidate]: right_candidate += 1 else: break if right_candidate == len(arr): return len(arr[crop_start: crop_end + 1]) else: if max_crop_length < (left_candidate + 1) + (len(arr) - right_candidate): crop_start, crop_end = left_candidate + 1, right_candidate - 1 max_crop_length = (left_candidate + 1) + (len(arr) - right_candidate) else: if max_crop_length < (left_candidate + 1) + (len(arr) - right_candidate): crop_start, crop_end = left_candidate + 1, right_candidate - 1 max_crop_length = (left_candidate + 1) + (len(arr) - right_candidate) left_candidate += 1 return len(arr[crop_start: crop_end + 1])
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: i,j = -1,-1 L = len(arr) for k in range(L-1): if arr[k]>arr[k+1]: j = k+1 if i==-1: i = k #print(i,j) if i==-1: return 0 res = j for m in range(i+1): while j<=L-1: if arr[j]>=arr[m]: break j += 1 if j==L: return min(res,L-i-1) else: res = min(res,j-m-1) #print(m,j,res) return res
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: queue = deque([arr[-1]]) for i in range(len(arr)-1, 0, -1): if arr[i-1] > arr[i]: break queue.appendleft(arr[i-1]) if len(queue) == len(arr): return 0 l = len(arr) - len(queue) def search(target): lo = l hi = l+len(queue) while lo < hi: mid = (lo + hi) // 2 if arr[mid] >= target: hi = mid else: lo = mid+1 return lo curMin = len(arr) - len(queue) i = 0 while i < len(arr)-1 and arr[i] <= arr[i+1]: idx = search(arr[i]) curMin = min(idx-i-1, curMin) i += 1 idx = search(arr[i]) curMin = min(idx-i-1, curMin) return curMin
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n, l = len(arr), 0 while l+1 < n and arr[l+1] >= arr[l]: l += 1 if l == n-1: return 0 r = n-1 while r > 0 and arr[r-1] <= arr[r]: r -= 1 if arr[l] <= arr[r]: return r-l-1 ans = min(n-l-1, r) for i in range(l+1): if arr[i] <= arr[r]: ans = min(ans, r-i-1) elif r < n-1: r += 1 return ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: l,r = 0,len(arr) - 1 while l < r and arr[l + 1] >= arr[l]: l += 1 if l == len(arr) - 1: return 0 while r < len(arr) and arr[r - 1] <= arr[r]: r -= 1 toRemove = min(len(arr) - l - 1,r) for iL in range(l + 1): if arr[iL] <= arr[r]: toRemove = min(toRemove,r - iL - 1) elif r < len(arr) - 1: r += 1 else: break return toRemove
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n = len(arr) if n < 2: return 0 left, right = 1, n - 1 while left < n and arr[left] >= arr[left - 1]: left += 1 if left == n: return 0 while left < right and arr[right] >= arr[right - 1]: right -= 1 res = min(n - left, right) left -= 1 while left >= 0: l, r = right - 1, n - 1 # l < arr[m] <= r while r - l > 1: m = (l + r) // 2 if arr[m] >= arr[left]: r = m else: l = m if arr[left] > arr[r]: r += 1 res = min(res, r - left - 1) left -= 1 return res # 5 1 2 3 4 # 1 2 3 4 1 # 1 2 3 4 5 100 2 3 4 5 4 7 8 10 3 4 5 6 7 8 # l r # 10 11 12 5 6 7 8 9 10 11 12 1 2 3 4
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: res = 0 if len(arr) > 1: mr = len(arr)-1 while mr - 1 >= 0 and arr[mr - 1] <= arr[mr]: mr -= 1 if mr > 0: cr, res = mr, mr prev = -1 for ml in range(0, len(arr)): if prev <= arr[ml]: while mr < len(arr) and arr[ml] > arr[mr]: mr += 1 cr += 1 cr -= 1 res = min(res, cr) else: break prev = arr[ml] return res
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: n = len(arr) left, right = n-1, 0 for i in range(n): if i > 0 and arr[i] < arr[i-1]: left = i-1 break for i in range(n-1, -1, -1): if i < n-1 and arr[i] > arr[i+1]: right = i+1 break if left >= right: return 0 ans = min(n-left, right) j = right for i in range(left+1): if j < len(arr) and arr[j] < arr[i]: j += 1 ans = min(ans, j-i-1) return ans
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4]. Example 2: Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. Example 3: Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements. Example 4: Input: arr = [1] Output: 0   Constraints: 1 <= arr.length <= 10^5 0 <= arr[i] <= 10^9
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: peak=0 while peak<len(arr)-1 and arr[peak]<=arr[peak+1]: peak+=1 if peak==len(arr)-1: return 0 valley=len(arr)-1 while valley>0 and arr[valley]>=arr[valley-1]: valley=valley-1 ans=min(valley,len(arr)-peak-1) i=0 j=valley while i<=peak and j<len(arr): if arr[j]>=arr[i]: ans=min(ans,j-i-1) i+=1 else: j+=1 return ans