diff --git a/H-Index.java b/H-Index.java new file mode 100644 index 00000000..a962ef0b --- /dev/null +++ b/H-Index.java @@ -0,0 +1,32 @@ +/* +* Approach1: Sort the array and apply the concept of H-index2 here +Approach 2: + - As we know the max val of h index is going to be length of n, so we can use that to get a solution in linear time + - first pass: So idea is to use bucket sort and add the number of papers that has citations = that index + - second pass: go through the bucket array and you can compare number of papers >= number of citations +- TC: O(n) -> its actually 2n -> one for iterating the citations array and another one to iterate through the buckets array but 2 doesnt matter so its O(n) +- SC: O(n) -> extra space used to do bucket sort + */ +class Solution { + public int hIndex(int[] citations) { + //bucket array to store the freq of papers where index will act like citations and element will act like papers + int[] bucket = new int[citations.length + 1]; + int papers = 0; + //go through citations array to get the citation for each paper + for(int i = 0; i < citations.length; i++){ + // if the citation for a particular paper falls within the range of bucket, we increment the citation index by 1 else anything greater than the bucket size, the number of citations doesnt so we increment the value of papers in the max citation possible + if(citations[i] < bucket.length) + bucket[citations[i]] += 1; + else + bucket[citations.length] += 1; + } + + // iterate through the bucket array, check when the equality for number of papers >= number of citations and return citations else return 0 + for(int citation = bucket.length - 1; citation >= 0; citation--){ + papers += bucket[citation]; + if(papers >= citation) + return citation; + } + return 0; + } +} \ No newline at end of file diff --git a/RotateArray.java b/RotateArray.java new file mode 100644 index 00000000..3d5bbb04 --- /dev/null +++ b/RotateArray.java @@ -0,0 +1,31 @@ +/* +* Approach 1: first approach that comes to mind, is to use extra space and start iterating from n - k to n - 1 and then iterate from 0 to n - k - 1 and store it in an array and then copy from the new array to the current array. +TC: O(2n)-> O(n) -> iterate through the array and again iterate through the new array and save it to the given array +SC: O(n) -> additional array space + +Approach 2: Inplace without extra space +- Reverse the entire array, then reverse from 0th index to n - k and then from n - k + 1 to n - 1 or rotate two halves first and then rotate the entire array +TC: O(n) -> iterate through all the elements in the nums array +SC: O(1) -> no additional space +*/ +class Solution { + public void rotate(int[] nums, int k) { + k = k % nums.length; + int n = nums.length; + reverse(nums, 0, n - k - 1); + reverse(nums, n - k, n - 1); + reverse(nums, 0, n - 1); + + } + + private int[] reverse(int[] nums, int l, int r){ + while(l < r){ + int temp = nums[l]; + nums[l] = nums[r]; + nums[r] = temp; + l++; + r--; + } + return nums; + } +} \ No newline at end of file diff --git a/TrappingRainWater.java b/TrappingRainWater.java new file mode 100644 index 00000000..98e16fc3 --- /dev/null +++ b/TrappingRainWater.java @@ -0,0 +1,50 @@ +/* +* Approach 1: Vertical water trapping strategy +- The idea is to find the tallest wall that will act as a dam to hold water from both the sides +- We need to track the tallest wall ecountered on the left side of the current height(lw) and similary we need to keep track of the tallest wall encountered on the right when parsing through the heights from the right +- While iterating the height from the 0th element to the tallest wall, + - if lw < height[i] -> that means for the elements moving forward, that should act as a lw, so move lw to i + - if lw > height[i] -> that means, we can trap water between the current building and the left wall +- Similary when moving from right to the tallest wall, + - if rw < height[i] -> that means for the elements moving forward, that should act as a rw, so move rw to i + - if rw > height[i] -> that means, we can trap water between the current building and the right wall +TC: O(2n) -> O(n) -> one pass to get the tallest building, and another pass to traverse the array from right and left till the tallest building +SC: O(1) -> no additional space used +*/ + +class Solution { + public int trap(int[] height) { + int lw = 0; + int rw = height.length - 1; + int tallest = 0; + int total = 0; + + //find max height wall which will act as a dam + for(int i = 0; i < height.length; i++){ + if(height[i] > height[tallest]) + tallest = i; + } + + // count total water trapped from left to the tallest height building + for(int i = 0; i < tallest; i++){ + if(height[lw] > height[i]){ + //we can trap water of unit difference of left wall(tallest wall on the left) and current height + total += height[lw] - height[i]; + } + else if(height[lw] < height[i]){ + //move the lw to current greater height encountered + lw = i; + } + } + + // count total water trapped from right to the tallest height building + for(int i = rw; i > tallest; i--){ + if(height[rw] > height[i]){ + total += height[rw] - height[i]; + } + else if(height[rw] < height[i]) + rw = i; + } + return total; + } +} \ No newline at end of file