Skip to content

Commit 1337448

Browse files
authored
feat: add solutions to lc problem: No.0976 (#4742)
1 parent 41a01df commit 1337448

File tree

5 files changed

+43
-40
lines changed

5 files changed

+43
-40
lines changed

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,11 @@ tags:
6262

6363
### 方法一:排序 + 贪心
6464

65-
> 三角形由三条边组成,且满足 <var>C</var> >= <var>B</var> && <var>C</var> >= <var>A</var> && <var>C</var> < <var>A</var> + <var>B</var>
65+
我们不妨假设三角形的三条边长分别为 $a \leq b \leq c$,则三角形的面积不为零等价于 $a + b \gt c$。
6666

67-
贪心策略,尽可能使用长边来组成三角形
67+
我们可以枚举最大的边长 $c$,然后从剩下的边长中选取两个最大的边长 $a$ 和 $b$,如果 $a + b \gt c$,则可以构成一个面积不为零的三角形,且该三角形的周长最大;否则继续枚举下一个最大的边长 $c$
6868

69-
1.`nums` 排序(从大到小)。
70-
2. 遍历 `nums`,以三个元素一组,进行条件判断,如滑动窗口一般。
71-
3. 当找到满足条件的三个元素时直接返回即可。
72-
4. 否则,在遍历结束时返回 0。
69+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
7370

7471
<!-- tabs:start -->
7572

@@ -108,10 +105,12 @@ class Solution {
108105
class Solution {
109106
public:
110107
int largestPerimeter(vector<int>& nums) {
111-
sort(nums.begin(), nums.end());
112-
for (int i = nums.size() - 1; i >= 2; --i) {
108+
ranges::sort(nums);
109+
for (int i = nums.size() - 1; i > 1; --i) {
113110
int c = nums[i - 1] + nums[i - 2];
114-
if (c > nums[i]) return c + nums[i];
111+
if (c > nums[i]) {
112+
return c + nums[i];
113+
}
115114
}
116115
return 0;
117116
}
@@ -124,8 +123,7 @@ public:
124123
func largestPerimeter(nums []int) int {
125124
sort.Ints(nums)
126125
for i := len(nums) - 1; i >= 2; i-- {
127-
c := nums[i-1] + nums[i-2]
128-
if c > nums[i] {
126+
if c := nums[i-1] + nums[i-2]; c > nums[i] {
129127
return c + nums[i]
130128
}
131129
}
@@ -137,11 +135,10 @@ func largestPerimeter(nums []int) int {
137135

138136
```ts
139137
function largestPerimeter(nums: number[]): number {
140-
const n = nums.length;
141-
nums.sort((a, b) => b - a);
142-
for (let i = 2; i < n; i++) {
143-
const [a, b, c] = [nums[i - 2], nums[i - 1], nums[i]];
144-
if (a < b + c) {
138+
nums.sort((a, b) => a - b);
139+
for (let i = nums.length - 1; i > 1; --i) {
140+
const [a, b, c] = nums.slice(i - 2, i + 1);
141+
if (a + b > c) {
145142
return a + b + c;
146143
}
147144
}

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ tags:
3535
<pre>
3636
<strong>Input:</strong> nums = [1,2,1,10]
3737
<strong>Output:</strong> 0
38-
<strong>Explanation:</strong>
38+
<strong>Explanation:</strong>
3939
You cannot use the side lengths 1, 1, and 2 to form a triangle.
4040
You cannot use the side lengths 1, 1, and 10 to form a triangle.
4141
You cannot use the side lengths 1, 2, and 10 to form a triangle.
@@ -56,7 +56,13 @@ As we cannot use any three side lengths to form a triangle of non-zero area, we
5656

5757
<!-- solution:start -->
5858

59-
### Solution 1
59+
### Solution 1: Sorting + Greedy
60+
61+
Suppose the three sides of the triangle are $a \leq b \leq c$. The triangle has non-zero area if and only if $a + b \gt c$.
62+
63+
We can enumerate the largest side $c$, then select the two largest remaining sides $a$ and $b$. If $a + b \gt c$, a triangle with non-zero area can be formed, and its perimeter will be the largest possible; otherwise, continue to enumerate the next largest side $c$.
64+
65+
The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$, where $n$ is the length of the array $\textit{nums}$.
6066

6167
<!-- tabs:start -->
6268

@@ -95,10 +101,12 @@ class Solution {
95101
class Solution {
96102
public:
97103
int largestPerimeter(vector<int>& nums) {
98-
sort(nums.begin(), nums.end());
99-
for (int i = nums.size() - 1; i >= 2; --i) {
104+
ranges::sort(nums);
105+
for (int i = nums.size() - 1; i > 1; --i) {
100106
int c = nums[i - 1] + nums[i - 2];
101-
if (c > nums[i]) return c + nums[i];
107+
if (c > nums[i]) {
108+
return c + nums[i];
109+
}
102110
}
103111
return 0;
104112
}
@@ -111,8 +119,7 @@ public:
111119
func largestPerimeter(nums []int) int {
112120
sort.Ints(nums)
113121
for i := len(nums) - 1; i >= 2; i-- {
114-
c := nums[i-1] + nums[i-2]
115-
if c > nums[i] {
122+
if c := nums[i-1] + nums[i-2]; c > nums[i] {
116123
return c + nums[i]
117124
}
118125
}
@@ -124,11 +131,10 @@ func largestPerimeter(nums []int) int {
124131

125132
```ts
126133
function largestPerimeter(nums: number[]): number {
127-
const n = nums.length;
128-
nums.sort((a, b) => b - a);
129-
for (let i = 2; i < n; i++) {
130-
const [a, b, c] = [nums[i - 2], nums[i - 1], nums[i]];
131-
if (a < b + c) {
134+
nums.sort((a, b) => a - b);
135+
for (let i = nums.length - 1; i > 1; --i) {
136+
const [a, b, c] = nums.slice(i - 2, i + 1);
137+
if (a + b > c) {
132138
return a + b + c;
133139
}
134140
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
class Solution {
22
public:
33
int largestPerimeter(vector<int>& nums) {
4-
sort(nums.begin(), nums.end());
5-
for (int i = nums.size() - 1; i >= 2; --i) {
4+
ranges::sort(nums);
5+
for (int i = nums.size() - 1; i > 1; --i) {
66
int c = nums[i - 1] + nums[i - 2];
7-
if (c > nums[i]) return c + nums[i];
7+
if (c > nums[i]) {
8+
return c + nums[i];
9+
}
810
}
911
return 0;
1012
}
11-
};
13+
};
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
func largestPerimeter(nums []int) int {
22
sort.Ints(nums)
33
for i := len(nums) - 1; i >= 2; i-- {
4-
c := nums[i-1] + nums[i-2]
5-
if c > nums[i] {
4+
if c := nums[i-1] + nums[i-2]; c > nums[i] {
65
return c + nums[i]
76
}
87
}
98
return 0
10-
}
9+
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
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) {
2+
nums.sort((a, b) => a - b);
3+
for (let i = nums.length - 1; i > 1; --i) {
4+
const [a, b, c] = nums.slice(i - 2, i + 1);
5+
if (a + b > c) {
76
return a + b + c;
87
}
98
}

0 commit comments

Comments
 (0)