Advertisement
Alexxik

Untitled

Mar 16th, 2024
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.59 KB | None | 0 0
  1. // MARK: - Найти MIN подотрезок суммой X (все +)
  2.  
  3. func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int {
  4.     var r = 0
  5.     var l = 0
  6.     var summ = 0
  7.     var minLength = Int.max
  8.    
  9.     while r < nums.count {
  10.         summ += nums[r]
  11.         while summ > target {
  12.             summ -= nums[l]
  13.             l += 1
  14.         }
  15.        
  16.         if summ == target {
  17.             minLength = min(minLength, r-l+1)
  18.         }
  19.        
  20.         r += 1
  21.     }
  22.     return minLength == Int.max ? 0 : minLength
  23. }
  24.  
  25. let numsMin = [1,2,3,4,5]
  26. minSubArrayLen(11, numsMin)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement