LC135: Candy 작성일 2020-01-26 https://leetcode.com/problems/longest-consecutive-sequence/ 연휴동안 100문제 풀기 (3/100) 완전 어렵게 돌아서 풀어서 이게 맞나 싶다. 코드도 정말 거지같다. 다시풀자. 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748struct Node { int n, dir; }; class Solution { public: const int DOWN = -1, UP = 1; int candy(vector<int>& ratings) { queue<Node> q; int prev = INT_MAX, dir = DOWN, N = ratings.size(); vector<int> count(N+2, -1); count[0] = 0; count[N+1] = 0; q.push({0,1}); q.push({N+1,-1}); for(int i = 0; i < N; i++) { if(prev < ratings[i] && dir == DOWN) { dir = UP; count[i] = 1; q.push({i, 1}); q.push({i, -1}); } else if( prev > ratings[i] && dir == UP) { dir = DOWN; } else if( prev == ratings[i] && dir == DOWN) { count[i] = 1; q.push({i,-1}); } else if( prev == ratings[i] && dir == UP) { count[i+1] = 1; q.push({i+1,1}); } prev = ratings[i]; } while(q.size()) { Node cur = q.front(); q.pop(); int next = cur.n + cur.dir; if(!cur.n || cur.n == N+1 || ( next-1 < N && next-1 >= 0 && ratings[next-1] > ratings[cur.n-1])) { count[next] = max(count[next], count[cur.n] + 1); q.push({next, cur.dir}); } } int ans = 0; for(int n : count) ans += n; return ans; } };