mercredi 30 septembre 2020

Time Limit exceeded ,how to write efficient code?

I had previously used array and then sorted but giving the same result. This is the link to problem https://www.spoj.com/problems/ARMY/

import java.util.Scanner;

public class main {

    public static void main(String args[])
    {
        
            Scanner s=new Scanner(System.in);
            int t=s.nextInt();      
            
            while(t-->0) {
                int temp1=-1;
                int temp2=-1;
                
                int temp=0;

                int ng =s.nextInt();
                int nm =s.nextInt();
                
                for(int i=0;i<ng;i++) {
                    temp=s.nextInt();
                    if(temp>=temp1) {
                        temp1=temp;
                    }
                }
                
                for(int i=0;i<nm;i++) {
                    temp=s.nextInt();
                    if(temp>=temp2) {
                        temp2=temp;
                    }
                }
                
                
                if(temp1>=temp2) {
                    System.out.println("Godzilla");
                }
                else {
                    System.out.println("MechaGodzilla");
                }
                

            }

   
    }

}

Aucun commentaire:

Enregistrer un commentaire