From 8284765c58b1517ceb020cf51e9622b2c21b0ac1 Mon Sep 17 00:00:00 2001 From: Afroz Chakure Date: Sun, 27 Oct 2024 19:16:39 +0530 Subject: [PATCH] add changes (#98) --- LeetCode/Algorithms/Hard/BusRoutes.cpp | 63 +++++++++ LeetCode/Algorithms/Hard/CherryPickup2.cpp | 124 ++++++++++++++++++ .../Hard/FindTheMaximumSumOfNodeValues.cpp | 45 +++++++ 3 files changed, 232 insertions(+) create mode 100644 LeetCode/Algorithms/Hard/BusRoutes.cpp create mode 100644 LeetCode/Algorithms/Hard/CherryPickup2.cpp create mode 100644 LeetCode/Algorithms/Hard/FindTheMaximumSumOfNodeValues.cpp diff --git a/LeetCode/Algorithms/Hard/BusRoutes.cpp b/LeetCode/Algorithms/Hard/BusRoutes.cpp new file mode 100644 index 0000000..fa10f47 --- /dev/null +++ b/LeetCode/Algorithms/Hard/BusRoutes.cpp @@ -0,0 +1,63 @@ +class Solution { +public: + int numBusesToDestination(vector>& routes, int source, int target) { + int n = routes.size(); + int level = 0; + + unordered_map> m; // store - bus stop no. -> bus numbers list + + for(int i=0; i queue; + unordered_set busStopVisited; + unordered_set 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 buses = m[rem]; + for(int bus: buses) { + if(busVisited.find(bus) != busVisited.end()) { + continue; + } + + vector 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 \ No newline at end of file diff --git a/LeetCode/Algorithms/Hard/CherryPickup2.cpp b/LeetCode/Algorithms/Hard/CherryPickup2.cpp new file mode 100644 index 0000000..621d406 --- /dev/null +++ b/LeetCode/Algorithms/Hard/CherryPickup2.cpp @@ -0,0 +1,124 @@ +class Solution { +public: + int cherryPickup(vector>& grid) { + int r = grid.size(); + int c = grid[0].size(); + vector>> dp(r, vector>(c, vector(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> &grid, vector>> &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 + + diff --git a/LeetCode/Algorithms/Hard/FindTheMaximumSumOfNodeValues.cpp b/LeetCode/Algorithms/Hard/FindTheMaximumSumOfNodeValues.cpp new file mode 100644 index 0000000..970dd14 --- /dev/null +++ b/LeetCode/Algorithms/Hard/FindTheMaximumSumOfNodeValues.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + long long maximumValueSum(vector& nums, int k, vector>& 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 delta; + for(long long i=0; i()); + for(long long i=0; i