From 7e42009beda0e5db2f54839fe23a863091f7bb69 Mon Sep 17 00:00:00 2001 From: mandliya Date: Thu, 22 Oct 2015 00:02:02 -0400 Subject: [PATCH] Day-61 One DP problem and finished chap 2 of CTCI2 --- @ | 12 +++++ README.md | 7 +-- leet_code_problems/longestConsecutiveSeq.cpp | 51 ++++++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 @ create mode 100644 leet_code_problems/longestConsecutiveSeq.cpp diff --git a/@ b/@ new file mode 100644 index 0000000..8131705 --- /dev/null +++ b/@ @@ -0,0 +1,12 @@ +#! /usr/bin/env python +import os + +os.chdir("/home/ubuntu/practice/git/algorithms_and_ds_playground") +os.system("git pull origin master 2>&1 | tee ~/temp/outputfile.txt") +os.system("git add -A | tee ~/temp/outputfile.txt") +os.system("git commit -m 'Day-61 One DP problem and finished chap 2 of CTCI'2>&1 | tee ~/temp/outputfile.txt") +os.system("git push origin master 2>&1 | tee ~/temp/outputfile.txt") +print("\n\n\nPushed to Git hopefully. Here's a status:\n\n\n") +os.system("mutt -s 'day-61 details' ravi.mandliya@gmail.com < ~/temp/outputfile.txt") + + diff --git a/README.md b/README.md index ce4f014..a63ef0f 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ | Current Status| Stats | | :------------: | :----------: | -| Total Problems | 94 | -| Current Streak | 66 days | -| Longest Streak | 66 ( August 17, 2015 - October 21, 2015 ) | +| Total Problems | 95 | +| Current Streak | 67 days | +| Longest Streak | 67 ( August 17, 2015 - October 22, 2015 ) | @@ -171,3 +171,4 @@ Include contains single header implementation of data structures and some algori | Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].| [summary_ranges.cpp](leet_code_problems/summary_ranges.cpp)| | Given a 2D matrix, with following properties |[search2DII.cpp](leet_code_problems/search2DII.cpp)| | Given an unsorted integer array, find the first missing positive integer.Example: [1,2,0] should return 3 and [3,4,-1,1] should return 2. Expected time complexity O(n) and solution should use constant space| [firstMissingPositiveNum.cpp](leet_code_problems/firstMissingPositiveNum.cpp)| +|Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example: Given [100, 4, 200, 1, 3, 2]. The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Algorithm should run in O(n) complexity.| [longestConsecutiveSeq.cpp](leet_code_problems/longestConsecutiveSeq.cpp)| diff --git a/leet_code_problems/longestConsecutiveSeq.cpp b/leet_code_problems/longestConsecutiveSeq.cpp new file mode 100644 index 0000000..c9b10db --- /dev/null +++ b/leet_code_problems/longestConsecutiveSeq.cpp @@ -0,0 +1,51 @@ +/** + * Leet code problem: + * Given an unsorted array of integers, find the length of the longest consecutive elements sequence. + * For example, + * Given [100, 4, 200, 1, 3, 2], + * The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. + */ + + +#include +#include +#include + +int longestConsecutive(std::vector& nums) { + std::unordered_map hashMap; + for ( auto n : nums ) { + hashMap.insert({n, -1}); + } + int max_so_far = 0; + for ( size_t i = 0; i < nums.size(); ++i ) { + int j = 0; + int k = nums[i]; + while( hashMap.find(k + j) != hashMap.end() ) { + if (hashMap[k+j] != -1 ) { + j += hashMap[k+j]; + break; + } + ++j; + } + hashMap[k] = j; + if ( j > max_so_far ) { + max_so_far = j; + } + } + return max_so_far; +} + +void printVec(std::vector & vec) { + for ( auto & v : vec ) { + std::cout << v << " "; + } + std::cout << std::endl; +} +int main() { + + std::cout << "Vector:"; + std::vector vec{ 100, 4, 200, 1, 3, 2 }; + printVec(vec); + std::cout << "Longest consecutive sequence length:" << longestConsecutive(vec) << std::endl; + return 0; +}