문제 · 알고리즘 어려움 JavaScript ·
Largest rectangle in a histogram
The list gives the heights of bars of width 1 standing side by side. Return the area of the largest rectangle that fits entirely under the bars. Up to 200,000 bars.
함수 시그니처
/**
* @param {number[]} heights
* @returns {number}
*/
function largest_rectangle(heights)예제
largest_rectangle([2, 1, 5, 6, 2, 3]) // -> 10
largest_rectangle([2, 4]) // -> 4제한 시간이 적용되는 6 개의 비공개 테스트와 2 개의 대규모 입력 테스트가 추가로 진행되며, 입력값은 공개되지 않습니다. 문제는 모델에 영어로 주어집니다.
1위Google Gemini 4 Argon해결 · 테스트 10/10 통과응답 소요 6.8 초 · 실행 시간 121 ms · $0.0032 ✓ largest_rectangle([2, 1, 5, 6, 2, 3])기댓값 10 7 ms ✓ largest_rectangle([2, 4])기댓값 4 2 ms ✓ <1 ms✓ <1 ms✓ 3 ms✓ 3 ms✓ 1 ms✓ <1 ms⏱ 51 ms⏱ 52 ms코드
/** * @param {number[]} heights * @returns {number} */ function largest_rectangle(heights) { const n = heights.length; if (n === 0) return 0; // Using Int32Array for the stack avoids dynamic array allocations and is very fast const stack = new Int32Array(n + 1); let top = -1; let maxArea = 0; for (let i = 0; i <= n; i++) { const h = i === n ? 0 : heights[i]; while (top >= 0 && heights[stack[top]] >= h) { const height = heights[stack[top--]]; const width = top === -1 ? i : i - stack[top] - 1; const area = height * width; if (area > maxArea) { maxArea = area; } } stack[++top] = i; } return maxArea; }1위DeepSeek V4.1 Flash해결 · 테스트 10/10 통과응답 소요 2.4 초 · 실행 시간 128 ms · $0.0001 ✓ largest_rectangle([2, 1, 5, 6, 2, 3])기댓값 10 2 ms ✓ largest_rectangle([2, 4])기댓값 4 2 ms ✓ 1 ms✓ 1 ms✓ <1 ms✓ 22 ms✓ 3 ms✓ 4 ms⏱ 26 ms⏱ 67 ms코드
function largest_rectangle(heights) { const n = heights.length; if (n === 0) return 0; const stack = new Int32Array(n + 1); let top = -1; let best = 0; for (let i = 0; i <= n; i++) { const h = i < n ? heights[i] : 0; while (top >= 0 && heights[stack[top]] >= h) { const height = heights[stack[top--]]; const left = top >= 0 ? stack[top] : -1; const area = height * (i - left - 1); if (area > best) best = area; } stack[++top] = i; } return best; }3위SpaceXAI Grok 4.7해결 · 테스트 10/10 통과응답 소요 16 초 · 실행 시간 190 ms · $0.0099 ✓ largest_rectangle([2, 1, 5, 6, 2, 3])기댓값 10 2 ms ✓ largest_rectangle([2, 4])기댓값 4 3 ms ✓ <1 ms✓ 1 ms✓ 18 ms✓ 1 ms✓ 2 ms✓ 1 ms⏱ 44 ms⏱ 118 ms코드
function largest_rectangle(heights) { const n = heights.length; const stack = []; let maxArea = 0; for (let i = 0; i <= n; i++) { const h = i === n ? 0 : heights[i]; while (stack.length > 0 && heights[stack[stack.length - 1]] > h) { const height = heights[stack.pop()]; const width = stack.length === 0 ? i : i - stack[stack.length - 1] - 1; const area = height * width; if (area > maxArea) maxArea = area; } stack.push(i); } return maxArea; }3위Mistral Devstral 2 2512해결 · 테스트 10/10 통과응답 소요 1.4 초 · 실행 시간 200 ms · $0.0004 ✓ largest_rectangle([2, 1, 5, 6, 2, 3])기댓값 10 3 ms ✓ largest_rectangle([2, 4])기댓값 4 1 ms ✓ 3 ms✓ 5 ms✓ 1 ms✓ 1 ms✓ <1 ms✓ 1 ms⏱ 68 ms⏱ 117 ms코드
function largest_rectangle(heights) { const stack = []; let maxArea = 0; const n = heights.length; for (let i = 0; i <= n; i++) { const h = i === n ? 0 : heights[i]; while (stack.length > 0 && h < heights[stack[stack.length - 1]]) { const height = heights[stack.pop()]; const width = stack.length === 0 ? i : i - stack[stack.length - 1] - 1; maxArea = Math.max(maxArea, height * width); } stack.push(i); } return maxArea; }
토큰 비용: $0.01 · 진행 방식 및 점수 산정 안내 →