Minimum Size Subarray Sum Solutions in Java
Number 209
Difficulty Medium
Acceptance 38.2%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// https://leetcode.com/problems/minimum-size-subarray-sum/description//// Author : liuyubobobo/// Time : 2017-11-13// Sum Prefix// Time Complexity: O(n^2)// Space Complexity: O(n)public class Solution1 {public int minSubArrayLen(int s, int[] nums) {if(s <= 0 || nums == null)throw new IllegalArgumentException("Illigal Arguments");int[] sums = new int[nums.length + 1];sums[0] = 0;for(int i = 1 ; i <= nums.length ; i ++)sums[i] = sums[i-1] + nums[i-1];int res = nums.length + 1;for(int l = 0 ; l < nums.length ; l ++)for(int r = l ; r < nums.length ; r ++)if(sums[r+1] - sums[l] >= s)res = Math.min(res, r - l + 1);return res == nums.length + 1 ? 0 : res;}public static void main(String[] args) {int[] nums = {2, 3, 1, 2, 4, 3};int s = 7;System.out.println((new Solution1()).minSubArrayLen(s, nums));}}
Java solution by liuyubobobo/Play-Leetcode
/// https://leetcode.com/problems/minimum-size-subarray-sum/description//// Author : liuyubobobo/// Time : 2019-03-03// Brute Force + Greedy// Time Complexity: O(n^2)// Space Complexity: O(1)public class Solution2 {public int minSubArrayLen(int s, int[] nums) {if(s <= 0 || nums == null)throw new IllegalArgumentException("Illigal Arguments");int res = nums.length + 1;for(int l = 0 ; l < nums.length ; l ++) {int sum = 0;for (int r = l; r < nums.length; r++){sum += nums[r];if(sum >= s){res = Math.min(res, r - l + 1);break;}}}return res == nums.length + 1 ? 0 : res;}public static void main(String[] args) {int[] nums = {2, 3, 1, 2, 4, 3};int s = 7;System.out.println((new Solution2()).minSubArrayLen(s, nums));}}
Java solution by liuyubobobo/Play-Leetcode
/// https://leetcode.com/problems/minimum-size-subarray-sum/description//// Author : liuyubobobo/// Time : 2017-11-13// Sum Prefix + Binary Search// Time Complexity: O(nlogn)// Space Complexity: O(n)public class Solution3 {public int minSubArrayLen(int s, int[] nums) {if(s <= 0 || nums == null)throw new IllegalArgumentException("Illigal Arguments");int[] sums = new int[nums.length + 1];sums[0] = 0;for(int i = 1 ; i <= nums.length ; i ++)sums[i] = sums[i-1] + nums[i-1];int res = nums.length + 1;for(int l = 0 ; l < nums.length; l ++){// Unfortunately, there's no lowerBound method in Java,// We need to implement our own lowerBound :(int r = lowerBound(sums, sums[l] + s);if(r != sums.length)res = Math.min(res, r - l);}return res == nums.length + 1 ? 0 : res;}// Find the smallest number index which is larger or equal to target// in a sorted array nums// If there's no such a number, in which all number in nums are smaller than target// return nums.lengthprivate int lowerBound(int[] nums, int target){if(nums == null /*|| !isSorted(nums)*/)throw new IllegalArgumentException("Illegal argument nums in lowerBound.");int l = 0, r = nums.length;while(l != r){int mid = l + (r - l) / 2;if(nums[mid] >= target)r = mid;elsel = mid + 1;}return l;}private boolean isSorted(int[] nums){for(int i = 1 ; i < nums.length ; i ++)if(nums[i] < nums[i-1])return false;return true;}public static void main(String[] args) {int[] nums = {2, 3, 1, 2, 4, 3};int s = 7;System.out.println((new Solution3()).minSubArrayLen(s, nums));}}
Java solution by liuyubobobo/Play-Leetcode
/// https://leetcode.com/problems/minimum-size-subarray-sum/description//// Author : liuyubobobo/// Time : 2017-11-13// Sliding Window// Time Complexity: O(n)// Space Complexity: O(1)public class Solution4 {public int minSubArrayLen(int s, int[] nums) {if(s <= 0 || nums == null)throw new IllegalArgumentException("Illigal Arguments");int l = 0 , r = -1; // sliding window: nums[l...r]int sum = 0;int res = nums.length + 1;while(l < nums.length){if(r + 1 < nums.length && sum < s)sum += nums[++r];elsesum -= nums[l++];if(sum >= s)res = Math.min(res, r - l + 1);}return res == nums.length + 1 ? 0 : res;}public static void main(String[] args) {int[] nums = {2, 3, 1, 2, 4, 3};int s = 7;System.out.println((new Solution4()).minSubArrayLen(s, nums));}}
Java solution by liuyubobobo/Play-Leetcode
/// https://leetcode.com/problems/minimum-size-subarray-sum/description//// Author : liuyubobobo/// Time : 2017-11-13// Sliding Window// Another Implementation// Time Complexity: O(n)// Space Complexity: O(1)public class Solution5 {public int minSubArrayLen(int s, int[] nums) {if(s <= 0 || nums == null)throw new IllegalArgumentException("Illigal Arguments");int l = 0 , r = -1; // sliding window: nums[l...r]int sum = 0;int res = nums.length + 1;while(r + 1 < nums.length){while(r + 1 < nums.length && sum < s)sum += nums[++r];if(sum >= s)res = Math.min(res, r - l + 1);while(l < nums.length && sum >= s){sum -= nums[l++];if(sum >= s)res = Math.min(res, r - l + 1);}}return res == nums.length + 1 ? 0 : res;}public static void main(String[] args) {int[] nums = {2, 3, 1, 2, 4, 3};int s = 7;System.out.println((new Solution5()).minSubArrayLen(s, nums));}}