7 lines, simplest O(n) C++ solution


  • 1

    status represent current sequence increasing or decreasing.

        int wiggleMaxLength(vector<int>& nums) {
            int count = nums.size() > 0 ? 1 : 0;
            int status = 0;
            for (int i = 1; i < nums.size(); ++i) {
                int newStatus = nums[i] - nums[i-1];
                if (status == 0 && newStatus != 0 || newStatus * status < 0) {
                    status = newStatus;
                    ++count;
                }
            }
            return count;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.