diff --git a/Problem1.cs b/Problem1.cs new file mode 100644 index 00000000..74a1a7dd --- /dev/null +++ b/Problem1.cs @@ -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; + } +} \ No newline at end of file diff --git a/Problem2.cs b/Problem2.cs new file mode 100644 index 00000000..50422c92 --- /dev/null +++ b/Problem2.cs @@ -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--; + } + } + } +} \ No newline at end of file diff --git a/Problem3.cs b/Problem3.cs new file mode 100644 index 00000000..e0e4e35b --- /dev/null +++ b/Problem3.cs @@ -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; + } +} \ No newline at end of file