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
38 changes: 38 additions & 0 deletions Problem1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 void SortColors(int[] nums) {
int slow = 0, mid = 0, fast = nums.Length - 1;

while(mid <= fast)
{
if(nums[mid] == 2)
{
Swap(nums, mid, fast);
fast--;
}

else if(nums[mid] == 0)
{
Swap(nums, slow, mid);
slow++;
mid++;
}

else
{
mid++;
}
}
}

public void Swap(int[] nums, int i, int j)
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
50 changes: 50 additions & 0 deletions Problem2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Time Complexity : O(n) where n is the total length of nums array
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

public class Solution {
public IList<IList<int>> ThreeSum(int[] nums) {
Array.Sort(nums);
List<IList<int>> result = new();
for(int i = 0; i < nums.Length - 2; i++)
{
if(i>0 && nums[i]==nums[i-1])
{
continue;
}

int first = nums[i];
int second = i+1, third = nums.Length -1;
while(second < third)
{
if(nums[second] + nums[third] == -first)
{
List<int> temp = new();
temp.Add(first);
temp.Add(nums[second]);
temp.Add(nums[third]);
result.Add(temp);
while(second < third && nums[second] == nums[second+1])
second++;
while(second < third && nums[third] == nums[third-1])
third--;
second++;
third--;
}

else if(nums[second] + nums[third] < -first)
{
second ++;
}

else
{
third --;
}
}
}

return result;
}
}
29 changes: 29 additions & 0 deletions Problem3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Time Complexity : O(n) where n is the total length of height 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 MaxArea(int[] height) {
int low = 0, high = height.Length - 1;
int maxArea = 0;

while(low<=high)
{
int minHeight = Math.Min(height[low], height[high]);
maxArea = Math.Max(maxArea, minHeight * (high-low));

if(height[low] < height[high])
{
low++;
}

else
{
high--;
}
}

return maxArea;
}
}