Sort by

recency

|

4158 Discussions

|

  • + 0 comments
    public static int countingValleys(int steps, String path) {
            // Write your code here
            int difference = 0;
            boolean isValley = false;
            int count = 0;
            
            for (int i = 0; i < path.length(); i++) {
                if (difference == 0 && isValley) {
                    count++;
                }
                if (path.charAt(i) == 'U') {
                    if (difference == 0) {
                        isValley = false;
                    }
                    difference++;
                } else {
                    if (difference == 0) {
                        isValley = true;
                    }
                    difference--;
                }
            }
            
            if (isValley && difference == 0) {
                count++;
            }
            return count;
        }
    
  • + 0 comments

    **Logic to solve the problem is if the previous step was a "U" and the sealevel count becomes 0 it is a valley. **

            int sealevel = 0;
            int valley = 0;
            char[] charPath = path.toCharArray();
            for (int i = 0; i < steps ; i++) {
                if (charPath[i]=='U'){
                    sealevel++;
                    if(sealevel==0) valley++;
                } 
                if (charPath[i]=='D') sealevel--;
            }
            return valley;
        }
    }
    
  • + 0 comments
    def counting_valleys(steps, path):
        count = level = 0
        in_valley = False
        for i in range(steps):
            level += 1 if path[i] == "U" else -1
            if level == -1 and path[i] == "D" and not in_valley:
                count += 1
                in_valley = True
            if level == 0 and path[i] == "U" and in_valley:
                in_valley = False
        return count
    
  • + 0 comments
    int countingValleys(int steps, string path) {
        int path_counter = 0, valeys = 0;
        bool topo = true;
        
        for(char c : path){
            
            if(c=='U'){
                path_counter++;
                
                if(path_counter>=0){
                    topo = true;
                }
            }
            
            if(c=='D'){
                path_counter--;
                
                if(path_counter<0 && topo){
                    valeys++;
                    topo = false;
                }
                
            }
        }
        
        return valeys;
    }
    
  • + 1 comment

    Can you help to integrate this algirithem script with my drive zone game page? I want to use it on my Worldpress website.