題しI、1
第二に、アイデア
トラバース、最大出力。
個々の最適化のアイデア:離れて中央に向かって両端からトラバーサル時間。
第三に、コード
public class T011 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] height = {1,8,6,2,5,4,8,3,7};
System.out.println( maxArea(height) ); //49
}
public static int maxArea(int[] height) {
int area = 0;
for ( int i = 0; i < height.length; i++ ){
for ( int j = i+1; j < height.length; j++ ){
int tmp = (j-i)*Math.min( height[i], height[j] );
if ( tmp > area )
area = tmp;
}
}
return area;
}
}
出典:滞在ボタン(LeetCode)
リンクします。https://leetcode-cn.com/problems/container-with-most-water
すべてのネットワークからの控除が著作権を保有。商業転載は、ソースを明記してください許可公式、非商用の転載をご連絡ください。↩︎