Skip to content

Commit 681af0e

Browse files
authored
Merge branch 'main' into 23
2 parents 9b3d283 + 543ad75 commit 681af0e

8 files changed

+156
-75
lines changed

124. Binary Tree Maximum Path Sum.cpp

Lines changed: 0 additions & 30 deletions
This file was deleted.

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+
};

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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
bool hasSameDigits(string s) {
4+
while(s.length() > 2){
5+
string temp = "";
6+
for(int i = 0; i < s.length()-1; i++){
7+
int a = s[i] - '0';
8+
int b = s[i + 1] - '0';
9+
temp.push_back(((a + b) % 10) + '0');
10+
}
11+
s = temp;
12+
}
13+
return s.length() && s[0] == s[1];
14+
}
15+
};

50.Pow(x,n).cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Approach
2+
3+
We use exponentiation by squaring to efficiently compute x^n:
4+
5+
Handle base cases:
6+
7+
If n == 0, return 1.
8+
If x == 0, return 0 (unless n is 0).
9+
Handle negative powers:
10+
11+
If n < 0, compute x^{-n} by inverting x (x = 1/x) and making n positive.
12+
For positive powers:
13+
14+
If n is even: x^n = (x^(n/2)) * (x^(n/2))
15+
If n is odd: x^n = x * (x^(n-1))
16+
Implement iteratively to avoid recursion stack overflow for large n:
17+
18+
While n > 0:
19+
If n is odd, multiply result by x.
20+
Square x and divide n by 2.
21+
Return the accumulated result. */
22+
23+
class Solution {
24+
public:
25+
double myPow(double x, int n) {
26+
long long N = n; // Convert to long long to handle INT_MIN
27+
if (N < 0) { // Handle negative powers
28+
x = 1 / x;
29+
N = -N;
30+
}
31+
32+
double result = 1.0;
33+
while (N > 0) {
34+
if (N % 2 == 1) { // If the current power is odd
35+
result *= x;
36+
}
37+
x *= x; // Square x
38+
N /= 2;
39+
}
40+
41+
return result;
42+
}
43+
};
44+

51.N-Queens.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
void solve(int col, int n, vector<string> &board,
7+
vector<vector<string>> &ans,
8+
vector<int> &leftrow, vector<int> &upperdiag, vector<int> &lowerdiag) {
9+
10+
if (col == n) {
11+
ans.push_back(board);
12+
return;
13+
}
14+
15+
for (int row = 0; row < n; row++) {
16+
if (leftrow[row] == 0 && lowerdiag[row + col] == 0 && upperdiag[(n - 1) + (col - row)] == 0) {
17+
18+
board[row][col] = 'Q';
19+
leftrow[row] = 1;
20+
lowerdiag[row + col] = 1;
21+
upperdiag[(n - 1) + (col - row)] = 1;
22+
23+
solve(col + 1, n, board, ans, leftrow, upperdiag, lowerdiag);
24+
25+
board[row][col] = '.'; // backtrack
26+
leftrow[row] = 0;
27+
lowerdiag[row + col] = 0;
28+
upperdiag[(n - 1) + (col - row)] = 0;
29+
}
30+
}
31+
}
32+
33+
vector<vector<string>> solveNQueens(int n) {
34+
vector<vector<string>> ans;
35+
vector<string> board(n, string(n, '.'));
36+
37+
vector<int> leftrow(n, 0);
38+
vector<int> lowerdiag(2 * n - 1, 0);
39+
vector<int> upperdiag(2 * n - 1, 0);
40+
41+
solve(0, n, board, ans, leftrow, upperdiag, lowerdiag);
42+
return ans;
43+
}
44+
};

987. Vertical Order Traversal of a Binary Tree.cpp

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)