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
29 changes: 29 additions & 0 deletions problem1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Three line explanation of solution in plain english

// Your code here along with comments explaining your approach
// First, we go over the array from left to right to find the left running product
// Then we go over the array from right to left, maintaining a right running product
// The product of an array except a particular element will be the left product (existing result array) * right product

class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n = nums.size();
vector<int> result(n);
result[0] = 1;
int leftProduct = 1; // left running product
for(int i=1; i<n; i++) {
leftProduct = leftProduct*nums[i-1];
result[i] = leftProduct;
}
int rightProduct = 1; // right running product
for(int i=n-2; i>=0; i--) {
rightProduct = rightProduct*nums[i+1];
result[i] = result[i]*rightProduct;
}
return result;
}
};
49 changes: 49 additions & 0 deletions problem2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Time Complexity : O(m*n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Three line explanation of solution in plain english

// Your code here along with comments explaining your approach
// Start traversion from element at (0,0)
// Collect the element at each step and put it in the answer vector
// If we hit the boundary of the matrix (top most row, bottom most col, upper right element, bottom left element) change the direction

class Solution {
public:
vector<int> findDiagonalOrder(vector<vector<int>>& mat) {
int m = mat.size();
int n = mat[0].size();
vector<int> ans(m*n);
bool dir = true; // up
int r=0;
int c=0;
for(int i=0; i<m*n; i++) {
// collect the element
ans[i] = (mat[r][c]);
if(dir) { // up
if(r == 0 && c != n-1) {
c++;
dir = false; // flip direction when we hit boundary
} else if(c == n-1) {
r++;
dir = false; // flip direction when we hit boundary
} else {
r--;
c++;
}
} else { // down
if(c == 0 && r != m-1) {
r++;
dir = true; // flip direction when we hit boundary
} else if(r == m-1) {
c++;
dir = true; // flip direction when we hit boundary
} else {
r++;
c--;
}
}
}
return ans;
}
};
47 changes: 47 additions & 0 deletions problem3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Time Complexity : O(m*n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Three line explanation of solution in plain english

// Your code here along with comments explaining your approach
// Keep track of the top, bottom, left, right boundaries of the matrix
// Once we consume a row or column, shrink the appropriate boundaries
// Check the base case (bounds check) inside the while loop as well, as we are mutating the bounds inside the loop

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

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

if(left <= right && top <= bottom) {
for(int j=right; j>=left; j--) {
result.push_back(matrix[bottom][j]);
}
}
bottom--;

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