Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 10 new solutions of easy and medium level #288

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Easy/1-two-sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> ans;

for(int i=0; i<nums.size(); i++) {
for(int j=i+1; j<nums.size(); j++) {
if(nums[i] + nums[j] == target) {
ans.push_back (i);
ans.push_back (j);
}
}
}

return ans;
}
};
82 changes: 82 additions & 0 deletions Easy/2-add-two-numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/

class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {

int carry = 0;
int sum;
int size = 0;

ListNode* answer = new ListNode();

if(l1->val == 0 && l1->next == nullptr){
answer = l2;
}
else if(l2->val == 0 && l2->next == nullptr){
answer = l1;
}
else{
while(true){

sum = l1->val + l2->val + carry;
carry = sum/10;
addElementToLinkedList(answer,sum%10,size);

if(l1->next == nullptr && l2->next == nullptr){
if(carry>0){
addElementToLinkedList(answer,carry,100);
}
break;
}

if(l1->next != nullptr){
l1 = l1->next;
}
else{
l1->val = 0;
}

if(l2->next != nullptr){
l2 = l2->next;
}
else{
l2->val = 0;
}
size++;

}
}

return answer;

}

void addElementToLinkedList(ListNode* head,int value,int size){

ListNode* cur = head;

if(size==0){
head->val = value;
return;
}

while(cur->next != nullptr){
cur = cur->next;
}
ListNode* newNode = new ListNode(value);
cur->next = newNode;
return;
}


};
13 changes: 13 additions & 0 deletions Easy/26-remove-duplicates-from-sorted-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
for(int i=0; i<nums.size()-1;) {
if(nums[i] == nums[i+1])
nums.erase (nums.begin()+i);
else
i++;
}

return nums.size();
}
};
11 changes: 11 additions & 0 deletions Easy/29-divide-two-integers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public:
int divide(int dividend, int divisor) {

if(dividend==INT_MIN and divisor==-1)
return INT_MAX;
else
return (dividend/divisor);

}
};
29 changes: 29 additions & 0 deletions Easy/50-powx-n.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
double myPow(double x, int n) {
// For negative power (n<0)
if(n<0) {
if(n==-1)
return 1/x;
double ans = myPow(x, n/2); // Recursive call
if(n&1)
return (1/x)*ans*ans;
else
return ans*ans;
}

// For positive power (n>0)
if(n>0) {
if(n==1)
return x;
double ans = myPow(x, n/2); // Recursive call
if(n&1)
return x*ans*ans;
else
return ans*ans;
}

// For zero power (n==0)
return 1;
}
};
38 changes: 38 additions & 0 deletions Medium/160-intersection-of-two-linked-lists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *a=headA, *b=headB;
int count1 = 0, count2 = 0;

for(ListNode *curr=headA; curr!=NULL; curr=curr->next)
count1++;

for(ListNode *curr=headB; curr!=NULL; curr=curr->next)
count2++;

while(count1 > count2) {
count1--;
a = a->next;
}

while(count2 > count1) {
count2--;
b = b->next;
}

while(a != b) {
a = a->next;
b = b->next;
}

return a;
}
};
27 changes: 27 additions & 0 deletions Medium/240-search-a-2d-matrix-ii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int row = matrix.size();
int col = matrix[0].size();
int rowCount = 0;
int colCount = col - 1;

while((rowCount < row) && (colCount >= 0)) {
int element = matrix[rowCount][colCount];

if(element == target) {
return true;
}

if(element > target) {
colCount--;
}

if(element < target) {
rowCount++;
}
}

return false;
}
};
33 changes: 33 additions & 0 deletions Medium/659-split-array-into-consecutive-subsequences.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public:

bool isPossible(vector<int>& nums) {
unordered_map<int,int> freq;

for(int x:nums) {
freq[x]++;
}

unordered_map<int,int> need;

for(int n:nums){
if(freq[n]==0) {
continue;
} else if(need[n]>0){
need[n]--;
freq[n]--;
need[n+1]++;
} else if(freq[n]>0 && freq[n+1]>0 && freq[n+2]>0) {
freq[n]--;
freq[n+1]--;
freq[n+2]--;

need[n+3]++;
} else {
return false;
}
}

return true;
}
};
13 changes: 13 additions & 0 deletions Medium/858-mirror-reflection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
int mirrorReflection(int p, int q) {
while(p%2==0 && q%2==0) {
p/=2;
q/=2;
}

int ans = 1 - (p%2) + (q%2);

return ans;
}
};
58 changes: 58 additions & 0 deletions Medium/994-rotting-oranges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
queue<vector<int> > q;
int time = 0;
int count = 0;
int orange = 0;

int m = grid.size();
int n = grid[0].size();

for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
if(grid[i][j] == 1 || grid[i][j] == 2)
orange++;

if(grid[i][j] == 2) {
q.push({i,j});
}
}
}

if(orange == 0)
return 0;

if(q.size() == 0)
return -1;

int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

while(!q.empty()) {
int k = q.size();
count += k;
while(k--) {
vector<int> curr = q.front();
q.pop();
int x = curr[0];
int y = curr[1];

for(int i=0; i<4; i++) {
int nx = x+dx[i];
int ny = y+dy[i];

if(nx<0 || nx>=m || ny<0 || ny>=n || grid[nx][ny] != 1)
continue;

grid[nx][ny] = 2;
q.push({nx,ny});
}
}
if(!q.empty())
time++;
}

return count==orange ? time : -1;
}
};