Skip to content

Instantly share code, notes, and snippets.

@esthicodes
Created August 24, 2021 01:19
Show Gist options
  • Save esthicodes/6ec853f86215f813d430509b515c019c to your computer and use it in GitHub Desktop.
Save esthicodes/6ec853f86215f813d430509b515c019c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%i", &n);
char str[n];
scanf("%s", str);
int level = 0, result = 0, valley = 0;
for (int i = 0; i < n; i++) {
if(str[i] == 'U') {
level++;
if(level == 0 && valley) {
valley = 0;
result++;
}
}
else if(str[i] == 'D') {
if(level == 0)
valley = 1;
level--;
}
}
printf("%i", result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment