Leetcode Weekly Challenge 343

·

1 min read

Problem 1 - https://leetcode.com/contest/weekly-contest-343/problems/determine-the-winner-of-a-bowling-game/

class Solution {
    public int isWinner(int[] p1, int[] p2) {

        int n = p1.length;

        if(n==0)
            return 0;
        if(n==1)
        {
            if(p1[0]==p2[0])
                return 0;
            else if(p1[0]>p2[0])
                return 1;
            else return 2;
        }


        int sum1 = p1[0];
        int sum2 = p2[0];

        boolean isTen1 = (p1[0]==10)? true:false;
        boolean isTen2 = (p2[0]==10)? true:false;

        for(int i=1; i<n; i++) {

            if(isTen1) {
                sum1+= 2*p1[i];
            }
            else {
                sum1+=p1[i];
            }

            if(isTen2) 
            {
                sum2+=2*p2[i];
            }
            else {
                sum2+=p2[i];
            }

            if(p1[i]==10 || p1[i-1]==10) {
                isTen1 = true;
            }
            else isTen1= false;

            if(p2[i]==10 || p2[i-1]==10) {
                isTen2 = true;
            }
            else isTen2= false;

        }

        if(sum1==sum2)
            return 0;
        return (sum1>sum2)? 1:2;

    }
}

Problem 2 - https://leetcode.com/contest/weekly-contest-343/problems/first-completely-painted-row-or-column/

class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {

        int m = mat.length;
        int n = mat[0].length;

        int minIndex = Integer.MAX_VALUE;

        HashMap<Integer, Integer> hmap = new HashMap<>();
        for(int i=0; i<arr.length;i++) {
            hmap.put(arr[i],i);
        }

        // Row wise traversal 
        for(int i=0; i<m; i++) {
            int localMaxIndex = -1;
            for(int j=0; j<n; j++) {
                if(hmap.get(mat[i][j]) > localMaxIndex)
                    localMaxIndex = hmap.get(mat[i][j]);
            }
            if(localMaxIndex<minIndex)
                minIndex = localMaxIndex;
        }
        // column wise traversal
        for(int j=0; j<n; j++) {
            int localMaxIndex = -1;
            for(int i=0; i<m; i++) {
                if(hmap.get(mat[i][j]) > localMaxIndex)
                    localMaxIndex = hmap.get(mat[i][j]);
            }
            if(localMaxIndex<minIndex)
                minIndex = localMaxIndex;
        }

        return minIndex;

    }
}