From 032e84adbc0d2dc87543ec4b8d3a640ecdf684cf Mon Sep 17 00:00:00 2001 From: Monit Sharma <65262068+MonitSharma@users.noreply.github.com> Date: Thu, 31 Aug 2023 12:30:38 +0800 Subject: [PATCH] Added 1326 --- .../solution.cpp | 23 +++++++++++++++++ .../solution.java | 25 +++++++++++++++++++ .../solution.py | 20 +++++++++++++++ README.md | 1 + 4 files changed, 69 insertions(+) create mode 100644 1326-Minimum number of taps to open to water a garden/solution.cpp create mode 100644 1326-Minimum number of taps to open to water a garden/solution.java create mode 100644 1326-Minimum number of taps to open to water a garden/solution.py diff --git a/1326-Minimum number of taps to open to water a garden/solution.cpp b/1326-Minimum number of taps to open to water a garden/solution.cpp new file mode 100644 index 0000000..ed4c357 --- /dev/null +++ b/1326-Minimum number of taps to open to water a garden/solution.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int minTaps(int n, vector& ranges) { + vector arr(n + 1, 0); + for(int i = 0; i < ranges.size(); ++i) { + if(ranges[i] == 0) continue; + int left = max(0, i - ranges[i]); + arr[left] = max(arr[left], i + ranges[i]); + } + + int end = 0, far_can_reach = 0, cnt = 0; + for(int i = 0; i <= n; ++i) { + if(i > end) { + if(far_can_reach <= end) return -1; + end = far_can_reach; + ++cnt; + } + far_can_reach = max(far_can_reach, arr[i]); + } + + return cnt + (end < n); + } +}; \ No newline at end of file diff --git a/1326-Minimum number of taps to open to water a garden/solution.java b/1326-Minimum number of taps to open to water a garden/solution.java new file mode 100644 index 0000000..127b764 --- /dev/null +++ b/1326-Minimum number of taps to open to water a garden/solution.java @@ -0,0 +1,25 @@ +class Solution { + public int minTaps(int n, int[] ranges) { + int[] nums = new int[n + 1]; + + for (int i = 0; i <= n; ++i) { + int l = Math.max(0, i - ranges[i]); + int r = Math.min(n, i + ranges[i]); + nums[l] = Math.max(nums[l], r - l); + } + + int ans = 0; + int end = 0; + int farthest = 0; + + for (int i = 0; i < n; i++) { + farthest = Math.max(farthest, i + nums[i]); + if (i == end) { + ++ans; + end = farthest; + } + } + + return end == n ? ans : -1; + } +} \ No newline at end of file diff --git a/1326-Minimum number of taps to open to water a garden/solution.py b/1326-Minimum number of taps to open to water a garden/solution.py new file mode 100644 index 0000000..99df804 --- /dev/null +++ b/1326-Minimum number of taps to open to water a garden/solution.py @@ -0,0 +1,20 @@ +class Solution: + def minTaps(self, n: int, ranges: List[int]) -> int: + nums = [0] * (n + 1) + + for i, range_ in enumerate(ranges): + l = max(0, i - range_) + r = min(n, range_ + i) + nums[l] = max(nums[l], r - l) + + ans = 0 + end = 0 + farthest = 0 + + for i in range(n): + farthest = max(farthest, i + nums[i]) + if i == end: + ans += 1 + end = farthest + + return ans if end == n else -1 \ No newline at end of file diff --git a/README.md b/README.md index 164649f..2fb3d73 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ This repository contains solutions to over 400 Leetcode problems that are organi | 1232 | Check If It Is a Straight Line | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1232-check-if-it-is-a-straight-line-bf34f9460302) | | 1312 | Minimum Insertion Steps to Make a String Palindrome | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1312-minimum-insertion-steps-to-make-a-string-palindrome-1f639f96ad68) | | 1318 | Minimum Flips to Make a OR b Equal to c | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1318-minimum-flips-to-make-a-or-b-equal-to-c-6690d91c0e47) | +| 1326 | Minimum Number of Taps to Open to Water a Garden | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1326-minimum-number-of-taps-to-open-to-water-a-garden-83bb886e54c0) | | 1351 | Count Negative Numbers in a Sorted Matrix | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1351-count-negative-numbers-in-a-sorted-matrix-13b7bb33487e) | | 1372 | Longest ZigZag Path in a Binary Tree | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1372-longest-zigzag-path-in-a-binary-tree-8cc04cadfe21) | | 1376 | Time needed to inform all employees | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1376-time-needed-to-inform-all-employees-2787441bc80c) |