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 Problem1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english

/*
I maintain two pointers - slow pointing at index 0 of the array and fast pointing at index 1. I initialize the count to 1 and loop through the array till fast does not exceed the array limits. If nums[slow]
equals nums[fast], I increment the count. Else set count back to 1. If count <= 2, I swap element at fast with element at slow + 1. At the end, I return slow + 1 which is the last index of the sorted
array
*/

public class Solution
{
public int RemoveDuplicates(int[] nums)
{
int count = 1;
int slow = 0, fast = 1;

while (fast < nums.Length)
{

if (nums[slow] == nums[fast])
{
count++;
}

else
{
count = 1;
}

if (count <= 2)
{
Swap(nums, slow + 1, fast);
slow++;
}

fast++;
}

return slow + 1;
}

public void Swap(int[] nums, int i, int j)
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
43 changes: 43 additions & 0 deletions Problem2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english

/*
I maintain three pointers a, b and c that point to the last element of internal sorted array within nums1, last element of nums2 and the last element of nums1 . I loop through the array
elements while a>=0 && b>=0. If nums2[b] < nums1[a] I place the element of nums1[a] at nums1[c] and decrement a by 1. Else I place the element at nums2[b] at nums1[c] and decrement b by 1.
If there are any remaining elements in nums2 array, I move them all to nums1 and return the final result.
*/

public class Solution {
public void Merge(int[] nums1, int m, int[] nums2, int n) {
int c = m + n - 1, b = n - 1, a = m - 1;

while(a>=0 && b>=0)
{
if(nums2[b] < nums1[a])
{
nums1[c] = nums1[a];
a--;
}

else
{
nums1[c] = nums2[b];
b--;
}

c--;
}

if(b>=0)
{
while(b>=0)
{
nums1[c] = nums2[b];
b--;
c--;
}
}
}
}
39 changes: 39 additions & 0 deletions Problem3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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

/*
I start from the last element of the first row of the matrix. Since the matrix is both column and row sorted, any element greater than my current element would lie in the column below the element
and any smaller element would lie on the row before the element. Using this knowledge, I traverse through the array while my row index is less than total number of rows and column index is greater
than or equal to 0
*/

public class Solution
{
public bool SearchMatrix(int[][] matrix, int target)
{
int rows = matrix.Length, columns = matrix[0].Length;
int i = 0, j = columns - 1;

while (i < rows && j >= 0)
{
if (matrix[i][j] == target)
{
return true;
}

else if (matrix[i][j] < target)
{
i += 1;
}

else
{
j -= 1;
}
}

return false;
}
}