<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>matrix &#8211; JSAlgorithm</title>
	<atom:link href="https://jsalgorithm.com/tag/matrix/feed/" rel="self" type="application/rss+xml" />
	<link>https://jsalgorithm.com</link>
	<description></description>
	<lastBuildDate>Thu, 04 Jun 2020 01:11:55 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.5.15</generator>
<site xmlns="com-wordpress:feed-additions:1">177240629</site>	<item>
		<title>Spiral Matrix</title>
		<link>https://jsalgorithm.com/2020/06/04/spiral-matrix/</link>
					<comments>https://jsalgorithm.com/2020/06/04/spiral-matrix/#respond</comments>
		
		<dc:creator><![CDATA[Ronald]]></dc:creator>
		<pubDate>Thu, 04 Jun 2020 01:11:55 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Loop]]></category>
		<category><![CDATA[Matrix]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[matrix]]></category>
		<guid isPermaLink="false">http://50.19.13.106/?p=140</guid>

					<description><![CDATA[function spiralMatrix(dimension) { let matrix = []; // create blank matrix based from given dimension for (let i = 0; i &#60; dimension; i++) { matrix.push(new Array(dimension).fill(0)) } let counter = 1; let startRow = 0, endRow = dimension, startColumn = 0, endColumn = dimension; while (startRow &#60; endRow &#38;&#38; startColumn &#60; endColumn) { // [&#8230;]]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-preformatted">function spiralMatrix(dimension) {

    let matrix = [];
    // create blank matrix based from given dimension
    for (let i = 0; i &lt; dimension; i++) {
        matrix.push(new <strong><em>Array</em></strong>(dimension).fill(0))
    }

    let counter = 1;
    let startRow = 0, endRow = dimension, startColumn = 0, endColumn = dimension;


    while (startRow &lt; endRow &amp;&amp; startColumn &lt; endColumn) {

        // top left -> top right
        for (let i = startColumn; i &lt; endColumn; i++) {
            matrix[startRow][i] = counter++;
        }
        startRow++

        // top right -> bottom right
        for (let i = startRow; i &lt; endRow; i++) {
            matrix[i][endColumn - 1] = counter++;
        }
        endColumn--;

        // bottom right -> bottom left
        for (let i = endColumn - 1; i >= startColumn; i--) {
            matrix[endRow - 1][i] = counter++;
        }
        endRow--;


        // bottom left -> top left
        for (let i = endRow; i > startRow; i--) {
            matrix[i - 1][startColumn] = counter++;
        }
        startColumn++;

    }

     return matrix;
}</pre>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://jsalgorithm.com/2020/06/04/spiral-matrix/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">140</post-id>	</item>
		<item>
		<title>Game Of Life (Optimized)</title>
		<link>https://jsalgorithm.com/2020/05/21/game-of-life-optimized/</link>
					<comments>https://jsalgorithm.com/2020/05/21/game-of-life-optimized/#respond</comments>
		
		<dc:creator><![CDATA[Ronald]]></dc:creator>
		<pubDate>Thu, 21 May 2020 02:46:57 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Loop]]></category>
		<category><![CDATA[Matrix]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[bitshift]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[matrix]]></category>
		<guid isPermaLink="false">http://50.19.13.106/?p=132</guid>

					<description><![CDATA[let gameOfLife = function(board) { if (!board) return []; let LIVE = 1, DEAD = 0, ALIVE_TO_DEAD = 0b01, // [next state, current state] ALIVE_TO_ALIVE = 0b11, DEAD_TO_ALIVE = 0b10, DEAD_TO_DEAD = 0b00, maxRow = board.length - 1, maxCol = board[0].length - 1; function getAdjacentCount(row, col, valueToCompare) { let sum = 0; for (let i [&#8230;]]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-preformatted">let gameOfLife = function(board) {

    if (!board) return [];

    let LIVE = 1,
        DEAD = 0,
        ALIVE_TO_DEAD = 0b01, // [next state, current state]
        ALIVE_TO_ALIVE = 0b11,
        DEAD_TO_ALIVE = 0b10,
        DEAD_TO_DEAD = 0b00,
        maxRow = board.length - 1,
        maxCol = board[0].length - 1;

    function getAdjacentCount(row, col, valueToCompare) {
        let sum = 0;
        for (let i = <strong><em>Math</em></strong>.max(row - 1, 0); i &lt;= <strong><em>Math</em></strong>.min(row + 1, maxRow); i++) {
            for (let j = <strong><em>Math</em></strong>.max(col - 1, 0); j &lt;= <strong><em>Math</em></strong>.min(col + 1, maxCol); j++) {
                if (i === row &amp;&amp; j === col) continue;
                if (board[i][j] &amp; 1 === valueToCompare) sum = sum + 1;
            }
        }
        return sum;
    }

    function getNextValue(row, col) {
        const val = board[row][col];
        let liveNeighbors = getAdjacentCount(row, col, LIVE);
        if (val === LIVE) {

            // Any live cell with fewer than two live neighbors dies, as if caused by under-population.
            // Any live cell with more than three live neighbors dies, as if by over-population..
            if (liveNeighbors &lt; 2 || liveNeighbors > 3) {
                return ALIVE_TO_DEAD;
            }
            // Any live cell with two or three live neighbors lives on to the next generation.
            return ALIVE_TO_ALIVE;
        }
        // cell is DEAD
        else {
            // Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
            if (liveNeighbors === 3) {
                return DEAD_TO_ALIVE;
            }
            // no condition
            return DEAD_TO_DEAD;
        }
    }

    for (let row = 0; row &lt;= maxRow; row++) {
        for (let col = 0; col &lt;= maxCol;col++) {
            board[row][col] = getNextValue(row, col);
        }
    }

    for (let row = 0; row &lt;= maxRow; row++) {
        for (let col = 0; col &lt;= maxCol;col++) {
            board[row][col] = board[row][col] >> 1;
        }
    }

};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://jsalgorithm.com/2020/05/21/game-of-life-optimized/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">132</post-id>	</item>
		<item>
		<title>Game Of Life</title>
		<link>https://jsalgorithm.com/2020/05/21/game-of-life/</link>
					<comments>https://jsalgorithm.com/2020/05/21/game-of-life/#respond</comments>
		
		<dc:creator><![CDATA[Ronald]]></dc:creator>
		<pubDate>Thu, 21 May 2020 02:11:49 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Loop]]></category>
		<category><![CDATA[Matrix]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[matrix]]></category>
		<guid isPermaLink="false">http://50.19.13.106/?p=130</guid>

					<description><![CDATA[let gameOfLife = function(board) { if (!board) return []; let copy = [], LIVE = 1, DEAD = 0, maxRow = board.length - 1, maxCol = board[0].length - 1; function getAdjacentCount(row, col, valueToCompare) { let sum = 0; for (let i = Math.max(row - 1, 0); i &#60;= Math.min(row + 1, maxRow); i++) { for [&#8230;]]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-preformatted">let gameOfLife = function(board) {<br><br>    if (!board) return [];<br><br>    let copy = [],<br>        LIVE = 1,<br>        DEAD = 0,<br>        maxRow = board.length - 1,<br>        maxCol = board[0].length - 1;<br><br>    function getAdjacentCount(row, col, valueToCompare) {<br>        let sum = 0;<br>        for (let i = <strong><em>Math</em></strong>.max(row - 1, 0); i &lt;= <strong><em>Math</em></strong>.min(row + 1, maxRow); i++) {<br>            for (let j = <strong><em>Math</em></strong>.max(col - 1, 0); j &lt;= <strong><em>Math</em></strong>.min(col + 1, maxCol); j++) {<br>                if (i === row &amp;&amp; j === col) continue;<br>                if (copy[i] &amp;&amp; copy[i][j] !== undefined) {<br>                    if (copy[i][j] === valueToCompare) sum = sum + 1; //?<br>                }<br>                else {<br>                    if (board[i][j] === valueToCompare) sum = sum + 1; //?<br>                }<br>            }<br>        }<br>        return sum;<br>    }<br><br>    function getNextValue(row, col) {<br>        const val = board[row][col];<br>        let liveNeighbors = getAdjacentCount(row, col, LIVE);<br>        if (val === LIVE) {<br><br>            // Any live cell with fewer than two live neighbors dies, as if caused by under-population.<br>            if (liveNeighbors &lt; 2) {<br>                return DEAD;<br>            }<br>            // Any live cell with more than three live neighbors dies, as if by over-population..<br>            else if (liveNeighbors &gt; 3) {<br>                return DEAD;<br>            }<br>            // Any live cell with two or three live neighbors lives on to the next generation.<br>            return LIVE;<br>        }<br>        // cell is DEAD<br>        else {<br>            // Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.<br>            if (liveNeighbors === 3) {<br>                return LIVE;<br>            }<br>            // no condition<br>            return DEAD;<br>        }<br>    }<br><br>    for (let row = 0; row &lt;= maxRow; row++) {<br>        copy[row] = [];<br>        for (let col = 0; col &lt;= maxCol;col++) {<br>            copy[row][col] = board[row][col];<br>            board[row][col] = getNextValue(row, col);<br>        }<br>    }<br><br>};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://jsalgorithm.com/2020/05/21/game-of-life/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">130</post-id>	</item>
		<item>
		<title>Unique Paths with Obstacles</title>
		<link>https://jsalgorithm.com/2020/05/18/unique-paths-with-obstacles/</link>
					<comments>https://jsalgorithm.com/2020/05/18/unique-paths-with-obstacles/#respond</comments>
		
		<dc:creator><![CDATA[Ron]]></dc:creator>
		<pubDate>Mon, 18 May 2020 22:32:55 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[dynamic-programming]]></category>
		<category><![CDATA[leetcode]]></category>
		<category><![CDATA[matrix]]></category>
		<guid isPermaLink="false">http://50.19.13.106/?p=106</guid>

					<description><![CDATA[let uniquePathsWithObstacles = function(obstacleGrid) { if (!obstacleGrid &#124;&#124; !obstacleGrid.length) return 0; function markNextRowsAsZero(col) { for (let i = col; i &#60; maxCol; i++) { obstacleGrid[0][i] = 0; } } function markNextColumnsAsZero(row) { for (let i = row; i &#60; maxRow; i++) { obstacleGrid[i][0] = 0; } } let maxRow = obstacleGrid.length, maxCol = obstacleGrid[0].length, OBSTACLE [&#8230;]]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-preformatted">let uniquePathsWithObstacles = function(obstacleGrid) {<br><br>    if (!obstacleGrid || !obstacleGrid.length) return 0;<br><br>    function markNextRowsAsZero(col) {<br>        for (let i = col; i &lt; maxCol; i++) {<br>            obstacleGrid[0][i] = 0;<br>        }<br>    }<br>    function markNextColumnsAsZero(row) {<br>        for (let i = row; i &lt; maxRow; i++) {<br>            obstacleGrid[i][0] = 0;<br>        }<br>    }<br><br>    let maxRow = obstacleGrid.length,<br>        maxCol = obstacleGrid[0].length,<br>        OBSTACLE = 1, VISITED = -1;<br><br>    // first block is obstacle return 0;<br>    if (obstacleGrid[0][0] === OBSTACLE) {<br>        return 0;<br>    }<br><br>    // mark all first rows as either 1 or zero<br>    for (let col = 0; col &lt; maxCol;col++) {<br>        if (obstacleGrid[0][col] === OBSTACLE) {<br>            markNextRowsAsZero(col);<br>            break;<br>        }<br>        else {<br>            obstacleGrid[0][col] = 1;<br>        }<br>    }<br><br>    // mark all first columns as either 1 or zero<br>    for (let row = 1; row &lt; maxRow;row++) {<br>        if (obstacleGrid[row][0] === OBSTACLE) {<br>            markNextColumnsAsZero(row);<br>            break;<br>        }<br>        else {<br>            obstacleGrid[row][0] = 1;<br>        }<br>    }<br><br>    for (let row = 1; row &lt; maxRow;row++) {<br>        for (let col = 1; col &lt; maxCol;col++) {<br>            if (obstacleGrid[row][col] === OBSTACLE) {<br>                obstacleGrid[row][col] = 0;<br>            }<br>            else {<br>                obstacleGrid[row][col] =<br>                    obstacleGrid[row - 1][col] +<br>                    obstacleGrid[row][col - 1];<br>            }<br><br>        }<br>    }<br><br>    return obstacleGrid[maxRow - 1][maxCol - 1];<br>};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://jsalgorithm.com/2020/05/18/unique-paths-with-obstacles/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">106</post-id>	</item>
		<item>
		<title>Number Of Distinct Islands</title>
		<link>https://jsalgorithm.com/2020/05/16/number-of-distinct-islands/</link>
					<comments>https://jsalgorithm.com/2020/05/16/number-of-distinct-islands/#respond</comments>
		
		<dc:creator><![CDATA[Ron]]></dc:creator>
		<pubDate>Sat, 16 May 2020 01:20:03 +0000</pubDate>
				<category><![CDATA[Depth First Search]]></category>
		<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[Loop]]></category>
		<category><![CDATA[Matrix]]></category>
		<category><![CDATA[Recursion]]></category>
		<category><![CDATA[depth-first-search]]></category>
		<category><![CDATA[leetcode]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[recursion]]></category>
		<guid isPermaLink="false">http://50.19.13.106/?p=80</guid>

					<description><![CDATA[if (!grid &#124;&#124; !grid.length) return 0;let numRows = grid.length, numCols = grid[0].length, LAND = 1, VISITED = 2;function markAndVisit(row, col, island = [], level = 0) { if (grid[row][col] === LAND) { grid[row][col] = VISITED; island.push('X' + level); // up if (row &#62; 0 &#38;&#38; grid[row - 1][col] === LAND) { island.push('U' + level); markAndVisit(row [&#8230;]]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-preformatted">if (!grid || !grid.length) return 0;<br><br>let numRows = grid.length,<br>    numCols = grid[0].length,<br>    LAND = 1, VISITED = 2;<br><br>function markAndVisit(row, col, island = [], level = 0) {<br>    if (grid[row][col] === LAND) {<br>        grid[row][col] = VISITED;<br>        island.push('X' + level);<br><br>        // up<br>        if (row &gt; 0 &amp;&amp; grid[row - 1][col] === LAND) {<br>            island.push('U' + level);<br>            markAndVisit(row - 1, col, island, level + 1);<br>        }<br><br>        // down<br>        if (row &lt; numRows - 1 &amp;&amp; grid[row + 1][col] === LAND) {<br>            island.push('D' + level);<br>            markAndVisit(row + 1, col, island, level + 1);<br>        }<br><br>        // left<br>        if (col &gt; 0 &amp;&amp; grid[row][col - 1] === LAND) {<br>            island.push('L' + level);<br>            markAndVisit(row, col - 1, island, level + 1);<br>        }<br><br>        // right<br>        if (col &lt; numCols - 1 &amp;&amp; grid &amp;&amp; grid[row][col + 1] === LAND) {<br>            island.push('R' + level);<br>            markAndVisit(row, col + 1, island, level + 1);<br>        }<br>    }<br><br>    return island;<br>}<br><br>let islands = new <strong><em>Set</em></strong>();<br>for (let row = 0; row &lt; numRows;row++) {<br>    for (let col = 0; col &lt; numCols;col++) {<br>        if (grid[row][col] === LAND) {<br>            let island = markAndVisit(row, col);<br>            // console.log(island);<br>            if (island.length) islands.add(island.join(''));<br>        }<br>    }<br>}<br><br>// console.table(grid);<br>return islands.size;</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://jsalgorithm.com/2020/05/16/number-of-distinct-islands/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">80</post-id>	</item>
		<item>
		<title>Search Matrix</title>
		<link>https://jsalgorithm.com/2020/05/14/search-matrix/</link>
					<comments>https://jsalgorithm.com/2020/05/14/search-matrix/#respond</comments>
		
		<dc:creator><![CDATA[Ronald]]></dc:creator>
		<pubDate>Thu, 14 May 2020 01:27:46 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[Recursion]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[binary-search]]></category>
		<category><![CDATA[leetcode]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[recursion]]></category>
		<guid isPermaLink="false">http://50.19.13.106/?p=66</guid>

					<description><![CDATA[let searchMatrix = function(matrix, target) { if(!matrix.length) return false; function search(startRow, endRow, startCol, endCol) { console.log(startRow, endRow, startCol, endCol); let midRow = Math.floor((startRow + endRow) / 2); //? let midCol = Math.floor((startCol + endCol) / 2); //? if (startRow &#60; endRow &#38;&#38; startCol &#60; endCol) { const midValue = matrix[midRow][midCol]; //? if(midValue === target) { [&#8230;]]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-preformatted">let searchMatrix = function(matrix, target) {<br>    if(!matrix.length) return false;<br><br>    function search(startRow, endRow, startCol, endCol) {<br>        console.log(startRow, endRow, startCol, endCol);<br>        let midRow = <strong><em>Math</em></strong>.floor((startRow + endRow) / 2); //?<br>        let midCol = <strong><em>Math</em></strong>.floor((startCol + endCol) / 2); //?<br><br><br>        if (startRow &lt; endRow &amp;&amp; startCol &lt; endCol) {<br>            const midValue = matrix[midRow][midCol]; //?<br>            if(midValue === target) {<br>                return true;<br>            }<br>            if (midValue &lt; target) {<br>                return search(midRow + 1, endRow, startCol, endCol)<br>                        || search(startRow, endRow, midCol + 1, endCol);<br>            }<br>            else {<br>                return search(startRow, midRow, startCol, endCol)<br>                    || search(startRow, endRow, startCol, midCol);<br>            }<br>        }<br><br>        return false;<br>    }<br><br>    return search(0, matrix.length, 0, matrix[0].length);<br>};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://jsalgorithm.com/2020/05/14/search-matrix/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">66</post-id>	</item>
	</channel>
</rss>
