Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
Example:
Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
defminSubArrayLen(self,s,nums): min_len = sys.maxint Lst = [sys.max] *len(nums) cur_sum =0 left =0for i inxrange(0, len(nums)): cur_sum += nums[i]#[i, j] inclusiveif cur_sum < s:continuewhile left <len(nums)and cur_sum >= s: min_len =min(min_len, i - left +1) Lst[i]= min_len cur_sum -= nums[left] left +=1if min_len == sys.maxint:return0else:return min_len
defSolution():iflen(nums)==0:return0 l, r, k =0,0,1 zero =0 ret =1for i inxrange(0, len(nums)):if nums[r]==0: zero+=1while zero > k:if nums[l]==0 zero -=1 l +=1 ret =max(ret, r-l+1)return ret
Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20, 100.
The order of output does not matter.
Example 1:
Input:
s: "cbaebabacd" p: "abc"
Output:
[0, 6]
classSolution(object):deffindAnagrams(self,s,p):iflen(s)<len(p):return []iflen(p)==0:return [] hashP =dict()for c in p:if c notin hashP: hashP[c]=0 hashP[c]+=1 j =0 hashS =dict() cnt =0 ret = []for i inrange(len(s)):if s[i]notin hashS: hashS[s[i]]=0 hashS[s[i]]+=1if s[i]in hashP and hashS[s[i]]== hashP[s[i]]:#这里没有判断if in hash,发生了key error cnt +=1if i - j +1==len(p):if cnt ==len(hashP): ret.append(j) hashS[s[j]]-=1if s[j]in hashP and hashS[s[j]]== hashP[s[j]]-1:#bug 不能用 hashS[s[j]] < hashP[s[j]], 必须在临界点上 cnt -=1 j +=1return ret
Subarray Product Less Than K
Your are given an array of positive integers nums.
Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k.
Example 1:
Input: nums = [10, 5, 2, 6], k = 100
Output: 8
Explanation:
The 8 subarrays that have product less than 100 are: [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]. Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
classSolution(object):defnumSubarrayProductLessThanK(self,nums,k):if k <=1:return0 left =0 product =1 ret =0for i inxrange(len(nums)): product = product * nums[i]while product >= k: product = product / nums[left] left +=1 ret += (i - left +1)return ret
Subarray Sum Equals K (all positives)
defsubarraySumALLPOS(self,nums,k): left =0 ret =0sum=0for i inxrange(len(nums)):sum+= nums[i]whilesum>= k and left <len(nums): left +=1sum-= nums[left]ifsum== k: ret +=1return ret
Subarray Sum Equals K (general)
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.
Example 1:
Input:nums = [1,1,1], k = 2
Output: 2
The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
defsubarraySum(self,nums,k): prefix_hash =dict()#key is sum, value is cnt prefix_hash[0]=1 prefix_sum =0 ret =0for n in nums: prefix_sum += nif prefix_sum - k in prefix_hash: ret += prefix_hash[prefix_sum - k]if prefix_sum notin prefix_hash: prefix_hash[prefix_sum]=0 prefix_hash[prefix_sum]+=1return ret
Longest Substring contains at most k distinct characters
# 调用counter,它其实是一个default是int的dictionary,这是为了避免使用dict的话带来keyerrorfrom collections import Counterclasssolution(object):deflengthOfLongestSubstringKDistinct(self,s,k):""" type s: string type k: int rtype: int """ start, end, ans =0,0,0 counter=Counter()while end<len(s): ans =max(ans, end-start) counter[s[end]]+=1whilelen(counter)==k+1: counter[s[start]]-=1if counter[s[start]]==0:del counter[s[start]] start+=1 end+=1returnmax(ans, end-start)
classSolution(object):deflengthOfLongestSubstringKDistinct(self,s,k):""" type s: string type k: int rtype: int """ start, end, ans =0,0,0 last_position_by_char={}while end<len(s): ans =max(ans, end-start) last_position_by_char[s[end]]=endiflen(last_position_by_char)==k+1: char, pos =min(last_position_by_char.items(), key=lambdaitem:item[1])del last_position_by_char[char] start = pos+1 end+=1returnmax(ans, end-start)