An Interest In:
Web News this Week
- March 28, 2024
- March 27, 2024
- March 26, 2024
- March 25, 2024
- March 24, 2024
- March 23, 2024
- March 22, 2024
Day 6 of Studying LeetCode Solution until I Can Solve One on My Own: Problem1534.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 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 ofx
.
Return the number of good triplets.
Example 1:
Input: arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3Output: 4Explanation: 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 = 1Output: 0Explanation: 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) andabsolute 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) whichis for sorting. Once the array of intervals is sorted, mergingtakes 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) whichis for sorting. Once the array of intervals is sorted, mergingtakes 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
Original Link: https://dev.to/corndog_com567/day-6-of-studying-leetcode-solution-until-i-can-solve-one-on-my-own-problem1534count-good-tripletseasyjavascript-2feo
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To