diff --git a/Greedy/2345.Finding-the-Number-of-Visible-Mountains/2345.Finding-the-Number-of-Visible-Mountains.cpp b/Greedy/2345.Finding-the-Number-of-Visible-Mountains/2345.Finding-the-Number-of-Visible-Mountains.cpp new file mode 100644 index 000000000..b913c51eb --- /dev/null +++ b/Greedy/2345.Finding-the-Number-of-Visible-Mountains/2345.Finding-the-Number-of-Visible-Mountains.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int visibleMountains(vector>& peaks) + { + sort(peaks.begin(), peaks.end(), [](vector&a, vector&b){ + int l1=a[0]-a[1], r1=a[0]+a[1]; + int l2=b[0]-b[1], r2=b[0]+b[1]; + if (l1!=l2) return l1r2; + }); + + int n = peaks.size(); + int rightMost = -1; + int ret = 0; + for (int i=0; i0 && peaks[i]==peaks[i-1]) continue; + if (peaks[i][0]+peaks[i][1]> rightMost) + { + rightMost = peaks[i][0]+peaks[i][1]; + if (i==n-1 || peaks[i]!=peaks[i+1]) + ret++; + } + } + + return ret; + } +}; diff --git a/Readme.md b/Readme.md index ea0c20744..795ee99d1 100644 --- a/Readme.md +++ b/Readme.md @@ -1201,6 +1201,7 @@ [2250.Count-Number-of-Rectangles-Containing-Each-Point](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2250.Count-Number-of-Rectangles-Containing-Each-Point) (H-) [2343.Query-Kth-Smallest-Trimmed-Number](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2343.Query-Kth-Smallest-Trimmed-Number) (H-) [2412.Minimum-Money-Required-Before-Transactions](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2412.Minimum-Money-Required-Before-Transactions) (H-) +[2345.Finding-the-Number-of-Visible-Mountains](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2345.Finding-the-Number-of-Visible-Mountains) (H-) * ``Indexing Sort`` [041.First-Missing-Positive](https://github.com/wisdompeak/LeetCode/blob/master/Greedy/041.First-Missing-Positive/Readme.md) (H) [268.Missing-Number](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/268.Missing-Number) (H-)

AltStyle によって変換されたページ (->オリジナル) /