| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.MaximumAreaOfLongestDiagonalRectangle; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MaximumAreaOfLongestDiagonalRectangleOnePass : IMaximumAreaOfLongestDiagonalRectangle |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="dimensions"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int AreaOfMaxDiagonal(int[][] dimensions) |
| 4 | 24 | | { |
| 4 | 25 | | var longestDiagonal = 0; |
| 4 | 26 | | var maximumArea = 0; |
| | 27 | |
|
| 42 | 28 | | foreach (var dimension in dimensions) |
| 15 | 29 | | { |
| 15 | 30 | | var length = dimension[0]; |
| 15 | 31 | | var width = dimension[1]; |
| | 32 | |
|
| 15 | 33 | | var currentDiagonal = (length * length) + (width * width); |
| | 34 | |
|
| 15 | 35 | | if (currentDiagonal < longestDiagonal) |
| 6 | 36 | | { |
| 6 | 37 | | continue; |
| | 38 | | } |
| | 39 | |
|
| 9 | 40 | | var currentArea = length * width; |
| | 41 | |
|
| 9 | 42 | | if (currentDiagonal == longestDiagonal) |
| 1 | 43 | | { |
| 1 | 44 | | maximumArea = Math.Max(maximumArea, currentArea); |
| 1 | 45 | | } |
| | 46 | | else |
| 8 | 47 | | { |
| 8 | 48 | | longestDiagonal = currentDiagonal; |
| 8 | 49 | | maximumArea = currentArea; |
| 8 | 50 | | } |
| 9 | 51 | | } |
| | 52 | |
|
| 4 | 53 | | return maximumArea; |
| 4 | 54 | | } |
| | 55 | | } |