|
48 | 48 |
|
49 | 49 | <!-- 这里可写通用的实现逻辑 -->
|
50 | 50 |
|
| 51 | +**方法一:区间排序 + 遍历** |
| 52 | + |
| 53 | +我们先将每座山 $(x, y)$ 转换成横坐标的区间 $(x - y, x + y)$,然后对区间按照左端点升序排序,右端点降序排序。 |
| 54 | + |
| 55 | +接下来,初始化当前区间的右端点为 $-\infty$,遍历每座山,如果当前山的右端点小于等于当前区间的右端点,则跳过该山,否则更新当前区间的右端点为当前山的右端点,如果当前山的区间只出现一次,则答案加一。 |
| 56 | + |
| 57 | +遍历结束后返回答案即可。 |
| 58 | + |
| 59 | +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为山的数量。 |
| 60 | + |
51 | 61 | <!-- tabs:start -->
|
52 | 62 |
|
53 | 63 | ### **Python3**
|
54 | 64 |
|
55 | 65 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
56 | 66 |
|
57 | 67 | ```python
|
58 |
| - |
| 68 | +class Solution: |
| 69 | + def visibleMountains(self, peaks: List[List[int]]) -> int: |
| 70 | + arr = [(x - y, x + y) for x, y in peaks] |
| 71 | + cnt = Counter(arr) |
| 72 | + arr.sort(key=lambda x: (x[0], -x[1])) |
| 73 | + ans, cur = 0, -inf |
| 74 | + for l, r in arr: |
| 75 | + if r <= cur: |
| 76 | + continue |
| 77 | + cur = r |
| 78 | + if cnt[(l, r)] == 1: |
| 79 | + ans += 1 |
| 80 | + return ans |
59 | 81 | ```
|
60 | 82 |
|
61 | 83 | ### **Java**
|
62 | 84 |
|
63 | 85 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
64 | 86 |
|
65 | 87 | ```java
|
| 88 | +class Solution { |
| 89 | + public int visibleMountains(int[][] peaks) { |
| 90 | + int n = peaks.length; |
| 91 | + int[][] arr = new int[n][2]; |
| 92 | + Map<String, Integer> cnt = new HashMap<>(); |
| 93 | + for (int i = 0; i < n; ++i) { |
| 94 | + int x = peaks[i][0], y = peaks[i][1]; |
| 95 | + arr[i] = new int[] {x - y, x + y}; |
| 96 | + cnt.merge((x - y) + "" + (x + y), 1, Integer::sum); |
| 97 | + } |
| 98 | + Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); |
| 99 | + int ans = 0; |
| 100 | + int cur = Integer.MIN_VALUE; |
| 101 | + for (int[] e : arr) { |
| 102 | + int l = e[0], r = e[1]; |
| 103 | + if (r <= cur) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + cur = r; |
| 107 | + if (cnt.get(l + "" + r) == 1) { |
| 108 | + ++ans; |
| 109 | + } |
| 110 | + } |
| 111 | + return ans; |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +```java |
| 117 | +class Solution { |
| 118 | + public int visibleMountains(int[][] peaks) { |
| 119 | + int n = peaks.length; |
| 120 | + int[][] arr = new int[n][2]; |
| 121 | + for (int i = 0; i < n; ++i) { |
| 122 | + int x = peaks[i][0], y = peaks[i][1]; |
| 123 | + arr[i] = new int[] {x - y, x + y}; |
| 124 | + } |
| 125 | + Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); |
| 126 | + int ans = 0; |
| 127 | + int cur = Integer.MIN_VALUE; |
| 128 | + for (int i = 0; i < n; ++i) { |
| 129 | + int l = arr[i][0], r = arr[i][1]; |
| 130 | + if (r <= cur) { |
| 131 | + continue; |
| 132 | + } |
| 133 | + cur = r; |
| 134 | + if (!(i < n - 1 && arr[i][0] == arr[i + 1][0] && arr[i][1] == arr[i + 1][1])) { |
| 135 | + ++ans; |
| 136 | + } |
| 137 | + } |
| 138 | + return ans; |
| 139 | + } |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +### **C++** |
| 144 | + |
| 145 | +```cpp |
| 146 | +class Solution { |
| 147 | +public: |
| 148 | + int visibleMountains(vector<vector<int>>& peaks) { |
| 149 | + vector<pair<int, int>> arr; |
| 150 | + for (auto& e : peaks) { |
| 151 | + int x = e[0], y = e[1]; |
| 152 | + arr.emplace_back(x - y, -(x + y)); |
| 153 | + } |
| 154 | + sort(arr.begin(), arr.end()); |
| 155 | + int n = arr.size(); |
| 156 | + int ans = 0, cur = INT_MIN; |
| 157 | + for (int i = 0; i < n; ++i) { |
| 158 | + int l = arr[i].first, r = -arr[i].second; |
| 159 | + if (r <= cur) { |
| 160 | + continue; |
| 161 | + } |
| 162 | + cur = r; |
| 163 | + ans += i == n - 1 || (i < n - 1 && arr[i] != arr[i + 1]); |
| 164 | + } |
| 165 | + return ans; |
| 166 | + } |
| 167 | +}; |
| 168 | +``` |
66 | 169 |
|
| 170 | +### **Go** |
| 171 | +
|
| 172 | +```go |
| 173 | +func visibleMountains(peaks [][]int) (ans int) { |
| 174 | + n := len(peaks) |
| 175 | + type pair struct{ l, r int } |
| 176 | + arr := make([]pair, n) |
| 177 | + for _, p := range peaks { |
| 178 | + x, y := p[0], p[1] |
| 179 | + arr = append(arr, pair{x - y, x + y}) |
| 180 | + } |
| 181 | + sort.Slice(arr, func(i, j int) bool { return arr[i].l < arr[j].l || (arr[i].l == arr[j].l && arr[i].r > arr[j].r) }) |
| 182 | + cur := math.MinInt32 |
| 183 | + for i, e := range arr { |
| 184 | + l, r := e.l, e.r |
| 185 | + if r <= cur { |
| 186 | + continue |
| 187 | + } |
| 188 | + cur = r |
| 189 | + if !(i < n-1 && l == arr[i+1].l && r == arr[i+1].r) { |
| 190 | + ans++ |
| 191 | + } |
| 192 | + } |
| 193 | + return |
| 194 | +} |
67 | 195 | ```
|
68 | 196 |
|
69 | 197 | ### **TypeScript**
|
|
0 commit comments