Skip to content

Commit 22ba898

Browse files
committed
feat: add solutions to lc problems: No.0976,1779
- No.0976.Largest Perimeter Triangle - No.1779.Find Nearest Point That Has the Same X or Y Coordinate
1 parent 7a911fd commit 22ba898

File tree

9 files changed

+278
-1
lines changed

9 files changed

+278
-1
lines changed

solution/0900-0999/0976.Largest Perimeter Triangle/README.md

+70
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43+
> 三角形由三条边组成,且满足 <var>C</var> >= <var>B</var> && <var>C</var> >= <var>A</var> && <var>C</var> < <var>A</var> + <var>B</var>
44+
45+
贪心策略,尽可能使用长边来组成三角形。
46+
47+
1.`nums` 排序(从大到小)。
48+
2. 遍历 `nums`,以三个元素一组,进行条件判断,如滑动窗口一般。
49+
3. 当找到满足条件的三个元素时直接返回即可。
50+
4. 否则,在遍历结束时返回 0。
51+
4352
<!-- tabs:start -->
4453

4554
### **Python3**
@@ -58,6 +67,67 @@
5867

5968
```
6069

70+
### **C++**
71+
72+
```cpp
73+
class Solution {
74+
public:
75+
int largestPerimeter(vector<int>& A) {
76+
priority_queue<int> q(A.begin(), A.end()) ; // 大顶堆
77+
78+
int a, b, c ;
79+
b = q.top() ;
80+
q.pop() ;
81+
c = q.top() ;
82+
q.pop() ;
83+
while ( !q.empty() )
84+
{
85+
a = b ;
86+
b = c ;
87+
c = q.top() ;
88+
q.pop() ;
89+
if ( b + c > a )
90+
return a + b + c ;
91+
}
92+
return 0 ;
93+
}
94+
};
95+
```
96+
97+
### **TypeScript**
98+
99+
```ts
100+
function largestPerimeter(nums: number[]): number {
101+
const n = nums.length;
102+
nums.sort((a, b) => b - a);
103+
for (let i = 2; i < n; i++) {
104+
const [a, b, c] = [nums[i - 2], nums[i - 1], nums[i]];
105+
if (a < b + c) {
106+
return a + b + c;
107+
}
108+
}
109+
return 0;
110+
}
111+
```
112+
113+
### **Rust**
114+
115+
```rust
116+
impl Solution {
117+
pub fn largest_perimeter(mut nums: Vec<i32>) -> i32 {
118+
let n = nums.len();
119+
nums.sort_unstable_by(|a, b| b.cmp(&a));
120+
for i in 2..n {
121+
let (a, b, c) = (nums[i - 2], nums[i - 1], nums[i]);
122+
if a < b + c {
123+
return a + b + c;
124+
}
125+
}
126+
0
127+
}
128+
}
129+
```
130+
61131
### **...**
62132

63133
```

solution/0900-0999/0976.Largest Perimeter Triangle/README_EN.md

+61
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,67 @@
4545

4646
```
4747

48+
### **C++**
49+
50+
```cpp
51+
class Solution {
52+
public:
53+
int largestPerimeter(vector<int>& A) {
54+
priority_queue<int> q(A.begin(), A.end()) ; // 大顶堆
55+
56+
int a, b, c ;
57+
b = q.top() ;
58+
q.pop() ;
59+
c = q.top() ;
60+
q.pop() ;
61+
while ( !q.empty() )
62+
{
63+
a = b ;
64+
b = c ;
65+
c = q.top() ;
66+
q.pop() ;
67+
if ( b + c > a )
68+
return a + b + c ;
69+
}
70+
return 0 ;
71+
}
72+
};
73+
```
74+
75+
### **TypeScript**
76+
77+
```ts
78+
function largestPerimeter(nums: number[]): number {
79+
const n = nums.length;
80+
nums.sort((a, b) => b - a);
81+
for (let i = 2; i < n; i++) {
82+
const [a, b, c] = [nums[i - 2], nums[i - 1], nums[i]];
83+
if (a < b + c) {
84+
return a + b + c;
85+
}
86+
}
87+
return 0;
88+
}
89+
```
90+
91+
### **Rust**
92+
93+
```rust
94+
impl Solution {
95+
pub fn largest_perimeter(mut nums: Vec<i32>) -> i32 {
96+
let n = nums.len();
97+
nums.sort_unstable_by(|a, b| b.cmp(&a));
98+
for i in 2..n {
99+
let (a, b, c) = (nums[i - 2], nums[i - 1], nums[i]);
100+
if a < b + c {
101+
return a + b + c;
102+
}
103+
}
104+
0
105+
}
106+
}
107+
```
108+
48109
### **...**
49110

50111
```

solution/0900-0999/0976.Largest Perimeter Triangle/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution {
22
public:
33
int largestPerimeter(vector<int>& A) {
44
priority_queue<int> q(A.begin(), A.end()) ; // 大顶堆
5-
5+
66
int a, b, c ;
77
b = q.top() ;
88
q.pop() ;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn largest_perimeter(mut nums: Vec<i32>) -> i32 {
3+
let n = nums.len();
4+
nums.sort_unstable_by(|a, b| b.cmp(&a));
5+
for i in 2..n {
6+
let (a, b, c) = (nums[i - 2], nums[i - 1], nums[i]);
7+
if a < b + c {
8+
return a + b + c;
9+
}
10+
}
11+
0
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function largestPerimeter(nums: number[]): number {
2+
const n = nums.length;
3+
nums.sort((a, b) => b - a);
4+
for (let i = 2; i < n; i++) {
5+
const [a, b, c] = [nums[i - 2], nums[i - 1], nums[i]];
6+
if (a < b + c) {
7+
return a + b + c;
8+
}
9+
}
10+
return 0;
11+
}

solution/1700-1799/1779.Find Nearest Point That Has the Same X or Y Coordinate/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,50 @@
6767

6868
```
6969

70+
### **TypeScript**
71+
72+
```ts
73+
function nearestValidPoint(x: number, y: number, points: number[][]): number {
74+
let res = -1;
75+
let midDif = Infinity;
76+
points.forEach(([px, py], i) => {
77+
if (px != x && py != y) {
78+
return;
79+
}
80+
const dif = Math.abs(px - x) + Math.abs(py - y);
81+
if (dif < midDif) {
82+
midDif = dif;
83+
res = i;
84+
}
85+
});
86+
return res;
87+
}
88+
```
89+
90+
### **Rust**
91+
92+
```rust
93+
impl Solution {
94+
pub fn nearest_valid_point(x: i32, y: i32, points: Vec<Vec<i32>>) -> i32 {
95+
let n = points.len();
96+
let mut min_dif = i32::MAX;
97+
let mut res = -1;
98+
for i in 0..n {
99+
let (p_x, p_y) = (points[i][0], points[i][1]);
100+
if p_x != x && p_y != y {
101+
continue;
102+
}
103+
let dif = (p_x - x).abs() + (p_y - y).abs();
104+
if dif < min_dif {
105+
min_dif = dif;
106+
res = i as i32;
107+
}
108+
}
109+
res
110+
}
111+
}
112+
```
113+
70114
### **...**
71115

72116
```

solution/1700-1799/1779.Find Nearest Point That Has the Same X or Y Coordinate/README_EN.md

+44
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,50 @@
5757

5858
```
5959

60+
### **TypeScript**
61+
62+
```ts
63+
function nearestValidPoint(x: number, y: number, points: number[][]): number {
64+
let res = -1;
65+
let midDif = Infinity;
66+
points.forEach(([px, py], i) => {
67+
if (px != x && py != y) {
68+
return;
69+
}
70+
const dif = Math.abs(px - x) + Math.abs(py - y);
71+
if (dif < midDif) {
72+
midDif = dif;
73+
res = i;
74+
}
75+
});
76+
return res;
77+
}
78+
```
79+
80+
### **Rust**
81+
82+
```rust
83+
impl Solution {
84+
pub fn nearest_valid_point(x: i32, y: i32, points: Vec<Vec<i32>>) -> i32 {
85+
let n = points.len();
86+
let mut min_dif = i32::MAX;
87+
let mut res = -1;
88+
for i in 0..n {
89+
let (p_x, p_y) = (points[i][0], points[i][1]);
90+
if p_x != x && p_y != y {
91+
continue;
92+
}
93+
let dif = (p_x - x).abs() + (p_y - y).abs();
94+
if dif < min_dif {
95+
min_dif = dif;
96+
res = i as i32;
97+
}
98+
}
99+
res
100+
}
101+
}
102+
```
103+
60104
### **...**
61105

62106
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn nearest_valid_point(x: i32, y: i32, points: Vec<Vec<i32>>) -> i32 {
3+
let n = points.len();
4+
let mut min_dif = i32::MAX;
5+
let mut res = -1;
6+
for i in 0..n {
7+
let (p_x, p_y) = (points[i][0], points[i][1]);
8+
if p_x != x && p_y != y {
9+
continue;
10+
}
11+
let dif = (p_x - x).abs() + (p_y - y).abs();
12+
if dif < min_dif {
13+
min_dif = dif;
14+
res = i as i32;
15+
}
16+
}
17+
res
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function nearestValidPoint(x: number, y: number, points: number[][]): number {
2+
let res = -1;
3+
let midDif = Infinity;
4+
points.forEach(([px, py], i) => {
5+
if (px != x && py != y) {
6+
return;
7+
}
8+
const dif = Math.abs(px - x) + Math.abs(py - y);
9+
if (dif < midDif) {
10+
midDif = dif;
11+
res = i;
12+
}
13+
});
14+
return res;
15+
}

0 commit comments

Comments
 (0)