Registrations for the November 2021 System Design Cohort are now open Enroll Today

COUNTING-VALLEYS Solution



#include <stdio.h>

int countingValleys(int n, char* s) {
    int level = 0, count = 0;
    for(int i = 0; s[i]; i++) {

        int prevLevel = level;

        if (s[i] == 'U') {
            level ++;
        } else {
            level --;
        }

        if (level < 0 && prevLevel >= 0) {
            count ++;
        }
    }
    return count;
}

int main()
{
    int n;
    char str[1000001];

    scanf("%d", &n);
    scanf("%s", str);

    printf("%d\n", countingValleys(n, str));

    return 0;
}

470+ Competitive Programming Solutions

If you like what you read subscribe to my newsletter.

I write essays on various engineering topics and share it through my weekly newsletter 👇