Skip to content

Instantly share code, notes, and snippets.

@HELLoWorlD01100
Created March 7, 2021 13:15
Show Gist options
  • Save HELLoWorlD01100/bae5d7c06fa9026190d462b1855eaa91 to your computer and use it in GitHub Desktop.
Save HELLoWorlD01100/bae5d7c06fa9026190d462b1855eaa91 to your computer and use it in GitHub Desktop.
Контейнер с наибольшим объемом
using System;
namespace ConsoleApp1
{
public static class Solution
{
public static int FindMaxArea(int[] heights)
{
var leftIndex = 0;
var rightIndex = heights.Length - 1;
var maxArea = 0;
while (leftIndex < rightIndex)
{
var width = rightIndex - leftIndex;
var height = Math.Min(heights[leftIndex], heights[rightIndex]);
var area = width * height;
maxArea = Math.Max(area, maxArea);
if (heights[leftIndex] < heights[rightIndex])
leftIndex++;
else
rightIndex--;
}
return maxArea;
}
}
}
using System;
using ConsoleApp1;
using FluentAssertions;
using NUnit.Framework;
namespace Tests
{
public class SolutionTests
{
[Test]
public void FindMaxArea_ShouldReturn0_WhenOnlyOneHeight()
{
var heights = new [] {4};
var expected = 0;
var actual = Solution.FindMaxArea(heights);
actual.Should().Be(expected);
}
[Test]
public void FindMaxArea_ShouldReturn0_WhenNoHeights()
{
var heights = Array.Empty<int>();
var expected = 0;
var actual = Solution.FindMaxArea(heights);
actual.Should().Be(expected);
}
[Test]
public void FindMaxArea_ShouldPassExampleOne()
{
var heights = new[] {1, 8, 6, 2, 5, 4, 8, 3, 7};
var expected = 49;
var actual = Solution.FindMaxArea(heights);
actual.Should().Be(expected);
}
[Test]
public void FindMaxArea_ShouldPassExampleTwo()
{
var heights = new[] {1, 3, 1};
var expected = 2;
var actual = Solution.FindMaxArea(heights);
actual.Should().Be(expected);
}
[Test]
public void FindMaxArea_ShouldPass_WhenOnlySameHeights()
{
var heights = new[] {2, 2, 2, 2};
var expected = 6;
var actual = Solution.FindMaxArea(heights);
actual.Should().Be(expected);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment