From 06ceedd3ca9c41d6dcc5f48fc3551c51a2f53a95 Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Wed, 25 Oct 2023 15:13:29 -0700 Subject: [PATCH] day 26: 268-missing-number, 15-3-sum --- 15-3-sum/driver.cpp | 18 ++++++++++++++ 15-3-sum/soln.cpp | 47 +++++++++++++++++++++++++++++++++++ 15-3-sum/soln.hpp | 7 ++++++ 268-missing-number/driver.cpp | 10 ++++++++ 268-missing-number/soln.cpp | 18 ++++++++++++++ 268-missing-number/soln.hpp | 8 ++++++ thoughts.md | 1 + 7 files changed, 109 insertions(+) create mode 100644 15-3-sum/driver.cpp create mode 100644 15-3-sum/soln.cpp create mode 100644 15-3-sum/soln.hpp create mode 100644 268-missing-number/driver.cpp create mode 100644 268-missing-number/soln.cpp create mode 100644 268-missing-number/soln.hpp diff --git a/15-3-sum/driver.cpp b/15-3-sum/driver.cpp new file mode 100644 index 0000000..81ce976 --- /dev/null +++ b/15-3-sum/driver.cpp @@ -0,0 +1,18 @@ +#include "soln.hpp" + +int main() +{ + vector nums{-2,0,1,1,2}; + Solution soln; + vector> result = soln.threeSum(nums); + cout << "Three sum triplets:" << endl; + for (auto itr : result) + { + for (int num : itr) + { + cout << num << " "; + } + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/15-3-sum/soln.cpp b/15-3-sum/soln.cpp new file mode 100644 index 0000000..7f6e32b --- /dev/null +++ b/15-3-sum/soln.cpp @@ -0,0 +1,47 @@ +#include "soln.hpp" + +vector> Solution::threeSum(vector &nums) +{ + vector> result; + sort(nums.begin(), nums.end()); + int low, high, sum; + vector new_triplet; + for (int i = 0; i < nums.size() - 2; i++) + { + if (i > 0 && nums[i] == nums[i - 1]) + continue; + // two sum + int low = i + 1, high = nums.size() - 1; + while (low < high) + { + // if(nums[low] == nums[low-1]) { + // low++; + // continue; + // } + // if(nums[high] == nums[high+1]) { + // high--; + // continue; + // } + sum = nums[i] + nums[low] + nums[high]; + if (sum == 0) + { + new_triplet = vector{nums[i], nums[low], nums[high]}; + // if(find(result.begin(), result.end(), new_triplet) == result.end()) { + result.push_back({nums[i], nums[low], nums[high]}); + // } + low++; + while (nums[low] == nums[low - 1] && low < high) + low++; + } + else if (sum > 0) + { + high--; + } + else + { + low++; + } + } + } + return result; +} diff --git a/15-3-sum/soln.hpp b/15-3-sum/soln.hpp new file mode 100644 index 0000000..fa467d1 --- /dev/null +++ b/15-3-sum/soln.hpp @@ -0,0 +1,7 @@ +#include +using namespace std; +class Solution +{ +public: + vector> threeSum(vector &nums); +}; \ No newline at end of file diff --git a/268-missing-number/driver.cpp b/268-missing-number/driver.cpp new file mode 100644 index 0000000..4561641 --- /dev/null +++ b/268-missing-number/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" + +int main() +{ + vector nums{9, 6, 4, 2, 3, 5, 7, 0, 1}; + int answer = 8; + Solution soln; + cout << "Found missing number correctly? " << soln.test(nums, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/268-missing-number/soln.cpp b/268-missing-number/soln.cpp new file mode 100644 index 0000000..13ae2ca --- /dev/null +++ b/268-missing-number/soln.cpp @@ -0,0 +1,18 @@ +#include "soln.hpp" + +int Solution::missingNumber(vector &nums) +{ + int n = nums.size(); + int calc_sum = (n * (n + 1)) / 2; + int act_sum = 0; + for (int num : nums) + { + act_sum += num; + } + return calc_sum - act_sum; +} + +bool Solution::test(vector &nums, int answer) +{ + return missingNumber(nums) == answer; +} \ No newline at end of file diff --git a/268-missing-number/soln.hpp b/268-missing-number/soln.hpp new file mode 100644 index 0000000..fe71216 --- /dev/null +++ b/268-missing-number/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + int missingNumber(vector &nums); + bool test(vector &nums, int answer); +}; \ No newline at end of file diff --git a/thoughts.md b/thoughts.md index c3258a7..e315c48 100644 --- a/thoughts.md +++ b/thoughts.md @@ -4,3 +4,4 @@ - essentials: bits/stdc++.h, iostream - data structures: vector - (q21)when working with structs, can't return locally created objects from a function, even when using new keyword...? +- 2sum, 3sum, palindrome, and other two pointer problems can prolly be identified by how they require comparison-based solutions across the entire array, and the array is to be sorted or ordered in some way for traversal