{"id":7188,"date":"2020-08-01T21:15:38","date_gmt":"2020-08-02T04:15:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7188"},"modified":"2020-08-01T21:30:22","modified_gmt":"2020-08-02T04:30:22","slug":"leetcode-1534-count-good-triplets","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1534-count-good-triplets\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1534. Count Good Triplets"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>arr<\/code>, and three integers&nbsp;<code>a<\/code>,&nbsp;<code>b<\/code>&nbsp;and&nbsp;<code>c<\/code>. You need to find the number of good triplets.<\/p>\n\n\n\n<p>A triplet&nbsp;<code>(arr[i], arr[j], arr[k])<\/code>&nbsp;is&nbsp;<strong>good<\/strong>&nbsp;if the following conditions are true:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= i &lt; j &lt; k &lt;&nbsp;arr.length<\/code><\/li><li><code>|arr[i] - arr[j]| &lt;= a<\/code><\/li><li><code>|arr[j] - arr[k]| &lt;= b<\/code><\/li><li><code>|arr[i] - arr[k]| &lt;= c<\/code><\/li><\/ul>\n\n\n\n<p>Where&nbsp;<code>|x|<\/code>&nbsp;denotes the absolute value of&nbsp;<code>x<\/code>.<\/p>\n\n\n\n<p>Return<em>&nbsp;the number of good triplets<\/em>.<\/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> arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong>&nbsp;There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)].\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> arr = [1,1,2,2,3], a = 0, b = 0, c = 1\n<strong>Output:<\/strong> 0\n<strong>Explanation: <\/strong>No triplet satisfies all conditions.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>3 &lt;= arr.length &lt;= 100<\/code><\/li><li><code>0 &lt;= arr[i] &lt;= 1000<\/code><\/li><li><code>0 &lt;= a, b, c &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^3)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countGoodTriplets(vector<int>& arr, int a, int b, int c) {\n    const int n = arr.size();\n    int ans = 0;\n    for (int i = 0; i < n; ++i)\n      for (int j = i + 1; j < n; ++j)\n        for (int k = j + 1; k < n; ++k)\n          if (abs(arr[i] - arr[j]) <= a &#038;&#038;\n              abs(arr[j] - arr[k]) <= b &#038;&#038;\n              abs(arr[i] - arr[k]) <= c)\n            ++ans;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;arr, and three integers&nbsp;a,&nbsp;b&nbsp;and&nbsp;c. You need to find the number of good triplets. A triplet&nbsp;(arr[i], arr[j], arr[k])&nbsp;is&nbsp;good&nbsp;if the following conditions are&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,106,222],"class_list":["post-7188","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-brute-force","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7188","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=7188"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7188\/revisions"}],"predecessor-version":[{"id":7190,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7188\/revisions\/7190"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}