Jumping on the Clouds: Revisited

Sort by

recency

|

1182 Discussions

|

  • + 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

    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

    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;
    }
    
  • + 0 comments
    def jumpingOnClouds(c, k):
        e: int = 100
        n: int = len(c)
        idx: int = (0 + k) % n
        
        while True:
            e -= 1
            if c[idx] == 1:
                e -= 2
            if idx == 0:
                break
            idx = (idx + k) % n
        return e
    
  • + 0 comments

    Java:

        static int jumpingOnClouds(int[] c, int k) {
            int e = 100;
            int pos = 0;
            
            do {
                pos = (pos + k) % c.length;
                e -= 1 + 2 * c[pos];
            } while (pos != 0);
            
            return e;
        }