commit 6f3f55c021c479cc0846dd84583a9ae688304d4d parent 527c5d5a74094132209859e0f751b87ceffe945c Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Sat, 13 May 2023 13:17:38 -0500 Target sum using brute force method Diffstat:
| A | target-sum/target-sum.dart | | | 22 | ++++++++++++++++++++++ |
1 file changed, 22 insertions(+), 0 deletions(-)
diff --git a/target-sum/target-sum.dart b/target-sum/target-sum.dart @@ -0,0 +1,22 @@ +//Find all possible variations of nums where the values +//added together are equal to the target. Achieve +//this by adding either a + or - to the front of the value. +//This code is the brute force solution that gives a tle in leetcode. +//The time complexity of this code is 2^n where n is the length of the list +//nums. +class Solution { + int findTargetSumWays(List<int> nums, int target) { + if(nums.length == 0){ + if(target == 0){ + return 1; + } + return 0; + } + int first_val = nums[0]; + nums.removeAt(0); + int return_val = findTargetSumWays( List.from(nums) , target - first_val); + return_val += findTargetSumWays( List.from(nums) , target + first_val ); + return return_val;; + + } +}