We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
We only care about the number of valleys...
So just figure out the number of times you came back up to sea level.
publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);intn=sc.nextInt();Strings=sc.next();intv=0;// # of valleysintlvl=0;// current levelfor(charc:s.toCharArray()){if(c=='U')++lvl;if(c=='D')--lvl;// if we just came UP to sea levelif(lvl==0&&c=='U')++v;}System.out.print(v);}
Counting Valleys
You are viewing a single comment's thread. Return to all comments →
Java solution
We only care about the number of valleys... So just figure out the number of times you came back up to sea level.