| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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.BuyTwoChocolates; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class BuyTwoChocolatesBruteForce : IBuyTwoChocolates |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n^2) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="prices"></param> |
| | | 22 | | /// <param name="money"></param> |
| | | 23 | | /// <returns></returns> |
| | | 24 | | public int BuyChoco(int[] prices, int money) |
| | 5 | 25 | | { |
| | 5 | 26 | | int? result = null; |
| | | 27 | | |
| | 74 | 28 | | for (var i = 0; i < prices.Length; i++) |
| | 32 | 29 | | { |
| | 278 | 30 | | for (var j = i + 1; j < prices.Length; j++) |
| | 107 | 31 | | { |
| | 107 | 32 | | var leftover = money - prices[i] - prices[j]; |
| | | 33 | | |
| | 107 | 34 | | if (leftover < 0) |
| | 89 | 35 | | { |
| | 89 | 36 | | continue; |
| | | 37 | | } |
| | | 38 | | |
| | 18 | 39 | | if (result.HasValue) |
| | 14 | 40 | | { |
| | 14 | 41 | | if (leftover > result) |
| | 5 | 42 | | { |
| | 5 | 43 | | result = leftover; |
| | 5 | 44 | | } |
| | 14 | 45 | | } |
| | | 46 | | else |
| | 4 | 47 | | { |
| | 4 | 48 | | result = leftover; |
| | 4 | 49 | | } |
| | 18 | 50 | | } |
| | 32 | 51 | | } |
| | | 52 | | |
| | 5 | 53 | | return result ?? money; |
| | 5 | 54 | | } |
| | | 55 | | } |