Enhanced coding efficiency

Today I heard about this site called codility, where the user can conduct various programming tests to check the performance of their code.

When I started, they presented me with this sample test,

Description of the task A little frog wants to get to the other side of the road. Currently, the frog is in position X and wants to get into a position greater than or equal to Y. The little frog always jumps a fixed distance, D. Count the minimum number of jumps that the little frog must complete in order to reach its goal.

Write a function: class Solution { public int solution(int X, int Y, int D); } class Solution { public int solution(int X, int Y, int D); } class Solution { public int solution(int X, int Y, int D); } class Solution { public int solution(int X, int Y, int D); } that, given the three integers X , Y and D , returns the minimum number of jumps from position X to a position equal to or greater than Y

For example, given:
X = 10
Y = 85
D = 30 function should return 3 , because the frog will be located as follows:

after the first jump in position 10 + 30 = 40

after the second jump at position 10 + 30 + 30 = 70

after the third jump in position 10 + 30 + 30 + 30 = 100

Suppose: X, Y, and D are integers in the range

[1..1,000,000,000]; X ≤ Y. Difficulty: expected worst case time

complexity O (1); expected worst case complexity is O (1).

The question was pretty straightforward and it took me 2 minutes to write a solution, the following:

 class Solution { public int solution(int X, int Y, int D) { int p = 0; while (X < Y){ p++; X = X + D; } return p; } } 

However, the test results show that the performance of my code is only 20% and I scored only 55% ,

enter image description here

Here is the link to the result, https://codility.com/demo/results/demo66WP2H-K25/

It was so simple code where I just used one in a while loop, how could this be done much faster?

+8
source share
15 answers

Basic math:

 X + nD >= Y nD >= Y - X n >= (Y - X) / D 

The minimum value for n will be the result of rounding the division (Y - X) by D.

Big O analysis for this operation:

  • Difficulty: O (1). This is difference, division and rounding.
  • The most complex space complexity is O (1): you can have no more than 3 variables:
    • The difference for Y is X, assign it to Z.
    • Separation between Z by D, let it be assigned to E.
    • Rounding E up, let me assign it to R (from the result).
+6
source

Java (one line), 100% correct, 100% performance, 100% task score

 // you can also use imports, for example: // import java.util.*; // you can write to stdout for debugging purposes, eg // System.out.println("this is a debug message"); class Solution { public int solution(int X, int Y, int D) { return (int) Math.ceil((double) (Y - X) / (double) D); } } 
+3
source
 class Solution { public int solution(int x, int y, int d) { // write your code in Java SE 8 System.out.println("this is a debug message"+(yx)%d); if((yx)%d == 0) return ((yx)/d); else return (((yx)/d)+1); } } 
+2
source
 class Solution { public int solution(int x, int y, int d) { return (y - x + d - 1) / d; } } 
+2
source

Here's the Scala solution:

 def solution(X: Int, Y: Int, D: Int): Int = { //divide distance (YX) with fixed jump distance. If there is reminder then add 1 to result to // cover that part with one jump val jumps = (YX) / D + (if(((YX) % D) >0 ) 1 else 0) jumps } 

Performance: https://codility.com/demo/results/trainingTQS547-ZQW/

+1
source

C # got 100 out of 100 points

 using System; // you can also use other imports, for example: // using System.Collections.Generic; // you can write to stdout for debugging purposes, eg // Console.WriteLine("this is a debug message"); class Solution { public int solution(int X, int Y, int D) { int Len= YX; if (Len%D==0) { return Len/D; } else { return (Len/D)+1; } } } 
+1
source

Here is a solution that brings test performance to 100%

 class Solution { public int solution(int X, int Y, int D) { if (X >= Y) return 0; if (D == 0) return -1; int minJump = 0; if ((Y - X) % D == 0) { minJump = (Y - X) / D; } else minJump= (Y - X) / D +1; return minJump; } } 
+1
source

JavaScript Solution 100/100

 function solution (x,y,d) { if ((yx) % d === 0) { return (yx)/d; } else { return Math.ceil((yx)/d); } } 
0
source

Javascript solution, 100/100, and shorter than the existing answer:

 function solution(Y, Y, D) { return Math.ceil((Y - X) / D); } 
0
source

Using Perfect Java Code

 100 score code in Java 

public int solution (int X, int Y, int D) {

if (X <0 && Y <0) return 0;

  if(X==Y) return 0; if((YX)%D==0) return (YX)/D; else return (((YX)/D)+1); } 

0
source

this is corrected code using Java giving 91% pass

 int solution(int A[]) { int len = A.length; if (len == 2) { return Math.abs(A[1] - A[0]); } int[] sumArray = new int[A.length]; int sum = 0; for (int j = 0; j < A.length; j++) { sum = sum + A[j]; sumArray[j] = sum; } int min = Integer.MAX_VALUE; for (int j = 0; j < sumArray.length; j++) { int difference = Math.abs(sum - 2 * sumArray[j]); // System.out.println(difference); if (difference < min) min = difference; } return min; } 
0
source

This is my solution with 100% (C #):

  int result = 0; if (y <= x || d == 0) { result = 0; } else { result = (y - x + d - 1) / d; } return result; 
0
source

Here is my PHP solution, 100% performance.

  function solution($X, $Y, $D) { return (int)ceil(($Y-$X)/$D); //ceils returns a float and so we cast (int) } 
0
source

YX gives you the actual distance that the object should go if this distance is directly divided by the jump of the object (D), then ans will be (sum / D), if there is some decimal value, then we need to add 1 more to it, i.e. . amount / D), +1

  int sum=YX; if(X!=Y && X<Y){ if(sum%D==0){ return (int )(sum/D); } else{ return ((int)(sum/D)+1); }} else{ return 0; } 
0
source

I like all the other solutions, especially "(y - x + d - 1) / d". It was awesome. This is what I came up with.

 public int solution(int X, int Y, int D) { if (X == Y || X > Y || D == 0) { return 0; } int total = (Y - X) / D; int left = (Y - X) - (D * total); if (left > 0) { total++; } return total; } 
0
source

Source: https://habr.com/ru/post/981624/


All Articles