Sort by

recency

|

15 Discussions

|

  • + 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));
        }
    
  • + 0 comments

    Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Police Operation Problem Solution

  • + 0 comments

    Here is Police Operation problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-police-operation-problem-solution.html

  • + 1 comment

    The question did not explain the rule of how many officer to recruite. Please explain.

  • + 0 comments

    To understand this problem which concepts exactly we should learn in mathematics.