{"id":175,"date":"2017-09-09T09:09:07","date_gmt":"2017-09-09T16:09:07","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=175"},"modified":"2018-07-10T18:33:52","modified_gmt":"2018-07-11T01:33:52","slug":"leetcode-611-valid-triangle-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-611-valid-triangle-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 611. Valid Triangle Number"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"LeetCode 611. Valid Triangle Number - \u82b1\u82b1\u9171 \u5237\u9898\u627e\u5de5\u4f5c EP43\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/bojX9bdra-w?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem:<\/strong><\/h1>\n<p>Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> [2,2,3,4]\r\n<b>Output:<\/b> 3\r\n<b>Explanation:<\/b>\r\nValid combinations are: \r\n2,3,4 (using the first 2)\r\n2,3,4 (using the second 2)\r\n2,2,3\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The length of the given array won&#8217;t exceed 1000.<\/li>\n<li>The integers in the given array are in the range of [0, 1000].<\/li>\n<\/ol>\n<h1><strong>Idea:<\/strong><\/h1>\n<p>Greedy<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/611-ep43.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-176\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/611-ep43.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/611-ep43.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/611-ep43-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/611-ep43-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/611-ep43-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Time Complexity:<\/strong><\/p>\n<p>O(n^2)<\/p>\n<h1><strong>Solution:<\/strong><\/h1>\n<pre class=\"lang:c++ decode:true  \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n    int triangleNumber(vector&lt;int&gt;&amp; nums) {\r\n        if (nums.size() &lt; 3) return 0;\r\n        std::sort(nums.rbegin(), nums.rend());\r\n        \r\n        int n = nums.size();\r\n        int ans = 0;\r\n        for (int c = 0; c &lt; n-2; ++c) {        \r\n            int b = c + 1;\r\n            int a = n - 1;\r\n            while (b &lt; a) {\r\n                if (nums[a] + nums[b] &gt; nums[c]) {\r\n                    ans += (a - b);\r\n                    ++b;\r\n                } else {\r\n                    --a;\r\n                }\r\n            }\r\n        }\r\n        \r\n        return ans;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles&#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,49],"tags":[],"class_list":["post-175","post","type-post","status-publish","format-standard","hentry","category-greedy","category-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/175","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=175"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":3063,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/175\/revisions\/3063"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}