DEV Community

kevin074
kevin074

Posted on

Leetcode diary: 1630 - Arithmetic Subarrays

This is a new series where I document my struggles of leetcode questions hoping seeing however small of an audience I get gives me the motivation to continue.

Today's question

This was an okay question. After getting over the beginning confusion over the parameters, I noticed a trend is that the steps should be the same if a given arithmetic sequence is sorted. This is just an intuition, it's not something I can justify fully, hopefully in the interviews the interviewer is okay with just the observation.

I then wrote out my process as below:

so we want an answerArray = [];
it will contain the list of booleans for final return
then we loop on l & r
get the range of numbers via: nums.slice(l, r);
we then put the numbers into isArith function
said function will sort(?) the array
and determine the difference between each step
finally return the result and append to answerArray
Enter fullscreen mode Exit fullscreen mode

I tried my initial code, this passed the given test case:

var checkArithmeticSubarrays = function(nums, l, r) {    

    const answerArray = [];

    l.forEach(function(lindex, index){
        answerArray.push(
            isArithSimpleSort(nums.slice(lindex, r[index]+1))
        );
    });

    return answerArray;
};

function isArithSimpleSort(nums) {
    if (nums.length <= 2) { return true } 

    nums = nums.sort();

    const step = Math.abs(nums[0] - nums[1]);

    for (let i=2; i < nums.length; i++) {
        if(step != Math.abs(nums[i] - nums[i-1])) {
            return false;
        }
    }

    return true;
}
Enter fullscreen mode Exit fullscreen mode

However I failed the submission. As I looked at the failed test case, I realized perhaps I need to do sorting over absolute values and the default; whichever returns true, I'll record.

This quick modification worked, but it had terrible performance, so I found a solution in the discussion with good performance:

const checkArithmeticSubarraysa = (nums, l, r) => {

  const chechArithmetic = ar => {
    const diff = ar[1] - ar[0];
    for(let i = 2; i < ar.length; i++)
      if(ar[i] - ar[i-1] != diff)
        return false;
    return true;
  }

  return l.map((_,i) => chechArithmetic(nums.slice(l[i], r[i]+1).sort((a,b) => a-b)) )
};
Enter fullscreen mode Exit fullscreen mode

After reading this crazy code in detail, it's always hard to read others' code, I realize our code is basically the same. As I compared, I realized that the sort function is just a-b instead of absolute values or plain sort function. I then tried just the absolute function sort and see if I can pass the submission. It did. I was confused for a minute, then I realized that javascript sort function on ints are string based, not ints. After realizing my newbie mistake I quickly modified my code and got this final solution faster than at least 21%: (this solution is the same as the fastest, guess i am just unlucky that the server is busier or something :P)

var checkArithmeticSubarrays = function(nums, l, r) {    
    const answerArray = []
    l.forEach(function(lindex, index){
        answerArray.push(
            isArithAbsSort(nums.slice(lindex, r[index]+1))
        );
    });

    return answerArray;
};

function isArithAbsSort(nums) {
    if (nums.length <= 2) { return true } 

    nums = nums.sort(function(a,b){
        return a > b ? 1 : -1
    });

    const step = Math.abs(nums[0] - nums[1]);

    for (let i=2; i < nums.length; i++) {
        if(step != Math.abs(nums[i] - nums[i-1])) {
            return false;
        }
    }

    return true;
}
Enter fullscreen mode Exit fullscreen mode

Let me know anything on your mind after reading through this, THANKS!

Top comments (0)