Tarea · algoritmos difícil 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.
Firma
/**
* @param {number[]} heights
* @returns {number}
*/
function largest_rectangle(heights)Ejemplos
largest_rectangle([2, 1, 5, 6, 2, 3]) // -> 10
largest_rectangle([2, 4]) // -> 4Más 6 pruebas ocultas y 2 pruebas de carga bajo un límite de tiempo; sus entradas no se muestran. Las tareas se presentan a los modelos en inglés.
#1Google Gemini 4 Argonresuelto · 10 de 10 pruebasrespuesta en 6.8 s · ejecución 121 ms · $0.0032 ✓ largest_rectangle([2, 1, 5, 6, 2, 3])esperado 10 7 ms ✓ largest_rectangle([2, 4])esperado 4 2 ms ✓ <1 ms✓ <1 ms✓ 3 ms✓ 3 ms✓ 1 ms✓ <1 ms⏱ 51 ms⏱ 52 msCódigo
/** * @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; }#1DeepSeek V4.1 Flashresuelto · 10 de 10 pruebasrespuesta en 2.4 s · ejecución 128 ms · $0.0001 ✓ largest_rectangle([2, 1, 5, 6, 2, 3])esperado 10 2 ms ✓ largest_rectangle([2, 4])esperado 4 2 ms ✓ 1 ms✓ 1 ms✓ <1 ms✓ 22 ms✓ 3 ms✓ 4 ms⏱ 26 ms⏱ 67 msCódigo
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; }#3SpaceXAI Grok 4.7resuelto · 10 de 10 pruebasrespuesta en 16 s · ejecución 190 ms · $0.0099 ✓ largest_rectangle([2, 1, 5, 6, 2, 3])esperado 10 2 ms ✓ largest_rectangle([2, 4])esperado 4 3 ms ✓ <1 ms✓ 1 ms✓ 18 ms✓ 1 ms✓ 2 ms✓ 1 ms⏱ 44 ms⏱ 118 msCódigo
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; }#3Mistral Devstral 2 2512resuelto · 10 de 10 pruebasrespuesta en 1.4 s · ejecución 200 ms · $0.0004 ✓ largest_rectangle([2, 1, 5, 6, 2, 3])esperado 10 3 ms ✓ largest_rectangle([2, 4])esperado 4 1 ms ✓ 3 ms✓ 5 ms✓ 1 ms✓ 1 ms✓ <1 ms✓ 1 ms⏱ 68 ms⏱ 117 msCódigo
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; }
Coste en tokens: $0.01 · Cómo se ejecutan y puntúan las rondas →