@@ -63,24 +63,119 @@ Note that it does not matter if the fence encloses any area, the first and secon
63
63
64
64
## Solutions
65
65
66
- ### Solution 1
66
+ ### Solution 1: Sorting and Classification
67
+
68
+ First, we sort the array. Then, we can classify the results based on the properties of a triangle.
69
+
70
+ - If the sum of the two smaller numbers is less than or equal to the largest number, it cannot form a triangle. Return "Invalid".
71
+ - If the three numbers are equal, it is an equilateral triangle. Return "Equilateral".
72
+ - If two numbers are equal, it is an isosceles triangle. Return "Isosceles".
73
+ - If none of the above conditions are met, it is a scalene triangle. Return "Scalene".
74
+
75
+ The time complexity is $O(1)$, and the space complexity is $O(1)$.
67
76
68
77
<!-- tabs:start -->
69
78
70
79
``` python
71
-
80
+ class Solution :
81
+ def numberOfPairs (self , points : List[List[int ]]) -> int :
82
+ points.sort(key = lambda x : (x[0 ], - x[1 ]))
83
+ ans = 0
84
+ for i, (_, y1) in enumerate (points):
85
+ max_y = - inf
86
+ for _, y2 in points[i + 1 :]:
87
+ if max_y < y2 <= y1:
88
+ max_y = y2
89
+ ans += 1
90
+ return ans
72
91
```
73
92
74
93
``` java
75
-
94
+ class Solution {
95
+ public int numberOfPairs (int [][] points ) {
96
+ Arrays . sort(points, (a, b) - > a[0 ] == b[0 ] ? b[1 ] - a[1 ] : a[0 ] - b[0 ]);
97
+ int ans = 0 ;
98
+ int n = points. length;
99
+ final int inf = 1 << 30 ;
100
+ for (int i = 0 ; i < n; ++ i) {
101
+ int y1 = points[i][1 ];
102
+ int maxY = - inf;
103
+ for (int j = i + 1 ; j < n; ++ j) {
104
+ int y2 = points[j][1 ];
105
+ if (maxY < y2 && y2 <= y1) {
106
+ maxY = y2;
107
+ ++ ans;
108
+ }
109
+ }
110
+ }
111
+ return ans;
112
+ }
113
+ }
76
114
```
77
115
78
116
``` cpp
79
-
117
+ class Solution {
118
+ public:
119
+ int numberOfPairs(vector<vector<int >>& points) {
120
+ sort(points.begin(), points.end(), [ ] (const vector<int >& a, const vector<int >& b) {
121
+ return a[ 0] < b[ 0] || (a[ 0] == b[ 0] && b[ 1] < a[ 1] );
122
+ });
123
+ int n = points.size();
124
+ int ans = 0;
125
+ for (int i = 0; i < n; ++i) {
126
+ int y1 = points[ i] [ 1 ] ;
127
+ int maxY = INT_MIN;
128
+ for (int j = i + 1; j < n; ++j) {
129
+ int y2 = points[ j] [ 1 ] ;
130
+ if (maxY < y2 && y2 <= y1) {
131
+ maxY = y2;
132
+ ++ans;
133
+ }
134
+ }
135
+ }
136
+ return ans;
137
+ }
138
+ };
80
139
```
81
140
82
141
```go
142
+ func numberOfPairs(points [][]int) (ans int) {
143
+ sort.Slice(points, func(i, j int) bool {
144
+ return points[i][0] < points[j][0] || points[i][0] == points[j][0] && points[j][1] < points[i][1]
145
+ })
146
+ for i, p1 := range points {
147
+ y1 := p1[1]
148
+ maxY := math.MinInt32
149
+ for _, p2 := range points[i+1:] {
150
+ y2 := p2[1]
151
+ if maxY < y2 && y2 <= y1 {
152
+ maxY = y2
153
+ ans++
154
+ }
155
+ }
156
+ }
157
+ return
158
+ }
159
+ ```
83
160
161
+ ``` ts
162
+ function numberOfPairs(points : number [][]): number {
163
+ points .sort ((a , b ) => (a [0 ] === b [0 ] ? b [1 ] - a [1 ] : a [0 ] - b [0 ]));
164
+ const n = points .length ;
165
+ let ans = 0 ;
166
+ for (let i = 0 ; i < n ; ++ i ) {
167
+ const [_, y1] = points [i ];
168
+ let maxY = - Infinity ;
169
+ for (let j = i + 1 ; j < n ; ++ j ) {
170
+ const [_, y2] = points [j ];
171
+ if (maxY < y2 && y2 <= y1 ) {
172
+ maxY = y2 ;
173
+ ++ ans ;
174
+ }
175
+ }
176
+ }
177
+ return ans ;
178
+ }
84
179
```
85
180
86
181
<!-- tabs: end -->
0 commit comments