Skip to content

Commit a35bcbe

Browse files
solved on 22/10/23
1 parent 6055e68 commit a35bcbe

File tree

6 files changed

+288
-0
lines changed

6 files changed

+288
-0
lines changed

DynamicMemoryAllocation.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
void print(int **v, int rows, int cols)
6+
{
7+
for (int i = 0; i < rows; i++)
8+
{
9+
for (int j = 0; j < cols; j++)
10+
{
11+
cout << v[i][j] << " ";
12+
}
13+
cout << endl;
14+
}
15+
}
16+
17+
int main()
18+
{
19+
// // 1. allocate single int
20+
// int *intPtr = new int(5);
21+
22+
// // allocate single int using malloc
23+
// int *mptr = (int *)malloc(4);
24+
// *mptr = 5;
25+
26+
// cout << *intPtr << " " << *mptr << endl;
27+
// delete intPtr;
28+
// free(mptr);
29+
30+
// // 1D array allocation
31+
// int *arrnew = new int[5];
32+
33+
// // 1D array allocation using malloc
34+
// int *arrmalloc = (int *)malloc(5 * sizeof(int));
35+
36+
// for (int i = 0; i < 5; i++)
37+
// {
38+
// int d;
39+
// cin >> d;
40+
// arrnew[i] = arrmalloc[i] = d;
41+
// }
42+
43+
// for (int i = 0; i < 5; i++)
44+
// {
45+
// cout << arrnew[i] << " " << arrmalloc[i] << endl;
46+
// }
47+
// delete[] arrnew;
48+
// free(arrmalloc);
49+
50+
// 2D array allocation
51+
int rows = 5, cols = 5;
52+
int **ptr2d = new int *[rows];
53+
54+
// 2D array allocation using malloc
55+
for (int i = 0; i < rows; ++i)
56+
ptr2d[i] = new int[cols];
57+
58+
int **ptr2dmalloc = (int **)malloc(sizeof(int *) * rows);
59+
for (int i = 0; i < rows; i++)
60+
ptr2dmalloc[i] = (int *)malloc(sizeof(int) * cols);
61+
62+
for (int i = 0; i < rows; i++)
63+
{
64+
for (int j = 0; j < cols; j++)
65+
{
66+
ptr2d[i][j] = 7;
67+
ptr2dmalloc[i][j] = 8;
68+
}
69+
}
70+
71+
print(ptr2d, rows, cols);
72+
print(ptr2dmalloc, rows, cols);
73+
74+
for (int i = 0; i < rows; ++i)
75+
delete[] ptr2d[i];
76+
delete[] ptr2d;
77+
78+
for (int i = 0; i < rows; ++i)
79+
free(ptr2dmalloc[i]);
80+
free(ptr2dmalloc);
81+
return 0;
82+
}

LC_60_HouseRobber2.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int solve(vector<int> &nums, int idx, int n)
5+
{
6+
// base case
7+
if (idx > n)
8+
{
9+
return 0;
10+
}
11+
12+
// rob this house
13+
int opt1 = nums[idx] + solve(nums, idx + 2, n);
14+
15+
// don't rob this house
16+
int opt2 = 0 + solve(nums, idx + 1, n);
17+
18+
int ans = max(opt1, opt2);
19+
return ans;
20+
}
21+
22+
// recursive solution, gives TLE on leetcode. we'll use DP to optimize it in future
23+
// tc: O(2^n) sc: O(n)
24+
int rob(vector<int> &nums)
25+
{
26+
int n = nums.size();
27+
if (n == 1)
28+
return nums[0];
29+
int opt1 = solve(nums, 0, n - 2);
30+
int opt2 = solve(nums, 1, n - 1);
31+
32+
int ans = max(opt1, opt2);
33+
return ans;
34+
}
35+
36+
int main()
37+
{
38+
vector<int> nums = {1, 2, 3, 1};
39+
cout << "Maximum amount that can be robbed: ";
40+
cout << rob(nums) << endl;
41+
return 0;
42+
}

LC_61_EditDistance.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int solve(string &word1, string &word2, int i, int j)
5+
{
6+
// base cases
7+
// if we reach the end of any of the strings, we return the length of the remaining string
8+
if (i >= word1.length())
9+
{
10+
return word2.length() - j;
11+
}
12+
if (j >= word2.length())
13+
{
14+
return word1.length() - i;
15+
}
16+
17+
int ans = 0;
18+
if (word1[i] == word2[j]) // if characters are same, no operation is required
19+
{
20+
ans = 0 + solve(word1, word2, i + 1, j + 1);
21+
}
22+
else // if characters are not same, we have 3 options: insert, remove, replace (all 3 are equally valid) and we'll take the minimum of all 3 options as our answer for that particular call
23+
{
24+
// insert
25+
int opt1 = 1 + solve(word1, word2, i, j + 1); // we don't increment i because we want to insert the character at jth position of word2 in word1
26+
// remove
27+
int opt2 = 1 + solve(word1, word2, i + 1, j); // we don't increment j because we want to remove the character at ith position of word1
28+
// replace
29+
int opt3 = 1 + solve(word1, word2, i + 1, j + 1); // we increment both i and j because we want to replace the character at ith position of word1 with the character at jth position of word2 and then move forward
30+
ans = min(opt1, min(opt2, opt3)); // as required by the question we take the minimum of all 3 options
31+
}
32+
return ans; // return the answer for that particular call
33+
}
34+
35+
// recursive solution, gives TLE, we'll use DP to optimize it in future
36+
// tc: O(3^n) sc: O(n)
37+
int minDistance(string word1, string word2)
38+
{
39+
int i = 0;
40+
int j = 0;
41+
int ans = solve(word1, word2, i, j);
42+
// horse -> rorse (replace 'h' with 'r')
43+
// rorse -> rose (remove 'r')
44+
// rose -> ros (remove 'e')
45+
return ans;
46+
}
47+
48+
int main()
49+
{
50+
string word1 = "horse";
51+
string word2 = "ros";
52+
cout << "Minimum number of operations required to convert word1 to word2: ";
53+
cout << minDistance(word1, word2) << endl;
54+
return 0;
55+
}

LC_62_MaximalSquare.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int solve(vector<vector<char>> &matrix, int i, int j, int row, int col, int &maxi)
5+
{
6+
// base case
7+
if (i >= row || j >= col)
8+
{
9+
return 0;
10+
}
11+
12+
// explore all 3 directions
13+
int right = solve(matrix, i, j + 1, row, col, maxi);
14+
int diagnol = solve(matrix, i + 1, j + 1, row, col, maxi);
15+
int down = solve(matrix, i + 1, j, row, col, maxi);
16+
17+
// check can we build sqaure form current position
18+
if (matrix[i][j] == '1')
19+
{
20+
// if yes, we'll take the minimum of all 3 directions and add 1 to it because we can build a square of size 1 from current position
21+
int ans = 1 + min(right, min(down, diagnol));
22+
// update the maximum square size found so far
23+
maxi = max(maxi, ans);
24+
return ans; // return the answer for that particular call
25+
}
26+
else
27+
{
28+
// if current position is 0, we can't build square from here so we return 0
29+
return 0;
30+
}
31+
}
32+
33+
int maximalSquare(vector<vector<char>> &matrix)
34+
{
35+
int i = 0;
36+
int j = 0;
37+
int row = matrix.size();
38+
int col = matrix[0].size();
39+
int maxi = 0;
40+
int ans = solve(matrix, i, j, row, col, maxi);
41+
return maxi * maxi;
42+
}
43+
44+
int main()
45+
{
46+
vector<vector<char>> matrix = {{'1', '0', '1', '0', '0'},
47+
{'1', '0', '1', '1', '1'},
48+
{'1', '1', '1', '1', '1'},
49+
{'1', '0', '0', '1', '0'}};
50+
cout << maximalSquare(matrix) << endl;
51+
return 0;
52+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int solve(int n)
5+
{
6+
7+
// base case
8+
if (n == 1)
9+
{
10+
return 0;
11+
}
12+
if (n == 2)
13+
{
14+
return 1;
15+
}
16+
17+
int ans = (n - 1) * (solve(n - 1) + solve(n - 2));
18+
return ans;
19+
}
20+
21+
int main()
22+
{
23+
24+
int n;
25+
vector<int> v = {0, 1, 2}; // possible derangements: {2, 0, 1} and {1, 2, 0}
26+
n = v.size();
27+
cout << solve(n) << endl;
28+
29+
return 0;
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int getPaintWays(int n, int k)
5+
{
6+
// base case
7+
if (n == 1)
8+
{
9+
return k;
10+
}
11+
if (n == 2)
12+
{
13+
return k + k * (k - 1);
14+
}
15+
// k-1 is because we can't have more than 2 consecutive fences of same color in a row
16+
int ans = (k - 1) * (getPaintWays(n - 1, k) + getPaintWays(n - 2, k));
17+
return ans;
18+
}
19+
20+
int main()
21+
{
22+
int n = 3;
23+
int k = 3;
24+
int ans = getPaintWays(n, k);
25+
cout << ans << endl;
26+
return 0;
27+
}

0 commit comments

Comments
 (0)