mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 07:44:07 +00:00
day 25: 338-valid-counting-bits, 167-two-sum-ii-input-array-is-sorted
This commit is contained in:
parent
8acacbbc59
commit
191a4262d0
10
167-two-sum-ii-input-array-is-sorted/driver.cpp
Normal file
10
167-two-sum-ii-input-array-is-sorted/driver.cpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
vector<int> nums{-1,2,3,7,9,11,15};
|
||||||
|
vector<int> answer{1,6};
|
||||||
|
int target = 10;
|
||||||
|
Solution soln;
|
||||||
|
cout << "Found two sum correctly? " << soln.test(nums, target,answer) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
35
167-two-sum-ii-input-array-is-sorted/soln.cpp
Normal file
35
167-two-sum-ii-input-array-is-sorted/soln.cpp
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
vector<int> Solution::twoSum(vector<int> &numbers, int target)
|
||||||
|
{
|
||||||
|
// two sum approach
|
||||||
|
// unordered_map<int,int>mp;
|
||||||
|
// for(int i = 0; i < numbers.size(); i++) {
|
||||||
|
// if(mp.find(target-numbers[i]) == mp.end()) {
|
||||||
|
// mp[numbers[i]] = i;
|
||||||
|
// } else {
|
||||||
|
// return vector<int>{(mp.find(target-numbers[i])->second)+1, i+1};
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return vector<int>{-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<int> &numbers, int target, vector<int> &answer)
|
||||||
|
{
|
||||||
|
return twoSum(numbers, target) == answer;
|
||||||
|
}
|
||||||
8
167-two-sum-ii-input-array-is-sorted/soln.hpp
Normal file
8
167-two-sum-ii-input-array-is-sorted/soln.hpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
vector<int> twoSum(vector<int> &numbers, int target);
|
||||||
|
bool test(vector<int> &numbers, int target, vector<int> &answer);
|
||||||
|
};
|
||||||
10
338-counting-bits/driver.cpp
Normal file
10
338-counting-bits/driver.cpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int n = 7;
|
||||||
|
vector<int> answer{0, 1, 1, 2, 1, 2, 2, 3};
|
||||||
|
Solution soln;
|
||||||
|
cout << "Counted bits correctly? " << soln.test(n, answer) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
37
338-counting-bits/soln.cpp
Normal file
37
338-counting-bits/soln.cpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
vector<int> Solution::countBits(int n)
|
||||||
|
{
|
||||||
|
vector<int> 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<int> &answer)
|
||||||
|
{
|
||||||
|
return countBits(n) == answer;
|
||||||
|
}
|
||||||
8
338-counting-bits/soln.hpp
Normal file
8
338-counting-bits/soln.hpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
vector<int> countBits(int n);
|
||||||
|
bool test(int n, vector<int> &answer);
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user