From 5857e59fbe3acbfece5ef3a3cbd77b3cb50c79dd Mon Sep 17 00:00:00 2001 From: PurvalBhude Date: Thu, 21 Dec 2023 15:59:24 +0530 Subject: [PATCH 1/4] Create trapping-rain-water.cpp Putting code in C++ for problem 42.trapping-rain-water problem --- C++/trapping-rain-water.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 C++/trapping-rain-water.cpp 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 &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 From 3d18e30f07ad8d6f26193b7ba3bf00b535c2c5ef Mon Sep 17 00:00:00 2001 From: PurvalBhude Date: Wed, 8 May 2024 18:53:59 +0530 Subject: [PATCH 2/4] Create 42. Trapping Rain Water.cpp --- C++/42. Trapping Rain Water.cpp | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 C++/42. Trapping Rain Water.cpp diff --git a/C++/42. Trapping Rain Water.cpp b/C++/42. Trapping Rain Water.cpp new file mode 100644 index 00000000..d7210165 --- /dev/null +++ b/C++/42. Trapping Rain Water.cpp @@ -0,0 +1,45 @@ +/*difficulty: hard*/ +/* question link: https://leetcode.com/problems/trapping-rain-water/ */ + +class Solution { +public: + int trap(vector& 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 From 42a223d77c12363def300b99efb6c6ebde45689e Mon Sep 17 00:00:00 2001 From: PurvalBhude Date: Sun, 15 Sep 2024 16:28:22 +0530 Subject: [PATCH 3/4] h-index added in cpp --- C++/H-Index.cpp | 37 +++++++++++++++++++++++++++++++++++++ README.md | 4 +++- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 C++/H-Index.cpp 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 &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/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 | |
@@ -515,6 +516,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if | [Shrimadh V Rao](https://github.com/Shrimadh)
| India | C++ | [GitHub](https://github.com/Shrimadh) | [Shreyas Shrawage](https://github.com/shreyventure)
| India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)
[LeetCode](https://leetcode.com/shreyventure/)
[HackerRank](https://www.hackerrank.com/shreyas_shrawage) | [Surbhi Mayank](https://github.com/surbhi2408)
| India | C++ | [GitHub](https://github.com/surbhi2408) +| [Purval Bhude](https://github.com/PurvalBhude)
| India | C++ | [Linkedin](https://www.linkedin.com/in/purvalbhude)
[LeetCode](https://leetcode.com/Purval_Bhude/)
[Github](https://github.com/PurvalBhude) From fe5aa6916c2f78959521f1e59d9a2e8fc5e832f5 Mon Sep 17 00:00:00 2001 From: PurvalBhude Date: Sun, 15 Sep 2024 16:31:43 +0530 Subject: [PATCH 4/4] delete --- C++/42. Trapping Rain Water.cpp | 45 --------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 C++/42. Trapping Rain Water.cpp diff --git a/C++/42. Trapping Rain Water.cpp b/C++/42. Trapping Rain Water.cpp deleted file mode 100644 index d7210165..00000000 --- a/C++/42. Trapping Rain Water.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/*difficulty: hard*/ -/* question link: https://leetcode.com/problems/trapping-rain-water/ */ - -class Solution { -public: - int trap(vector& 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