Skip to content
Merged
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
23 changes: 23 additions & 0 deletions JuneChallenge/Week1/1:InvertBinaryTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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:
TreeNode* invertTree(TreeNode* root) {
if (root == nullptr) return root;
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
invertTree(root->left);
invertTree(root->right);
return root;
}
};
19 changes: 19 additions & 0 deletions JuneChallenge/Week1/2:DeleteNodeinaLinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
while(node->next->next != nullptr) {
node->val = node->next->val;
node = node->next;
}
node->val = node->next->val;
node->next = nullptr;
}
};
17 changes: 17 additions & 0 deletions JuneChallenge/Week1/3:TwoCityScheduling.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
static bool comparator(vector<int> a, vector<int> b) {
return (a[0]-a[1]) < (b[0]-b[1]);
}
int twoCitySchedCost(vector<vector<int>>& costs) {
int n = costs.size()/2;
int ans = 0;
sort(costs.begin(), costs.end(), comparator);
for (int i = 0; i < n; i++) {
ans += costs[i][0];
ans += costs[i + n][1];
}
return ans;
}
};

15 changes: 15 additions & 0 deletions JuneChallenge/Week1/4:ReverseString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
void reverseString(vector<char>& s) {
int n = s.size();
int first = 0, second = n - 1;
while (first <= second) {
char temp = s[first];
s[first] = s[second];
s[second] = temp;
first++;
second--;
}
}
};

22 changes: 22 additions & 0 deletions JuneChallenge/Week1/5:RandomPickwithWeight.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
vector<int> weight;
int total = 0;
Solution(vector<int>& w) {
srand(time(NULL));
for (int i = 0; i < w.size(); i++) {
total += w[i];
weight.push_back(total);
}
}
int pickIndex() {
int ind = rand() % weight.back();
return upper_bound(weight.begin(), weight.end(), ind) - weight.begin();
}
};

/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(w);
* int param_1 = obj->pickIndex();
*/
17 changes: 17 additions & 0 deletions JuneChallenge/Week1/6:QueueReconstructionbyHeight.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
static bool comparator(vector<int> a, vector<int> b){
if (a[0] == b[0])
return a[1] > b[1];
return a[0] < b[0];
}
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(), people.end(), comparator);
vector <vector<int>> ans;
for(int i = people.size() - 1; i >= 0; i--){
ans.insert(ans.begin() + people[i][1], people[i]);
}
return ans;
}
};

13 changes: 13 additions & 0 deletions JuneChallenge/Week1/7:CoinChange2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
int change(int amount, vector<int>& coins) {
int n = coins.size();
vector<int> dp(amount + 1, 0);
dp[0] = 1;
for(int i = 0; i < n; i++)
for(int j = coins[i]; j <= amount; j++)
dp[j] += dp[j - coins[i]];
return dp[amount];
}
};

50 changes: 33 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@

## My Leetcode submissions, primarily in C++

- Files contain only the solution class provided by the platform.
- Files contain only the solution class provided by the platform.

- I make all submissions from [this profile](https://leetcode.com/thepushkarp/).
- I make all submissions from [this profile](https://leetcode.com/thepushkarp/).

## Contents

- [leetcode](#leetcode)
- [My Leetcode submissions, primarily in C++](#my-leetcode-submissions-primarily-in-c)
- [Contents](#contents)
- [April 30 Day Challenge](#april-30-day-challenge)
- [Week 1](#week-1)
- [Week 2](#week-2)
- [Week 3](#week-3)
- [Week 4](#week-4)
- [Week 5](#week-5)
- [May Challenge](#may-challenge)
- [Week 1](#week-1-1)
- [Week 2](#week-2-1)
- [Week 3](#week-3-1)
- [Week 4](#week-4-1)
- [Week 5](#week-5-1)
- [leetcode](#leetcode)
- [My Leetcode submissions, primarily in C++](#my-leetcode-submissions-primarily-in-c)
- [Contents](#contents)
- [April 30 Day Challenge](#april-30-day-challenge)
- [Week 1](#week-1)
- [Week 2](#week-2)
- [Week 3](#week-3)
- [Week 4](#week-4)
- [Week 5](#week-5)
- [May Challenge](#may-challenge)
- [Week 1](#week-1-1)
- [Week 2](#week-2-1)
- [Week 3](#week-3-1)
- [Week 4](#week-4-1)
- [Week 5](#week-5-1)
- [June Challenge](#june-challenge)
- [Week 1](#week-1-2)

## April 30 Day Challenge

Expand Down Expand Up @@ -140,3 +142,17 @@ Since I am completing this in May, I could not access the 7th question of each w
| 29 | [Course Schedule](MayChallenge/Week5/29:CourseSchedule.cpp) |
| 30 | [K Closest Points to Origin](MayChallenge/Week5/30:KClosestPointstoOrigin.cpp) |
| 31 | [Edit Distance](MayChallenge/Week5/31:EditDistance.cpp) |

## June Challenge

### Week 1

| Day | Name |
| --- | --------------------------------------------------------------------------------------- |
| 1 | [Invert Binary Tree](JuneChallenge/Week1/1:InvertBinaryTree.cpp) |
| 2 | [Delete Node in a Linked List](JuneChallenge/Week1/2:DeleteNodeinaLinkedList.cpp) |
| 3 | [Two City Scheduling](JuneChallenge/Week1/3:TwoCityScheduling.cpp) |
| 4 | [Reverse String](JuneChallenge/Week1/4:ReverseString.cpp) |
| 5 | [Random Pick with Weight](JuneChallenge/Week1/5:RandomPickwithWeight.cpp) |
| 6 | [Queue Reconstruction by Height](JuneChallenge/Week1/6:QueueReconstructionbyHeight.cpp) |
| 7 | [Coin Change 2](JuneChallenge/Week1/7:CoinChange2.cpp) |