Skip to content

Commit cc48369

Browse files
committed
feat: add solutions to lc problem: No.0976
No.0976.Largest Perimeter Triangle
1 parent 316b163 commit cc48369

File tree

6 files changed

+118
-53
lines changed

6 files changed

+118
-53
lines changed

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

+42-18
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

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

43+
**方法一:排序 + 贪心**
44+
4345
> 三角形由三条边组成,且满足 <var>C</var> >= <var>B</var> && <var>C</var> >= <var>A</var> && <var>C</var> < <var>A</var> + <var>B</var>
4446
4547
贪心策略,尽可能使用长边来组成三角形。
@@ -56,44 +58,66 @@
5658
<!-- 这里可写当前语言的特殊实现逻辑 -->
5759

5860
```python
59-
61+
class Solution:
62+
def largestPerimeter(self, nums: List[int]) -> int:
63+
nums.sort()
64+
for i in range(len(nums) - 1, 1, -1):
65+
if (c := nums[i - 1] + nums[i - 2]) > nums[i]:
66+
return c + nums[i]
67+
return 0
6068
```
6169

6270
### **Java**
6371

6472
<!-- 这里可写当前语言的特殊实现逻辑 -->
6573

6674
```java
67-
75+
class Solution {
76+
public int largestPerimeter(int[] nums) {
77+
Arrays.sort(nums);
78+
for (int i = nums.length - 1; i >= 2; --i) {
79+
int c = nums[i - 1] + nums[i - 2];
80+
if (c > nums[i]) {
81+
return c + nums[i];
82+
}
83+
}
84+
return 0;
85+
}
86+
}
6887
```
6988

7089
### **C++**
7190

7291
```cpp
7392
class Solution {
7493
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() )
94+
int largestPerimeter(vector<int>& nums) {
95+
sort(nums.begin(), nums.end());
96+
for (int i = nums.size() - 1; i >= 2; --i)
8497
{
85-
a = b ;
86-
b = c ;
87-
c = q.top() ;
88-
q.pop() ;
89-
if ( b + c > a )
90-
return a + b + c ;
98+
int c = nums[i - 1] + nums[i - 2];
99+
if (c > nums[i]) return c + nums[i];
91100
}
92-
return 0 ;
101+
return 0;
93102
}
94103
};
95104
```
96105
106+
### **Go**
107+
108+
```go
109+
func largestPerimeter(nums []int) int {
110+
sort.Ints(nums)
111+
for i := len(nums) - 1; i >= 2; i-- {
112+
c := nums[i-1] + nums[i-2]
113+
if c > nums[i] {
114+
return c + nums[i]
115+
}
116+
}
117+
return 0
118+
}
119+
```
120+
97121
### **TypeScript**
98122

99123
```ts

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

+40-18
Original file line numberDiff line numberDiff line change
@@ -36,42 +36,64 @@
3636
### **Python3**
3737

3838
```python
39-
39+
class Solution:
40+
def largestPerimeter(self, nums: List[int]) -> int:
41+
nums.sort()
42+
for i in range(len(nums) - 1, 1, -1):
43+
if (c := nums[i - 1] + nums[i - 2]) > nums[i]:
44+
return c + nums[i]
45+
return 0
4046
```
4147

4248
### **Java**
4349

4450
```java
45-
51+
class Solution {
52+
public int largestPerimeter(int[] nums) {
53+
Arrays.sort(nums);
54+
for (int i = nums.length - 1; i >= 2; --i) {
55+
int c = nums[i - 1] + nums[i - 2];
56+
if (c > nums[i]) {
57+
return c + nums[i];
58+
}
59+
}
60+
return 0;
61+
}
62+
}
4663
```
4764

4865
### **C++**
4966

5067
```cpp
5168
class Solution {
5269
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() )
70+
int largestPerimeter(vector<int>& nums) {
71+
sort(nums.begin(), nums.end());
72+
for (int i = nums.size() - 1; i >= 2; --i)
6273
{
63-
a = b ;
64-
b = c ;
65-
c = q.top() ;
66-
q.pop() ;
67-
if ( b + c > a )
68-
return a + b + c ;
74+
int c = nums[i - 1] + nums[i - 2];
75+
if (c > nums[i]) return c + nums[i];
6976
}
70-
return 0 ;
77+
return 0;
7178
}
7279
};
7380
```
7481
82+
### **Go**
83+
84+
```go
85+
func largestPerimeter(nums []int) int {
86+
sort.Ints(nums)
87+
for i := len(nums) - 1; i >= 2; i-- {
88+
c := nums[i-1] + nums[i-2]
89+
if c > nums[i] {
90+
return c + nums[i]
91+
}
92+
}
93+
return 0
94+
}
95+
```
96+
7597
### **TypeScript**
7698

7799
```ts
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
class Solution {
22
public:
3-
int largestPerimeter(vector<int>& A) {
4-
priority_queue<int> q(A.begin(), A.end()) ; // 大顶堆
5-
6-
int a, b, c ;
7-
b = q.top() ;
8-
q.pop() ;
9-
c = q.top() ;
10-
q.pop() ;
11-
while ( !q.empty() )
3+
int largestPerimeter(vector<int>& nums) {
4+
sort(nums.begin(), nums.end());
5+
for (int i = nums.size() - 1; i >= 2; --i)
126
{
13-
a = b ;
14-
b = c ;
15-
c = q.top() ;
16-
q.pop() ;
17-
if ( b + c > a )
18-
return a + b + c ;
7+
int c = nums[i - 1] + nums[i - 2];
8+
if (c > nums[i]) return c + nums[i];
199
}
20-
return 0 ;
10+
return 0;
2111
}
22-
};
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func largestPerimeter(nums []int) int {
2+
sort.Ints(nums)
3+
for i := len(nums) - 1; i >= 2; i-- {
4+
c := nums[i-1] + nums[i-2]
5+
if c > nums[i] {
6+
return c + nums[i]
7+
}
8+
}
9+
return 0
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int largestPerimeter(int[] nums) {
3+
Arrays.sort(nums);
4+
for (int i = nums.length - 1; i >= 2; --i) {
5+
int c = nums[i - 1] + nums[i - 2];
6+
if (c > nums[i]) {
7+
return c + nums[i];
8+
}
9+
}
10+
return 0;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def largestPerimeter(self, nums: List[int]) -> int:
3+
nums.sort()
4+
for i in range(len(nums) - 1, 1, -1):
5+
if (c := nums[i - 1] + nums[i - 2]) > nums[i]:
6+
return c + nums[i]
7+
return 0

0 commit comments

Comments
 (0)