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
30 changes: 30 additions & 0 deletions Problem1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Time Complexity : O(n) where n is the total length of nums array
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

public class Solution
{
public int[] ProductExceptSelf(int[] nums)
{
int[] result = new int[nums.Length];
result[0] = 1;
int product = 1;

for (int i = 1; i < nums.Length; i++)
{
product *= nums[i - 1];
result[i] = product;
}

product = 1;

for (int i = nums.Length - 2; i >= 0; i--)
{
product *= nums[i + 1];
result[i] *= product;
}

return result;
}
}
66 changes: 66 additions & 0 deletions Problem2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Time Complexity : O(m*n) where n is the total number of rows and m is the total number of columns of mat
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


public class Solution
{
public int[] FindDiagonalOrder(int[][] mat)
{
int rows = mat.Length, columns = mat[0].Length;
int dir = 1, count = 0, i = 0, j = 0;
int[] result = new int[rows * columns];

while (count < rows * columns)
{
result[count] = mat[i][j];
count++;

if (dir == 1)
{
if (j == columns - 1)
{
i += 1;
dir = -1;
}

else if (i == 0)
{
j += 1;
dir = -1;
}

else
{
i--;
j++;
}
}

else if (dir == -1)
{
if (i == rows - 1)
{
j++;
dir = 1;
}

else if (j == 0)
{
i += 1;
dir = 1;
}

else
{
i++;
j--;
}
}
}

return result;

}
}
58 changes: 58 additions & 0 deletions Problem3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Time Complexity : O(m*n) where n is the total number of rows and m is the total number of columns of mat
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

public class Solution {
public IList<int> SpiralOrder(int[][] matrix) {
List<int> result = new();
int rows = matrix.Length, columns = matrix[0].Length;
int top = 0, bottom = rows - 1, left = 0, right = columns - 1;

while(top <= bottom && left <= right)
{
// left
for(int i = left; i <= right; i++)
{
result.Add(matrix[top][i]);
}

top += 1;

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

right -= 1;
}

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

bottom -= 1;
}

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

left += 1;
}
}

return result;
}
}