Showing posts with label memorization. Show all posts
Showing posts with label memorization. Show all posts

Thursday, December 26, 2013

Largest Sum Contiguous Subarray..

Reference: http://www.geeksforgeeks.org/largest-sum-contiguous-subarray/

public class MaxSubArray {

    /*
    Notes: Algorithm doesn't work for all negative numbers. It simply returns 0
    if all numbers are negative. For handling this we can add an extra phase
    before actual implementation. The phase will look if all numbers are negative,
    if they are it will return maximum of them (or smallest in terms of absolute
    value). There may be other ways to handle it though.
    */
    public int findMaxSubArray(int a[]){
        int max_so_far = 0;
        int max_ending_here = 0;
        for(int i=0; i<a.length; i++){
            max_ending_here += a[i];
            if(max_ending_here < 0)
                max_ending_here = 0;
            if(max_so_far < max_ending_here)
                max_so_far = max_ending_here;
        }
        return max_so_far;
    }
 
    /*
     * Another implementation which takes care of all +ve as well as -ve value.
     */
    public int findMaxSubArray2(int a[]){
        int max_so_far = a[0];
        int current_max = a[0];
        for(int i=1; i<a.length; i++){
            current_max = Math.max(a[i], current_max+a[i]);
            max_so_far = Math.max(max_so_far, current_max);
        }
        return max_so_far;
    }
 
    public static void main(String args[]){
        MaxSubArray msa = new MaxSubArray();
        int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
        int res = msa.findMaxSubArray(a);
        System.out.println("MaxSum: " + res);
        int b[] = {-2, -3, -4, -1, -2, -1, -5, -3};
        res = msa.findMaxSubArray2(b);
        System.out.println("MaxSum2: " + res);
    }
}

# Output
MaxSum: 7
MaxSum2: -1

Saturday, December 21, 2013

Find the min number of coin to get sum S

public class FindMinCoin {

    public void findMinCoin() {
        int valueCoins[] = new int[]{1, 3, 5};
        int sum = 11;
        int min[] = new int[sum + 1];
       
        // assign sum to infinity
        for(int i=1; i<min.length; i++)
            min[i] = 999999;
       
        // initialize min sum = 0
        min[0] = 0;
       
        for(int i=1; i<= sum; i++)
            for(int j=0; j<valueCoins.length; j++){
                if(valueCoins[j] == i){
                    min[i] = 1;
                }else if((valueCoins[j] < i) && (((min[i-valueCoins[j]]) + 1) < min[i])){
                    min[i] = (min[i-valueCoins[j]]) + 1;
                }
            }
       
        for(int k=1; k<min.length; k++){
            System.out.println(k + " " + min[k] + " ");
        }      
    }

    public void printArr(int coins[][], int min[]){
        System.out.println("Min:-----------");
        for(int i=0; i<min.length; i++)
            System.out.println("min[" + i + "] = " + min[i]);
       
        System.out.println("Coins:--------");
        for(int i=0; i<coins.length; i++)
            System.out.println("conins[" + i + "][0] = " + coins[i][0] + "\tcoins[" + i + "][1]=" + coins[i][1]);
    }
   
    public static void main(String args[]) {
        FindMinCoin fmc = new FindMinCoin();
        fmc.findMinCoin();
    }
}