Skip to content

Commit 7e42009

Browse files
committed
Day-61 One DP problem and finished chap 2 of CTCI2
1 parent 1a7a866 commit 7e42009

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

@

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#! /usr/bin/env python
2+
import os
3+
4+
os.chdir("/home/ubuntu/practice/git/algorithms_and_ds_playground")
5+
os.system("git pull origin master 2>&1 | tee ~/temp/outputfile.txt")
6+
os.system("git add -A | tee ~/temp/outputfile.txt")
7+
os.system("git commit -m 'Day-61 One DP problem and finished chap 2 of CTCI'2>&1 | tee ~/temp/outputfile.txt")
8+
os.system("git push origin master 2>&1 | tee ~/temp/outputfile.txt")
9+
print("\n\n\nPushed to Git hopefully. Here's a status:\n\n\n")
10+
os.system("mutt -s 'day-61 details' [email protected] < ~/temp/outputfile.txt")
11+
12+

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 94 |
10-
| Current Streak | 66 days |
11-
| Longest Streak | 66 ( August 17, 2015 - October 21, 2015 ) |
9+
| Total Problems | 95 |
10+
| Current Streak | 67 days |
11+
| Longest Streak | 67 ( August 17, 2015 - October 22, 2015 ) |
1212

1313
</center>
1414

@@ -171,3 +171,4 @@ Include contains single header implementation of data structures and some algori
171171
| 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)|
172172
| 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)|
173173
| 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)|
174+
|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)|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Leet code problem:
3+
* Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
4+
* For example,
5+
* Given [100, 4, 200, 1, 3, 2],
6+
* The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
7+
*/
8+
9+
10+
#include <iostream>
11+
#include <vector>
12+
#include <unordered_map>
13+
14+
int longestConsecutive(std::vector<int>& nums) {
15+
std::unordered_map<int,int> hashMap;
16+
for ( auto n : nums ) {
17+
hashMap.insert({n, -1});
18+
}
19+
int max_so_far = 0;
20+
for ( size_t i = 0; i < nums.size(); ++i ) {
21+
int j = 0;
22+
int k = nums[i];
23+
while( hashMap.find(k + j) != hashMap.end() ) {
24+
if (hashMap[k+j] != -1 ) {
25+
j += hashMap[k+j];
26+
break;
27+
}
28+
++j;
29+
}
30+
hashMap[k] = j;
31+
if ( j > max_so_far ) {
32+
max_so_far = j;
33+
}
34+
}
35+
return max_so_far;
36+
}
37+
38+
void printVec(std::vector<int> & vec) {
39+
for ( auto & v : vec ) {
40+
std::cout << v << " ";
41+
}
42+
std::cout << std::endl;
43+
}
44+
int main() {
45+
46+
std::cout << "Vector:";
47+
std::vector<int> vec{ 100, 4, 200, 1, 3, 2 };
48+
printVec(vec);
49+
std::cout << "Longest consecutive sequence length:" << longestConsecutive(vec) << std::endl;
50+
return 0;
51+
}

0 commit comments

Comments
 (0)