Sort by

recency

|

4081 Discussions

|

  • + 0 comments

    My solutions in Java:

    1:

        public static int countingValleys(int steps, String path) {
        
            int seaLevel = 0;
            int valleys = 0;
            boolean isInValley = false;
            for (int i = 0; i < steps; i++) {
                if(path.charAt(i) == 'U'){
                    seaLevel++;
    
                } else {
                    seaLevel--;
                }
                
                if(seaLevel < 0) {
                    isInValley = true;
                } else if (seaLevel > 0){
                    isInValley = false;
                }
                
                if(seaLevel == 0 && isInValley) {
                    valleys++;
                }
                
            }
            
            return valleys;
        }
    

    2: more concise

    public static int countingValleys(int steps, String path) {
    
        int seaLevel = 0;
        int valleys = 0;
        for (char step : path.toCharArray()) {
           seaLevel += (step == 'U') ? 1 : -1;
    
            // Detect if a valley has been exited
           if (seaLevel == 0 && step == 'U') {
             valleys++;
           }
        }
    
        return valleys;
    }
    

    sd

  • + 1 comment

    One of the Test Cases is: DUDDDUUDUU

    Which clearly has only 1 valley but the expected out is 2. Is this mistake or am I missing something?

  • + 0 comments

    can you help to integrate this algorithem script with my Menu prices realted page?I want to use it on my WordPress website.

  • + 0 comments

    Here is my c++ solution you can find the video here : https://youtu.be/fgJ-i8RJ1Qw

    int countingValleys(int steps, string path) {
        int res = 0, level = 0;
        for(char c : path){
            if(c == 'U'){
                level++;
                if(level == 0) res ++;
            }
            else level--;
        }
        return res;
    }
    
  • + 0 comments

    Great problem and insightful solutions shared here! For those interested in improving their problem-solving skills and learning through a variety of tech resources, I recently came across an app that offers a unique platform for explore various tech-related tools, including learning how to manage different software solutions and APIs. It's always beneficial to incorporate multiple resources to enhance one’s programming journey.