diff --git a/Problem1.cs b/Problem1.cs new file mode 100644 index 00000000..3b1bf7b4 --- /dev/null +++ b/Problem1.cs @@ -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> Generate(int numRows) { + List> result = new(); + result.Add(new List{1}); + + if(numRows == 1) + { + return result; + } + + for(int i = 2; i <= numRows; i++) + { + List temp = new(); + temp.Add(1); + IList 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; + + } +} \ No newline at end of file diff --git a/Problem2.cs b/Problem2.cs new file mode 100644 index 00000000..ee731870 --- /dev/null +++ b/Problem2.cs @@ -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 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; + } +} \ No newline at end of file