Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3 _24 sep problem added. #177

Merged
merged 6 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions September_2024/gfg_potd_2_sep_24.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//πŸ‘‰πŸ» Q.Given a square grid of size N, each cell of which contains an integer cost that represents a cost to traverse through that cell, we need to find a path from the top left cell to the bottom right cell by which the total cost incurred is minimum.
// From the cell (i,j) we can go (i,j-1), (i, j+1), (i-1, j), (i+1, j).


class Cell{
public:
int i, j, x;
Cell(int i, int j, int x){
this->i = i;
this->j = j;
this->x = x;
}
};

class Comparator{
public:
bool operator()(Cell& c1, Cell& c2){
// This should be "less than" for min-heap behavior
return c1.x > c2.x;
}
};

class Solution{
public:
int minimumCostPath(vector<vector<int>>& grid){
int n = grid.size(), m = grid[0].size();

// Min-heap priority queue
priority_queue<Cell, vector<Cell>, Comparator> pq;

// DP table to store the minimum cost to reach each cell
vector<vector<int>> dp(n, vector<int>(m, INT_MAX));

// Possible directions for movement (left, right, up, down)
vector<vector<int>> dir = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

// Start with the top-left corner (0,0)
pq.push(Cell(0, 0, grid[0][0]));
dp[0][0] = grid[0][0];

while (!pq.empty()){
Cell temp = pq.top();
pq.pop();

// Explore neighbors in 4 directions
for (auto d : dir){
int ni = temp.i + d[0];
int nj = temp.j + d[1];

// Check if the new coordinates are valid
if (ni < 0 || nj < 0 || ni >= n || nj >= m)
continue;

int newCost = grid[ni][nj] + temp.x;

// If a cheaper path to the neighbor is found, update it
if (dp[ni][nj] > newCost){
dp[ni][nj] = newCost;
pq.push(Cell(ni, nj, newCost));


//πŸ‘‰πŸ» logic
// A.Initialization-
// The algorithm starts with the assumption that the first element a[0] is already sorted.
// It begins iterating from the second element (i = 1) to the last element (i = n-1).

// B .Key Selection-
// The current element, a[i], is picked as the key. This is the element that we need to insert into the correct position within the sorted portion of the array (which is to the left of i).

//C. Comparison and Shifting-
// The inner loop compares the key with the elements in the sorted portion of the array (i.e., the elements to the left of i).
// If an element in the sorted portion is greater than the key, it is shifted one position to the right to make space for the key.
// This shifting process continues until we find the correct position where the key can be inserted.

//D. Insertion-
// Once the correct position is found (i.e., when we find an element smaller than or equal to the key), the key is inserted into that position (a[j + 1] = key).

// E.Continue to the Next Element-
// The process repeats for the next element, gradually growing the sorted portion of the array.


//πŸ‘‰πŸ» Time complexcity
// -The algorithm processes each cell at most once using a priority queue. In a grid of size n Γ— m, the number of cells is -𝑂((n*m)log(n*m))

//πŸ‘‰πŸ» space complexcity
// -The space complexity is 𝑂((n*m),due to the DP table (dp) and the priority queue (pq), which store information for each cells.
37 changes: 37 additions & 0 deletions September_2024/gfg_potd_3_Sep_2024.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//πŸ‘‰πŸ» Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.

int minOperations(string str1, string str2)
{
// Your code goes here
int n = str1.length(), m = str2.length();
int dp[n+1][m+1];
for(int i=0;i<=n;i++)dp[i][0]=0;
for(int j=0;j<=m;j++)dp[0][j]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j] = str1[i-1]==str2[j-1]?1+dp[i-1][j-1]:max(dp[i-1][j],dp[i][j-1]);
}
}
return n+m-2*dp[n][m];
}

//πŸ‘‰πŸ» logic

// A.LCS (Longest Common Subsequence)
// The LCS is a subsequence that appears in both strings in the same order, but not necessarily consecutively.
// If you find the LCS between str1 and str2, you know that this part of the two strings can remain unchanged during the transformation. The rest of the characters need to be inserted or deleted.


//B. Insertions and Deletions-
// Deletions: Any character in str1 that is not part of the LCS must be deleted.
// Insertions: Any character in str2 that is not part of the LCS must be inserted into str1.

//C.formula-
// -Operation =(length of str1-lcs)+(length of str2-lcs).

//πŸ‘‰πŸ» time complexcity
// -O(n*m)because we fill the dp table with dimensions (n+1) x (m+1) using nested loops.

//πŸ‘‰πŸ»Space complexcity
// - O(n*m) the space required for the DP table

Loading