Jumping on the Clouds: Revisited

Sort by

recency

|

1184 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/retgbr5jSsg

    int jumpingOnClouds(vector<int> c, int k) {
        int position = 0, energie = 100;
        do{
            position = (position + k) % c.size();
            energie -= 1 + c[position] * 2;
        }while(position != 0);
        return energie;
    }
    
  • + 0 comments

    Here's a C solution: int jumpingOnClouds(int c_count, int* c, int k) { int e = 100, i=0, beg = c[0], end = -1; c[0] = 111;
    while(end!=111){ if(c[i]==1){ e = e-2; } e--; if((i+k)>c_count-1) {
    i = abs(i+k-c_count); } else{ i+=k; }
    end = c[i]; } if(beg == 1){ e-=2; } return e; }

  • + 0 comments

    Here's a C# solution:

                int energy = 100;
        int cloudIndex = k % c.Count();
        if (c[cloudIndex] == 1)
        {
            energy -= 3;
        }
        else energy--;
        while (cloudIndex % c.Count() != 0)
        {
            cloudIndex += k;
            if (c[cloudIndex % c.Count()] == 1)
            {
                energy -= 3;
            }
            else energy--;
        }
        return energy;
    
  • + 0 comments

    Perl solution:

    sub jumpingOnClouds {
        my ($c, $k) = @_;
        
        my $energy = 100;
        my $ind = 0;
        for (my $i = 0; $i < scalar(@$c); $i++) {
            $ind = ($k + $ind) % scalar(@$c);
            $energy--;
            $energy -= 2 if ($c->[$ind] == 1);
            last if ($ind == 0);
        }
        return $energy;
    }
    
  • + 0 comments

    Java 8 Code

    // Complete the jumpingOnClouds function below.

    static int jumpingOnClouds(int[] c, int k) {
        int index =0;
        int e =100;
        for(int i =0;i<c.length;i++){
            index = (index+k)%c.length;
            if(c[index]==1){
                e = e-3;
            }else{
                e = e-1;
            }
            if(index==0){
                break;
            }
        }
    
      return e;
    }