Paper Cutting (with.Java)
This is an article about the “Paper Cutting (with.Java)” problem.
As I solve coding test problems, I look back on the problems I solved and look into different solution methods to learn more.
Let’s look at the problem first.
problem
I’m trying to cut a large piece of paper into 1 x 1 pieces.
For example, cutting a 2 x 2 piece of paper into a 1 x 1 piece requires at least three strokes of the scissors.
When integers M and N are given as parameters, complete the solution function to return the minimum number of times a piece of paper of size M x N must be scissored.
Restrictions
- 0 < M, N < 100
- Paper cannot be overlapped and cut.
Input/Output Example
M | N | result |
---|---|---|
2 | 2 | 3 |
2 | 5 | 9 |
1 | 1 | 0 |
My solution to the problem
class Solution {
public int solution(int M, int N) {
int answer = (M * N) - 1;
return answer;
}
}
Solution review
This is a function that calculates the number of cells remaining in a grid-shaped area excluding one cell using the given M and N.
First, declare the variable answer and initialize it to (M * N) - 1
.
This is the total number of cells in the grid minus one.
Finally, it returns an answer.