{"id":4640,"date":"2019-01-14T20:50:26","date_gmt":"2019-01-15T04:50:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4640"},"modified":"2019-01-14T20:53:00","modified_gmt":"2019-01-15T04:53:00","slug":"leetcode-976-largest-perimeter-triangle","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-976-largest-perimeter-triangle\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 976. Largest Perimeter Triangle"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>A<\/code>&nbsp;of positive lengths, return the largest perimeter of a triangle with&nbsp;<strong>non-zero area<\/strong>, formed from 3 of these lengths.<\/p>\n\n\n\n<p>If it is impossible to form any&nbsp;triangle of non-zero area, return&nbsp;<code>0<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[2,1,2]\n<strong>Output: <\/strong>5\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[1,2,1]\n<strong>Output: <\/strong>0\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[3,2,3,4]\n<strong>Output: <\/strong>10\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[3,6,2,3]\n<strong>Output: <\/strong>8\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>3 &lt;= A.length &lt;= 10000<\/code><\/li><li><code>1 &lt;= A[i] &lt;= 10^6<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution:&nbsp;Greedy<\/strong><\/h2>\n\n\n\n<p>Answer must be 3 consecutive numbers in the sorted array<br>if A[i] &gt;= A[i+1] + A[i+2], then A[i] &gt;= A[i+j] + A[i+k],  1 &lt; j &lt; k<br>if A[i] &lt; A[i+1] + A[i+2], then A[i] + A[i+1] + A[i+2] is the answer<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int largestPerimeter(vector<int>& A) {\n    sort(rbegin(A), rend(A));\n    for (int i = 0; i < A.size() - 2; ++i)\n      if (A[i] < A[i + 1] + A[i + 2])\n        return A[i] + A[i + 1] + A[i + 2];\n    return 0;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;A&nbsp;of positive lengths, return the largest perimeter of a triangle with&nbsp;non-zero area, formed from 3 of these lengths. If it is impossible to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[20,88,23,293],"class_list":["post-4640","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-array","tag-greedy","tag-sort","tag-triangle","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4640"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4640\/revisions"}],"predecessor-version":[{"id":4647,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4640\/revisions\/4647"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}