टीएल;डीआर
- समस्या: सीटीसीआई समस्या १७.२१ का तकनीकी विवरण।
- दृष्टिकोण: सीटीसीआई problem १७.२१: compute total volume of water trapped between bars in a २D histogram in O(N) time.
- जटिलता: इष्टतम समय और मेमोरी संतुलन।
यह लेख सीटीसीआई समस्या १७.२१ का एक स्पष्ट विवरण प्रदान करता है।
१. संदर्भ और समस्या कथन
सीटीसीआई problem १७.२१: compute total volume of water trapped between bars in a २D histogram in O(N) time.
२. कोड और कार्यान्वयन
public static int computeVolume(int[] histo) {
int left = 0, right = histo.length - 1;
int leftMax = 0, rightMax = 0, volume = 0;
while (left < right) {
if (histo[left] < histo[right]) {
if (histo[left] >= leftMax) leftMax = histo[left];
else volume += leftMax - histo[left];
left++;
} else {
if (histo[right] >= rightMax) rightMax = histo[right];
else volume += rightMax - histo[right];
right--;
}
}
return volume;
}
३. सारांश और एज केसेस
हमेशा सीमांत स्थितियों और इनपुट की जांच करें।
