Sort by

recency

|

3254 Discussions

|

  • + 0 comments
    def jumpingOnClouds(c):
        idx = 0
        jumps = 0
        while idx != len(c)-1:
            # Greedy, if can jump 2, then do it
            idx = idx+2 if idx+2 < len(c) and c[idx+2] == 0 else idx+1
            jumps += 1
        return jumps
    
  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/GNof9B-9CN0

    int jumpingOnClouds(vector<int> c) {
        int result = 0, index = 0;
        while(index < c.size() - 1){
            if(c[index + 2] == 1) index++;
            else index+=2;
            result++;
        }
        return result;
    }
    
  • + 1 comment

    In C#

    public static int jumpingOnClouds(List<int> c)
        {
            int jumps = 0;
            int position = 0;
    
            while (position < c.Count - 1)
            {
                if (position + 2 < c.Count && c[position + 2] == 0)
                {
                    position += 2;
                }
                else
                {
                    position += 1;
                }
                jumps++;
            }
    
        return jumps;
    
    }
    

    }

  • + 0 comments

    java 8

    public static int jumpingOnClouds(List<Integer> c) {
        // Write your code here
        int jump = 0;
        int start = 0;
    
        while (start < c.size() - 1) {
            if (start == c.size() - 2) {
                jump++;
                break;
    
            }
            int idx = (c.get(start + 2) != 0) ? 1 : 2;
            start += idx;
            jump++;
    
        }
        return jump;
    
  • + 1 comment

    Problem is not very clear, what if there are consecutive thunderheads clouds. Example c=[0, 1, 1, 1, 0] , what will be the jumping rule?