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
43 changes: 43 additions & 0 deletions Problem1.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
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach

/*
I create a list of integer lists and add [1] to it. If numRows == 1, I return the result straightaway. Else I iterate i = 2 to numRows number of times, I create a temp list for each iteration and add
[1,1] as the first and last element of the list. All the other elements of the list are made up by the sum of each pair of numbers present in the previous list.
*/

public class Solution {
public IList<IList<int>> Generate(int numRows) {
List<IList<int>> result = new();
result.Add(new List<int>{1});

if(numRows == 1)
{
return result;
}

for(int i = 2; i <= numRows; i++)
{
List<int> temp = new();
temp.Add(1);
IList<int> previousList = result[result.Count - 1];

for(int j = 1; j < previousList.Count; j++)
{
temp.Add(previousList[j] + previousList[j-1]);
}

temp.Add(1);
result.Add(temp);

}

return result;

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


// Your code here along with comments explaining your approach

/*
I maintain a dictionary which holds the frequency of each element present in nums. I then iterate through the keys of the dictionary and obtain the frequency of the key. If k == 0 and frequency of the key
>1, I add 1 to the result. For other values of k, I check if the dictionary contains key + k, if so I increment result by 1.
*/

public class Solution {
public int FindPairs(int[] nums, int k) {
int result = 0;
Dictionary<int, int> hashMap = new();
for(int i = 0; i < nums.Length; i++)
{
hashMap[nums[i]] = hashMap.GetValueOrDefault(nums[i], 0) + 1;
}

foreach(int key in hashMap.Keys)
{
int frequency = hashMap[key];

if(k==0)
{
if(frequency>1)
{
result += 1;
}
}

else
{
if(hashMap.ContainsKey(key + k))
{
result += 1;
}
}
}

return result;
}
}