-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f964c94
commit 8284765
Showing
3 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
class Solution { | ||
public: | ||
int numBusesToDestination(vector<vector<int>>& routes, int source, int target) { | ||
int n = routes.size(); | ||
int level = 0; | ||
|
||
unordered_map<int, vector<int>> m; // store - bus stop no. -> bus numbers list | ||
|
||
for(int i=0; i<n; i++) { | ||
for(int j=0; j<routes[i].size(); j++) { | ||
int busStopNo = routes[i][j]; | ||
int busNo = i; | ||
m[busStopNo].push_back(busNo); | ||
} | ||
} | ||
|
||
// bfs | ||
queue<int> queue; | ||
unordered_set<int> busStopVisited; | ||
unordered_set<int> busVisited; | ||
|
||
queue.push(source); | ||
busStopVisited.insert(source); | ||
|
||
while(!queue.empty()) { | ||
int size = queue.size(); | ||
while(size--) { | ||
int rem = queue.front(); | ||
queue.pop(); | ||
|
||
if(rem == target) { | ||
return level; | ||
} | ||
|
||
vector<int> buses = m[rem]; | ||
for(int bus: buses) { | ||
if(busVisited.find(bus) != busVisited.end()) { | ||
continue; | ||
} | ||
|
||
vector<int> arr = routes[bus]; | ||
|
||
for(int busStop: arr) { | ||
if(busStopVisited.find(busStop) != busStopVisited.end()) { | ||
continue; | ||
} | ||
|
||
queue.push(busStop); | ||
busStopVisited.insert(busStop); | ||
} | ||
busVisited.insert(bus); | ||
} | ||
} | ||
level += 1; | ||
} | ||
|
||
return -1; | ||
|
||
} | ||
}; | ||
|
||
// Time Complexity - O(N) | ||
// Space Complexity - O(N), since we're using a map to keep track of bus and busStop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
class Solution { | ||
public: | ||
int cherryPickup(vector<vector<int>>& grid) { | ||
int r = grid.size(); | ||
int c = grid[0].size(); | ||
vector<vector<vector<int>>> dp(r, vector<vector<int>>(c, vector<int>(c, -1))); | ||
return func(0, 0, c-1, r, c, grid, dp); | ||
} | ||
|
||
int func(int i, int j1, int j2, int r, int c, vector<vector<int>> &grid, vector<vector<vector<int>>> &dp) { | ||
if(i < 0 || j1 < 0 || j2 < 0 || j1 >= c || j2 >= c) { | ||
return -1e5; | ||
} | ||
|
||
if(dp[i][j1][j2] != -1) { | ||
return dp[i][j1][j2]; | ||
} | ||
|
||
if(i == r -1 ) { | ||
if(j1 == j2) { | ||
return grid[i][j1]; | ||
} else { | ||
return grid[i][j1] + grid[i][j2]; | ||
} | ||
} | ||
|
||
// explore all paths of alice and bob simultaneously | ||
int maxi = -1e5; | ||
for(int dj1 = -1; dj1 <= +1; dj1++) { | ||
for(int dj2 = -1; dj2 <= +1; dj2++) { | ||
int value = 0; | ||
if(j1 == j2) { | ||
value = grid[i][j1]; | ||
} else { | ||
value = grid[i][j1] + grid[i][j2]; | ||
} | ||
|
||
value += func(i+1, j1 + dj1, j2 + dj2, r, c, grid, dp); | ||
maxi = max(maxi, value); | ||
} | ||
} | ||
return dp[i][j1][j2] = maxi; | ||
} | ||
}; | ||
|
||
|
||
// Rules | ||
// 1. Express everything in terms (i1, j1) && (i2, j2); | ||
// 2. Explore all the paths (i+1, j-1), (i + 1, j), (i + 1, j + 1) | ||
// 3. Give maximum sum | ||
|
||
// Fixed starting point variable ending point | ||
// (0, 0) (0, m-1) (set any column in the last row) | ||
|
||
// (all paths by Alice) + (all paths by Bob) | ||
// (recursion) (recursion) | ||
|
||
// f(i1, j1, i2, j2) | ||
// f(0, 0, 0, m-1); | ||
// alice bob | ||
|
||
// base case | ||
// 1. Destination base case | ||
// 2. Out of bounds case | ||
|
||
// f(i, j1, j2) // i1 == i2; | ||
// { | ||
// if(i1 < 0 || j1 >= m || j2 < 0 || j2 >= m) { | ||
// return -1e8; // not INT_MIN (since it can go out of bounds) | ||
// } | ||
|
||
// if(dp[i][j1][j2] != -1) { | ||
// return dp[i][j1][j2]; | ||
// } | ||
|
||
// if(i == n-1) { | ||
// if(j1 == j2) { | ||
// return a[i][j1]; | ||
// } else { | ||
// return a[i][j1] + a[i][j2]; | ||
// } | ||
// } | ||
|
||
// } | ||
|
||
// 3 * 3 = 9 combos of paths | ||
// for each movement of alice, there are 3 moves for Bob | ||
|
||
// dj[] = {-1, 0, +1}; | ||
// fun(del1() -> -1 -> +1) | ||
// fun(del2() -> -1 -> +1) | ||
|
||
// Explore all paths Alice & Bob can go together | ||
|
||
// func(){ | ||
// for(dj1 -> -1 -> +1) { | ||
// for(dj2 -> -1 -> +1) { | ||
// if(j1 == j2) { | ||
// maxi = max(maxi, a[i][j1] + f(i + 1, j1 + dj1, j2 + dj2)); | ||
// } else { | ||
// maxi = max(maxi, a[i][j1] + a[i][j2] + f(i + 1, j1 + dj1, j2 + dj2)); | ||
// } | ||
// } | ||
// } | ||
// dp[i][j1][j2] = maxi; | ||
// return maxi; | ||
// } | ||
|
||
// Recursion - TC -> (3^n X 3^n), exponential | ||
// SC -> O(N), auxillary stack space | ||
|
||
// Optimize - find overlapping sub problems | ||
// Memoization | ||
|
||
// i - N | ||
// j1 - M | ||
// j2 - M | ||
|
||
// dp[i][j1][j2] = maxi; | ||
|
||
// Memoization - TC - O(N * M * M) * 9 | ||
// Space Complexity - O(N * M * M) * O(N), O(N) is the Auxillary stack space | ||
|
||
|
45 changes: 45 additions & 0 deletions
45
LeetCode/Algorithms/Hard/FindTheMaximumSumOfNodeValues.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
class Solution { | ||
public: | ||
long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) { | ||
// When would XOR increase a value? | ||
// What if it increases one but decreases other? | ||
// One num can be XOR'd multiple times | ||
// Does a traversal make sense? | ||
|
||
// XOR is not a blackbox, n ^ k ^ k = n | ||
// Thus we can (and must) XOR any two nodes at a time | ||
// Greedy, sort by XOR delta | ||
|
||
long long result = 0; | ||
vector<long long> delta; | ||
for(long long i=0; i<nums.size(); i++) { | ||
result += nums[i]; | ||
delta.push_back((nums[i] ^ k) - nums[i]); | ||
} | ||
|
||
sort(delta.begin(), delta.end(), greater<>()); | ||
for(long long i=0; i<delta.size(); i+= 2) { | ||
if(i == delta.size() - 1) { | ||
break; | ||
} | ||
long long path_delta = delta[i] + delta[i + 1]; | ||
if(path_delta <= 0) { | ||
break; | ||
} | ||
result += path_delta; | ||
} | ||
|
||
return result; | ||
} | ||
}; | ||
|
||
// Tree is a connected graph that doesn't have cycles in it | ||
|
||
// n ^ k ^ k = n | ||
|
||
// either you leave the node as it is or XOR nodes twice | ||
|
||
// maximize the total sum of values | ||
|
||
// Time Complexity - O(NlogN) | ||
// Space Complexity - O(N), extra space |