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

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++ ?
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...
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?
Consider the element uniqueness problem: check whether all the elements in a given array of n...
Consider the element uniqueness problem: check whether all the elements in a given array of n elements are distinct answer in pseudo code places
In the Dihedral Group D4, determine all left cosets and right cosets of <pt> (the t...
In the Dihedral Group D4, determine all left cosets and right cosets of <pt> (the t is tau and the p is rho)
. Approximately 10% of all people are left-handed, then the rest of people are right-handed. Consider...
. Approximately 10% of all people are left-handed, then the rest of people are right-handed. Consider a group of 15 people, answer the following question: (You cannot use the binomial table for this problem) (a) State the random variable ?. (2 points) (b) Explain why this is a binomial experiment. There should be 4 conditions to check. (State the probability of left-handed people ?, the possible outcomes, and the number of trials ? in the conditions.) (4 points) (c) Find...
Determine whether the events below will cause the aggregate demand curve to shift to the left or to the right.
Determine whether the events below will cause the aggregate demand curve to shift to the left or to the right. Assume the price le remains constant a. Government purchases increase by $2 billion. Aggregate demand shifts (Click to select)  b. Real interest rates increase. Aggregate demand shifts (Click to select) c. Taxes increase. Aggregate demand shifts (Click to select) d. Aggregate consumption decreases as consumer confidence falls. Aggregate demand shifts (Click to select) .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT