commit 824a9e213577e5b42d87e2753f3a7dbaa5cf8b3b parent 8a717f36582ce4df1f919d547ebd45dced460025 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Tue, 30 May 2023 15:02:52 -0500 Completed subsets using DP, recursion, and take it or leave it. Diffstat:
| A | subsets/subsets.dart | | | 30 | ++++++++++++++++++++++++++++++ |
1 file changed, 30 insertions(+), 0 deletions(-)
diff --git a/subsets/subsets.dart b/subsets/subsets.dart @@ -0,0 +1,30 @@ +//Given a list of nums return all possible subsets +//where there are no repeats. + +//To solve this I used it take it leave it approach to ensure +//no two lists will be the same. If you take it then remove the current +//first element and on the way down the stack add on the current nums[0]. +//If you leave it then do not add a value on the way down the stack. + +//Time: 261ms Beats: 70% +//Memory: 141.4MB Beats: 20% + +class Solution { + List<List<int>> subsets(List<int> nums) { + if(nums.length == 0){ + return [[]]; + } + List<int> subl = nums.sublist(1); + List<List<int>> return_take = subsets(subl); + List<List<int>> return_leave = subsets(subl); + List<List<int>> return_list = []; + for(int i = 0 ; i < return_take.length ; ++i){ + return_list.add(return_take[i]); + return_list[return_list.length - 1].add(nums[0]); + } + for(int i = 0 ; i < return_leave.length ; ++i){ + return_list.add(return_leave[i]); + } + return return_list; + } +}