tharshikan
9/4/2018 - 6:02 AM

Algorithms


import java.util.*;

import sun.text.resources.cldr.ar.FormatData_ar_QA;

public class MedianTwoArrays {
    // static int c = 0;

    public static void main(String[] args) {

        // int[] list = { 0, 2, 0, 0, 0, 0, 7, 0, 10, 0, 0, 12 };
        // shift(list);
        // double a = findMedianSortedArrays(new int[] { 1, 3, 9, 10 }, new int[] { 2,
        // 6, 7, 20 });
        // System.out.println(a);

        // char[] c = new char[] { 'a', 'b', 'c', 'd', 'e' };
        // String s = "abcdefg";
        // printSubstings(s);
        // String p = "zanabcbanvyu";
        // String n = longestPalindrome(p);
        // System.out.println(n);

        // int h = reversDigits(123456);
        // System.out.println(h);

        // String str1 = "ABCDE";
        // lastIndex = str1.length() - 1;
        // permute(str1, 0);

        // String p = "acb*e*";
        // Boolean bool2 = isMatch("aceeeee", p);
        // System.out.println(bool2);

        // String p = "acb*e*";
        // Boolean bool = isMatch("cbcacbaaabcbabbcaa", "b*aa*a*.*b*..*c*bc*");
        // System.out.println(bool);

        // Boolean bool = isMatch("ab", ".*");
        // System.out.println(bool);

        // Boolean bool3 = isMatch("aaa", "ab*a*c*a");
        // System.out.println(bool3);

        // printRomans(950);
        int a = maxSumSubArray(new int[] { -2, -3, 4, -1, -2, 1, 5, -3 });
        System.out.println(a);

        // int a = coinChange(4);
        // System.out.println(a);
    }

    public static int coinChange(int amount) {
        if (amount == 1) {
            return 1;
        }
        if (amount < 1) {
            return 0;
        }

        return coinChange(amount - 5) + coinChange(amount - 2);
    }

    public static int maxSumSubArray(int[] list) {
        List<Integer> subArray = new ArrayList<Integer>();
        List<Integer> finalSubArray = new ArrayList<Integer>();
        int maxSoFar = 0;
        int maxEndingHere = 0;
        for (int i = 0; i < list.length; i++) {
            maxEndingHere += list[i];
            subArray.add(list[i]);
            maxEndingHere = (maxEndingHere < 0) ? 0 : maxEndingHere;
            maxSoFar = (maxEndingHere > maxSoFar) ? maxEndingHere : maxSoFar;

        }
        return maxSoFar;
    }

    public static boolean isMatch(String s, String p) {

        Stack<Character> stack = new Stack<Character>();
        int j = 0;
        for (int i = 0; i < p.length(); i++) {
            char curr = p.charAt(i);
            System.out.println(curr);

            if (curr == '*') {
                char c = stack.peek();
                if (c != '.') {
                    if ((i + 1) < p.length() && p.charAt(i + 1) == c) {
                        while ((j + 1) < s.length() && c == s.charAt(j) && c == s.charAt(j + 1)) {
                            j++;
                        }
                    } else {
                        while (j < s.length() && c == s.charAt(j)) {
                            j++;
                        }
                    }
                } else if ((i + 1) < p.length()) {
                    char nextChar = p.charAt(i + 1);
                    while ((j + 1) < s.length() && nextChar != s.charAt(j)) {
                        j++;
                    }
                } else {
                    j++;
                }

            } else if (curr == '.') {
                stack.push(curr);
                j++;

            } else {
                if (j < s.length() && s.charAt(j) == curr) {
                    stack.push(curr);
                    j++;
                } else if ((i + 1) < p.length() && p.charAt(i + 1) == '*') {
                    // stack.push(curr);
                    i++;
                } else {
                    return false;
                }
            }
        }

        if (j == s.length()) {
            return true;
        }
        return false;

    }

    public static void printRomans(int num) {

        int[] rmlist = { 1000, 500, 200, 100, 10 };
        for (int rm : rmlist) {
            while (num >= rm) {
                num -= rm;
                System.out.println(rm);

            }
        }
    }

}