Skip to content

Commit 0ed9758

Browse files
authored
Oct 2025 5 (#115)
* add changes * add changes * add changes * add changes * add changes * add changes * add change 3 * add change 4 * add change 5 * add changes 6
1 parent 555a12c commit 0ed9758

15 files changed

+940
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class DetectSquares {
2+
public:
3+
unordered_map<vector<int>, int> ptsCount;
4+
vector<vector<int>> points;
5+
DetectSquares() {
6+
7+
}
8+
9+
void add(vector<int> point) {
10+
ptsCount[point]++;
11+
points.push_back(point);
12+
}
13+
14+
int count(vector<int> point) {
15+
int result = 0;
16+
int px = point[0];
17+
int py = point[1];
18+
19+
for(auto &ele: points) {
20+
int x = ele[0];
21+
int y = ele[1];
22+
if(abs(py - y) != abs(px - x) or x == px or y == py) {
23+
continue;
24+
}
25+
26+
result += ptsCount[{x, py}] * ptsCount[{px, y}];
27+
}
28+
return result;
29+
}
30+
};
31+
32+
/**
33+
* Your DetectSquares object will be instantiated and called as such:
34+
* DetectSquares* obj = new DetectSquares();
35+
* obj->add(point);
36+
* int param_2 = obj->count(point);
37+
*/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution {
2+
public:
3+
4+
5+
int maximumDetonation(vector<vector<int>>& bombs) {
6+
7+
vector<vector<int>> adj(bombs.size());
8+
9+
for(int i=0; i<bombs.size(); i++) {
10+
for(int j=i + 1; j<bombs.size(); j++) {
11+
auto &temp = bombs[i];
12+
int x1 = temp[0];
13+
int y1 = temp[1];
14+
int r1 = temp[2];
15+
16+
auto &temp2 = bombs[j];
17+
int x2 = temp2[0];
18+
int y2 = temp2[1];
19+
int r2 = temp2[2];
20+
21+
int d = sqrt((x1 - x2)**2 + (y1 - y2)**2);
22+
23+
if(d <= r1) {
24+
adj[i].push_back(j); // bomb at index i can detonate bomb at index j
25+
}
26+
if(d <= r2) {
27+
adj[j].push_back(i); // bomb at index j can detonate bomb at index i
28+
}
29+
}
30+
}
31+
32+
int result = 0;
33+
unordered_set<int> set;
34+
for(int i=0; i<bombs.size(); i++) {
35+
36+
result = max(res, dfs(i, visit));
37+
}
38+
}
39+
40+
dfs(int i, unordered_set<int> &visit) {
41+
if(visit.find(i) != visit.end()) {
42+
return 0;
43+
}
44+
45+
visit.insert(i);
46+
for(auto nei: adj[i]) {
47+
dfs(nei, visit);
48+
}
49+
return len(visit);
50+
}
51+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Solution {
2+
public:
3+
int closestMeetingNode(vector<int>& edges, int node1, int node2) {
4+
int n = edges.size();
5+
vector<vector<int>> adj(n);
6+
7+
// some of the destination nodes can be -1
8+
// it would also get added in the distance map in bfs traversal
9+
// it won't affect our result because we are traversing
10+
// from 0 to n indexes only
11+
for(int i=0; i<edges.size(); i++) {
12+
adj[i].push_back(edges[i]);
13+
}
14+
15+
unordered_map<int, int> node1Dist; // map node -> distance from node1
16+
unordered_map<int, int> node2Dist; // map node -> distance fromm node2
17+
18+
bfs(node1, node1Dist, adj);
19+
bfs(node2, node2Dist, adj);
20+
21+
int res = -1;
22+
int resDist = INT_MAX;
23+
24+
for(int i=0; i<n; i++) {
25+
if(node1Dist.count(i) && node2Dist.count(i)) {
26+
dist = max(node1Dist[i] node2Dist[i]);
27+
if(dist < resDist) { // we have to take the smallest distance node and return the result node
28+
res = i;
29+
resDist = dist;
30+
}
31+
}
32+
}
33+
return res;
34+
}
35+
36+
void bfs(int src, unordered_map<int, int> distMap, vector<vector<int>> &adj) {
37+
queue<pair<int, int>> q;
38+
q.push({src, 0});
39+
distMap[src] = 0;
40+
while(!q.empty()) {
41+
auto temp = q.front();
42+
int node = temp.first;
43+
int dist = temp.second;
44+
45+
for(auto &nei: adj[node]) {
46+
if(distMap.find(i) == distMap.end()) {
47+
q.push({nei, dist + 1});
48+
distMap[nei] = dist + 1;
49+
}
50+
}
51+
}
52+
}
53+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int maxEqualRowsAfterFlips(vector<vector<int>>& matrix) {
4+
unordered_map<string, int> m;
5+
int count = 0;
6+
for(auto &mat: matrix) {
7+
string s = "";
8+
for(auto &ele: mat) {
9+
s += to_string(ele);
10+
}
11+
12+
if(s[0] == '1') {
13+
for(int i=0; i<s.size(); i++) {
14+
if(s[i] == '0') {
15+
s[i] = '1';
16+
} else {
17+
s[i] = '0';
18+
}
19+
}
20+
}
21+
22+
m[s] += 1;
23+
}
24+
25+
for(auto &ele: m) {
26+
count = max(count, ele.second);
27+
}
28+
return count;
29+
}
30+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Solution {
2+
public:
3+
#define ll long long
4+
long long gridGame(vector<vector<int>>& grid) {
5+
int n = grid[0].size();
6+
if(n < 2) return 0;
7+
8+
ll minSum = LONG_MAX; // remember this is LONG_MAX
9+
ll bottomSum = 0;
10+
ll topSum = accumulate(grid[0].begin(), grid[0].end(), 0LL);
11+
12+
for(int i=0; i<n; i++) {
13+
topSum -= grid[0][i];
14+
minSum = min(minSum, max(topSum, bottomSum));
15+
bottomSum += grid[1][i];
16+
}
17+
return minSum;
18+
}
19+
};
20+
21+
// Time Complexity - O(N)
22+
// Space Complmexity - O(1), since we're using variables
23+
24+
/*
25+
26+
Required time complexity - < N^2
27+
28+
Note: If a robo comes down then it can't go up.
29+
30+
Robo 1 choses a path which minimizes the sum for robo-2
31+
Return the sum for Robo - 2 (it also tries to maximize the sum)
32+
33+
Optimal strategy ->
34+
35+
36+
Robo-2 picks = max(top sum, bottom sum)
37+
38+
Robo-1 will choose a partition point which
39+
minimizes the value for Robo-2 pick
40+
41+
for(pp=0; pp<n; ++pp) {
42+
topSum += grid[0][pp];
43+
minSum = min(minSum, max(topSum, bottomSum));
44+
bottomSum -= grid[1][pp];
45+
}
46+
47+
48+
Available Option
49+
50+
TopSum = 10
51+
minSum = INT_MAX
52+
BottomSum = 0
53+
54+
Time Complexity - O(N)
55+
Space Complmexity - O(1), since we're using variables
56+
*/
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
class Solution {
2+
public:
3+
int longestNiceSubarray(vector<int>& nums) {
4+
int left =0 ;
5+
int right = 0;
6+
int max_window_size = 0;
7+
int curr_sum = 0;
8+
int curr_xor = 0;
9+
int n = nums.size();
10+
11+
while(left <= right && right < n) {
12+
curr_sum += nums[right];
13+
curr_xor ^= nums[right];
14+
15+
while(curr_xor != curr_sum) {
16+
curr_sum -= nums[left];
17+
curr_xor ^= nums[left];
18+
left++;
19+
}
20+
21+
max_window_size = max(max_window_size, right - left + 1);
22+
right++;
23+
}
24+
return max_window_size;
25+
}
26+
};
27+
28+
/*
29+
30+
Time Complexity - O(N)
31+
Space Complexity - O(1)
32+
33+
Bitwise AND
34+
35+
x ---> 8 ----> 1000
36+
& y ---> 6 ----> 0110
37+
? 0 0000
38+
39+
If not set bits occur at same position then Bitwise AND is always 0
40+
41+
x -> 10 ---> 1010
42+
y -> 6 ---> & 0110
43+
0010 (non-zero)
44+
45+
Bitwise and with sum
46+
47+
If x and y have no set bits at the same position then
48+
49+
x ^ y = x + y , this means Bitwise and will be 0
50+
51+
|
52+
(10) x ---> 1010 (10) x ---> 1010
53+
(4) y ---> ^ 0100 (6) y ---> ^ 0110
54+
(14) x^y ---> 1110 (12) x^y ---> 1100 (not equal)
55+
Equal
56+
57+
10 + 6 = 16
58+
59+
Note: If x and y have corresponding set bits at the same position then XOR
60+
will make it 0 in result
61+
62+
// XOR decreases value in this case
63+
64+
Nice Subarray
65+
66+
Goal: find longest subarray starting at index 0
67+
0 1 2 3
68+
[8, 5, 2, 6]
69+
70+
71+
8 4 2 1
72+
8 - 1 0 0 0
73+
5 - 0 1 0 1
74+
2 - 0 0 1 0
75+
6 - 0 1 1 0
76+
77+
curr_sum = 0 -> 8 -> 13 -> 15 -> 21 - 8 = 13 - 5 = 8 - 2 = 6
78+
xor_sum = 0 -> 8 -> 13 -> 15 -> 9 -> 1 (xor 5) -> 4 (xor 2) -> 6 (both equal)
79+
80+
when it seases to be valid, remove 8 from 21 (take xor with 8 and minus 8 from sum)
81+
82+
8 ^ 8 = 0
83+
84+
1 1 0 1
85+
0 0 1 0 ^
86+
1 1 1 1
87+
0 1 1 0 ^
88+
1 0 0 1
89+
90+
1 0 0 1 - 9
91+
1 0 0 0 - 8 (xor)
92+
0 0 0 1 - 1
93+
0 1 0 1 - 5 (xor)
94+
0 1 0 0 - 4
95+
0 0 1 0 -> 2 (xor)
96+
0 1 1 0 -> 6
97+
98+
max_window_size
99+
curr_sum = 0
100+
xor_sum = 0
101+
102+
while(left <= right && right < n) {
103+
curr_sum += a[right];
104+
curr_xor ^= a[right];
105+
106+
if(curr_sum == curr_xor) {
107+
right++;
108+
} else {
109+
curr_sum -= a[left];
110+
curr_xor ^= a[left];
111+
left++;
112+
}
113+
114+
max_window_size = max(max_window_size, right -left + 1);
115+
}
116+
*/
117+

0 commit comments

Comments
 (0)