LeetCode 2388: Change Null Values in a Table to the Previous Value Solution
Master LeetCode problem 2388 (Change Null Values in a Table to the Previous Value), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2388. Change Null Values in a Table to the Previous Value
Problem Explanation
Explanation:
Given a table with null values, the task is to change those null values to the value from the previous non-null cell in the same column.
Algorithm:
- Iterate through each row of the table.
- For each cell, if it is null, update it with the value of the previous non-null cell in the same column.
- Keep track of the previous non-null value for each column.
Time Complexity: O(m*n) where m is the number of rows and n is the number of columns in the table. Space Complexity: O(n) where n is the number of columns in the table.
:
Solution Code
class Solution {
public void fillTable(int[][] table) {
if (table == null || table.length == 0) {
return;
}
int numRows = table.length;
int numCols = table[0].length;
int[] prevValues = new int[numCols];
Arrays.fill(prevValues, -1);
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (table[i][j] == -1) {
table[i][j] = prevValues[j];
} else {
prevValues[j] = table[i][j];
}
}
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2388 (Change Null Values in a Table to the Previous Value)?
This page provides optimized solutions for LeetCode problem 2388 (Change Null Values in a Table to the Previous Value) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2388 (Change Null Values in a Table to the Previous Value)?
The time complexity for LeetCode 2388 (Change Null Values in a Table to the Previous Value) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2388 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2388 in Java, C++, or Python.