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 IList<int> FindDisappearedNumbers(int[] nums) {
List<int> result = new();

for(int i = 0; i < nums.Length; i++)
{
int index = Math.Abs(nums[i]) - 1;

if(nums[index] > 0)
{
nums[index] *= -1;
}
}

for(int i = 0; i < nums.Length; i++)
{
if(nums[i] > 0)
{
result.Add(i+1);
}
}

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

public class Solution {
public List<int> getMinMax(int[] arr) {
// code here
List<int> result = new();
int min = 0, max = 0;
int i;

if(arr.Length % 2 != 0)
{
min = arr[0];
max = arr[0];
i=1;
}

else
{
min = Math.Min(arr[0], arr[1]);
max = Math.Max(arr[0], arr[1]);
i=2;
}


while(i<arr.Length)
{
if(arr[i] < arr[i+1])
{
min = Math.Min(arr[i], min);
max = Math.Max(arr[i+1], max);
}

else
{
min = Math.Min(arr[i+1], min);
max = Math.Max(arr[i], max);
}

i+=2;
}

result.Add(min);
result.Add(max);
return result;
}
}
88 changes: 88 additions & 0 deletions Problem3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Time Complexity : O(m*n) where m & n are the dimensions of board
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
public class Solution {
private int rows;
private int columns;
private int[][] dirs;
public void GameOfLife(int[][] board) {
rows = board.Length;
columns = board[0].Length;
dirs = new int[][]
{
new int[] {0,1},
new int[] {0,-1},
new int[] {1,0},
new int[] {-1,0},
new int[] {-1,-1},
new int[] {1,1},
new int[] {-1,1},
new int[] {1,-1},
};

Helper(board);

for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
// 1 -> 0 : 2
if(board[i][j] == 2)
{
board[i][j] = 0;
}

// 0 -> 1 : 3
else if(board[i][j] == 3)
{
board[i][j] = 1;
}
}
}
}

public void Helper(int[][] board)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
int neighbours = GetNeighbours(board, i , j);

if(board[i][j] == 1)
{
if(neighbours < 2 || neighbours > 3)
{
board[i][j] = 2;
}
}

else if(board[i][j] == 0)
{
if(neighbours == 3)
{
board[i][j] = 3;
}
}
}
}
}

public int GetNeighbours(int[][] board, int i, int j)
{
int neighbours = 0;

foreach(int[] dir in dirs)
{
int nr = i + dir[0];
int nc = j + dir[1];
if(nr>=0 && nr<rows && nc>=0 && nc<columns && (board[nr][nc] == 1 || board[nr][nc] == 2))
{
neighbours += 1;
}
}

return neighbours;
}
}