Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn’t one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the minimal length under the problem constraint. Solution: public class Solution { public int minSubArrayLen(int s, int[] nums) { int slow=0, fast=0, sum=0, ans=Integer.MAX_VALUE; while(fast<nums.length){ while(sum<s && fast<nums.length) sum += nums[fast++]; while(sum>=s && slow<fast){ ans = Math.min(ans, fast-slow); sum -= nums[slow++]; } } return ans==Integer.MAX_VALUE ?

First Bad Version

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you have n versions [1, 2, …, n] and you want to find out the first bad one, which causes all the following ones to be bad.

Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6], 2 → 1 [1,3,5,6], 7 → 4 [1,3,5,6], 0 → 0 Solution: public class Solution { public int searchInsert(int[] nums, int target)

Word Break

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", dict = ["leet", "code"]. Return true because “leetcode” can be segmented as “leet code”. Solution: public class Solution { public boolean wordBreak(String s, Set<String> wordDict) { if(s.length()==0) return false; boolean[] dp = new boolean[s.length()+1]; dp[0] = true; for(int

Reconstruct Itinerary

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK. Note: If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary [“JFK”, “LGA”] has a smaller lexical order than [“JFK”, “LGB”].

Data Stream as Disjoint Intervals

Given a data stream input of non-negative integers a1, a2, …, an, …, summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, …, then the summary will be: [1, 1] [1, 1], [3, 3] [1, 1], [3, 3], [7, 7] [1, 3], [7, 7] [1, 3], [6, 7] Follow up: What if there are lots of merges and the number of disjoint intervals are small compared to the data stream’s size?

Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return [“0->2”,“4->5”,“7”]. Solution: public class Solution { public List<String> summaryRanges(int[] nums) { List<String> ans = new ArrayList<>(); for(int i=0; i<nums.length; i++){ String s = "" + nums[i]; int start = i; while(i+1<nums.length && nums[i+1]==nums[i]+1) i++; if(start!=i){ s += "->" + nums[i]; } ans.add(s); } return ans; } }

Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h. Solution: /** * Definition for a binary tree node. * public

Sqrt(x)

Implement int sqrt(int x). Compute and return the square root of x. Solution: public class Solution { public int mySqrt(int x) { long r = x; while(r*r>x) r = (r+x/r)>>1; return (int)r; } }

Coin Change

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1. Example 1: coins = [1, 2, 5], amount = 11 return 3 (11 = 5 + 5 + 1) Example 2: coins = [2], amount = 3 return -1.