diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/longest_consecutive_sequence.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/longest_consecutive_sequence.cpp')
-rw-r--r-- | other/longest_consecutive_sequence.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/other/longest_consecutive_sequence.cpp b/other/longest_consecutive_sequence.cpp new file mode 100644 index 0000000..2fda5e6 --- /dev/null +++ b/other/longest_consecutive_sequence.cpp @@ -0,0 +1,36 @@ +#include "leetcode.h" + +class Solution { +public: + int longestConsecutive(vector<int> &nums) { + unordered_set<int> us; + int ans = 0; + for (int i = 0; i < nums.size(); i++) + us.insert(nums[i]); + while (us.size() > 0) { + int num = *us.begin(); + int count = 1; + us.erase(num); + int smaller = num - 1; + int bigger = num + 1; + while (us.count(smaller) > 0) { + us.erase(smaller); + smaller--; + count++; + } + while (us.count(bigger) > 0) { + us.erase(bigger); + bigger++; + count++; + } + ans = max(ans, count); + } + return ans; + } +}; + +int main() { + Solution obj; + vector<int> nums = {100, 4, 200, 1, 3, 2}; + cout << obj.longestConsecutive(nums); +} |