Skip to content

Commit

Permalink
Day 3 complete
Browse files Browse the repository at this point in the history
  • Loading branch information
muditmahajan21 committed Apr 29, 2022
1 parent 8fb611a commit 6ac8b6b
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Day03/Majority Element II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
vector<int> majorityElement(vector<int>& nums) {
int ans1 = 0;
int ans2 = 0;
int count1 = 0;
int count2 = 0;

for(auto num: nums) {
if(num == ans1) {
count1++;
}
else if(num == ans2) {
count2++;
}
else if(count1 == 0) {
ans1 = num;
count1++;
}
else if(count2 == 0 ){
ans2 = num;
count2++;
}
else
{
count1--;
count2--;
}
}

count1 = count2 = 0;
for(auto num: nums) {
if(num == ans1) count1++;
else if(num == ans2) count2++;
}


cout << ans1 << " " << ans2;

vector<int> ans;
if(count1 > nums.size() / 3) ans.push_back(ans1);
if(count2 > nums.size() / 3) ans.push_back(ans2);

return ans;
}
19 changes: 19 additions & 0 deletions Day03/Majority Element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
int majorityElement(vector<int>& nums) {
int ans = 0;
int count = 0;

for(auto num: nums) {
if(!count) {
ans = num;
count++;
} else {
if(ans == num) {
count++;
} else {
count--;
}
}
}

return ans;
}
16 changes: 16 additions & 0 deletions Day03/Pow(x, n).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
double myPow(double x, int n) {
if(n < 0) {
x = 1 / x;
}
long num = abs(n);
double ans = 1.0;

while(num) {
if(num % 2) {
ans*= x;
}
x *= x;
num /= 2;
}
return ans;
}
86 changes: 86 additions & 0 deletions Day03/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Day3

## Majority Element

Link: [https://leetcode.com/problems/majority-element/]

- Initialize and and count, both as zero.
- For every element in the array, if
- The count is zero, set the element as the answer.
- If not,
- If the answer is equal to current element, then increment count.
- Else, decrement the count.

Time Complexity: O(n)

## Majority Element II

Link: [https://leetcode.com/problems/majority-element-ii/]

- Similar to above, except we will take node of two answers and two count variables.
- As, there will be at most 2 majority elements.
- After the initial loop, run another loop to check if the variables we found are actually majority or not.

Time Complexity: O(n)

## Pow(x, n)

### Topic: Binary Exponentiation

Link: [https://leetcode.com/problems/powx-n/]

- If n is negative, take the reciprocal of x.
- Initialize ans as 1.0 and take another temp variable for n.
- Whilw temp is greater than zero, if
- it's odd, then multiply x to answer
- Multiply x with itself.
- Half the temp variable.

Time Complexity: O(logn)

## Search a 2D Matrix

Link: [https://leetcode.com/problems/search-a-2d-matrix/]

- Basically, binary search in a matrix instead of an array.
- Check from the middle row if the element exists at the starting or the ending of the row.
- If not, then change the low and high accordingly to the values of the starting and the ending elements of the row.
- After finding the target row, run the basic binary search on the row to check for the target element.

Time Complexity: O(log(n * m))

## Unique Paths

Link: [https://leetcode.com/problems/unique-paths/]

- Easy PnC Solution avaialable for the problem.
- There are exactly n + m - 2 steps followed everytime to get to the end.
- So, we will calculate the value of <sup>n + m - 2</sup>C<sub>n - 1</sub> *OR* <sup>n + m - 2</sup>C<sub>m - 1</sub>

Time complexity: O(n - 1) *OR* O(m - 1).

## Reverse Pairs

Link: [https://leetcode.com/problems/reverse-pairs/]

### Topic: Merge Sort

- We will be using the Merge Sort Algorithm to solve this problem. We split the whole array into 2 parts creating a Merge Sort Tree-like structure. During the conquer step we do the following task :

- We take the left half of the Array and Right half of the Array, both are sorted in themselves.

- We will be checking the following condition arr[i] <= 2*arr[j] where i is the pointer in the Left Array and j is the pointer in the Right Array.

- If at any point arr[i] <= 2*arr[j] , we add 1 to the answer as this pair has a contribution to the answer.

- If Left Array gets exhausted before Right Array we keep on adding the distance j pointer traveled as both the arrays are Sorted so the next ith element from Left Subarray will equally contribute to the answer.

### Implementation

- We first of all call a Merge Sort function, in that we recursively call Left Recursion and Right Recursion after that we call Merge function in order to merge both Left and Right Calls we initially made and compute the final answer.

- In the Merge function, we will be using low, mid, high values to count the total number of inversion pairs for the Left half and Right half of the Array.

- Now, after the above task, we need to Merge the both Left and Right sub-arrays using a temporary vector.

- After this, we need to copy back the temporary vector to the Original Array. Then finally we return the number of pairs we counted.
38 changes: 38 additions & 0 deletions Day03/Reverse Pairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
int count = 0;

void merge(vector<int> &nums, int start, int end, int mid)
{
int l = start;
int r = mid + 1;

while (l <= mid and r <= end)
{
if((long)nums[l] > (long) 2 * nums[r])
{
count += (mid + 1 - l);
r++;
}
else
{
l++;
}
}

sort(nums.begin() + start, nums.begin() + end + 1);
}

void merge_sort(vector<int> &nums, int start, int end)
{
if(start == end) return;

int mid = (start + end) / 2;
merge_sort(nums, start, mid);
merge_sort(nums, mid + 1, end);

merge(nums, start, end, mid);
}
int reversePairs(vector<int>& nums) {
count = 0;
merge_sort(nums, 0, nums.size() - 1);
return count;
}
45 changes: 45 additions & 0 deletions Day03/Search a 2D Matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int n = matrix.size();
int m = matrix[0].size();
int i, j;
int low = 0;
int high = n - 1;

while(low <= high) {

int mid = (low + high) / 2;
if(matrix[mid][0] == target) {
return true;
}
if(matrix[mid][m - 1] == target) {
return true;
}
if(matrix[mid][0] < target and matrix[mid][m - 1] > target) {
int left = 0;
int right = m - 1;
int row = mid;

while(left <= right) {
int middle = (left + right) / 2;

if(matrix[row][middle] == target) {
return true;
}
if(matrix[row][middle] < target) {
left = middle + 1;
}
if(matrix[row][middle] > target) {
right = middle - 1;
}
}
return false;
}
if(matrix[mid][0] < target) {
low = mid + 1;
}
if(matrix[mid][0] > target) {
high = mid - 1;
}
}
return false;
}
11 changes: 11 additions & 0 deletions Day03/Unique Paths.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int uniquePaths(int m, int n) {
int N = m + n - 2;
int R = m - 1;

double ans = 1;

for(int i = 1; i <= R; i++) {
ans = ans * (N - R + i) / i;
}
return int(ans);
}

0 comments on commit 6ac8b6b

Please sign in to comment.