Skip to content

Commit 0cc54fc

Browse files
committed
add change 5
1 parent dcffcd3 commit 0cc54fc

7 files changed

Lines changed: 469 additions & 0 deletions
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
class Solution {
2+
public:
3+
int minOperations(vector<vector<int>>& grid, int x) {
4+
// add x or substract x from any eleemnt in grid
5+
6+
// minimum no. of opertions to make grid uni-value else -1
7+
8+
vector<int> array;
9+
int remainder = grid[0][0]%x;
10+
// Step 1: insert the array elements
11+
for(int i=0; i<grid.size(); i++) {
12+
for(int j=0; j<grid[0].size(); j++) {
13+
if(grid[i][j] % x != remainder) {
14+
return -1;
15+
}
16+
17+
array.push_back(grid[i][j]);
18+
}
19+
}
20+
21+
// Step 2 : Sort 1D array
22+
sort(array.begin(), array.end());
23+
int n = array.size();
24+
int median = n / 2;
25+
26+
// Step 3: Count steps required
27+
int steps = 0;
28+
for(int i=0; i<n; i++) {
29+
steps += abs(array[median] - array[i]) / x;
30+
}
31+
32+
return steps;
33+
}
34+
};
35+
36+
37+
/*
38+
Time Complexity - O(MNlogMN + MN) ~~ O(MN log MN) (sorting + flattening)
39+
Space Complexity - O(MN)
40+
41+
42+
min max
43+
44+
gap = 10^4, no. of items - 10^5, total 10^9 > 10^8
45+
46+
Pair Equality
47+
48+
Goal: 1. Make a & b equal
49+
a = 10, b = 30
50+
51+
2. Do it in min steps
52+
x = 5
53+
{For a pair (a, b) any point from a to b will be optimal}
54+
55+
Note:
56+
1. a and b can become equal only when a % x == b % x
57+
58+
c = 12 % 5 = 2 e = 10 % 5 = 0
59+
d = 32 % 5 = 2 f = 31 % 5 = 1
60+
x = 5 x = 5
61+
62+
Array Uni-value
63+
64+
a[] = [9, 14, 29, 39], x = 5
65+
66+
if a[i] % x is same for all elements then they can meet
67+
68+
if all pairs(c, d) where c % x == d % x then the array
69+
can be made uni-value more optmially
70+
71+
if a[i] % x is same for all i -> 0 to (N - 1),
72+
then the array can be made uni-value
73+
74+
75+
meeting point
76+
77+
9 14 (19) (24) 29 39 -- even (find the points within two middle points)
78+
79+
9 14 29 34 39 -- odd (take middle element)
80+
81+
82+
9 29
83+
39 14
84+
85+
Flatten - [9, 29, 39, 14] -> mn
86+
87+
[9, 14, 29, 39] --> mnlogmn
88+
89+
median = N / 2 = 2
90+
91+
// meet at index = 2
92+
93+
Iterate and check remainder whilte flattening
94+
95+
rem = 9 % 5 = 4
96+
97+
steps += abs(a[i] - a[median]) / x
98+
99+
steps = 0 -> 4 -> 7 -> 9
100+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public:
3+
string multiply(string num1, string num2) {
4+
if(num1 == "0" || num2 == "0") {
5+
return "0";
6+
}
7+
8+
int m = num1.size();
9+
int n = num2.size();
10+
11+
vector<int> result(m + n, 0);
12+
13+
reverse(num1.begin(), num1.end());
14+
reverse(num2.begin(), num2.end());
15+
16+
for(int i=0; i<m; i++) {
17+
for(int j=0; j<n; j++) {
18+
int digit = (num1[i] - '0') * (num2[i] - '0');
19+
20+
result[i + j] += digit;
21+
result[i + j + 1] += (result[i + j] / 10);
22+
result[i + j] = result[i + j] % 10;
23+
}
24+
}
25+
26+
reverse(result.begin(), result.end());
27+
int begin = 0;
28+
while(begin < result.size() && result[begin] == 0) {
29+
begin++;
30+
}
31+
32+
string output = "";
33+
for(int i=begin; i<result.size(); i++) {
34+
output += result[i];
35+
}
36+
return output;
37+
}
38+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
public:
3+
bool primeSubOperation(vector<int>& nums) {
4+
5+
int prev = 0;
6+
7+
// O(n * m * sqrt(m))
8+
for(auto &n : nums) {
9+
int upper_bound = n - prev; // non-inclusive [2, n)
10+
int largest_p = 0;
11+
12+
for(int i=upper_bound-1; i>=2; i--) {
13+
if(is_prime(i)) {
14+
largest_p = i;
15+
break;
16+
}
17+
}
18+
19+
if((n - largest_p) <= prev) {
20+
return false;
21+
}
22+
23+
prev = n - largest_p;
24+
25+
}
26+
}
27+
28+
bool isPrime(int n) {
29+
for(int i=2; i<pow(n, 0.5) + 1; i++) {
30+
if(n % i == 0) {
31+
return false;
32+
}
33+
}
34+
return true;
35+
}
36+
};
37+
38+
// Time Complexity - O(N * M * sqrt(M))
39+
// Space Complexity - O(N)
40+
41+
// 1. nums[i-1] < nums[i] - p < nums[i + 1]
42+
// 2. If nums[i] - p < nums[i - 1]
43+
// should we backtrack?
44+
// 3. Greedy: make each num as small as possible
45+
// from the get go
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
class Solution {
2+
public:
3+
int findLengthOfShortestSubarray(vector<int>& arr) {
4+
// remove prefix
5+
int n = arr.size();
6+
int r = n-1;
7+
8+
while(r > 0 && arr[r-1] > arr[r]) {
9+
r--;
10+
}
11+
12+
int res = r;
13+
14+
// remove postfix
15+
int l = 0;
16+
while(l + 1 < n && arr[l] < arr[l + 1]) {
17+
l++;
18+
}
19+
20+
res = min(res, n - l - 1);
21+
22+
// remove middle
23+
l = 0
24+
r = n-1;
25+
while(l < r) {
26+
// shrink the valid window (make it l+1 so that res is not negative when both r and l are equal)
27+
while(r < n && (l + 1) < r && arr[r-1] <= arr[r] && arr[l] <= arr[r]) {
28+
r--;
29+
}
30+
31+
// [1, 2, 3, 10, 6, 1, 2, 3]
32+
33+
// Expand Invalid Window
34+
while(r < n && arr[l] > arr[r]) {
35+
r++;
36+
}
37+
38+
res = min(res, r - l - 1);
39+
40+
if(arr[l] > arr[l + 1]) {
41+
break;
42+
}
43+
l += 1;
44+
}
45+
return res;
46+
}
47+
};
48+
49+
// 1. Prefix
50+
// 2. Postfix
51+
// 3. Middle
52+
// Take the minimum of all 3 above
53+
54+
// Time Complexity - O(N)
55+
// Space Complexity - O(1)
56+
57+
// Method - 2
58+
59+
class Solution {
60+
public:
61+
int findLengthOfShortestSubarray(vector<int>& arr) {
62+
int n = arr.size();
63+
int l = 0;
64+
int r = n-1;
65+
// remove prefix
66+
while(r >0 && arr[r-1] <= arr[r]) {
67+
r--;
68+
}
69+
70+
int res = r;
71+
72+
// l r
73+
// remove middle and postfix -- [1, 10, 6, 1, 2, 3]
74+
l = 0;
75+
while(l < r) {
76+
77+
// Expand Invalid Window
78+
while(r < n && arr[l] > arr[r]) {
79+
r++;
80+
}
81+
82+
res = min(res, r - l - 1);
83+
84+
if(arr[l + 1] < arr[l]) {
85+
break;
86+
}
87+
l++;
88+
}
89+
return res;
90+
}
91+
};
92+
93+
// 1. Prefix
94+
// 2. Postfix
95+
// 3. Middle
96+
// Take the minimum of all 3 above
97+
98+
// Time Complexity - O(N)
99+
// Space Complexity - O(1)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Solution {
2+
public:
3+
void updateFreq(vector<int> &bitFreq, int val) {
4+
for(int i=0; i<32; i++) {
5+
if(number & (1<<i)) {
6+
bitFreq[i] += val;
7+
}
8+
}
9+
}
10+
11+
int getNumber(vector<int> &bitFreq) {
12+
int number = 0;
13+
long long pow = 1;
14+
for(int i =0; i<32; i++) {
15+
if(bitFreq[i] > 0) {
16+
number += pow;
17+
}
18+
pow *= 2;
19+
}
20+
return number;
21+
}
22+
23+
int minimumSubarrayLength(vector<int>& nums, int k) {
24+
// A ^ B =
25+
26+
// 0 ^ 0 = 1
27+
28+
// Find bit value of k
29+
30+
// find all
31+
32+
if(k == 0) {
33+
return 1;
34+
}
35+
36+
int n = nums.size();
37+
int shortest = INT_MAX;
38+
int left = 0, right = 0;
39+
int currOR = 0;
40+
vector<int> bitFreq(32);o
41+
42+
while(right < n) {
43+
updateFreq(bitFreq, nums[right], 1);
44+
currOR |= nums[right];
45+
46+
// resize window
47+
while(left <= right and currOR >= k) {
48+
shortest = min(shortest, right - left + 1);
49+
updateFreq(bitFreq, nums[left], -1);
50+
currOR = getNumber(bitFreq);
51+
left++;
52+
}
53+
right++;
54+
}
55+
return shortest == INT_MAX ? -1: shortest;
56+
}
57+
};
58+
59+
// Observations
60+
61+
// 1. 0 or 0 or ... or 0 or 1 = 1
62+
// 2. OR for +ve nos will always be non-decreasing
63+
64+
// In non-decreasing curve to find smallest subarray use sliding window
65+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public:
3+
int takeCharacters(string s, int k) {
4+
// minimum no . of minutes to take atleast k of each character
5+
6+
// total counts
7+
int a=0, b=0, c =0;
8+
int n = s.size();
9+
for(auto &ch: s) {
10+
if(ch == 'a') a++;
11+
if(ch == 'b') b++;
12+
if(ch == 'c') c++;
13+
}
14+
15+
if(min(a, min(b, c)) < k) {
16+
return -1;
17+
}
18+
19+
// Sliding Window
20+
int l=0;
21+
int result = 0;
22+
int ans = INT_MAX;
23+
for(int r=0; r<s.size(); r++) {
24+
if(s[r] == 'a') a--;
25+
if(s[r] == 'b') b--;
26+
if(s[r] == 'c') c--;
27+
28+
while(min(a, min(b, c)) < k) {
29+
if(s[l] == 'a') a++;
30+
if(s[l] == 'b') b++;
31+
if(s[l] == 'c') c++;
32+
l++;
33+
}
34+
35+
ans = min(ans, n - (r - l + 1));
36+
}
37+
return ans;
38+
}
39+
};
40+
41+
// Time Complexity - O(N)
42+
// Space Complexity - O(1)

0 commit comments

Comments
 (0)