Skip to content

Commit 2561e27

Browse files
committed
Added leetcode solutions in arrays, strings and backtracking
1 parent c8a98ac commit 2561e27

File tree

14 files changed

+497
-0
lines changed

14 files changed

+497
-0
lines changed

leetcode/cpp/Array/1296.cpp

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 isPossibleDivide(vector<int>& nums, int k) {
4+
sort(nums.begin(),nums.end());
5+
vector<pair<int,int>> v;
6+
int n= nums.size();
7+
8+
//filling the vector with the appropriate values
9+
for (int i=0;i<n;i++){
10+
if (i==0|| v[v.size()-1].first!= nums[i]){
11+
v.push_back(make_pair(nums[i],1));
12+
}
13+
else
14+
v[v.size()-1].second++;
15+
}
16+
17+
for (int i=0;i<v.size();i++){
18+
if (v[i].second==0)
19+
continue;
20+
int j= i;
21+
22+
if (j+k-1>=v.size()) return false;
23+
//i denotes the element pair we are at now
24+
for (int l=j+1;l<j+k;l++){
25+
//check whether the successive elements are consecutive
26+
if (v[l].first!=v[l-1].first+1)
27+
return false;
28+
//the count of the present element should at least be equal to that of the ith elmt(min), in order to fill that bucket
29+
30+
if (v[l].second<v[j].second)
31+
return false;
32+
//if all the conditions above holds true, then we can use that element, as many as times as that of i, so decrementing
33+
v[l].second-= v[j].second;
34+
}
35+
}
36+
return true;
37+
38+
}
39+
};
40+
41+
42+
//==============================================================================
43+
//Time complexity of the above algorithm asymptotically: O(nlog(n)), where n is the size of the input array
44+
45+
//Space complexity: O(n)

leetcode/cpp/Array/1366.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
string rankTeams(vector<string>& votes) {
4+
//rank matrix
5+
int i,j,n= votes.size(),k= votes[0].size();
6+
vector<vector<int>> rank(26, vector<int>(k,0));
7+
for (i=0;i< n;i++){
8+
for (j=0;j<k;j++){
9+
rank[votes[i][j]-'A'][j]++;
10+
}
11+
}
12+
string res= votes[0];
13+
sort(res.begin(),res.end(),[&rank](char &a, char &b){
14+
int i=0;//index for both the rows of that 2 teams
15+
int n= rank[a-'A'].size();
16+
while (i<n && rank[a-'A'][i]==rank[b-'A'][i]){
17+
i++;//lkeep on going till there is a mismatch of votes
18+
}
19+
if (i==n)
20+
return a<b;//if everything is equal, arrange them alphabetically
21+
return rank[a-'A'][i]>rank[b-'A'][i];
22+
23+
24+
});
25+
return res;
26+
27+
}
28+
};
29+
//==========================================================
30+
//Time complexity of the algorithm asymptotically: O(n(k+log(n)))
31+
//Space complexity: O(n*k)

leetcode/cpp/Array/1524.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int numOfSubarrays(vector<int>& arr) {
4+
int a= pow(10,9)+7;
5+
int temp[2]= {1,0};
6+
unsigned int res= 0;
7+
int val= 0, n= arr.size();//temp for odd and even pair
8+
for (int i=0;i<n;i++){
9+
val= ((val+arr[i])%2 +2)%2;//compute
10+
temp[val]= (temp[val]+1)%a;
11+
}
12+
res= (long long)(temp[0]%a)*(temp[1]%a)%a;
13+
return res;
14+
15+
}
16+
};
17+
18+
19+
//========================================================
20+
//Time complexity of the above algorithm(asymptotically): O(n), where n is the length of the input array
21+
22+
//Space complexity: O(1), constant space

leetcode/cpp/Array/167.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& numbers, int target) {
4+
int i,j, n= numbers.size();
5+
vector<int> res(2,-1);
6+
i=0;j=n-1;
7+
while (i<j){
8+
//sum equal
9+
if (numbers[i]+numbers[j]==target){
10+
res[0]=i+1;res[1]=j+1;
11+
return res;
12+
}
13+
else if (numbers[i]+numbers[j]>target)
14+
j--;
15+
else
16+
i++;
17+
}
18+
return res;
19+
}
20+
};
21+
22+
//==============================================================
23+
//Time complexity of the above algorith: O(n), n is the length of the input array
24+
25+
//Space complexity: O(1), constant space

leetcode/cpp/Array/209.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int minSubArrayLen(int s, vector<int>& nums) {
4+
int n = nums.size();
5+
int ans = INT_MAX;
6+
int l = 0,r=0;
7+
int sum = 0;
8+
while (r<n){
9+
sum+=nums[r];
10+
11+
while (sum>=s){
12+
ans= min(ans,r-l+1);
13+
sum-=nums[l++];
14+
}
15+
r++;
16+
17+
}
18+
return (ans != INT_MAX) ? ans : 0;
19+
}
20+
};
21+
22+
//======================================================
23+
//Time complexity of the above algorithm asymptotically: O(n), n is the size of the input array.
24+
//The reason is because in the worse case, every element will be visited only twice.
25+
26+
//Space complexity: O(1), constant

leetcode/cpp/Array/238.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
vector<int> productExceptSelf(vector<int>& nums) {
4+
int n= nums.size(), i, prodSoFar= 1;
5+
vector<int> output(n,nums[n-1]);
6+
//preprocessing the array
7+
for (i=n-2;i>=0;i--){
8+
output[i]= output[i+1]*nums[i];
9+
}
10+
11+
for (i=0;i<n-1;i++){
12+
output[i]= prodSoFar*output[i+1];
13+
prodSoFar*= nums[i];
14+
}
15+
output[n-1]= prodSoFar;
16+
17+
return output;
18+
}
19+
20+
};
21+
22+
23+
//==============================================================
24+
//Time complexity of the above algorith: O(n), n is the length of the input array
25+
26+
//Space complexity: O(1), constant space

leetcode/cpp/Array/33.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Solution {
2+
public:
3+
int binarySearchHelper(vector<int> &nums, int lo, int hi, int target){
4+
//simple binary search
5+
int mid;
6+
while(lo<=hi){
7+
mid= lo + (hi-lo)/2;
8+
if (nums[mid]==target)
9+
return mid;
10+
else if (nums[mid]>target){
11+
hi= mid-1;
12+
}
13+
else
14+
lo= mid +1;
15+
}
16+
return -1;
17+
}
18+
19+
20+
// using the binary search predicate framework: more info at https://www.topcoder.com/community/competitive-programming/tutorials/binary-search/
21+
int search(vector<int>& nums, int target) {
22+
int n= nums.size(), lo= 0, hi= n-1, mid;
23+
int resIdx=-1;//final result index
24+
25+
if (n==0) return -1;
26+
27+
28+
//find the minimum of the input array using, the predicate
29+
//P(x): A[x]<=A[0]
30+
//3 4 5 1 2
31+
//F F F T T, x=mid, thus F*T* framework and the finding the first T is the aim
32+
while (lo<hi){
33+
mid= lo+ (hi-lo)/2;
34+
if (nums[mid]<nums[0])
35+
hi= mid;
36+
else
37+
lo= mid+1;
38+
}
39+
int min= lo;
40+
//lo is the index of the minimum element
41+
42+
//now we have 2 sorted(ascending) subarrays, thus do a binary search on each of them
43+
44+
if (nums[lo]==target){
45+
return lo;
46+
}
47+
else {
48+
//simple binary search on nums[0:lo-1]
49+
resIdx= binarySearchHelper(nums, 0, min-1, target);
50+
if (resIdx!=-1) return resIdx;
51+
//not found in the first part
52+
resIdx= binarySearchHelper(nums, min, n-1, target);
53+
54+
//binary search on nums[lo+1,n-1] second part
55+
56+
}
57+
return resIdx;//target not found anywhere
58+
}
59+
};
60+
61+
62+
//==================================================
63+
//Time complexity of the algorithm(asymptotically): O(log(n)), where n is the size of the input array
64+
65+
//Space complexity: O(1), constant

leetcode/cpp/Array/56.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> merge(vector<vector<int>>& intervals) {
4+
vector<vector<int>> res;
5+
int i, n= intervals.size(),count= 0;
6+
//pre processing part, creating the interval space
7+
vector<pair<int,int>> intervalSpace;
8+
for (i=0;i<n;i++){
9+
//push_back the first element
10+
intervalSpace.push_back(make_pair(intervals[i][0],0));
11+
//2nd element
12+
intervalSpace.push_back(make_pair(intervals[i][1],1));
13+
}
14+
//so the interval space contains pairs where the first elt is the number, and the second
15+
//signifies whether it is a opening or closing bracket, 0-> opening, 1->closing
16+
//ex: [[1,2],[3,4]]
17+
//intervalspace: [<1,0>,<2,1>,<3,0>,<4,1>]
18+
19+
sort(intervalSpace.begin(),intervalSpace.end(),[](pair<int,int> &a, pair<int,int> &b){
20+
// if they are equal
21+
if (a.first==b.first){
22+
return a.second <b.second;
23+
}
24+
return a.first<b.first;
25+
});
26+
27+
28+
//main logic
29+
for (i=0;i<intervalSpace.size();i++){
30+
//increment part
31+
if (intervalSpace[i].second==0){
32+
count++;//count signifies the level of the interval
33+
if (count==1){
34+
res.push_back({intervalSpace[i].first});
35+
}
36+
}
37+
//decrement part
38+
else {
39+
count--;
40+
if (count==0){
41+
//just finished one interval, merge with the previous interval
42+
res[res.size()-1].push_back(intervalSpace[i].first);
43+
}
44+
}
45+
}
46+
return res;
47+
}
48+
49+
};
50+
//=============================================
51+
//the underlying logic in the solution is:
52+
//If we imagine the interval space marked on the number line
53+
//arranged in ascending order, whenever there is a merging of intervals,
54+
// the crosses(X) increase from the base level zero to some value when there is a opening juncture
55+
// and decreases from some value to base value zero when there is a closing juncture.
56+
//count variable signifies the level of crosses, 1 means only 1 interval has that elt,
57+
// 2 means 2 overlapping intervals and so on...
58+
59+
//1 ---3 4---5---6
60+
// 2--3
61+
//thus:
62+
//XXXXXX XXXXXXXXX
63+
// XXXX
64+
65+
66+
//=============================================
67+
//Time complexity of the above algorithm asymptotically: O(nlog(n)), because of the custom sorting
68+
69+
//Space complexity: O(n) for the intervalSpace vector

leetcode/cpp/Array/560.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int subarraySum(vector<int>& nums, int k) {
4+
int n= nums.size(),i, cumSum= 0, res=0;
5+
unordered_map<int,int> m;//<sum till now,count>
6+
7+
m[0]= 1;
8+
9+
for (i=0;i<n;i++){
10+
cumSum+=nums[i];//update the cumulative sum or the sum of prefix subarray till the curr elem
11+
res+=m[cumSum-k];//find the required sum and update the result, if it doesnt exist it will return 0
12+
m[cumSum]++;//update the cumulative sum
13+
14+
}
15+
return res;
16+
17+
}
18+
};
19+
20+
21+
//======================================================
22+
//Time complexity of the above algorith: O(n), n is the length of the input array
23+
24+
//Space complexity: O(n), because of the hashmap

leetcode/cpp/Array/974.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int subarraysDivByK(vector<int>& A, int K) {
4+
int n= A.size(),j, cumSum= 0, res= 0, r;
5+
//key is the remainder given by the prefix sum on division with k
6+
//value is the count
7+
unordered_map<int,int> m;
8+
9+
m[0]= 1;
10+
for (j=0;j<n;j++){
11+
cumSum+=A[j];
12+
13+
//r2-r can be 0, k,-k
14+
//so r2 can be r, r+k, r-k;
15+
r= cumSum%K;
16+
res+= m[r];
17+
res+= m[r+K];
18+
res+= m[r-K];
19+
20+
m[r]++;
21+
}
22+
return res;
23+
24+
}
25+
};
26+
27+
//===============================================================
28+
//Time complexity of the above algorith: O(n), n is the length of the input array
29+
30+
//Space complexity: O(n), because of the hashmap

0 commit comments

Comments
 (0)