res to 0.j to 0 to keep track of the last unique element.i, check if it forms a hill or a valley by comparing it with the last unique element nums[j] and the next element nums[i + 1].res and update the last unique element pointer j to the current index i.res.