๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ/LeetCode

[LeetCode] Maximum Area Of A Piece Of Cake After Horizontal And Vertical Cuts

๋ฌธ์ œ๋งํฌ

 

Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

์ „์ฒด ๋†’์ด(h), ํญ(w)์ด ์ฃผ์–ด์ง€๊ณ 
์ˆ˜ํ‰์œผ๋กœ ์ž๋ฅด๋Š” ๋ถ€๋ถ„(horizontalCuts), ์ˆ˜์ง์œผ๋กœ ์ž๋ฅด๋Š” ๋ถ€๋ถ„(verticalCuts)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ด๋•Œ ์ž˜๋ ค์ง„ ๋ถ€๋ถ„ ์ค‘ ์ตœ๋Œ€ ๊ฐ’์„ ๊ตฌํ•˜๋ผ.

์ ‘๊ทผ ๋ฐฉ๋ฒ•

๋งŒ์•ฝ h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] ๋ผ๋ฉด
์‹ค์ œ ๋‚˜๋‰˜๋Š” ๋ถ€๋ถ„์€ horizontalCuts = [0,1,2,4,5], verticalCuts = [0,1,3,4]์ด ๋œ๋‹ค.
๋”ฐ๋ผ์„œ ์‹ค์ œ ๋‚˜๋‰˜๋Š” ๋ถ€๋ถ„์œผ๋กœ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ์ •๋ ฌ ํ•ด์ค€๋‹ค.
const newHorizontalCuts = [0, ...horizontalCuts, h].sort((a,b) => a - b);
const newVerticalCuts = [0, ...verticalCuts, w].sort((a,b) => a - b);โ€‹

 


๊ทธ๋ฆฌ๊ณ  ์ž˜๋ฆฌ๋Š” ์„ ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋‰œ ๋ถ€๋ถ„์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ด์ค€๋‹ค.
const cuttedHLen = [];
const cuttedWLen = [];

for(let i = 1; i < newHorizontalCuts.length; i++){
    cuttedHLen.push(newHorizontalCuts[i] - newHorizontalCuts[i-1]);
}
for(let i = 1; i < newVerticalCuts.length; i++){
    cuttedWLen.push(newVerticalCuts[i] - newVerticalCuts[i-1]);
}โ€‹

๋งˆ์ง€๋ง‰์œผ๋กœ ๋‚˜๋‰œ ๊ฐ€๋กœ์™€ ์„ธ๋กœ์˜ ๊ธธ์ด์˜ ์ตœ๋Œ€๊ฐ’์„ ๊ฐ€์ ธ์™€ ์„œ๋กœ ๊ณฑํ•ด์ค€๋‹ค.
let maxArea = BigInt(Math.max(...cuttedHLen)) * BigInt(Math.max(...cuttedWLen));
    
return maxArea % BigInt(1e9 + 7)โ€‹


๋ฐฐ์šด์ 

๋งค์šฐ ํฐ์ˆ˜๊ฐ€ ๋‚˜์˜ฌ ๊ฒฝ์šฐ 10^9 + 7๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ •๋‹ต์œผ๋กœ ๊ตฌํ•˜๋ผ๋Š” ์กฐ๊ฑด์ด ์žˆ์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ BigInt๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์ •๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์—†์—ˆ๋‹ค.

let maxArea = Math.max(...cuttedHLen) * Math.max(...cuttedWLen);
    
return maxArea % (1e9 + 7)โ€‹


์‹ค์ œ ๊ฐœ๋ฐœ์ž๋„๊ตฌ์—์„œ ํ™•์ธํ•ด ๋ณธ ๊ฒฐ๊ณผ BigInt๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด 1์˜ ์ž๋ฆฌ์˜ 4๋ฅผ ๊ตฌํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

MDN์—์„œ BigInt์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋‹ˆ 2^53 - 1 ์ด์ƒ์˜ ์ˆ˜๋ฅผ ๋‹ค๋ฃฐ ๊ฒฝ์šฐ BigInt๋ฅผ ์‚ฌ์šฉํ•˜๊ธธ ๊ถŒ์žฅํ•œ๋‹ค.

2^53 - 1 ~= 1e15 * 9

โ€ป BigInt๋ฅผ ๊ณ„์‚ฐํ• ๋•Œ์—” BigInt๋ผ๋ฆฌ๋งŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ( BigInt(2) * 2 ์•ˆ๋จ )

 

/**
 * @param {number} h
 * @param {number} w
 * @param {number[]} horizontalCuts
 * @param {number[]} verticalCuts
 * @return {number}
 */
 var maxArea = function(h, w, horizontalCuts, verticalCuts) {
  const newHorizontalCuts = [0, ...horizontalCuts, h].sort((a,b) => a - b);
  const newVerticalCuts = [0, ...verticalCuts, w].sort((a,b) => a - b);
  
  const cuttedHLen = [];
  const cuttedWLen = [];
  
  for(let i = 1; i < newHorizontalCuts.length; i++){
      cuttedHLen.push(newHorizontalCuts[i] - newHorizontalCuts[i-1]);
  }
  for(let i = 1; i < newVerticalCuts.length; i++){
      cuttedWLen.push(newVerticalCuts[i] - newVerticalCuts[i-1]);
  }
  
  let maxArea = BigInt(Math.max(...cuttedHLen)) * BigInt(Math.max(...cuttedWLen));
  
  return maxArea % BigInt(1e9 + 7)
};

'์ฝ”๋”ฉํ…Œ์ŠคํŠธ > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[LeetCode] Longest Consecutive Sequence  (0) 2022.07.05
[LeetCode] Candy  (0) 2022.07.04
[LeetCode] Maximum Bags With Full Capacity Of Rocks  (0) 2022.07.02
[LeetCode] Maximum Units On A Truck  (0) 2022.07.01
[LeetCode] Minimum Deletions To Make Character Frequencies Unique  (0) 2022.06.28