anushkanocoding's blog

By anushkanocoding, history, 3 weeks ago, In English

https://leetcode.com/problems/predict-the-winner/ Please check this question out on leetcode and can anyone tell me why my code wont work?

class Solution {
    public boolean stoneGame(int[] piles) {
        int total=0;
        for(int pile:piles)total+=pile;
        int [][]dp=new int[piles.length][piles.length];
        for(int []arr:dp)Arrays.fill(arr,-1);
        int alice=recursion(0,piles.length-1,piles,true,dp);
        int bob=total-alice;
        return alice>bob;
    }
    public int recursion(int start,int end,int []piles,boolean alice,int [][]dp){
        if(start>end){
            return 0;
        }
        if(dp[start][end]!=-1)return dp[start][end];
        if(alice){
            int stTake=piles[start]+recursion(start+1,end,piles,false,dp);
            int endTake=piles[end]+recursion(start,end-1,piles,false,dp);
            return dp[start][end]=Math.max(stTake,endTake);
        }
        else{
            int stTake=piles[start]+recursion(start+1,end,piles,true,dp);
            int endTake=piles[end]+recursion(start,end-1,piles,true,dp);
            return dp[start][end]=Math.min(stTake,endTake);
        }
    }
}

Full text and comments »

  • Vote: I like it
  • -21
  • Vote: I do not like it

By anushkanocoding, history, 3 weeks ago, In English

import java.util.*; public class balanced { public static void main(String[]args){ java.util.Scanner sc=new java.util.Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++){ int n=sc.nextInt(); int k=sc.nextInt(); int []arr=new int[n]; for(int j=0;j<arr.length;j++){ arr[j]=sc.nextInt(); }

int ops=0;
        Arrays.sort(arr);
        int a=0;
        int b=1;
        while(b<arr.length){ 
            if(arr[b]-arr[b-1]>k){
                int leftNums=b-a;
                int rightNums=arr.length-b;

                if(leftNums<rightNums){
                    a=b;
                    ops+=leftNums;
                }else if(leftNums>=rightNums){
                    ops+=rightNums;
                    break;
                }
            }
            b++;
        }
        System.out.println(ops);
    }
}

} For Question 1850D-BALANCED ROUND Can anyone please tell me what the problem is with the code? It Fails on TestCase 409 i cant see the particular testcase (also can we actually see particular testcases? if not, how is one supposed to know which testcase is wrong?

Full text and comments »

  • Vote: I like it
  • -24
  • Vote: I do not like it