[Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K

Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.

If there is no non-empty subarray with sum at least K, return -1.

Example 1:

Input: A = [1], K = 1
Output: 1

Example 2:

Input: A = [1,2], K = 4
Output: -1

Example 3:

Input: A = [2,-1,2], K = 3
Output: 3

Note:

  1. 1 <= A.length <= 50000
  2. -10 ^ 5 <= A[i] <= 10 ^ 5
  3. 1 <= K <= 10 ^ 9


返回 A 的最短的非空连续子数组的长度,该子数组的和至少为 K 。

如果没有和至少为 K 的非空子数组,返回 -1 。

示例 1:

输入:A = [1], K = 1
输出:1

示例 2:

输入:A = [1,2], K = 4
输出:-1

示例 3:

输入:A = [2,-1,2], K = 3
输出:3 

提示:

  1. 1 <= A.length <= 50000
  2. -10 ^ 5 <= A[i] <= 10 ^ 5
  3. 1 <= K <= 10 ^ 9

956ms

 1 class Solution {
 2     func shortestSubarray(_ A: [Int], _ K: Int) -> Int {
 3         var prefixSums = [Int]()
 4         prefixSums.append(0)
 5         var sum = 0
 6         for (i, num) in A.enumerated() {
 7             sum += num
 8             prefixSums.append(sum)
 9         }
10         var res = Int.max
11         var deque = Deque<(Int, Int)>()
12         for i in 0..<prefixSums.count {
13             while !deque.isEmpty && deque.back.0 >= prefixSums[i] {
14                 deque.popBack()
15             }
16             deque.push( (prefixSums[i], i) )
17             while !deque.isEmpty && prefixSums[i] - deque.front.0 >= K {
18                 res = min(res, i - deque.front.1)
19                 deque.popHead()
20             }
21         }
22         return res == Int.max ? -1 : res
23     }
24 }
25
26 struct Deque<T> {
27     var head = 0
28     var tail = 0
29     var arr = [T]()
30
31     mutating func push(_ val: T) {
32         arr.append(val)
33         tail += 1
34     }
35
36     @discardableResult mutating func popBack() -> T {
37         let val = arr.removeLast()
38         tail -= 1
39         return val
40     }
41
42     @discardableResult mutating func popHead() -> T {
43         let val = arr[head]
44         head += 1
45         return val
46     }
47
48     var front: T {
49         return arr[head]
50     }
51
52     var back: T {
53         return arr[tail - 1]
54     }
55
56     var isEmpty: Bool {
57         return head == tail
58     }
59 }


Runtime: 1240 ms

Memory Usage: 20.6 MB

 1 class Solution {
 2     func shortestSubarray(_ A: [Int], _ K: Int) -> Int {
 3         var N:Int = A.count
 4         var res:Int = N + 1
 5         var B:[Int] = [Int](repeating:0,count:N + 1)
 6         for i in 0..<N
 7         {
 8             B[i + 1] = B[i] + A[i]
 9         }
10         var d:[Int] = [Int]()
11         for i in 0..<(N + 1)
12         {
13             while (d.count > 0 && B[i] - B[d.first!] >= K)
14             {
15                 res = min(res, i - d.first!)
16                  d.removeFirst()
17             }
18             while (d.count > 0 && B[i] <= B[d.last!])
19             {
20                 d.popLast()
21             }
22             d.append(i)
23         }
24         return res <= N ? res : -1
25     }
26 }

原文地址:https://www.cnblogs.com/strengthen/p/10595590.html

时间: 2024-08-02 03:05:32

[Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K的相关文章

lc 862. Shortest Subarray with Sum at Least K

断网导致原来写的那么多答案全没了,博客园能不能实时保存草稿,醉. https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 给一个数组a,找和大于k的所有子数组中的最短的那个. 最近二分有点上头,因为的确很强大,两个考虑二分的情况(其实需要刻意去想想能不能使用二分才会想到,这会是个好习惯): 1.最优问题变判断问题.比如:行递增列递增的矩阵中找某个元素->行递增列递增的矩阵中找第k大元素 2.有相应的简单孪生问题.比

862. Shortest Subarray with Sum at Least K

Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there is no non-empty subarray with sum at least K, return -1. Example 1: Input: A = [1], K = 1 Output: 1 Example 2: Input: A = [1,2], K = 4 Output: -1 Exa

[算法]需要排序的最短子数组长度

题目: 给定一个无序数组,求出需要排序的最短子数组的长度. 例如:arr={1,5,3,4,2,6,7}返回4,因为只有[5,3,4,2]需要排序. 思路: 解决这个问题可以在时间复杂度为O(N).额外空间复杂度为O(1)完成. 初始化变量noMinIndex=-1,从右向左遍历,便利的过程记录右侧出现过的数的最小值,记为min.假设当前数为arr[i],如果arr[i]>min,说明如果要整体有序,min值必然会移到arr[i]的左边.用noMinIndex记录最左边出现这种情况的位置.如果遍

需要排序的最短子数组长度

要求: 给定一个无序数组arr,求出需要排序的最短子数组长度 1 // getMinLength.cpp : 定义控制台应用程序的入口点. 2 // 3 4 #include "stdafx.h" 5 #include <iostream> 6 7 using namespace std; 8 9 void getMinLength(int arr[],int len) 10 { 11 if(len == 0) 12 return; 13 14 //从后往前找到最小值,并记

2-1-需排序的最短子数组长度

题目描述: 对于一个无序数组A,请设计一个算法,求出需要排序的最短子数组的长度. 给定一个整数数组A及它的大小n,请返回最短子数组的长度. 测试样例: [1,5,3,4,2,6,7],7 返回:4 1 /* 2 这个题在牛客网上的讲解我感觉是有点问题的, 3 因为默认了升序排序! 4 如下实现对数组[8,7,6,5,4,3]的结果将会是6, 5 实际上此时的结果应该为0,因为现在已经是有序的了. 6 */ 7 #include <iostream> 8 #include <vector&

《程序员代码面试指南》第八章 数组和矩阵问题 需要排序的最短子数组长度

题目 需要排序的最短子数组长度 java代码 package com.lizhouwei.chapter8; /** * @Description: 需要排序的最短子数组长度 * @Author: lizhouwei * @CreateDate: 2018/4/29 8:03 * @Modify by: * @ModifyDate: */ public class Chapter8_5 { public int getMinLength(int[] arr) { if (arr == null

第二章 排序 || 第19节 最短子数组练习题

题目 对于一个数组,请设计一个高效算法计算需要排序的最短子数组的长度. 给定一个int数组A和数组的大小n,请返回一个二元组,代表所求序列的长度.(原序列位置从0开始标号,若原序列有序,返回0).保证A中元素均为正整数. 测试样例: [1,4,6,5,9,10],6 返回:2 解析 C++版 class Subsequence { public: int shortestSubsequence(vector<int> A, int n) { // write code here int sta

需要排序的最短子数组的长度——是一个排序好的数组,中间某一部分被打乱了,让你找出打乱的那个子数组

需要排序的最短子数组的长度 貌似在leetcode上遇到过,就是一个排序好的数组,中间某一部分被打乱了,让你找出打乱的那个子数组. from:https://blog.csdn.net/behboyhiex/article/details/80758686 [题目] 给定一个无序数组arr,求出需要排序的最短子数组长度. 例如: arr = [1, 5, 3, 4, 2, 6, 7]返回4,因为只有[5, 3, 4, 2]需要排序. [思路] 双指针 第一次从左向右遍历,找左边比当前位置大的 第

[Swift Weekly Contest 118]LeetCode974. 和可被 K 整除的子数组 | Subarray Sums Divisible by K

Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K. Example 1: Input: A = [4,5,0,-2,-3,1], K = 5 Output: 7 Explanation: There are 7 subarrays with a sum divisible by K = 5: [4, 5, 0, -2