Задача · алгоритмы сложная 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 мс · $0.0032 ✓ largest_rectangle([2, 1, 5, 6, 2, 3])ожидалось 10 7 мс ✓ largest_rectangle([2, 4])ожидалось 4 2 мс ✓ <1 мс✓ <1 мс✓ 3 мс✓ 3 мс✓ 1 мс✓ <1 мс⏱ 51 мс⏱ 52 мсКод
/** * @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 мс · $0.0001 ✓ largest_rectangle([2, 1, 5, 6, 2, 3])ожидалось 10 2 мс ✓ largest_rectangle([2, 4])ожидалось 4 2 мс ✓ 1 мс✓ 1 мс✓ <1 мс✓ 22 мс✓ 3 мс✓ 4 мс⏱ 26 мс⏱ 67 мсКод
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 мс · $0.0099 ✓ largest_rectangle([2, 1, 5, 6, 2, 3])ожидалось 10 2 мс ✓ largest_rectangle([2, 4])ожидалось 4 3 мс ✓ <1 мс✓ 1 мс✓ 18 мс✓ 1 мс✓ 2 мс✓ 1 мс⏱ 44 мс⏱ 118 мсКод
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 мс · $0.0004 ✓ largest_rectangle([2, 1, 5, 6, 2, 3])ожидалось 10 3 мс ✓ largest_rectangle([2, 4])ожидалось 4 1 мс ✓ 3 мс✓ 5 мс✓ 1 мс✓ 1 мс✓ <1 мс✓ 1 мс⏱ 68 мс⏱ 117 мсКод
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 · Как проходят и оцениваются раунды →