Skip to content

Commit b819ba0

Browse files
authored
Merge branch 'main' into 1857_Largest_Color_Value_in_a_Directed_Graph
2 parents 5f2b277 + 553e9ff commit b819ba0

15 files changed

+449
-0
lines changed

1094.Car Pooling.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
bool carPooling(vector<vector<int>>& trips, int capacity) {
4+
vector<pair<int,int>> pickups;
5+
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> drops;
6+
pickups.reserve(trips.size());
7+
for (auto &t : trips){
8+
pickups.emplace_back(t[1], t[0]);
9+
drops.push({t[2], t[0]});
10+
}
11+
sort(pickups.begin(), pickups.end());
12+
int i = 0, curr = 0;
13+
while (!drops.empty()){
14+
int nextDrop = drops.top().first;
15+
while (i < (int)pickups.size() && pickups[i].first < nextDrop){
16+
curr += pickups[i].second;
17+
if (curr > capacity) return false;
18+
++i;
19+
}
20+
curr -= drops.top().second;
21+
drops.pop();
22+
}
23+
while (i < (int)pickups.size()){
24+
curr += pickups[i].second;
25+
if (curr > capacity) return false;
26+
++i;
27+
}
28+
return true;
29+
}
30+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& a) {
4+
int n = a.size();
5+
vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(2, vector<int>(3, 0)));
6+
for(int i=n-1;i>=0;i--)
7+
{
8+
for(int buy=1;buy>=0;buy--)
9+
{
10+
for(int cap=1;cap<=2;cap++)
11+
{
12+
if (buy) {
13+
// Either buy now, or skip
14+
dp[i][buy][cap] = max(-a[i] + dp[i+1][0][cap],dp[i+1][1][cap]);
15+
} else {
16+
// Either sell now, or skip
17+
dp[i][buy][cap] = max(a[i] +dp[i+1][1][cap-1], dp[i+1][0][cap]);
18+
}
19+
}
20+
}
21+
}
22+
return dp[0][1][2];
23+
}
24+
};

23. Merge k Sorted Lists.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* mergeKLists(vector<ListNode*>& lists) {
14+
ListNode* res = new ListNode(0);
15+
ListNode* temp = res;
16+
17+
priority_queue<int, vector<int>, greater<int>> minHeap;
18+
19+
for (auto l : lists) {
20+
while (l != nullptr) {
21+
minHeap.push(l->val);
22+
l = l->next;
23+
}
24+
}
25+
26+
while (!minHeap.empty()) {
27+
int num = minHeap.top();
28+
minHeap.pop();
29+
30+
temp->next = new ListNode(num);
31+
temp = temp->next;
32+
}
33+
34+
return res->next;
35+
}
36+
};

231. Power of Two.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public:
3+
bool isPowerOfTwo(int n) {
4+
if (n <= 0) return false;
5+
return (n & (n - 1)) == 0;
6+
}
7+
};

242. Valid Anagram.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
bool isAnagram(string s, string t) {
4+
if(s.length() != t.length()) return false;
5+
6+
int count[26] = {0};
7+
8+
for(int i = 0; i < s.length(); i++) {
9+
count[s[i]-'a']++;
10+
count[t[i]-'a']--;
11+
}
12+
13+
for(int i = 0; i < 26; i++) {
14+
if(count[i] != 0) return false;
15+
}
16+
17+
return true;
18+
}
19+
};

2561. Rearranging Fruits.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
3+
class Solution {
4+
public:
5+
long long minCost(vector<int>& basket1, vector<int>& basket2) {
6+
unordered_map<int,int> freq;
7+
for (int x : basket1) freq[x]++;
8+
for (int x : basket2) freq[x]--;
9+
10+
vector<int> extra1, extra2;
11+
int minFruit = INT_MAX;
12+
13+
// Collect unbalanced fruits
14+
for (auto [fruit, diff] : freq) {
15+
minFruit = min(minFruit, fruit);
16+
if (diff % 2 != 0) return -1; // impossible to balance
17+
int count = abs(diff) / 2;
18+
if (diff > 0) while (count--) extra1.push_back(fruit);
19+
else while (count--) extra2.push_back(fruit);
20+
}
21+
22+
sort(extra1.begin(), extra1.end());
23+
sort(extra2.rbegin(), extra2.rend());
24+
25+
long long cost = 0;
26+
for (int i = 0; i < extra1.size(); ++i) {
27+
cost += min((long long)min(extra1[i], extra2[i]), 2LL * minFruit);
28+
}
29+
return cost;
30+
}
31+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int removeDuplicates(vector<int>& nums) {
4+
if (nums.empty()) return 0;
5+
int j = 0;
6+
for (int i = 1; i < nums.size(); i++) {
7+
if (nums[i] != nums[j]) {
8+
j++;
9+
nums[j] = nums[i];
10+
}
11+
}
12+
return j + 1;
13+
}
14+
};

283. Move Zeroes.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
void moveZeroes(vector<int>& nums) {
4+
int j = 0;
5+
for(int i = 0; i < nums.size(); i++) {
6+
if(nums[i] != 0) nums[j++] = nums[i];
7+
}
8+
while(j < nums.size()) nums[j++] = 0;
9+
}
10+
};

292.NimGame.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Includes Soln to 292. Nim Game Problem
2+
// Using trial and error it can be found that you can win the game for any n less than 4 or if n is not divisible by 4
3+
// This code uses the simple observation to solve the problem
4+
5+
6+
class Solution {
7+
public:
8+
bool canWinNim(int n) {
9+
if (n<=3 || n%4!=0) return true;
10+
return false;
11+
}
12+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// Definition for a binary tree node
5+
struct Node {
6+
int data;
7+
Node* left;
8+
Node* right;
9+
Node(int val) {
10+
data = val;
11+
left = right = nullptr;
12+
}
13+
};
14+
15+
class Solution {
16+
int maxSum; // Global maximum sum
17+
// Helper function to compute maximum gain from a node
18+
int findMaxPath(Node* root) {
19+
if (!root) return 0;
20+
21+
int leftGain = max(0, findMaxPath(root->left)); // Max path sum from left
22+
int rightGain = max(0, findMaxPath(root->right)); // Max path sum from right
23+
24+
// Update global maximum including current node
25+
maxSum = max(maxSum, leftGain + rightGain + root->data);
26+
27+
// Return max gain including current node for recursion
28+
return root->data + max(leftGain, rightGain);
29+
}
30+
31+
public:
32+
int maxPathSum(Node* root) {
33+
maxSum = INT_MIN;
34+
findMaxPath(root);
35+
return maxSum;
36+
}
37+
};
38+
39+
// Example usage
40+
int main() {
41+
/*
42+
10
43+
/ \
44+
2 10
45+
/ \ \
46+
20 1 -25
47+
/ \
48+
3 4
49+
Expected Output: 42 (20 + 2 + 10 + 10)
50+
*/
51+
52+
Node* root = new Node(10);
53+
root->left = new Node(2);
54+
root->right = new Node(10);
55+
root->left->left = new Node(20);
56+
root->left->right = new Node(1);
57+
root->right->right = new Node(-25);
58+
root->right->right->left = new Node(3);
59+
root->right->right->right = new Node(4);
60+
61+
Solution sol;
62+
cout << "Maximum Path Sum: " << sol.maxPathSum(root) << endl;
63+
64+
return 0;
65+
}

0 commit comments

Comments
 (0)