Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# These are supported funding model platforms

github: [codedecks-in]
custom: ["https://paypal.me/codedecks"]
18 changes: 18 additions & 0 deletions .github/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Configuration for behaviorbot - https://github.com/behaviorbot/

# Comment to be posted to on first time issues
newIssueWelcomeComment: >
# Thanks for helping us improve and opening your first issue here! Don't forget to give us a 🌟 to support us.

While you're waiting, I just wanted to make sure you've had a chance to look at our
[Readme](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/README.md) and [Pull Request Guidelines](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/PULL_REQUEST_PROCESS.md).

# Comment to be posted to on PRs from first time contributors in your repository
newPRWelcomeComment: >
I can tell this is your first pull request! Thank you I'm so honored. :tada::tada::tada:
I'll take a look at it ASAP!

# Comment to be posted to on pull requests merged by a first time user
firstPRMergeComment: >
Your code looks great! Congrats, I've gone ahead and merged your first pull request! Keep it up!
![alt text](https://media.giphy.com/media/d31w24psGYeekCZy/giphy.gif "WOOHOO")
13 changes: 0 additions & 13 deletions .github/workflows/greetings.yml

This file was deleted.

33 changes: 33 additions & 0 deletions C++/Baseball-Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public:
int calPoints(vector<string>& ops) {
stack<int> s;
auto it=ops.begin();
while(it!=ops.end()){
if(*it=="+"){ //if char is + then new record is sum of last two records
int val1=s.top();
s.pop();
int val2=s.top();
s.push(val1);
s.push(val1+val2);
}
else if(*it=="D"){ //if char is D then new record is twice the last record
s.push(2*s.top());
}
else if(*it=="C"){ //if char is C then the last record is invalidated , hence popped
s.pop();
}
else{ // if none of these conditions occur then just push the new record to stack
s.push(stoi(*it));
}
it++;
}
int count=0;
while(!s.empty()) //iteratively pop the top value of the stack and add it to the total
{
count+=s.top();
s.pop();
}
return count;
}
};
32 changes: 32 additions & 0 deletions C++/Best-Time-to-Buy-and-Sell-Stock-II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*Runtime: 8 ms, faster than 96.30% of C++ online submissions for Best Time to Buy and Sell Stock II.
Memory Usage: 13.4 MB, less than 5.04% of C++ online submissions for Best Time to Buy and Sell Stock II.
*/

class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int sum = 0;
//If the number of element in the array are zero or one just return zero.
if (prices.size() == 1 or prices.size() == 0)
{
return 0;
}

//Traverse the array and compare the consecutive two elements .
for (int i = 0; i < prices.size() - 1; i++)
{
// If first consecuitve element is less than second subtract both and add in the sum varibale.
if (prices[i] < prices[i + 1])
{
sum += prices[i + 1] - prices[i];

}
}

//Finally return the sum.
return sum;

}
};
34 changes: 34 additions & 0 deletions C++/Binary-Tree-Cameras.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int ans=0;
unordered_set<TreeNode*> covered;
void dfs(TreeNode* root,TreeNode* parent){
if(root==NULL)
return;
dfs(root->left,root);
dfs(root->right,root);
if((parent==NULL && covered.find(root)==covered.end()) || covered.find(root->left)==covered.end() || covered.find(root->right)==covered.end()){
ans++;
covered.insert(root);
covered.insert(parent);
covered.insert(root->left);
covered.insert(root->right);
}
}
int minCameraCover(TreeNode* root) {
covered.insert(NULL);
dfs(root,NULL);
return ans;
}
};
47 changes: 47 additions & 0 deletions C++/Binary-Tree-Maximum-Path-Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*
* Runtime : 40ms
* Memory : 28.5 MB
*
*
*/
class Solution {
public:

int solve(TreeNode* root, int &ans)
{
if(root == NULL)
return 0;

# to traverse left and right childs
int l = solve(root->left, ans);
int r = solve(root->right, ans);

/* to find max sum including root_val and left and child subtree
max sum or just including root val if result from subtree is - negative
*/
int temp = max(max(l,r)+root->val, root->val);

/* in case max sum does not include main root and
forms maximum sum path through sub tree
*/
int res = max(temp, root->val+l+r);
ans = max(res,ans);
return temp;
}
int maxPathSum(TreeNode* root) {
int ans = INT_MIN;
solve(root,ans);
return ans;

}
};
64 changes: 64 additions & 0 deletions C++/Binary-Tree-Zigzag-Level-Order-Traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> finalvec;
int flag = 1;
TreeNode *t;
stack<TreeNode*>s1;
stack<TreeNode*>s2;
if(root==NULL)
return finalvec;
s1.push(root);
// vector<int>temp;
// temp.push_back(root->val);
// finalvec.push_back(temp);
while(!s1.empty() || !s2.empty())
{
if(flag == 1)
{
vector<int>temp;
while(!s1.empty())
{
t = s1.top();
s1.pop();
temp.push_back(t->val);
if(t->left)
s2.push(t->left);
if(t->right)
s2.push(t->right);

}
finalvec.push_back(temp);
flag = 0;
}
else
{
vector<int>temp;
while(!s2.empty())
{
t = s2.top();
s2.pop();
temp.push_back(t->val);
if(t->right)
s1.push(t->right);
if(t->left)
s1.push(t->left);

}
finalvec.push_back(temp);
flag = 1;
}
}

return finalvec;
}
};
33 changes: 33 additions & 0 deletions C++/BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//Problem Statement: Binary Search

// Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.


//Solution:
class Solution {
public:
int search(vector<int>& nums, int target) {

int si=0, ei=nums.size()-1;

while(si <= ei) {
// index of the middle element
int mid = si + (ei-si)/2;

// found target element
if(nums[mid] == target) {
return mid;
} else if(nums[mid] < target) { // target on right side of middle element
si += 1;
} else { // target on left side of middle element
ei -= 1;
}
}

// target element not present in given array
return -1;
}
};

// Time Complexity: O(log(n))

39 changes: 39 additions & 0 deletions C++/Cherry-Pickup-II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
private:
// Let's run a simple dfs. If we come to the same cell with two robots, let's subtract that value.
int dfs(int n, int m, int r, int c1, int c2, unordered_map<pair<int, pair<int, int>>, int> &dp, vector<vector<int>> &grid) {
if (c1 < 0 || c1 >= m || c2 < 0 || c2 >= m) {
return 0;
} else if (r == n) {
return 0;
} else if (dp.count({r, {c1, c2}}) > 0) {
return dp[{r, {c1, c2}}];
} else {
int current_ceil = grid[r][c1] + grid[r][c2];

// Here is that subtraction
if (c1 == c2) {
current_ceil -= grid[r][c2];
}

// Running dfs from all possible cells
int mx = 0;
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
mx = max(mx, dfs(n, m, r + 1, c1 + i, c2 + j, dp, grid));
}
}

return dp[{r, {c1, c2}}] = current_ceil + mx;
}
}
public:
int cherryPickup(vector<vector<int>>& grid) {
unordered_map<pair<int, pair<int, int>>, int> dp;

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

return dfs(n, m, 0, 0, m - 1, dp, grid);
}
};
Loading