Day - 16

Day - 16

Hello, friends. How is going on? I came back again. Are you ready to read my article? It is the 16th day of my coding challenge. I think we are going well.


Dears, I would be happy, if I knew you started your own coding challenge. Because consistency is the key to success.


You can see 2 solutions here.


  1. Problem 389. Find Difference

/*
 * @param { string } s
 * @param { string } t
 * @returns { string }
 */

var findTheDifference = function (s, t) {
  var first = Array.from(s).sort();
  var second = Array.from(t).sort();

  let i = 0;
  let j = 0;
  while (i < first.length && j < second.length) {
    if (first[i] == second[i]) {
      i++;
      j++;
    } else {
      return second[j];
    }
  }
  return second[second.length - 1];
};        

2. Problem 1254. Number of Closed Islands

/*
 * @param {number[][]} grid
 * @return {number}
 */	

var closedIsland = function (grid) {
  let rows = grid[0].length,
    columns = grid.length,
    visited = [],
    result = 0,
    directories = [
      [0, 1],
      [-1, 0],
      [0, -1],
      [1, 0],
    ];
  for (let row of grid) visited.push(new Array(row.length).fill(0));
  const checkField = (x, y) => {
    return x >= 0 && y >= 0 && x < columns && y < rows;
  };
  function dfs(x, y) {
    if (!checkField(x, y)) return false;
    if (visited[x][y] || grid[x][y] === 1) return true;
    visited[x][y] = 1;
    let result = true;
    for (const directory of directories) {
      result = dfs(x + directory[0], y + directory[1]) && result;
    }
    return result;
  }

  for (let i = 0; i < columns; i++) {
   for (let j = 0; j < rows; j++) {
     if (!visited[i][j] && grid[i][j] === 0 && dfs(i, j)) result++;
   }
  }
  return result;
};        

That's all I am going to share with you. Thanks for your attention dears.


#coding #algorithms #javascript #leetcode #100daysofcodechallenge #programming

要查看或添加评论,请登录

Abbosbek Sulaymonov的更多文章

  • Hey, how long will this task take?

    Hey, how long will this task take?

    A PM walks up to a developer and asks, "Can you build this feature? When will it be done?" The developer pauses. If…

    2 条评论
  • Polyfills in JavaScript

    Polyfills in JavaScript

    Assalamu alaykum, everyone! I haven't been able to write anything for a long time. I hope you will forgive me for this.

  • Day - 100

    Day - 100

    Assalamu alaykum, everyone! And finally, today is the last day of my 100-day challenge. Getting to this day has not…

    2 条评论
  • Day - 99

    Day - 99

    Assalamu alaykum, everyone! How are you guys? It is the 99th day of my coding challenge! So, I am going to post a good…

    1 条评论
  • Day - 98

    Day - 98

    Assalamu alaykum, everyone! It is time to share the 98th day of my coding challenge! These days, I am solving problems…

  • Day - 97

    Day - 97

    Assalomu alaykum, everyone! It is the 97th day! I am glad to see you here! Today I am going to show a solution for…

    1 条评论
  • Day - 96

    Day - 96

    Assalamu alaykum, everyone! It is the 96th day of my coding challenge! We passed 96 days ). It is a great result! I…

  • Day - 95

    Day - 95

    Assalamu alaykum, everyone! Today, I am going to share the 95th day of my coding challenge! So, let's start the coding!…

  • Day - 94

    Day - 94

    Assalamu alaykum, everyone! It is the 94th day of my coding challenge! I do not know how to describe my thoughts! So…

  • Day - 93

    Day - 93

    Assalomu alaykum, everyone! I am glad to see you here! It is the 93rd day of my coding challenge! So, let's start the…

社区洞察

其他会员也浏览了