Sunday, January 23, 2022

ls in windows

Looking to run ls command in Windows.

doskey can set the alias.
According to https://winaero.com/how-to-set-aliases-for-the-command-prompt-in-windows/, I can set an alias on top of cmd command; however, it's not working on Windows Terminal.

 So, for the Windows Terminal, open the settings and add alias into cmd.





Tuesday, January 11, 2022

create-react-app

>npx create-react-app antd-tutorial
Need to install the following packages:
  create-react-app
Ok to proceed? (y) y

You are running `create-react-app` 4.0.3, which is behind the latest release (5.0.0).

We no longer support global installation of Create React App.

Please remove any global installs with one of the following commands:
- npm uninstall -g create-react-app
- yarn global remove create-react-app

The latest instructions for creating a new app can be found here:
https://create-react-app.dev/docs/getting-started/

How do we resolve this issue?

>npx clear-npx-cache

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);



Thursday, July 22, 2021

Elasticsearch multiple indexes

Just another post of how to do multiple indexes search.