fork download
  1. #include <iostream>
  2. #include <numeric>
  3. #include <vector>
  4.  
  5. int main()
  6. {
  7. std::vector<int> kidHeights = { 1, 3, 6, 5, 6, 4, 2, 3 };
  8.  
  9. int numberOfKidsThatCanSeeToTheRight =
  10. std::accumulate(kidHeights.rbegin(), kidHeights.rend(), 0,
  11. [](int curKidCount, int curHeight) {
  12.  
  13. // assuming positive heights
  14. static int curMaxHeight = 0;
  15.  
  16. if (curHeight > curMaxHeight) {
  17. ++ curKidCount;
  18. curMaxHeight = curHeight;
  19. }
  20.  
  21. return curKidCount;
  22. });
  23.  
  24. std::cout << numberOfKidsThatCanSeeToTheRight << std::endl;
  25. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
3