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
50 changes: 50 additions & 0 deletions daigonaltraverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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: set initial position at (0,0) and traverse the matrix in diagonal order by changing direction when hitting the boundaries until all elements are traversed.

// Your code here along with comments explaining your approach: Traverse the matrix in diagonal order by changing direction when hitting the boundaries. Set initial position at (0,0) and keep track of the direction of traversal. If the direction is upwards, move to the next column if at the last column, else move to the previous row if at the first row, else move diagonally up. If the direction is downwards, move to the next row if at the last row, else move to the next column if at the first column, else move diagonally down. Continue this process until all elements are traversed and stored in the result array.

class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length;
int n = mat[0].length;

int[] result = new int[m*n];
int r = 0, c = 0;

boolean dir = true;

for(int i = 0; i < m * n; i++) {
result[i] = mat[r][c];

if(dir) {
if(c == n - 1) {
r++;
dir = false;
}
else if(r == 0) {
c++;
dir = false;
}
else {
r--; c++;
}
} else {
if(r == m - 1) {
c++;
dir = true;
}
else if(c == 0) {
r++;
dir = true;
}
else {
r++; c--;
}
}
}

return result;
}
}
29 changes: 29 additions & 0 deletions prodself.java
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: running product from left and right and storing in result array, then multiplying both the products to get the final result.
// Your code here along with comments explaining your approach: calculate prdouct of all the elements to the left of the current element and store it in result array, then calculate product of all the elements to the right of the current element and multiply it with the value stored in result array to get the final result.
class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;

int[] result = new int[n];

int rp = 1;
result[0] = 1;

for(int i=1; i<n; i++){ // left pass
rp = rp * nums[i-1];
result[i] = rp;
}

rp = 1;

for(int i=n-2; i>=0; i--){ // right pass
rp = rp*nums[i+1];
result[i] = result[i] * rp;
}

return result;
}
}
50 changes: 50 additions & 0 deletions spiralmatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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: Traverse the matrix in a spiral order by maintaining boundaries and moving in four directions (right, down, left, up) while adjusting the boundaries after each direction.

// Your code here along with comments explaining your approach: Initialize four pointers (top, bottom, left, right) to represent the current boundaries of the matrix. Used a while loop to traverse the matrix until the pointers cross each other. In each iteration, traverse in the current direction (right, down, left, up) and adjust the corresponding pointer after traversing. Continue this process until all elements are added to the result list in spiral order.
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;

int top = 0, bottom = m-1, l = 0, r = n-1;

List<Integer> result = new ArrayList<>();

while(top <= bottom && l <= r){

for(int i=l; i<=r; i++)
{
result.add(matrix[top][i]);
}
top++;

for(int i=top; i<=bottom; i++)
{
result.add(matrix[i][r]);
}
r--;

if(top <= bottom)
{
for(int i=r; i>=l; i--){
result.add(matrix[bottom][i]);
}
bottom--;
}

if(l <= r)
{
for(int i=bottom; i>=top; i--)
{
result.add(matrix[i][l]);
}
l++;
}
}

return result;
}
}