diff --git a/167-two-sum-ii-input-array-is-sorted/driver.cpp b/167-two-sum-ii-input-array-is-sorted/driver.cpp new file mode 100644 index 0000000..55de66b --- /dev/null +++ b/167-two-sum-ii-input-array-is-sorted/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" + +int main() { + vector nums{-1,2,3,7,9,11,15}; + vector answer{1,6}; + int target = 10; + Solution soln; + cout << "Found two sum correctly? " << soln.test(nums, target,answer) << endl; + return 0; +} \ No newline at end of file diff --git a/167-two-sum-ii-input-array-is-sorted/soln.cpp b/167-two-sum-ii-input-array-is-sorted/soln.cpp new file mode 100644 index 0000000..23c0a56 --- /dev/null +++ b/167-two-sum-ii-input-array-is-sorted/soln.cpp @@ -0,0 +1,35 @@ +#include "soln.hpp" + +vector Solution::twoSum(vector &numbers, int target) +{ + // two sum approach + // unordered_mapmp; + // for(int i = 0; i < numbers.size(); i++) { + // if(mp.find(target-numbers[i]) == mp.end()) { + // mp[numbers[i]] = i; + // } else { + // return vector{(mp.find(target-numbers[i])->second)+1, i+1}; + // } + // } + // return vector{-1,-1}; + int left = 0, right = numbers.size() - 1; + int sum = numbers[left] + numbers[right]; + while (sum != target) + { + if (sum < target) + { + left++; + } + else + { + right--; + } + sum = numbers[left] + numbers[right]; + } + return {left + 1, right + 1}; +} + +bool Solution::test(vector &numbers, int target, vector &answer) +{ + return twoSum(numbers, target) == answer; +} \ No newline at end of file diff --git a/167-two-sum-ii-input-array-is-sorted/soln.hpp b/167-two-sum-ii-input-array-is-sorted/soln.hpp new file mode 100644 index 0000000..170f2c9 --- /dev/null +++ b/167-two-sum-ii-input-array-is-sorted/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + vector twoSum(vector &numbers, int target); + bool test(vector &numbers, int target, vector &answer); +}; \ No newline at end of file diff --git a/338-counting-bits/driver.cpp b/338-counting-bits/driver.cpp new file mode 100644 index 0000000..d8095f0 --- /dev/null +++ b/338-counting-bits/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" + +int main() +{ + int n = 7; + vector answer{0, 1, 1, 2, 1, 2, 2, 3}; + Solution soln; + cout << "Counted bits correctly? " << soln.test(n, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/338-counting-bits/soln.cpp b/338-counting-bits/soln.cpp new file mode 100644 index 0000000..674a98d --- /dev/null +++ b/338-counting-bits/soln.cpp @@ -0,0 +1,37 @@ +#include "soln.hpp" + +vector Solution::countBits(int n) +{ + vector result(n + 1); + // // O(n log n) approach, naive + // // O(n) + // int num; + // for(int i = 0; i <= n; i++) { + // num = i; + // // O(log n) + // while(num) { + // result[i] += num&1; + // num=num>>1; + // } + // } + + // O(n) approach + result[0] = 0; + if (n == 0) + { + return result; + } + for (int i = 1; i < n + 1; i = i * 2) + { + for (int j = i; (j < i * 2) && (j < n + 1); j++) + { + result[j] = result[(j / i) + (j - i) - 1] + 1; + } + } + return result; +} + +bool Solution::test(int n, vector &answer) +{ + return countBits(n) == answer; +} \ No newline at end of file diff --git a/338-counting-bits/soln.hpp b/338-counting-bits/soln.hpp new file mode 100644 index 0000000..e81d61d --- /dev/null +++ b/338-counting-bits/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + vector countBits(int n); + bool test(int n, vector &answer); +}; \ No newline at end of file