Skip to content

Commit

Permalink
Added solution for 3Sum: https://leetcode.com/problems/3sum
Browse files Browse the repository at this point in the history
  • Loading branch information
asesh committed Oct 16, 2024
1 parent 62ec3fe commit ef09820
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 43 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "228"
endingLineNumber = "228"
startingLineNumber = "230"
endingLineNumber = "230"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -142,8 +142,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "191"
endingLineNumber = "191"
startingLineNumber = "193"
endingLineNumber = "193"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -158,8 +158,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "150"
endingLineNumber = "150"
startingLineNumber = "152"
endingLineNumber = "152"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -190,8 +190,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "139"
endingLineNumber = "139"
startingLineNumber = "141"
endingLineNumber = "141"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -206,8 +206,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "125"
endingLineNumber = "125"
startingLineNumber = "127"
endingLineNumber = "127"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -270,8 +270,8 @@
filePath = "algorithm/number.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "908"
endingLineNumber = "908"
startingLineNumber = "923"
endingLineNumber = "923"
landmarkName = "reverse_integer(input)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -286,8 +286,8 @@
filePath = "algorithm/number.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "1134"
endingLineNumber = "1134"
startingLineNumber = "1149"
endingLineNumber = "1149"
landmarkName = "invoke_product_of_array_except_itself()"
landmarkType = "9">
</BreakpointContent>
Expand Down
6 changes: 3 additions & 3 deletions algorithm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include "graph.hpp"

int main(int argc, const char* argv[]) {
invoke_separate_black_and_white_balls();
invoke_find_three_number_sum();

// invoke_separate_black_and_white_balls();

// invoke_trapping_rain_water();

Expand Down Expand Up @@ -229,8 +231,6 @@ int main(int argc, const char* argv[]) {
// std::cout<<"Key: "<<data.first<<",value: "<<data.second<<std::endl;
// }

// invoke_find_three_number_sum();

//invoke_longest_substring_without_repeating_characters();

// invoke_find_pivot_index();
Expand Down
63 changes: 39 additions & 24 deletions algorithm/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,7 @@ void invoke_sliding_window() {
}

/*
724. Find Pivot Index
https://leetcode.com/problems/find-pivot-index/
*/
*/
int find_pivot_index(const std::vector<int>& input_array) {
int sum = 0;
int total_sum = std::accumulate(input_array.begin(), input_array.end(), 0);
Expand All @@ -317,34 +315,51 @@ void invoke_find_pivot_index() {
}

/*
Three number sum
https://www.algoexpert.io/questions/Three%20Number%20Sum
*/
Input: [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Process:
sort: [-4,-1,-1,0,1,2]
C L R
[-4,-1,-1,0,1,2] =
C L R
[-4,-1,-1,0,1,2] =
*/
std::vector<std::vector<int>> find_three_number_sum(std::vector<int>& input_array) {
std::vector<std::vector<int>> output;
std::set<std::vector<int>> unique_output;
// O(nlogn)
std::sort(input_array.begin(), input_array.end());

// O(n^2)
for(int current_index = 0; current_index < input_array.size() - 2; ++current_index) {
int right_index = input_array.size() - 1;
for(int left_index = current_index + 1; left_index < input_array.size() && left_index != right_index;) {
auto sum = input_array[current_index] + input_array[left_index] + input_array[right_index];
if(sum == 0) {
unique_output.insert({input_array[current_index], input_array[left_index], input_array[right_index]});
++left_index;
} else if(sum > 0) {
--right_index;
} else {
++left_index;
}
}
}

for(auto& item: unique_output) {
output.push_back(item);
}

for(const auto& number: input_array) {
std::cout<<number<<std::endl;
}

/*
Implementation:
Input: 12, 3, 1, 2, -6, 5, -8, 6
Target: 0
Sort:
Data: -8 -6 1 2 3 5 6 12
Time: O(nlogn)
// Found triplets:
1: -8+2+6
*/

return {};
return output;
}
void invoke_find_three_number_sum() {
std::vector<int> input{12, 3, 1, 2, -6, 5, -8, 6};
std::vector<int> input{-1,0,1,2,-1,-4};
auto triplets = find_three_number_sum(input);
std::cout<<"The triplets are: "<<std::endl;
std::for_each(triplets.begin(), triplets.end(), [](std::vector<int>& triplet) {
std::cout<<triplet[0]<<", "<<triplet[1]<<", "<<triplet[2]<<std::endl;
});
}

void bubble_sort(std::vector<int> array) {
Expand Down
8 changes: 6 additions & 2 deletions algorithm/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ void invoke_container_with_most_water();
int sliding_window_max_sum_result(const std::vector<int>& input);
void invoke_sliding_window();

int find_pivot_index(const std::vector<int>& input_array);
/*
724. Find Pivot Index: https://leetcode.com/problems/find-pivot-index/
*/
void invoke_find_pivot_index();

std::vector<std::vector<int>> find_three_number_sum(std::vector<int>& input_array);
/*
15. 3Sum: https://leetcode.com/problems/3sum
*/
void invoke_find_three_number_sum();

void bubble_sort(std::vector<int> array);
Expand Down
7 changes: 7 additions & 0 deletions algorithm/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,10 @@ void invoke_reverse_words_in_a_string() {
std::string input = " hello world test!! ";
std::cout<<reverse_words_in_a_string(input)<<std::endl;
}

std::string longest_happy_string(int a, int b, int c) {
return "";
}
void invoke_longest_happy_string() {
std::cout<<"Longest happy string: "<<longest_happy_string(1, 2, 3)<<std::endl;
}
5 changes: 5 additions & 0 deletions algorithm/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ void invoke_is_subsequence();
*/
void invoke_reverse_words_in_a_string();

/*
1405. Longest Happy String: https://leetcode.com/problems/longest-happy-string
*/
void invoke_longest_happy_string();

#endif /* string_hpp */

0 comments on commit ef09820

Please sign in to comment.