🤖המדריך להייטיקיסט המתחיל
בינוניLeetCode #11

Container With Most Water

עשויה להישאל ב:Melio

תיאור השאלה

Given n non-negative integers height where each represents a vertical line at coordinate i, find two lines that together with the x-axis form a container that holds the most water.

איך פותרים?

Move two pointers towards each other while tracking the best area, always discarding the shorter line.

פתרון קוד

JavaScript
Python
function maxArea(height) {
  let left = 0;
  let right = height.length - 1;
  let best = 0;
  while (left < right) {
    const current = Math.min(height[left], height[right]) * (right - left);
    best = Math.max(best, current);
    if (height[left] < height[right]) {
      left++;
    } else {
      right--;
    }
  }
  return best;
}

הסבר וידאו כיצד לפתור את השאלה

Book Cover

לעבור את ראיון העבודה הבא שלך בהצלחה

קורס דיגיטלי מקיף עם +25 שיעורים מעשיים, כשעתיים של וידאו, וליווי של מראיין בכיר.

התחל עכשיו ב-149 ₪ בלבד! 🚀