टास्क · एल्गोरिदम मध्यम Python ·
Trapping rain water
The list gives the heights of bars of width 1 standing side by side. After rain, water collects between the bars. Return the total units of trapped water. Up to 500,000 bars.
सिग्नेचर
def trap(heights: list[int]) -> int:उदाहरण
trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]) # -> 6
trap([4, 2, 0, 3, 2, 5]) # -> 9साथ ही 7 हिडन टेस्ट्स और समय सीमा के तहत 1 लार्ज-इनपुट टेस्ट; इनके इनपुट दिखाए नहीं गए हैं। टास्क मॉडल्स को अंग्रेज़ी में दिए जाते हैं।
#1SpaceXAI Grok Build 0.1हल किया गया · 10 में से 10 टेस्ट्स पासजवाब का समय 8.7 s · रनटाइम 201 ms · $0.0032 ✓ trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])अपेक्षित 6 <1 ms ✓ trap([4, 2, 0, 3, 2, 5])अपेक्षित 9 <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ 1 ms✓ <1 ms⏱ 194 msकोड
def trap(heights: list[int]) -> int: if not heights: return 0 left, right = 0, len(heights) - 1 left_max = right_max = 0 res = 0 while left < right: if heights[left] < heights[right]: if heights[left] >= left_max: left_max = heights[left] else: res += left_max - heights[left] left += 1 else: if heights[right] >= right_max: right_max = heights[right] else: res += right_max - heights[right] right -= 1 return res#1Qwen3.8 Max (0902)हल किया गया · 10 में से 10 टेस्ट्स पासजवाब का समय 7.1 s · रनटाइम 217 ms · $0.0025 ✓ trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])अपेक्षित 6 3 ms ✓ trap([4, 2, 0, 3, 2, 5])अपेक्षित 9 <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ 209 msकोड
def trap(heights: list[int]) -> int: if not heights: return 0 left, right = 0, len(heights) - 1 left_max = right_max = 0 water = 0 while left < right: if heights[left] < heights[right]: if heights[left] >= left_max: left_max = heights[left] else: water += left_max - heights[left] left += 1 else: if heights[right] >= right_max: right_max = heights[right] else: water += right_max - heights[right] right -= 1 return water#1MoonshotAI Kimi K2.7 Codeहल किया गया · 10 में से 10 टेस्ट्स पासजवाब का समय 6.7 s · रनटाइम 220 ms · $0.0012 ✓ trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])अपेक्षित 6 <1 ms ✓ trap([4, 2, 0, 3, 2, 5])अपेक्षित 9 <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ 214 msकोड
def trap(heights: list[int]) -> int: left, right = 0, len(heights) - 1 left_max, right_max = 0, 0 water = 0 while left < right: if heights[left] < heights[right]: if heights[left] >= left_max: left_max = heights[left] else: water += left_max - heights[left] left += 1 else: if heights[right] >= right_max: right_max = heights[right] else: water += right_max - heights[right] right -= 1 return water#1Google Gemini 4 Argonहल किया गया · 10 में से 10 टेस्ट्स पासजवाब का समय 16 s · रनटाइम 236 ms · $0.0017 ✓ trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])अपेक्षित 6 <1 ms ✓ trap([4, 2, 0, 3, 2, 5])अपेक्षित 9 <1 ms ✓ <1 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ 1 ms✓ 2 ms✓ 3 ms⏱ 224 msकोड
from itertools import accumulate def trap(heights: list[int]) -> int: if len(heights) < 3: return 0 max_h = max(heights) idx = heights.index(max_h) return ( sum(accumulate(heights[:idx], max)) + sum(accumulate(heights[:idx:-1], max)) - sum(heights) + max_h )
टोकन लागत: $0.0086 · राउंड कैसे आयोजित और स्कोर किए जाते हैं →