Guest User

Untitled

a guest
Nov 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int candy(vector<int>& ratings) {
  4. vector<int> candys;
  5. candys.resize(ratings.size(), 1);
  6. for(int i = 1; i < ratings.size(); i++) {
  7. if (ratings[i] > ratings[i-1]) {
  8. candys[i] = candys[i-1] + 1;
  9. }
  10. }
  11. for(int j = ratings.size() - 2; j >= 0; j--) {
  12. if (ratings[j] > ratings[j+1] && candys[j] <= candys[j+1]) {
  13. candys[j] = candys[j+1] + 1;
  14. }
  15. }
  16. int n = 0;
  17. for(int i = 0; i < candys.size(); i++) {
  18. n += candys[i];
  19. }
  20. return n;
  21. }
  22. };
Add Comment
Please, Sign In to add comment