| | 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.CalculateMoneyInLeetcodeBank; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class CalculateMoneyInLeetcodeBankArithmeticSequence : ICalculateMoneyInLeetcodeBank |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(1) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="n"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int TotalMoney(int n) |
| 3 | 24 | | { |
| 3 | 25 | | var fullWeeks = n / 7; |
| 3 | 26 | | var remainingDays = n % 7; |
| | 27 | |
|
| 3 | 28 | | var sumFullWeeks = 0; |
| | 29 | |
|
| 12 | 30 | | for (var week = 1; week <= fullWeeks; week++) |
| 3 | 31 | | { |
| 3 | 32 | | sumFullWeeks += 7 * ((2 * week) + ((7 - 1) * 1)) / 2; |
| 3 | 33 | | } |
| | 34 | |
|
| 3 | 35 | | var startAmount = fullWeeks + 1; |
| 3 | 36 | | var sumRemainingDays = remainingDays * ((2 * startAmount) + ((remainingDays - 1) * 1)) / 2; |
| | 37 | |
|
| 3 | 38 | | return sumFullWeeks + sumRemainingDays; |
| 3 | 39 | | } |
| | 40 | | } |