Showing posts with label Pointers. Show all posts
Showing posts with label Pointers. Show all posts

Monday, February 29, 2016

[LeetCode] 11. Container With Most Water

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.


1. Two pointer scan with greedy.

public class Solution {
    public int maxArea(int[] height) {
        int len = height.length;
        if(0 == len)
            return 0;

        int res = -1;
            
        int i = 0;
        int j = len-1;
        
        while(i < j){
            int hI = height[i];
            int hJ = height[j];
            
            int water = Math.min(hI, hJ)*(j - i);
            if(water > res){
                res = water;
            }
            
            if(hI < hJ){
                i++;
            }else{
                j--;
            }
        }
        
        return res;
    }
}

Monday, January 4, 2016

[LeetCode] 1. Two Sum [Private]

Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

Method 1: Hash Map.
Time: O(n)
Space: O(1)

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        int len = nums.length;
        int[] res = new int[2];
        
        if(0 == len)
            return res;
            
        Map m= new HashMap();
        for(int i = 0; i < len; ++i){
            if(m.containsKey(target - nums[i])){
               res[0] = m.get(target - nums[i])+1;
               res[1] = i+1;
            }
             m.put(nums[i], i);
        }
        return res;
    }
}

Bad:
Keep all elements, then go through it again. Result in complicated condition for equal number judgement.
But might works better when the exact one condition is relax. 
public class Solution {
    public int[] twoSum(int[] nums, int target) {
        int len = nums.length;
        int[] res = new int[2];
        
        if(0 == len)
            return res;
            
        Map m= new HashMap();
        for(int i = 0; i < len; ++i){
            if(m.containsKey(nums[i])){
                m.put(nums[i], m.get(nums[i])+1);
            }
            else{
                m.put(nums[i], 1);
            }
        }
        
        for(int i = 0; i < len; ++i){
            int x = nums[i];
            int y = target - x;
            if(x == y && m.get(x) < 2)
                continue;
            if(m.containsKey(y)){
                int index = 0;
                for(int j = 0; j < len; ++j){
                    if (nums[j] == x || nums[j] == y) res[index++] = j+1;
                    if(3 == index)
                        return res;
                }
            }
        }
        return res;
    }
}