diff --git a/Problem1.cs b/Problem1.cs new file mode 100644 index 00000000..d1f929c6 --- /dev/null +++ b/Problem1.cs @@ -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; + } +} \ No newline at end of file diff --git a/Problem2.cs b/Problem2.cs new file mode 100644 index 00000000..d8e8f145 --- /dev/null +++ b/Problem2.cs @@ -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; + + } +} \ No newline at end of file diff --git a/Problem3.cs b/Problem3.cs new file mode 100644 index 00000000..5c97d02c --- /dev/null +++ b/Problem3.cs @@ -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 SpiralOrder(int[][] matrix) { + List 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; + } +} \ No newline at end of file