Sort by

recency

|

3274 Discussions

|

  • + 0 comments

    Python:

        position = 0
        jumps = 0
        while position < n - 1:
            if position + 2 < n and c[position +2] == 0:
                position +=2
            else:
                position +=1
            jumps += 1
        return jumps
    
  • + 0 comments

    My C++ Solution:

    int jumpingOnClouds(vector<int> c) {
        int jumps = 0;
        for(int i=0;i < c.size();){
            if(i == (c.size()-1))return jumps;
            if(c[i+2] == 0){
                i +=2;
            }else{
                i += 1;                
            }
            jumps++;
        }
        return  jumps;
    }
    
  • + 0 comments

    Key factor - as per the task, the player always reaches the end. so he either jump 2 positions, or jumps 1 position if 2 lands on a thunder cloud. We try to jump 2 positions, if its a thunder cloud - we return 1 back to a guaranteed safe space. Looping until less than c.size() - 1, as the c.size() -1 is considered finish and player doesnt jump after landing there.

    // public static int jumpingOnClouds(List c) {

        int result = 1;
    
        for (int cloud = 2; cloud < c.size() - 1; cloud += 2) {
            if(c.get(cloud) == 1) cloud--;
            result++;
        }
    
        return result;
    }
    
  • + 0 comments
    public static int jumpingOnClouds(List<Integer> c) {
            int count = 0;
            for (int i = c.size() - 1; i > 0; i--) {
                int p = i - 2;
                int q = i - 1;
                if (p > -1 && c.get(p) != 1) {
                    i = q;
                }
                count++;
            }
            return count;
        }
    
  • + 0 comments

    give me your opinion please i need it

        public static int jumpingOnClouds(List<Integer> c) {
            if (c.isEmpty() || c.size() < 2) return 0;
            if (c.get(c.size() - 1) != 0) return 0;
            int jumpsRequired = 0;
            int i = 0;
            while (i < c.size() - 1) {
                if ((i + 1 < c.size() && c.get(i + 1) == 1) && (i + 2 < c.size() && c.get(i + 2) == 1)) {
                    break;
                }
                int next = i + 1;
                int afterNext = next + 1;
                if (afterNext < c.size() && c.get(afterNext) == 0) {
                    i = afterNext;
                    jumpsRequired++;
                } else if (next < c.size() && c.get(next) == 0) {
                    i = next;
                    jumpsRequired++;
                }
            }
            return jumpsRequired;
        }