• + 0 comments

    this is my code, but it did not success in all cases

        public static int policeOperation(int h, List<int> criminals)
        {
            return OldMethod(h, criminals);
         
            
            
        }
        
        private static int OldMethod(int h, List<int> criminals){
            int creminalNumber = criminals.Count;
            int officersNumber = 0;
            int tmpFuelPayment = 0;
            int fuelPayment = 0;
            
            int previousLocation = 0;
            int currentLocation = 0;
            
            for(int i = 0; i < creminalNumber; i++){
                //First Location
                if( i == 0){
                    previousLocation = criminals[i];
                    officersNumber++;
                    continue;
                }
                
                //check which is more suitable, one more officer or pay for fuel
                currentLocation = criminals[i];
                tmpFuelPayment = fuelNeededToMove(previousLocation, currentLocation);
                
                if(tmpFuelPayment < h){
                    // if it is last criminal
                    if(i == creminalNumber-1){
                        fuelPayment += tmpFuelPayment;
                    }
                }
                else{
                    tmpFuelPayment = fuelNeededToMove(previousLocation, criminals[i-1]);
                    fuelPayment += tmpFuelPayment;
                    tmpFuelPayment = 0;
                    
                    previousLocation = criminals[i];
                    officersNumber++;
                }
            }
            
            int salaries = officersNumber * h;
            
            return salaries + fuelPayment;
        }
        
        
        private static int fuelNeededToMove(int startPos, int endPos){
            return Convert.ToInt32(Math.Pow(endPos - startPos, 2));
        }