# Why are the two ways different?

• 1st: TLE occured.
2nd: AC.

But I think the 2nd one is faster than the 1st, considering height[i] and height[j] compare only once per round. But why is the 1st TLE?

``````        // 1st
int max = 0;
int i = 0;
int j = height.length - 1;
while (i < j) {
int loMax = height[i];
int hiMax = height[j];

// 重点
if (height[i] <= height[j]) {
max = Math.max(height[i] * (j - i), max);
while (i < j && height[i] <= loMax) ++i;
} else {
max = Math.max(height[j] * (j - i), max);
while (i < j && height[j] <= hiMax) --j;
}
}

return max;

``````
``````        // 2nd way
int max = 0;
int i = 0;
int j = height.length - 1;
while (i < j) {
int loMax = height[i];
int hiMax = height[j];
max = Math.max(Math.min(height[i], height[j]) * (j - i), max);

// without this, TLE will occur
if (height[i] <= height[j])
while (i < j && height[i] <= loMax) ++i;
else
while (i < j && height[j] <= hiMax) --j;
}

return max;``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.