-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Day-61 One DP problem and finished chap 2 of CTCI2
- Loading branch information
Showing
3 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |