Posts

Showing posts with the label Matrix

Richest Customer Wealth LeetCode Solution - The Coding Shala

Home >> LeetCode >> Richest Customer Wealth  In this post, we will learn how to solve LeetCode's Richest Customer Wealth problem and will implement its solution in Java. Richest Customer Wealth Problem You are given an m x n integer grid accounts where accounts[i][j] is the amount of money i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ bank. Return the wealth that the richest customer has.  A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth. Example 1: Input: accounts = [[1,2,3],[3,2,1]] Output: 6 Explanation: 1st customer has wealth = 1 + 2 + 3 = 6 2nd customer has wealth = 3 + 2 + 1 = 6 Both customers are considered the richest with a wealth of 6 each, so return 6. Example 2: Input: accounts = [[1,5],[7,3],[3,5]] Output: 10 Example 3: Input: accounts = [[2,8,7],[7,1,3],[1,9,5]] Output: 17 Practice this problem on LeetCode . LeetCode - Richest Customer...

Rotate Image - The Coding Shala

Home >> Interview Questions >> Rotate Image  In this post, we will learn how to Rotate an Image or in other words how to Rotate a Matrix. We will implement Rotate Image Java Solution. Rotate Image Problem You are given an n x n 2D matrix that represents an image. Rotate the image by 90 degrees (clockwise). Example 1: For a = [[1, 2, 3],              [4, 5, 6],              [7, 8, 9]] the output should be  rotateImage(a) =            [[7, 4, 1],            [8, 5, 2],            [9, 6, 3]] Rotate Image Java Solution Approach 1 Using extra space. We can do this easily by using extra matrix. Java Program:  int [][] rotateImage ( int [][] a ) { int len = a . length ; int [][] ans = new int [ len ][ len ]; for ( int i = 0 ; i < len ; i ++){ for ( int j = 0 ; j ...

Count Negative Numbers in a Sorted Matrix Java Program - The Coding Shala

Home >> Java Program >> Count Negative Numbers in a Sorted Matrix  In this post, we are going to write a Java Program to Count Negative Numbers in a Sorted Matrix. Count Negative Numbers in a Sorted Matrix Problem Given an m * n matrix grid which is sorted in non-increasing order both row-wise and column-wise. Return the number of negative numbers in the grid. Example 1: Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]] Output: 8 Explanation: There are 8 negatives numbers in the matrix. Example 2: Input: grid = [[3,2],[1,0]] Output: 0 Example 3: Input: grid = [[1,-1],[-1,-1]] Output: 3 Count Negative Numbers in a Sorted Matrix Java Solution Approach 1 By using two loops, we will check every element and count the negative numbers. We can improve this approach as we know the matrix is sorted, so in each row, if we get a negative number then the rest of the elements in that row will be negative. Java Program:   class Solution { p...

How to Check if given Sudoku is Valid or not - The Coding Shala

Image
Home >> Interview Questions >> Valid Sudoku How to Check if given Sudoku is Valid or not In this post, you will learn how to check if given Sudoku is valid or not. We will write a Java program to check if the given sudoku board is valid. Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. Example: For grid = [['.', '.', '.', '1', '4', '.', '.', '2', '.'],            ['.', '.', '6', '.', '.', '.', '.', '.', '.'],     ...

Diagonal Traverse Java Solution - The Coding Shala

Image
Home >> Interview Questions >> Diagonal Traverse Diagonal Traverse Problem:  Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. Example: Input: [  [ 1, 2, 3 ],  [ 4, 5, 6 ],  [ 7, 8, 9 ] ] Output:  [1,2,4,7,5,3,6,8,9] Explanation: Diagonal Traverse Java Solution Approach:  Traverse the Matrix. Java Code::  class Solution { public int [] findDiagonalOrder ( int [][] matrix ) { int row = matrix . length ; if ( row == 0 ) return ( new int [ 0 ]); //if empty matrix int col = matrix [ 0 ]. length ; int [] ans = new int [ row * col ]; int index = 0 ; int i = 0 , j = 0 ; while ( i < row && j < col ){ while ( i >= 0 && j < col ){ //moving up ...

Anti Diagonals - The Coding Shala

Image
Home >> Interview Questions >> Anti Diagonals Anti Diagonals Java Solution Give an N*N square matrix, return an array of its anti-diagonals. Look at the example for more details. Example: Input: 1 2 3 7 8 9 4 5 6 Return the following : [ [1], [2, 4], [3, 5, 7], [6, 8], [9] ] Input : 1 2 3 4 Return the following : [ [1], [2, 3], [4] ] Anti Diagonals Java Solution 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class Solution { public ArrayList < ArrayList < Integer >> diagonal ( ArrayList < ArrayList < Integer >> A ) { ArrayList < ArrayList < Integer >> ans = new ArrayList < ArrayList < Integer >>(); int N = A . size (); int i = N - 1 ; while ( i >= 0 ){ ArrayList < Integer > tmp = new ArrayList < Integer >(); ...

Spiral Order Matrix II - The Coding Shala

Image
Home >> Interview Questions >> Spiral Order matrix 2 Spiral Order Matrix II Problem Solution Given an integer n, generate a square matrix filled with elements from 1 to n 2  in spiral order. Example: Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] Solution: (Java) The concept is the same as how to print the Spiral Order Matrix. Approach: Let's see for n= 4, then numbers will come from 1 to 16(n*n) in the matrix. As we need to insert numbers in spiral order so here we need 4 things to check, first and last column and top and bottom row. The following four steps we need to do: First, we need to insert numbers in the top row. Our loop will go from the left column to the right then will increase the top row by one and now the top is our second row. Now we are inserting the numbers in the right column, will start the loop from top to bottom, and then decrease the value of the right ...

Spiral Order Matrix I - The Coding Shala

Image
Home >> Interview Questions >> Spiral Order Matrix 1 Spiral Order Matrix I Interview Bit Solution Given a matrix of m * n elements (m rows, n columns), return all elements of the matrix in spiral order. Example: Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1, 2, 3, 6, 9, 8, 7, 4, 5] Solution(Java): Take four variables top, bottom, left and right. Start printing with the top then right then bottom and then left. Increase the values of variables after printing each row and columns. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 public class Solution { // DO NOT MODIFY THE LIST. IT IS READ ONLY public ArrayList < Integer > spiralOrder ( final List < ArrayList < Integer >> A ) { ArrayList < Integer > ans = new ArrayList < Integer >...