#include #include #include #include int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ //get number of steps unsigned int num_steps; do{ scanf("%d",&num_steps); } while(num_steps <= 0); char* movement = (char*)malloc(num_steps * sizeof(char)); //char* movement; scanf("%s", movement); int height = 0; int temp = 0; //initialize valley counter = 0; int valley_counter = 0; //itirate through the string, U will add one to height, D will subtract one from height for(char* t = movement; *t != '\0'; t++) { if (*t == 'U') { height += 1; } else if (*t == 'D') { height -= 1; } //checking for sea level condition againts previous height and then updating previous height if (height == 0 && height > temp) {valley_counter++;} temp = height; } printf("%i\n", valley_counter); return 0; }