Day 6 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#1534.Count Good Triplets(Easy/JavaScript)

Intro: I am a former accountant turned software engineer graduated from coding bootcamp in January 2022. Algorithms and Data Structure is an unavoidable part of interviews for most of the tech companies now. And one of my friends told me that you need …


This content originally appeared on DEV Community and was authored by Corndog_com567

Intro: I am a former accountant turned software engineer graduated from coding bootcamp in January 2022. Algorithms and Data Structure is an unavoidable part of interviews for most of the tech companies now. And one of my friends told me that you need to solve a medium leetcode problem under 60 seconds in order to get into the top tech companies.So I thought I'd start learning how to do it while job searching.

Since I have no clue on how to solve any of the problems (even the easy ones), I thought there is no point for me to waste hours and can't get it figured out. Here is my approach:

  • Pick a leetcode problem randomly or Online Assessment from targeted companies.
  • Study 1-2 solutions from Youtube or LeetCode discussion section. One brute force solution, another one more optimal.
  • Write a blog post with detailed explanation and do a verbal walk through to help understand the solutions better.
  • Code out the solution in LeetCode without looking at the solutions
  • Combat the forgetting curve: Re-do the question for the next three days. And come back regularly to revisit the problem.

Problem#1534. Count Good Triplets

Difficulty: Easy Language: JavaScript
Company: IBM Backend Developer OA (not exactly - see note 1)

Given an array of integers arr, and three integers a, b and c. You need to find the number of good triplets.

A triplet (arr[i], arr[j], arr[k]) is good if the following conditions are true:

  • 0 <= i < j < k < arr.length
  • |arr[i] - arr[j]| <= a
  • |arr[j] - arr[k]| <= b
  • |arr[i] - arr[k]| <= c Where |x| denotes the absolute value of x.

Return the number of good triplets.

Example 1:

Input: arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3
Output: 4
Explanation: There are 4 good triplets: [(3,0,1), (3,0,1),
(3,1,1), (0,1,1)].

Example 2:

Input: arr = [1,1,2,2,3], a = 0, b = 0, c = 1
Output: 0
Explanation: No triplet satisfies all conditions.

Constraints:

  • 3 <= arr.length <= 100
  • 0 <= arr[i] <= 1000
  • 0 <= a, b, c <= 1000

Solution 1:

var countGoodTriplets = function(arr, a, b, c) {

    let output = []

/*create an empty array to store the triplets*/

    for (i = 0; i < arr.length; i++) {
        for (j = i + 1; j < arr.length; j++) {
            for (k = j + 1; k < arr.length; k++) {

/*loop (note 2) through three letters in the given 'arr'*/

                if (
                    Math.abs(arr[i] - arr[j]) <= a &&
                    Math.abs(arr[j] - arr[k]) <= b &&
                    Math.abs(arr[i] - arr[k]) <= c
                   ) 

/*use 'if statement' (note 4), 'Logical AND(&&)' (note 5) and
absolute value (note 3) to find the triplets meets the conditions*/

                 output.push((arr[i], arr[j], arr[k]))

/*Once triplets are found, push (note 6) it to the 'output'
array*/

            }
        }  
    }
    return output.length

/*return length (note 7) of the output array*/

};

Solution Submission detail as of 2/16/2022
(Data below could vary since there are new submissions daily)

  • Runtime: 143 ms
  • Memory Usage: 49.7 MB
  • Time complexity:Time complexity of the method is O(n3) which is for sorting. Once the array of intervals is sorted, merging takes linear time.
  • Space complexity:O(1)

Solution 2 (improve run time and space by just changing 2

lines):

var countGoodTriplets = function(arr, a, b, c) {

    let count = 0

    for (i = 0; i < arr.length; i++) {
        for (j = i + 1; j < arr.length; j++) {
            for (k = j + 1; k < arr.length; k++) {
                if (
                    Math.abs(arr[i] - arr[j]) <= a &&
                    Math.abs(arr[j] - arr[k]) <= b &&
                    Math.abs(arr[i] - arr[k]) <= c
                   ) count ++
            }
        }

    }
    return count
};

Solution Submission detail as of 2/16/2022
(Data below could vary since there are new submissions daily)

  • Runtime: 76 ms
  • Memory Usage: 42.1 MB
  • Time complexity:Time complexity of the method is O(n3) which is for sorting. Once the array of intervals is sorted, merging takes linear time.
  • Space complexity:O(1)

References:
LeetCode Problem Link
Note 1: IBM Back End Developer OA (Fall 2020)
Note 2: for loop
Note 3: Math.abs()
Note 4: if statement
Note 5: Logical AND(&&)
Note 6: Array.push
Note 6: Array.length
Blog Cover Image Credit


This content originally appeared on DEV Community and was authored by Corndog_com567


Print Share Comment Cite Upload Translate Updates
APA

Corndog_com567 | Sciencx (2022-02-16T16:01:35+00:00) Day 6 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#1534.Count Good Triplets(Easy/JavaScript). Retrieved from https://www.scien.cx/2022/02/16/day-6-of-studying-leetcode-solution-until-i-can-solve-one-on-my-own-problem1534-count-good-tripletseasy-javascript/

MLA
" » Day 6 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#1534.Count Good Triplets(Easy/JavaScript)." Corndog_com567 | Sciencx - Wednesday February 16, 2022, https://www.scien.cx/2022/02/16/day-6-of-studying-leetcode-solution-until-i-can-solve-one-on-my-own-problem1534-count-good-tripletseasy-javascript/
HARVARD
Corndog_com567 | Sciencx Wednesday February 16, 2022 » Day 6 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#1534.Count Good Triplets(Easy/JavaScript)., viewed ,<https://www.scien.cx/2022/02/16/day-6-of-studying-leetcode-solution-until-i-can-solve-one-on-my-own-problem1534-count-good-tripletseasy-javascript/>
VANCOUVER
Corndog_com567 | Sciencx - » Day 6 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#1534.Count Good Triplets(Easy/JavaScript). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/16/day-6-of-studying-leetcode-solution-until-i-can-solve-one-on-my-own-problem1534-count-good-tripletseasy-javascript/
CHICAGO
" » Day 6 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#1534.Count Good Triplets(Easy/JavaScript)." Corndog_com567 | Sciencx - Accessed . https://www.scien.cx/2022/02/16/day-6-of-studying-leetcode-solution-until-i-can-solve-one-on-my-own-problem1534-count-good-tripletseasy-javascript/
IEEE
" » Day 6 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#1534.Count Good Triplets(Easy/JavaScript)." Corndog_com567 | Sciencx [Online]. Available: https://www.scien.cx/2022/02/16/day-6-of-studying-leetcode-solution-until-i-can-solve-one-on-my-own-problem1534-count-good-tripletseasy-javascript/. [Accessed: ]
rf:citation
» Day 6 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#1534.Count Good Triplets(Easy/JavaScript) | Corndog_com567 | Sciencx | https://www.scien.cx/2022/02/16/day-6-of-studying-leetcode-solution-until-i-can-solve-one-on-my-own-problem1534-count-good-tripletseasy-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.