Solution.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class Solution : MonoBehaviour { public int [] heightArray = { 1, 8, 6, 2, 5, 4, 8, 3, 7 }; void Start() { Debug.Log(MaxArea(heightArray)); } public int MaxArea( int [] height) { int leftIndex = 0; int rightIndex = height.Length - 1; int maxArea = 0; while (leftIndex < rightIndex) { int area = Math.Min(height[leftIndex], height[rightIndex]) * (rightIndex - leftIndex); maxArea = Math.Max(maxArea, area); if (height[leftIndex] < height[rightIndex]) ++leftIndex; else if (height[rightIndex] < height[leftIndex]) --rightIndex; else { ++leftIndex; --rightIndex; } } return maxArea; } } |