diff --git a/C++/H-Index.cpp b/C++/H-Index.cpp new file mode 100644 index 00000000..f48b91f2 --- /dev/null +++ b/C++/H-Index.cpp @@ -0,0 +1,37 @@ +/* medium difficulty */ + +class Solution{ +public: + int hIndex(vector<int> &citations) + { + int n = citations.size(); + int mpp[1001] = {0}; + + for (int i = 0; i < n; i++) + { + if (citations[i] > 1000) + { + mpp[1000]++; + } + else + { + mpp[citations[i]]++; + } + } + + for (int i = 999; i >= 0; i--) + { + mpp[i] += mpp[i + 1]; + } + + for (int i = 1000; i >= 0; i--) + { + if (mpp[i] >= i) + { + return i; + } + } + + return 0; + } +}; \ No newline at end of file diff --git a/C++/trapping-rain-water.cpp b/C++/trapping-rain-water.cpp new file mode 100644 index 00000000..743afbdb --- /dev/null +++ b/C++/trapping-rain-water.cpp @@ -0,0 +1,37 @@ +int trap(vector<int> &height) +{ + int heightSize = height.size(); + + int res = 0; + + int lmax[heightSize], rmax[heightSize]; + + lmax[0] = height[0]; + for (int i = 1; i < heightSize; i++) + { + lmax[i] = max(height[i], lmax[i - 1]); + } + + rmax[heightSize - 1] = height[heightSize - 1]; + for (int i = heightSize - 2; i >= 0; i--) + { + rmax[i] = max(height[i], rmax[i + 1]); + } + + for (int i = 1; i < heightSize - 1; i++) + { + res = res + (min(lmax[i], rmax[i]) - height[i]); + } + + return res; +} + +// test case 1 +// height = [0,1,0,2,1,0,1,3,2,1,2,1] +// output = 6 + +// test case 2 +// height = [4,2,0,3,2,5] +// output = 9 + +// time complexity --> O(n) \ No newline at end of file diff --git a/README.md b/README.md index b2cac739..2c8f767a 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 1512 | [Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/) | [Java](./Java/Number-of-Good-Pairs.java) | O(N^2) | O(1) | Easy | Array | | 162 | [Find Peak element](https://leetcode.com/problems/find-peak-element/) | [javascript](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/JavaScript/findPeakElement.js) | o(Logn) | O(1) | Medium | Array | | 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [C++](./C++/Spiral-matrix.cpp) | O(M\*N) | O(M\*N) | Medium | Array | -| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [C++](./C++/238.Product_of_array_except_self) | O(N) | O(N) | Medium | Array | +| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [C++](./C++/238.Product_of_array_except_self) | O(N) | O(N) | Medium | Array || +274 | [H-Index](https://leetcode.com/problems/h-index) | [C++](./C++/H-Index.cpp) | O(n) | O(1) | Medium | Array | | <br/> @@ -515,6 +516,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if | [Shrimadh V Rao](https://github.com/Shrimadh) <br> <img src="https://avatars.githubusercontent.com/u/64469917?v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/Shrimadh) | [Shreyas Shrawage](https://github.com/shreyventure) <br> <img src = "https://avatars.githubusercontent.com/u/55741087?v=4" width="100" height="100"> | India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)<br/>[LeetCode](https://leetcode.com/shreyventure/)<br/>[HackerRank](https://www.hackerrank.com/shreyas_shrawage) | [Surbhi Mayank](https://github.com/surbhi2408) <br> <img src="https://avatars.githubusercontent.com/u/58289829?s=400&u=68fd396819b927ec4d8820d87d6d1e311c3abd01&v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/surbhi2408) +| [Purval Bhude](https://github.com/PurvalBhude) <br> <img src="https://avatars.githubusercontent.com/u/143245454?s=96&v=4" width="100" height="100"> | India | C++ | [Linkedin](https://www.linkedin.com/in/purvalbhude)<br/>[LeetCode](https://leetcode.com/Purval_Bhude/)<br/>[Github](https://github.com/PurvalBhude) <div align="right"> <b><a href="#algorithms">⬆️ Back to Top</a></b> </div>