Question

In: Computer Science

A slice consists of all the elements that are below, below right, or below left of the element above it. The Min Slice Weight is the minimum sum of all elements in the slice.

A Hackerrank Challenge:

Given an NxN matrix, slice it and find the minimum slice weight.

A slice consists of all the elements that are below, below right, or below left of the element above it. The Min Slice Weight is the minimum sum of all elements in the slice.

Example: given input

1 2 3

4 5 6

7 8 9

The slices would be 1 4 7 1; 1 4 8; 1 5 7; 1 5 8; 1 5 9; 2 4 7; 2 4 8; 2 5 8; 2 5 9;2 6 8; 2 6 9 ....etc.

KEEP IN MIND THAT THE MATRIX IS NOT 3 X 3, BUT N X N.

The minimum slice would be 1 4 7 in this case because 1+4+7 is the minimum of all the sums;

Write a function public int FindMinSlice (List<List> Matrix) that returns the min slice weight.


Solutions

Expert Solution

public int FindMinSlice (List> Matrix)
{
int rows=Matrix.size(); // number of rows in the matrix
if(rows==0)
return 0;
int columns=Matrix.get(0).size(); // number of columns in the matrix
if(columns==0)
return 0;
int sliceSum[]=new int[columns]; // maintains the current minimum slice sum starting form column j, we build this array bottom up
for(int i=0;i sliceSum[i]=Matrix.get(rows-1).get(i); // fill the sliceSum array with the values in the last row
for(int i=rows-2;i>=0;i--) // iterate through the rows upward starting from the second-last row
{
for(int j=columns-1;j>=0;j--) { // iterate through the columns leftward starting from the last column
// sliceSum[j] = Matrix[i][j]+ min(sliceSum[j-1], sliceSum[j], sliceSum[j+1]) if the required indices exists in sliceSum
if(j==columns-1)
sliceSum[j]=Math.min(sliceSum[j-1],sliceSum[j])+Matrix.get(i).get(j);
else if(j==0)
sliceSum[j]=Math.min(sliceSum[j],sliceSum[j+1])+Matrix.get(i).get(j);
else
sliceSum[j]=Math.min(sliceSum[j-1],Math.min(sliceSum[j],sliceSum[j+1]))+Matrix.get(i).get(j);
}
}
int minSliceSum=sliceSum[0]; // current minimum sliceSum
for(int i=1;i minSliceSum=Math.min(minSliceSum,sliceSum[i]); // update the minimum sliceSum if required
return minSliceSum;
}


Related Solutions

Sum all the elements in the two-dimensional array below. Print the // result to the console....
Sum all the elements in the two-dimensional array below. Print the // result to the console. var arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] javascript
a) The rectangles in the graph below illustrate a    ?    left endpoint    right endpoint    midpoint     Riemann sum for ?(?)=?29f(x)=x29 on the...
a) The rectangles in the graph below illustrate a    ?    left endpoint    right endpoint    midpoint     Riemann sum for ?(?)=?29f(x)=x29 on the interval [3,7][3,7].   The value of this Riemann sum is equation editor Equation Editor , and this Riemann sum is an    ?    overestimate of    equal to    underestimate of    there is ambiguity     the area of the region enclosed by ?=?(?)y=f(x), the x-axis, and the vertical lines x = 3 and x = 7. Riemann sum for ?=?29y=x29 on [3,7][3,7] b) The rectangles in the graph below illustrate a    ?    left endpoint    right endpoint    midpoint    Riemann sum for...
The first ionization energies of the elements __________ as you go from left to right across...
The first ionization energies of the elements __________ as you go from left to right across a period of the periodic table, and __________ as you go from the bottom to the top of a group in the table.
how do you get the sum of all elements in a stack in c++ ?
how do you get the sum of all elements in a stack in c++ ?
Write a MIPS assembly program that calculates the sum of all the elements in the following...
Write a MIPS assembly program that calculates the sum of all the elements in the following array: int array[10]={12,21,3,40,15,6,17,8,29,10}
2. For each of the following, find all minimum sum of products expressions. (If there is...
2. For each of the following, find all minimum sum of products expressions. (If there is more than one solution, the number of solutions is given in parentheses.) a. f(a, b, c) Σm(1, 2, 3, 6, 7) b. f(a, b, c, d) Σm(1, 2, 3, 5, 6, 7, 8, 11, 13, 15) c. h(a, b, c, d) Σm(2, 4, 5, 6, 7, 8, 10, 12, 13, 15) d. f(w, x, y, z) Σm(0, 1, 2, 4, 5, 6, 9, 10,...
Match each subtype of lipid listed on the right to all statements on the left that...
Match each subtype of lipid listed on the right to all statements on the left that are appropriate to the lipid subtype. Question 12 options: 1234 the bi-layer that makes up the plasma membrane around a cell is considered a 'sea' of this type of lipid molecules 1234 the phospholipid, arachodonic acid, serves as the starting molecule that leads to the formation of members of this lipid subtype via the action of lipogenase and cyclooxygenase enzymes 1234 the main member...
Read each of the below activities and then circle which hemisphere (the left or the right)...
Read each of the below activities and then circle which hemisphere (the left or the right) controls that behavior and explain why. 1.    Writing lecture notes while in class: LEFT or RIGHT        2.    Watching your favorite TV show: LEFT or RIGHT        3.    Catching a ball in left field: LEFT or RIGHT 4.    Doing math problems for a homework assignment: LEFT or RIGHT 5.    Doing a crossword puzzle: LEFT or RIGHT 6. Running a marathon:...
Find the largest and smallest element of a linked list, print total of all elements and...
Find the largest and smallest element of a linked list, print total of all elements and find out the average. Code needed in java
1) Choose a subgroup of Pentagons D5, and list all the left or right cosets of...
1) Choose a subgroup of Pentagons D5, and list all the left or right cosets of your pet. Will the set of right cosets be different from the left cosets? Explain. 2) Does Pentagons D5, have any non-trivial normal subgroups? If so, give an example.If not, explain why not. 3) Is there a second binary structure that would make Pentagons D5 a ring?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT