Skip to content

Commit

Permalink
feat(problem-2419/cpp): solve by finding longest subarray of max elem…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
threeal committed Sep 14, 2024
1 parent e4aa5e6 commit 0711eff
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion old-problems/2419/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
class Solution {
public:
int longestSubarray(std::vector<int>& nums) {
return nums.size();
int maxConsecutive{0};
int maxNum{0};
for (std::size_t i{0}; i < nums.size(); ++i) {
if (nums[i] < maxNum) continue;
if (nums[i] > maxNum) {
maxNum = nums[i];
maxConsecutive = 1;
for (++i; i < nums.size() && nums[i] == maxNum; ++i) ++maxConsecutive;
--i;
} else {
maxNum = nums[i];
int consecutive{1};
for (++i; i < nums.size() && nums[i] == maxNum; ++i) ++consecutive;
if (consecutive > maxConsecutive) maxConsecutive = consecutive;
--i;
}
}
return maxConsecutive;
}
};

0 comments on commit 0711eff

Please sign in to comment.