DEV Community

Cover image for Day 1 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#728.Self-Dividing Numbers(Easy/JavaScript)
KillingLeetCode
KillingLeetCode

Posted on • Updated on

Day 1 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#728.Self-Dividing Numbers(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 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#728.Self-Dividing Numbers

Difficulty: Easy Language: JavaScript

A self-dividing number is a number that is divisible by every digit it contains.

  • For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.

A self-dividing number is not allowed to contain the digit zero.

Given two integers left and right, return a list of all the self-dividing numbers in the range [left, right].

Example 1:

Input: left = 1, right = 22
Output: [1,2,3,4,5,6,7,8,9,11,12,15,22]
Enter fullscreen mode Exit fullscreen mode

Example 2:

Input: left = 47, right = 85
Output: [48,55,66,77]
Enter fullscreen mode Exit fullscreen mode

Constraints:
1 <= left <= right <= 104

Solution 1 with explaination:

var selfDividingNumbers = function(left, right) {

    let result = []

/*Declare an empty array with square brackets*/

    for (i = left; i <= right ; i++) {

/*Loop through each element in the array (see note 1)*/

        isSelfDividing(i)? result.push(i) : result; 

/*Use helper function (note 3) and conditional operator(note 2)

to push (note 4) desired result into the array*/

    }
    return result;
};

var isSelfDividing = function(n) {

/*This is the helper function (note 3)*/

    let number = n.toString();

/*convert n to a string (note 5)*/

    for (let digit of number) {

/*Iterate over the string (note 6)*/

        if(number % digit !==0 || digit === 0) return false;

/*If the remainder (note 7) of number mod by digit is not zero,
that means digit is not divisible by the number. Hence, the number
is not self-dividing. And division by zero is undefined*/

    }

    return true;

};
Enter fullscreen mode Exit fullscreen mode

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

  • Runtime: 98 ms, faster than 46.03% of JavaScript online submissions for Self Dividing Numbers.
  • Memory Usage: 44.7 MB, less than 21.16% of JavaScript online submissions for Self Dividing Numbers.

Solution 2 with explaination:

If solution 1 is brute forth, solution 2 runs faster and takes up a little less memory since string is not involved. *The key to this solution is to obtain the last digit of a number with "number % 10." * You will realized that if you divide a number by 10 the remainder is the last digit of this number. For example, if you use 128 divided by 10 you will get 8, which is the last digit of 128. (it actually took me a while to note this).

var selfDividingNumbers = function(left, right) {

    let result = [];

/*create output array same as in solution 1*/

    for (i = left; i <= right; i++) {

/*Loop through each element in the array (see note 1)*/

        if (isSelfDivisible(i)) result.push(i)

/*Use helper function (note 3) to push (note 4) desired result
into the array*/

    }
    return result;
}

var isSelfDivisible = function(number) {

    let temp = number

/*create a temp variable*/

    while (temp > 0){

        let lastDigit = temp % 10

/*Use this line to obtain last digit of the number*/

        if(number % lastDigit !== 0) return null

/*Check if last digist is divisible by the number*/

        temp = Math.floor(temp / 10)

/*Now we test if second last digit is divisible
Note that this line will pass 128 without 0 to the next 
iteration. For example: Math.floor(128 / 10) = 12 (note 8).
Now that temp is 12. We can use temp % 10 again to get
second last digit 2 for another divisible testing.
*/

    }
    return number;
}
Enter fullscreen mode Exit fullscreen mode

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

  • Runtime: 68 ms, faster than 98.41% of JavaScript online submissions for Self Dividing Numbers.
  • Memory Usage: 42.6 MB, less than 35.45% of JavaScript online submissions for Self Dividing Numbers.

References:

Top comments (0)