| | 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.TargetSum; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class TargetSumRecursion : ITargetSum |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n * totalSum) |
| | 19 | | /// Space complexity - O(n * totalSum) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="nums"></param> |
| | 22 | | /// <param name="target"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int FindTargetSumWays(int[] nums, int target) |
| 8 | 25 | | { |
| 8 | 26 | | var totalSum = nums.Sum(); |
| | 27 | |
|
| 8 | 28 | | return CalculateWays( |
| 8 | 29 | | nums, |
| 8 | 30 | | 0, |
| 8 | 31 | | 0, |
| 8 | 32 | | target, |
| 8 | 33 | | totalSum, |
| 8 | 34 | | InitializeMemo(nums.Length, (2 * totalSum) + 1) |
| 8 | 35 | | ); |
| 8 | 36 | | } |
| | 37 | |
|
| | 38 | | private static int[,] InitializeMemo(int rows, int cols) |
| 8 | 39 | | { |
| 8 | 40 | | var memo = new int[rows, cols]; |
| | 41 | |
|
| 150 | 42 | | for (var i = 0; i < rows; i++) |
| 67 | 43 | | { |
| 119640 | 44 | | for (var j = 0; j < cols; j++) |
| 59753 | 45 | | { |
| 59753 | 46 | | memo[i, j] = int.MinValue; |
| 59753 | 47 | | } |
| 67 | 48 | | } |
| | 49 | |
|
| 8 | 50 | | return memo; |
| 8 | 51 | | } |
| | 52 | |
|
| | 53 | | private static int CalculateWays(int[] nums, int currentIndex, int currentSum, int target, int totalSum, |
| | 54 | | int[,] memo) |
| 14752 | 55 | | { |
| 14752 | 56 | | if (currentIndex == nums.Length) |
| 2844 | 57 | | { |
| 2844 | 58 | | return currentSum == target ? 1 : 0; |
| | 59 | | } |
| | 60 | |
|
| 11908 | 61 | | if (memo[currentIndex, currentSum + totalSum] != int.MinValue) |
| 4536 | 62 | | { |
| 4536 | 63 | | return memo[currentIndex, currentSum + totalSum]; |
| | 64 | | } |
| | 65 | |
|
| 7372 | 66 | | memo[currentIndex, currentSum + totalSum] = |
| 7372 | 67 | | CalculateWays(nums, currentIndex + 1, currentSum + nums[currentIndex], target, totalSum, memo) + |
| 7372 | 68 | | CalculateWays(nums, currentIndex + 1, currentSum - nums[currentIndex], target, totalSum, memo); |
| | 69 | |
|
| 7372 | 70 | | return memo[currentIndex, currentSum + totalSum]; |
| 14752 | 71 | | } |
| | 72 | | } |