Skip to main content

Command Palette

Search for a command to run...

Modify the Matrix

Published
2 min read
Modify the Matrix
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Given a 0-indexed m x n integer matrix matrix, create a new 0-indexed matrix called answer. Make answer equal to matrix, then replace each element with the value -1 with the maximum element in its respective column.

Return the matrix answer.

LeetCode Problem - 3033

class Solution {
    // Method to modify the matrix based on specific rules
    public int[][] modifiedMatrix(int[][] matrix) {
        // Loop through each row of the matrix
        for (int i = 0; i < matrix.length; i++) {
            // Loop through each column in the current row
            for (int j = 0; j < matrix[0].length; j++) {
                // If the current element is -1, replace it with the largest element in the same column
                if (matrix[i][j] == -1) {
                    matrix[i][j] = largestElement(j, matrix);
                }
            }
        }
        // Return the modified matrix
        return matrix;
    }

    // Method to find the largest element in a given column
    public int largestElement(int col, int[][] matrix) {
        // Initialize the result with the smallest possible integer value
        int result = Integer.MIN_VALUE;

        // Loop through each row of the matrix to find the largest element in the specified column
        for (int i = 0; i < matrix.length; i++) {
            int currVal = matrix[i][col];

            // Update the result if the current value is larger than the existing largest value
            if (currVal > result) {
                result = currVal;
            }
        }

        // Return the largest element found in the column
        return result;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.