Skip to content

Commit

Permalink
Day-61 One DP problem and finished chap 2 of CTCI2
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Oct 22, 2015
1 parent 1a7a866 commit 7e42009
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
12 changes: 12 additions & 0 deletions @
Original file line number Diff line number Diff line change
@@ -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' [email protected] < ~/temp/outputfile.txt")


7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) |

</center>

Expand Down Expand Up @@ -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 <ul><li>Integers in each row are sorted in ascending from left to right.</li></ul><ul><li>Integers in each column are sorted in ascending from top to bottom.</ul></li>|[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)|
51 changes: 51 additions & 0 deletions leet_code_problems/longestConsecutiveSeq.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>
#include <unordered_map>

int longestConsecutive(std::vector<int>& nums) {
std::unordered_map<int,int> 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<int> & vec) {
for ( auto & v : vec ) {
std::cout << v << " ";
}
std::cout << std::endl;
}
int main() {

std::cout << "Vector:";
std::vector<int> vec{ 100, 4, 200, 1, 3, 2 };
printVec(vec);
std::cout << "Longest consecutive sequence length:" << longestConsecutive(vec) << std::endl;
return 0;
}

0 comments on commit 7e42009

Please sign in to comment.