Leetcode 566. Reshape the Matrix
I am a developer from Nashville, TN. I specialize in the .NET tech stack. I have created many projects in Blazor WASM, Xamarin, MAUI, etc.
Search for a command to run...
I am a developer from Nashville, TN. I specialize in the .NET tech stack. I have created many projects in Blazor WASM, Xamarin, MAUI, etc.
No comments yet. Be the first to comment.
A series documenting my journey to improving my ability to solve LeetCode problems through YouTube videos, study plans, articles, etc. Using my own words for later reference.
Intuition Loop through input array and fill answer array in one pass. Approach In one forloop set the ans[i] and ans[i + nums.Length]to the current value at nums[i]. Code public class Solution { public int[] GetConcatenation(int[] nums) { ...
Intuition Get the length of the subsequence (if any) and subtract it from the length of t to find out how many characters we have to append (if any) Approach Keep a count of the length of the subsequence while iterating through s and then subtract th...
Intuition Sort the array and check if the start and end of the array match. If not, increment or decrement accordingly. Approach First sort the array. Then use two pointers. One at the beginning, the other at the end. Check to see if the value at the...
Intuition Find position of ch (if any) then use two pointers to swap characters until string is reveresed from beginning to index of ch Approach Start by checking each character for ch. If we find it, that's where our right pointer will start. Our le...
Intuition Use two pointers to iterate through the string checking each adjacent character to see if it is "bad" or "good". Approach First if the string is 1 or fewer characters we know it is good so we can just return it. If that's not the case, we c...
Intuition Since both arrays are sorted, we can find the minimum common value by iterating from left to right with two pointers. Approach Initialize both pointers to 0 to start at the beginning of the array. While both pointers are within bounds of th...
First check to make sure there's enough elements in the original matrix to meet the new row and column requirements. I did this by creating a new list to store all of the original values (which we will then use later to fill the new matrix) and then get the count of the list and ensure it's the same as the required amount of rows and columns by multiplying r * c and comparing to the count.
If there's not enough, just return the original matrix.
If there is enough, create the new matrix and keep track of what posisiton we're at in the list of original values. Then iterate through the rows and columns and at each new row initialize the column array. At each column add the value and increment the original position.
public class Solution {
public int[][] MatrixReshape(int[][] mat, int r, int c)
{
//create new list to store original values
var nums = new List<int>();
//iterate through original matrix adding values to list
for(int row = 0; row < mat.Length; row++)
{
for(int col = 0; col < mat[row].Length; col++)
{
nums.Add(mat[row][col]);
}
}
//check that there's enough values to fill up required rows and columns
if((r * c) != nums.Count)
{
//if there's not enough, return original matrix
return mat;
}
//create new matrix
var answer = new int[r][];
//keep track of position in the list of original values
var originalPos = 0;
//iterate through rows and columns adding the value at the current
//position in the list of original numbers
for(int row = 0; row < r; row++)
{
//in C# you have to manually create the new columns
//so do this when we loop to a new row
answer[row] = new int[c];
for(int col = 0; col < c; col++)
{
answer[row][col] = nums[originalPos];
//iterate position in list of original numbers
originalPos++;
}
}
return answer;
}
}