diff --git a/347-top-k-frequent-elements/driver.cpp b/347-top-k-frequent-elements/driver.cpp new file mode 100644 index 0000000..9a9f70f --- /dev/null +++ b/347-top-k-frequent-elements/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" + +int main() +{ + vector nums{1, 1, 1, 2, 2, 3}, answer{1, 2}; + int k = 2; + Solution soln; + cout << "Found top frequent elements correctly? " << soln.test(nums, k, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/347-top-k-frequent-elements/soln.cpp b/347-top-k-frequent-elements/soln.cpp new file mode 100644 index 0000000..b76d823 --- /dev/null +++ b/347-top-k-frequent-elements/soln.cpp @@ -0,0 +1,33 @@ +#include "soln.hpp" + +vector Solution::topKFrequent(vector &nums, int k) +{ + // first get counts + unordered_map counts; + for (int num : nums) + { + counts[num]++; + } + // make a vector indexed by counts. nums can have max nums.size() unique elements, +1 for 0 + vector> counts_indexed(nums.size() + 1); + for (auto count : counts) + counts_indexed[count.second].push_back(count.first); + + // iterate in reverse order to get most frequent elements + vector result; + for (int i = counts_indexed.size() - 1; i > 0; i--) + { + for (int j = 0; j < counts_indexed[i].size(); j++) + { + result.push_back(counts_indexed[i][j]); + if (result.size() == k) + return result; + } + } + return result; +} + +bool Solution::test(vector &nums, int k, vector &answer) +{ + return topKFrequent(nums, k) == answer; +} \ No newline at end of file diff --git a/347-top-k-frequent-elements/soln.hpp b/347-top-k-frequent-elements/soln.hpp new file mode 100644 index 0000000..d0b5b4f --- /dev/null +++ b/347-top-k-frequent-elements/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + vector topKFrequent(vector &nums, int k); + bool test(vector &nums, int k, vector &answer); +}; \ No newline at end of file diff --git a/704-binary-search/driver.cpp b/704-binary-search/driver.cpp new file mode 100644 index 0000000..114906f --- /dev/null +++ b/704-binary-search/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" + +int main() +{ + vector nums{-1, 0, 3, 5, 9, 12}; + int target = 9, answer = 4; + Solution soln; + cout << "Binary search done correctly ? " << soln.test(nums, target, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/704-binary-search/soln.cpp b/704-binary-search/soln.cpp new file mode 100644 index 0000000..6234f80 --- /dev/null +++ b/704-binary-search/soln.cpp @@ -0,0 +1,25 @@ +#include "soln.hpp" + +int Solution::search(vector &nums, int target) +{ + int low = 0, high = nums.size() - 1; + int mid; + while (low != high) + { + mid = (low + high) / 2; + if (nums[mid] == target) + return mid; + else if (nums[mid] < target) + low = mid + 1; + else + high = mid; + } + if (nums[low] == target) + return low; + return -1; +} + +bool Solution::test(vector &nums, int target, int answer) +{ + return search(nums, target) == answer; +} \ No newline at end of file diff --git a/704-binary-search/soln.hpp b/704-binary-search/soln.hpp new file mode 100644 index 0000000..a81badc --- /dev/null +++ b/704-binary-search/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + int search(vector &nums, int target); + bool test(vector &nums, int target, int answer); +}; \ No newline at end of file