알고리즘 86 - Breaking chocolate problem

jabae·2021년 11월 1일
0

알고리즘

목록 보기
86/97

Q.

Your task is to split the chocolate bar of given dimension n x m into small squares. Each square is of size 1x1 and unbreakable. Implement a function that will return minimum number of breaks needed.

For example if you are given a chocolate bar of size 2 x 1 you can split it to single squares in just one break, but for size 3 x 1 you must do two breaks.

If input data is invalid you should return 0 (as in no breaks are needed if we do not have any chocolate to split). Input will always be a non-negative integer.

A)

function breakChocolate(n,m) {
  if (n <= 0 || m <= 0)
    return 0;
  else
    return (n * m) - 1;
}
profile
it's me!:)

1개의 댓글

comment-user-thumbnail
2021년 11월 9일

Yeah, splitting the chocolate bar is always a challenge. I love sweets, but also not everything, because I have been eating sweets for as long as I can remember, and therefore I have already tried almost everything. I wanted to try something new and unusual. My friend decided to please me and gave me a very tasty organic chocolate bar. If you also have a sweet tooth just go to the site https://chocolateandlove.com/products/sea-salt-55 and you can try this delicious food yourself.

답글 달기