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 Problem-1.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

// Your code here along with comments explaining your approach
// do a forward pass to multiply all the elements from start
// again do a backward pass and multiply all the elements after that
// now multiply both elements

class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] output = new int[n];
int prod = 1;
for(int i=0; i < n; i++){
output[i] = prod;
prod *= nums[i];
}
prod = 1;
for(int i = n-1;i>-1;i-- ){
output[i] *= prod;
prod *= nums[i];
}
return output;


}
}
22 changes: 22 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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
# do a forward pass to multiply all the elements from start
# again do a backward pass and multiply all the elements after that
# now multiply both elements

class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
output = []
prod =1
for i in range (len(nums)):
output.append(prod)
prod *= nums[i]
prod = 1
for i in range(len(nums)-1,-1,-1):
output[i] *= prod
prod *= nums[i]
return output
46 changes: 46 additions & 0 deletions Problem-2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Time Complexity : O(mn)
// 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
// Traverse the matrix diagonally, switching direction on each diagonal.
// Move upward-right when the flag is true, and downward-left when the flag is false.
// When you hit matrix boundaries, adjust the row or column and flip the direction to continue correctly.
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
boolean flag = true;
int[] output = new int[m*n];
int r =0;
int c =0;
for( int i = 0; i < m*n; i++){
output[i] = mat[r][c];
if (flag){
if (c == n-1){
r++;
flag = false;
}else if(r == 0){
c++;
flag = false;
}else{
r--;
c++;
}
}else{
if (r == m-1){
c++;
flag = true;
}else if(c == 0){
r++;
flag = true;
}else{
r++;
c--;
}
}
}
return output;
}
}
49 changes: 49 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Time Complexity : O(mn)
# 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
# Traverse the matrix diagonally, switching direction on each diagonal.
# Move upward-right when the flag is true, and downward-left when the flag is false.
# When you hit matrix boundaries, adjust the row or column and flip the direction to continue correctly.

class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
m = len(mat)
n = len(mat[0])
output =[]
flag = True #up
r = 0
c = 0
for i in range(m*n):
output.append(mat[r][c])
if flag:
if c == n-1:
r += 1
flag = False
elif r == 0:
c+= 1
flag = False
else:
r-= 1
c += 1
else:
if r == m-1:
c += 1
flag = True
elif c == 0:
r += 1
flag = True
else:
r += 1
c -= 1
return output








53 changes: 53 additions & 0 deletions Problem-3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Time Complexity :O(mn)
// 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
//Traverse the matrix layer by layer in a spiral order, moving top row → right column → bottom row → left column.
// After completing each side, update the boundaries (top++, bottom--, left++, right--) to move inward.
// put four pointers. Repeat until all elements are added to the output list.

class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> output = new ArrayList<>();
if (matrix == null || matrix.length == 0) return output;

int m = matrix.length;
int n = matrix[0].length;
int top = 0, bottom = m - 1;
int left = 0, right = n - 1;

while (left <= right && top <= bottom) {
// Top row
for (int i = left; i <= right; i++) {
output.add(matrix[top][i]);
}
top++;

// Right column
for (int i = top; i <= bottom; i++) {
output.add(matrix[i][right]);
}
right--;

// Bottom row
if (top <= bottom) {
for (int i = right; i >= left; i--) {
output.add(matrix[bottom][i]);
}
bottom--;
}

// Left column
if (left <= right) {
for (int i = bottom; i >= top; i--) {
output.add(matrix[i][left]);
}
left++;
}
}

return output;
}
}
35 changes: 35 additions & 0 deletions Problem-3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Time Complexity :O(mn)
# 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
# Traverse the matrix layer by layer in a spiral order, moving top row → right column → bottom row → left column.
# After completing each side, update the boundaries (top++, bottom--, left++, right--) to move inward.
# put four pointers. Repeat until all elements are added to the output list.

class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
m = len(matrix)
n = len(matrix[0])
top = 0
bottom = m-1
left = 0
right =n-1
output = []
while (left <= right and top <= bottom):
for i in range(left,right+1):
output.append(matrix[top][i])
top += 1
for i in range(top,bottom+1):
output.append(matrix[i][right])
right -= 1
if top <= bottom:
for i in range(right, left-1, -1):
output.append(matrix[bottom][i])
bottom -= 1
if left <= right:
for i in range(bottom,top-1,-1):
output.append(matrix[i][left])
left += 1
return output