diff --git a/solution/1700-1799/1762.Buildings With an Ocean View/README.md b/solution/1700-1799/1762.Buildings With an Ocean View/README.md index 0d2cc972fd183..8a37c823dfdba 100644 --- a/solution/1700-1799/1762.Buildings With an Ocean View/README.md +++ b/solution/1700-1799/1762.Buildings With an Ocean View/README.md @@ -58,11 +58,11 @@ **方法一:逆序遍历求右侧最大值** -逆序遍历数组 $height$ 每个元素 $v$,判断 $v$ 与右侧最大元素 $mx$ 的大小关系,若 $mx \lt v$,说明右侧所有元素都比当前元素小,当前位置能看到海景,加入结果数组 $ans$。 +我们逆序遍历数组 $height$ 每个元素 $v$,判断 $v$ 与右侧最大元素 $mx$ 的大小关系,若 $mx \lt v$,说明右侧所有元素都比当前元素小,当前位置能看到海景,加入结果数组 $ans$。然后我们更新 $mx$ 为 $v$。 -最后逆序返回 $ans$。 +遍历结束后,逆序返回 $ans$ 即可。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。 +时间复杂度 $O(n)$,其中 $n$ 为数组长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 @@ -73,13 +73,12 @@ ```python class Solution: def findBuildings(self, heights: List[int]) -> List[int]: - mx = 0 ans = [] + mx = 0 for i in range(len(heights) - 1, -1, -1): - v = heights[i] - if mx < v: + if heights[i] > mx: ans.append(i) - mx = v + mx = heights[i] return ans[::-1] ``` @@ -90,16 +89,17 @@ class Solution: ```java class Solution { public int[] findBuildings(int[] heights) { + int n = heights.length; + List ans = new ArrayList<>(); int mx = 0; - LinkedList ans = new LinkedList<>(); for (int i = heights.length - 1; i >= 0; --i) { - int v = heights[i]; - if (mx < v) { - ans.addFirst(i); - mx = v; + if (heights[i] > mx) { + ans.add(i); + mx = heights[i]; } } - return ans.stream().mapToInt(i -> i).toArray(); + Collections.reverse(ans); + return ans.stream().mapToInt(Integer::intValue).toArray(); } } ``` @@ -110,13 +110,12 @@ class Solution { class Solution { public: vector findBuildings(vector& heights) { - int mx = 0; vector ans; + int mx = 0; for (int i = heights.size() - 1; ~i; --i) { - int v = heights[i]; - if (mx < v) { + if (heights[i] > mx) { ans.push_back(i); - mx = v; + mx = heights[i]; } } reverse(ans.begin(), ans.end()); @@ -128,12 +127,10 @@ public: ### **Go** ```go -func findBuildings(heights []int) []int { +func findBuildings(heights []int) (ans []int) { mx := 0 - ans := []int{} for i := len(heights) - 1; i >= 0; i-- { - v := heights[i] - if mx < v { + if v := heights[i]; v > mx { ans = append(ans, i) mx = v } @@ -141,7 +138,23 @@ func findBuildings(heights []int) []int { for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 { ans[i], ans[j] = ans[j], ans[i] } - return ans + return +} +``` + +### **TypeScript** + +```ts +function findBuildings(heights: number[]): number[] { + const ans: number[] = []; + let mx = 0; + for (let i = heights.length - 1; ~i; --i) { + if (heights[i] > mx) { + ans.push(i); + mx = heights[i]; + } + } + return ans.reverse(); } ``` @@ -153,13 +166,12 @@ func findBuildings(heights []int) []int { * @return {number[]} */ var findBuildings = function (heights) { + const ans = []; let mx = 0; - let ans = []; - for (let i = heights.length - 1; i >= 0; --i) { - const v = heights[i]; - if (mx < v) { + for (let i = heights.length - 1; ~i; --i) { + if (heights[i] > mx) { ans.push(i); - mx = v; + mx = heights[i]; } } return ans.reverse(); diff --git a/solution/1700-1799/1762.Buildings With an Ocean View/README_EN.md b/solution/1700-1799/1762.Buildings With an Ocean View/README_EN.md index d3f83bbc77a99..e9f6f93bd0233 100644 --- a/solution/1700-1799/1762.Buildings With an Ocean View/README_EN.md +++ b/solution/1700-1799/1762.Buildings With an Ocean View/README_EN.md @@ -52,13 +52,12 @@ ```python class Solution: def findBuildings(self, heights: List[int]) -> List[int]: - mx = 0 ans = [] + mx = 0 for i in range(len(heights) - 1, -1, -1): - v = heights[i] - if mx < v: + if heights[i] > mx: ans.append(i) - mx = v + mx = heights[i] return ans[::-1] ``` @@ -67,16 +66,17 @@ class Solution: ```java class Solution { public int[] findBuildings(int[] heights) { + int n = heights.length; + List ans = new ArrayList<>(); int mx = 0; - LinkedList ans = new LinkedList<>(); for (int i = heights.length - 1; i >= 0; --i) { - int v = heights[i]; - if (mx < v) { - ans.addFirst(i); - mx = v; + if (heights[i] > mx) { + ans.add(i); + mx = heights[i]; } } - return ans.stream().mapToInt(i -> i).toArray(); + Collections.reverse(ans); + return ans.stream().mapToInt(Integer::intValue).toArray(); } } ``` @@ -87,13 +87,12 @@ class Solution { class Solution { public: vector findBuildings(vector& heights) { - int mx = 0; vector ans; + int mx = 0; for (int i = heights.size() - 1; ~i; --i) { - int v = heights[i]; - if (mx < v) { + if (heights[i] > mx) { ans.push_back(i); - mx = v; + mx = heights[i]; } } reverse(ans.begin(), ans.end()); @@ -105,12 +104,10 @@ public: ### **Go** ```go -func findBuildings(heights []int) []int { +func findBuildings(heights []int) (ans []int) { mx := 0 - ans := []int{} for i := len(heights) - 1; i >= 0; i-- { - v := heights[i] - if mx < v { + if v := heights[i]; v > mx { ans = append(ans, i) mx = v } @@ -118,7 +115,23 @@ func findBuildings(heights []int) []int { for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 { ans[i], ans[j] = ans[j], ans[i] } - return ans + return +} +``` + +### **TypeScript** + +```ts +function findBuildings(heights: number[]): number[] { + const ans: number[] = []; + let mx = 0; + for (let i = heights.length - 1; ~i; --i) { + if (heights[i] > mx) { + ans.push(i); + mx = heights[i]; + } + } + return ans.reverse(); } ``` @@ -130,13 +143,12 @@ func findBuildings(heights []int) []int { * @return {number[]} */ var findBuildings = function (heights) { + const ans = []; let mx = 0; - let ans = []; - for (let i = heights.length - 1; i >= 0; --i) { - const v = heights[i]; - if (mx < v) { + for (let i = heights.length - 1; ~i; --i) { + if (heights[i] > mx) { ans.push(i); - mx = v; + mx = heights[i]; } } return ans.reverse(); diff --git a/solution/1700-1799/1762.Buildings With an Ocean View/Solution.cpp b/solution/1700-1799/1762.Buildings With an Ocean View/Solution.cpp index 0c3dcbb8f2a85..bea5f6c9e632b 100644 --- a/solution/1700-1799/1762.Buildings With an Ocean View/Solution.cpp +++ b/solution/1700-1799/1762.Buildings With an Ocean View/Solution.cpp @@ -1,16 +1,15 @@ -class Solution { -public: - vector findBuildings(vector& heights) { - int mx = 0; - vector ans; - for (int i = heights.size() - 1; ~i; --i) { - int v = heights[i]; - if (mx < v) { - ans.push_back(i); - mx = v; - } - } - reverse(ans.begin(), ans.end()); - return ans; - } +class Solution { +public: + vector findBuildings(vector& heights) { + vector ans; + int mx = 0; + for (int i = heights.size() - 1; ~i; --i) { + if (heights[i] > mx) { + ans.push_back(i); + mx = heights[i]; + } + } + reverse(ans.begin(), ans.end()); + return ans; + } }; \ No newline at end of file diff --git a/solution/1700-1799/1762.Buildings With an Ocean View/Solution.go b/solution/1700-1799/1762.Buildings With an Ocean View/Solution.go index d3ee391ab8883..645d61b734d0e 100644 --- a/solution/1700-1799/1762.Buildings With an Ocean View/Solution.go +++ b/solution/1700-1799/1762.Buildings With an Ocean View/Solution.go @@ -1,9 +1,7 @@ -func findBuildings(heights []int) []int { +func findBuildings(heights []int) (ans []int) { mx := 0 - ans := []int{} for i := len(heights) - 1; i >= 0; i-- { - v := heights[i] - if mx < v { + if v := heights[i]; v > mx { ans = append(ans, i) mx = v } @@ -11,5 +9,5 @@ func findBuildings(heights []int) []int { for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 { ans[i], ans[j] = ans[j], ans[i] } - return ans + return } \ No newline at end of file diff --git a/solution/1700-1799/1762.Buildings With an Ocean View/Solution.java b/solution/1700-1799/1762.Buildings With an Ocean View/Solution.java index 457fdfdb436ba..245d6237a2bc7 100644 --- a/solution/1700-1799/1762.Buildings With an Ocean View/Solution.java +++ b/solution/1700-1799/1762.Buildings With an Ocean View/Solution.java @@ -1,14 +1,15 @@ -class Solution { - public int[] findBuildings(int[] heights) { - int mx = 0; - LinkedList ans = new LinkedList<>(); - for (int i = heights.length - 1; i >= 0; --i) { - int v = heights[i]; - if (mx < v) { - ans.addFirst(i); - mx = v; - } - } - return ans.stream().mapToInt(i -> i).toArray(); - } +class Solution { + public int[] findBuildings(int[] heights) { + int n = heights.length; + List ans = new ArrayList<>(); + int mx = 0; + for (int i = heights.length - 1; i >= 0; --i) { + if (heights[i] > mx) { + ans.add(i); + mx = heights[i]; + } + } + Collections.reverse(ans); + return ans.stream().mapToInt(Integer::intValue).toArray(); + } } \ No newline at end of file diff --git a/solution/1700-1799/1762.Buildings With an Ocean View/Solution.js b/solution/1700-1799/1762.Buildings With an Ocean View/Solution.js index ad83ac82a20fa..6f28548d3e8e7 100644 --- a/solution/1700-1799/1762.Buildings With an Ocean View/Solution.js +++ b/solution/1700-1799/1762.Buildings With an Ocean View/Solution.js @@ -3,13 +3,12 @@ * @return {number[]} */ var findBuildings = function (heights) { + const ans = []; let mx = 0; - let ans = []; - for (let i = heights.length - 1; i >= 0; --i) { - const v = heights[i]; - if (mx < v) { + for (let i = heights.length - 1; ~i; --i) { + if (heights[i] > mx) { ans.push(i); - mx = v; + mx = heights[i]; } } return ans.reverse(); diff --git a/solution/1700-1799/1762.Buildings With an Ocean View/Solution.py b/solution/1700-1799/1762.Buildings With an Ocean View/Solution.py index abbe432ea43d0..2c0ed5aec0375 100644 --- a/solution/1700-1799/1762.Buildings With an Ocean View/Solution.py +++ b/solution/1700-1799/1762.Buildings With an Ocean View/Solution.py @@ -1,10 +1,9 @@ -class Solution: - def findBuildings(self, heights: List[int]) -> List[int]: - mx = 0 - ans = [] - for i in range(len(heights) - 1, -1, -1): - v = heights[i] - if mx < v: - ans.append(i) - mx = v - return ans[::-1] +class Solution: + def findBuildings(self, heights: List[int]) -> List[int]: + ans = [] + mx = 0 + for i in range(len(heights) - 1, -1, -1): + if heights[i] > mx: + ans.append(i) + mx = heights[i] + return ans[::-1] diff --git a/solution/1700-1799/1762.Buildings With an Ocean View/Solution.ts b/solution/1700-1799/1762.Buildings With an Ocean View/Solution.ts new file mode 100644 index 0000000000000..789d47fa975b4 --- /dev/null +++ b/solution/1700-1799/1762.Buildings With an Ocean View/Solution.ts @@ -0,0 +1,11 @@ +function findBuildings(heights: number[]): number[] { + const ans: number[] = []; + let mx = 0; + for (let i = heights.length - 1; ~i; --i) { + if (heights[i] > mx) { + ans.push(i); + mx = heights[i]; + } + } + return ans.reverse(); +} diff --git a/solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md b/solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md index 0a10f917ba79e..14d9bf1bfa263 100644 --- a/solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md +++ b/solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md @@ -54,13 +54,13 @@ 我们将花按照开始时间和结束时间分别排序,然后对于每个人,我们可以使用二分查找来找到他们到达时在花期内花的数目。就是说,找出在每个人到达时,已经开花的花的数目,减去在每个人到达时,已经凋谢的花的数目,即可得到答案。 -时间复杂度 $O((m + n) \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $flowers$ 和 $persons$ 的长度。 +时间复杂度 $O((m + n) \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $flowers$ 和 $people$ 的长度。 **方法二:差分 + 排序 + 离线查询** -我们可以利用差分来维护每个时间点的花的数目。接下来,我们将 $persons$ 按照到达时间从小到大排序,在每个人到达时,我们对差分数组进行前缀和运算,就可以得到答案。 +我们可以利用差分来维护每个时间点的花的数目。接下来,我们将 $people$ 按照到达时间从小到大排序,在每个人到达时,我们对差分数组进行前缀和运算,就可以得到答案。 -时间复杂度 $O(m \times \log m + n \times \log n)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是数组 $flowers$ 和 $persons$ 的长度。 +时间复杂度 $O(m \times \log m + n \times \log n)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是数组 $flowers$ 和 $people$ 的长度。 @@ -71,15 +71,17 @@ ```python class Solution: def fullBloomFlowers( - self, flowers: List[List[int]], persons: List[int] + self, flowers: List[List[int]], people: List[int] ) -> List[int]: start, end = sorted(a for a, _ in flowers), sorted(b for _, b in flowers) - return [bisect_right(start, p) - bisect_left(end, p) for p in persons] + return [bisect_right(start, p) - bisect_left(end, p) for p in people] ``` ```python class Solution: - def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]: + def fullBloomFlowers( + self, flowers: List[List[int]], people: List[int] + ) -> List[int]: d = defaultdict(int) for st, ed in flowers: d[st] += 1 diff --git a/solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md b/solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md index 429c7758e2a24..05bfcc2fcb8a0 100644 --- a/solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md +++ b/solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md @@ -46,28 +46,30 @@ For each person, we return the number of flowers in full bloom during their arri We sort the flowers by their start and end time respectively, and then for each person, we can use binary search to find the number of flowers they bloom during the flowering period. That is, find the number of flowers that have bloomed when each person arrives, minus the number of flowers that have withered when each person arrives, to get the answer. -The time complexity is $O((m + n) \times \log n)$, and the space complexity is $O(n)$. Where $n$ and $m$ are the lengths of arrays $flowers$ and $persons$ respectively. +The time complexity is $O((m + n) \times \log n)$, and the space complexity is $O(n)$. Where $n$ and $m$ are the lengths of arrays $flowers$ and $people$ respectively. **Solution 2: Difference + Sort + Offline Query** -We can use the difference to maintain the number of flowers at each time point. Next, we sort $persons$ in ascending order of arrival time, and at each person's arrival, we perform a prefix sum operation on the difference array to get the answer. +We can use the difference to maintain the number of flowers at each time point. Next, we sort $people$ in ascending order of arrival time, and at each person's arrival, we perform a prefix sum operation on the difference array to get the answer. -The time complexity is $O(m \times \log m + n \times \log n)$, and the space complexity is $O(n + m)$. Where $n$ and $m$ are the lengths of arrays $flowers$ and $persons$ respectively. +The time complexity is $O(m \times \log m + n \times \log n)$, and the space complexity is $O(n + m)$. Where $n$ and $m$ are the lengths of arrays $flowers$ and $people$ respectively. ### **Python3** ```python class Solution: def fullBloomFlowers( - self, flowers: List[List[int]], persons: List[int] + self, flowers: List[List[int]], people: List[int] ) -> List[int]: start, end = sorted(a for a, _ in flowers), sorted(b for _, b in flowers) - return [bisect_right(start, p) - bisect_left(end, p) for p in persons] + return [bisect_right(start, p) - bisect_left(end, p) for p in people] ``` ```python class Solution: - def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]: + def fullBloomFlowers( + self, flowers: List[List[int]], people: List[int] + ) -> List[int]: d = defaultdict(int) for st, ed in flowers: d[st] += 1