rengetsu

HackerRank_CountingValleys

Jun 20th, 2018
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.39 KB | None | 0 0
  1. //Counting Valleys
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.     long long int n, lvl=0, dwn=0, val=0;
  7.     char z;
  8.     cin >> n;
  9.     for(int i=0;i<n;i++)
  10.     {
  11.         cin >> z;
  12.         if(z=='U'){lvl+=1;}
  13.         else if(z=='D'){lvl-=1;}
  14.        
  15.         if(lvl<0 && dwn!=1){dwn=1;}
  16.         if(dwn==1 && lvl>=0){val+=1;dwn=0;}
  17.     }
  18.     cout<<val;
  19.     return 0;
  20. }
Add Comment
Please, Sign In to add comment