Skip to content

Latest commit

 

History

History
41 lines (32 loc) · 1.23 KB

Matrix Summation.md

File metadata and controls

41 lines (32 loc) · 1.23 KB

Screen Shot 2021-10-28 at 12 38 47 AM

Screen Shot 2021-10-28 at 12 38 54 AM

public static void beforeMatrix(int[][] afterMatrix) {
		int m=afterMatrix.length;
		int n=afterMatrix[0].length;
		
		for(int i=m-1;i>=0;i--) {
			for(int j=n-1;j>=0;j--) {
				
				int left = j==0 ? 0 : afterMatrix[i][j-1];
				int up = i==0 ? 0 : afterMatrix[i-1][j];
				int corner = i==0 || j==0 ? 0 : afterMatrix[i-1][j-1];
				
					afterMatrix[i][j]-=left+up-corner;
				}			
		}		
	}
function beforeMatrix(afterMatrix) {
    let m = afterMatrix.length;
    let n = afterMatrix[0].length;

    for(let i = m - 1; i >= 0; i--) {
        for(let j = n - 1; j >= 0; j--) {
            let left = j ===0 ? 0 : afterMatrix[i][j-1];
            let up = i===0 ? 0 : afterMatrix[i-1][j];
            let corner = i===0 || j === 0 ? 0 : afterMatrix[i-1][j-1];
            afterMatrix[i][j] -= left + up - corner;
        }
    }
    return afterMatrix;
}

console.log(beforeMatrix([[1,2],[3,4]]));