Opening the reading…
Opening the reading…
GREEDY › PART I
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →A wiggle sequence is defined by the signs of its consecutive differences: positive, negative, positive, or the reverse. Equal values contribute no direction at all, so a zero difference cannot extend the sequence. The answer therefore depends on how many times the chosen subsequence can alternate between rising and falling, not on the actual sizes of those rises and falls.
When the sequence is currently rising, another rising value does not create a new wiggle. Keep the later value anyway, because it is the most recent endpoint and is at least as useful for a future fall. When the direction changes, that new value creates one more element in the subsequence. This replacement is the greedy choice: preserve the current length while making the endpoint ready for the next opposite direction.
Scan adjacent values and classify each difference as positive, negative, or zero. Count the first nonzero direction as the second element, then count a difference whenever its sign is opposite to the last accepted sign. Zero differences are skipped, and the last accepted sign is not changed by them. The scan is equivalent to keeping the best possible endpoint after every turn without storing the subsequence itself.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n) | The loop examines each adjacent pair once, and each pair performs only constant-time arithmetic and comparisons. No pair is revisited, so the work grows linearly and remains O(n) for increasing, decreasing, alternating, or equal-heavy input. |
| Space | O(1) extra space | The algorithm stores only the count, the last accepted difference, and the current difference. The returned value is output rather than working memory, so it is excluded; the extra space stays O(1) for every input shape. |
#include <vector>
using namespace std;
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
if (nums.size() < 2) return nums.size();
int prevDiff = nums[1] - nums[0];
int count = (prevDiff != 0) ? 2 : 1;
for (int i = 2; i < nums.size(); ++i) {
int diff = nums[i] - nums[i - 1];
if ((diff > 0 && prevDiff <= 0) ||
(diff < 0 && prevDiff >= 0)) {
++count;
prevDiff = diff;
}
}
return count;
}
};The important placement is prevDiff = diff inside the acceptance condition. A same-direction difference may move the conceptual endpoint, but it does not change the wiggle direction, so it cannot affect the count. Updating prevDiff only for an accepted turn makes the stored value represent the last meaningful sign, while the <= and >= comparisons allow a zero difference to be ignored naturally.
A standard dynamic programming formulation keeps two values for every position. up[i] is the longest wiggle subsequence ending at i whose last difference is positive, and down[i] is the corresponding length when the last difference is negative. For every earlier position j, a rise from nums[j] to nums[i] can extend down[j], while a fall can extend up[j].
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
int n = nums.size();
if (n < 2) return n;
vector<int> up(n, 1);
vector<int> down(n, 1);
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (nums[i] > nums[j]) {
up[i] = max(up[i], down[j] + 1);
} else if (nums[i] < nums[j]) {
down[i] = max(down[i], up[j] + 1);
}
}
}
return max(up[n - 1], down[n - 1]);
}
};This alternative is not an optimisation. It is a more explicit arrangement that records the best answer ending at every index, which can be useful when you need to reconstruct or inspect endpoint states. It costs O(n squared) time because every pair of indices is compared and O(n) extra space for the two state arrays, while the greedy version compresses all needed information into constant space.