Skip to content
Open
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
12 changes: 12 additions & 0 deletions AKASH KUMAR/1.TrappingRainWater/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Trapping Rain Water problem cam be done in 2 ways

1.Brute Force:Taking 2 arrays prefix and suffix prefix will store the height of the largest building till that index on traversal form left to right and suffix will store the height of the largest building till that index on traversal form right to left.And then min(prefix(i),suffix(i)) will be taken and subtracted with with the height of the i'th building i.e. min(prefix(i),suffix(i))-arr(i) and the value will be added in the ans varible which will return the total uints of water that can be stored in between the building at the end of the loop.

Time Complexity=0(n)
Space COmplexity=0(2n) .Since we are taking 2 extra arrays{prefix & suffix)


2.Optimal Solution:We can do it without using extra space by taking 2 pointers l & r(i.e. left and right) l will be at index 0(l=0) and r will be at last index(i.e.r=n-1).It will be traversed till l<r.When arr(l)<=arr(r) then if the current index height is < than max of l then in the ans (lmax-arr(i)) will be added other wise lmax will be updated.Similar will be done for rmax when arr(r)<arr(l)

Time Complexity=0(n)
Space COmplexity=0(1) Since no extra space is used.
Binary file added AKASH KUMAR/1.TrappingRainWater/Screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
115 changes: 115 additions & 0 deletions AKASH KUMAR/1.TrappingRainWater/soln.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//For LeetCode enviroment
//Time Complexity=0(n)
//Space COmplexity=0(1)
class Solution {
public:
int trap(vector<int>& height)
{
int n=height.size();
int l,r,ans=0,lmax=0,rmax=0;
l=0,r=n-1;
while(l<r)
{
if(height[l]<=height[r])
{
if(lmax>height[l])
{
ans+=lmax-height[l];
}
else
{
lmax=height[l];
}
l++;
}
else
{
if(rmax>height[r])
{
ans+=rmax-height[r];
}
else
{
rmax=height[r];
}
r--;
}
}
return ans;


}
};

















//For Local enviroment
//Time Complexity=0(n)
//Space COmplexity=0(1)
#include <bits/stdc++.h>
using namespace std;
int trap(vector<int>& height)
{
int n=height.size();
int l,r,ans=0,lmax=0,rmax=0;
l=0,r=n-1;
while(l<r)
{
if(height[l]<=height[r])
{
if(lmax>height[l])
{
ans+=lmax-height[l];
}
else
{
lmax=height[l];
}
l++;
}
else
{
if (rmax>height[r])
{
ans+=rmax-height[r];
}
else
{
rmax=height[r];
}
r--;
}
}
return ans;
}

int main()
{
int n;
cout<<"Enter number of elements:";
cin>>n;
vector<int> height(n);
cout << "Enter the heights:";
for (int i=0;i<n;i++)
{
cin>>height[i];
}
int result=trap(height);
cout << "Amount of trapped water: " << result << endl;

return 0;
}
Binary file added AKASH KUMAR/1.TrappingRainWater/soln.exe
Binary file not shown.
Binary file added AKASH KUMAR/2.Spiral Matrix/Screenshot .png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
127 changes: 127 additions & 0 deletions AKASH KUMAR/2.Spiral Matrix/soln.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//For Local enviroment
//Time Complexity=0(m*n)
//Space COmplexity=0(m*n)
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> spiralOrder(vector<vector<int>>& matrix)
{
vector<int>ans;
int n=matrix.size();
if (n==0)
{
return ans;
}
int m=matrix[0].size();
int top=0,bottom=n-1;
int left=0,right=m-1;
while (top<=bottom&&left<=right)
{
for (int i=left;i<=right;i++)
{
ans.push_back(matrix[top][i]);
}
top++;
for (int i=top;i<=bottom;i++)
{
ans.push_back(matrix[i][right]);
}
right--;
if (top<=bottom)
{
for (int i=right;i>=left;i--)
{
ans.push_back(matrix[bottom][i]);
}
bottom--;
}
if (left<=right)
{
for (int i=bottom;i>=top;i--)
{
ans.push_back(matrix[i][left]);
}
left++;
}
}
return ans;
}
};
int main()
{
int n,m;
cin>>n>>m;
vector<vector<int>>matrix(n,vector<int>(m));
for (int i=0;i<n;i++)
{
for (int j=0;j<m;j++)
{
cin>>matrix[i][j];
}
}
Solution obj;
vector<int>result=obj.spiralOrder(matrix);
for (int i=0;i<result.size();i++)
{
cout<<result[i]<<" ";
}
cout<<endl;
return 0;
}





//For LeetCode enviroment
//Time Complexity=0(m*n)
//Space COmplexity=0(m*n)

class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int>ans;
int n=matrix.size();
if (n==0)
{
return ans;
}
int m=matrix[0].size();
int top=0,bottom=n-1;
int left=0,right=m-1;
while (top<=bottom&&left<=right)
{
for (int i=left;i<=right;i++)
{
ans.push_back(matrix[top][i]);
}
top++;
for (int i=top;i<=bottom;i++)
{
ans.push_back(matrix[i][right]);
}
right--;
if (top<=bottom)
{
for (int i=right;i>=left;i--)
{
ans.push_back(matrix[bottom][i]);
}
bottom--;
}

if (left<=right)
{
for (int i=bottom;i>=top;i--)
{
ans.push_back(matrix[i][left]);
}
left++;
}
}

return ans;
}
};
Binary file removed Leetcode DSA sheet by Fraz 1.xlsx
Binary file not shown.
142 changes: 0 additions & 142 deletions README.md

This file was deleted.