Sunday, August 08, 2021

Leetcode 128 Longest Consecutive Sequence

 In here, I would like to discuss the dynamic set in Leetcode 128 Longest Consecutive Sequence.

This is my solution: 

  • Put all numbers into set.
  • The find the first number of each sequence. The first number is the number without any previous number (n-1) in the set.
  • Loop through the next number (n+1). And find out the max count. (Remove the current number from set in order to iterate it again.)


var longestConsecutive = function(nums) {
    
    let set = new Set(nums);
    let maxLen = 0;
    function next(num) {
        if (set.has(num)) {
            //set.delete(num);
            return next(num + 1) + 1;
        } else {
            return 0;
        }
    }
    console.log(set);
    for (let num of nums) {
        //console.log(num);
        let pre = num - 1;
        if (!set.has(pre)) {
            let len = next(num+1) + 1;
            if (len > maxLen) {
                maxLen = len;
            }
        }
    }

    return maxLen;
};

 

The particular I would discuss is 

Remove the current number from set in order to iterate it again.

set.delete(num)

I would say that can be optimize performance. However, base on the leetcode submission result, dynamic changing set is actually a performance penalty.



 

 

 

 

 

 

Sunday, August 01, 2021

LeetCode, 200, Number of islands

To solve LeetCode question 200, there is already one good explanation of how to solve it.
The basic idea is to use tree traverse algorithm (dfs or bfs) to check the adjacent land.

Here is my Javascript solution:

/**
 * @param {character[][]} grid
 * @return {number}
 */
var numIslands = function(grid) {
    let count = 0;
    if (grid == null || grid.length === 0) {
        return 0;
    }

    let row = grid.length;
    let col = grid[0].length;
    function dfs(ij) {
        if (i > row - 1 || j > col -1 || i0 || j < 0) {
            return;
        }
        if (grid[i][j] === "1") {
            grid[i][j] = "0";
            dfs(i+1,j); //down
            dfs(i,j+1); //right
            dfs(i-1,j); //up
            dfs(i,j-1); //left
        }
    }

    for (let i=0i<rowi++) {
        for (let j=0j<colj++) {
            if (grid[i][j] === "1") {
                grid[i][j] = "0";
                dfs(i+1,j); //down
                dfs(i,j+1); //right
                dfs(i-1,j); //up
                dfs(i,j-1); //left
                count++;
            }
        }
    }

    return count;
};

var grid = [
    ["1","1","1","1","0"],
    ["1","1","0","1","0"],
    ["1","1","0","0","0"],
    ["0","0","0","0","0"]
];
console.log(result = numIslands(grid));
console.assert(result === 1);

grid = [
    ["1","1","0","0","0"],
    ["1","1","0","0","0"],
    ["0","0","1","0","0"],
    ["0","0","0","1","1"]
];
console.log(result = numIslands(grid));
console.assert(result === 3);
    
grid = [
    ["1","1","1"],
    ["0","1","0"],
    ["1","1","1"]
];
console.log(result = numIslands(grid));
console.assert(result === 1);