daily log 10.02.20
START:
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.
The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
BREAKDOWN:
PROBLEM STATEMENT:
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target.
DETAILS:
You may return the combinations in any order. The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
BREAKDOWN OF PROBLEM STATEMENT:
PROBLEM STATEMENT:
Given an array of distinct integers “candidates” and a target integer “target”, return a list of all unique combinations of candidates where the chosen numbers sum to target. Given an array of distinct integers “candidates” and a target integer “target”, return a list of candidates where the chosen numbers sum to target. Given an array of distinct integers “candidates” and a target integer “target”, return a list of candidates
class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
results = []
# print(candidates[-1])
if target == candidates[-1]:
results.append([candidates.pop()])
sub_results = []
while len(candidates) > 1:
if candidates[-1] + candidates[-2] > target:
candidates.pop()
else:
if candidates[-1] + candidates[-2] == target:
sub_results.append(candidates.pop())
sub_results.append(candidates.pop())
if (candidates[-1] * 2) + candidates[-2] > target:
sub_results.append(candidates.pop())
if (candidates[-1] * 2) + sub_results[0] == target:
sub_results.append(candidates[-1])
sub_results.append(candidates[-1])
results.append(sub_results)
print(results)
print(sub_results)
return results
# curr_sum = 0
# while len(candidates) > 0:
# curr_num = candidates.pop()
# if curr_sum + curr_num == target:
# print(curr_num)
# Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.
# The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
Evening attempting
def getChildNodes(i, len_candidates):
left = (2*i)+1
right = (2*i)+2
if left > len_candidates - 1:
return [None, None]
if right > len_candidates - 1:
return [candidates[left], None]
return [candidates[left], candidates[right]]
len_candidates = len(candidates)
for i in range(len_candidates):
print(getChildNodes(i,len_candidates))
Graph Attempt
class Graph:
def __init__(self):
self.adjacencyList = {}
def addVertex(self, val):
self.adjacencyList[val] = []
def addEdge(self, vert1, vert2):
self.adjacencyList[vert1].append(vert2)
self.adjacencyList[vert2].append(vert1)
class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
# MAKE GRAPH (via adjacency list)
myGraph = Graph()
for candidate in candidates:
myGraph.addVertex(candidate)
print(myGraph)
# target = root
#
class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
def getChildNodes(i, len_candidates):
left_idx = (2*i)+1
right_idx = (2*i)+2
if left_idx > len_candidates - 1:
return [None, None]
if right_idx > len_candidates - 1:
return [candidates[left_idx], None]
left_can = candidates[left_idx]
right_can = candidates[right_idx]
# print(i+left, i+right)
return [left_can, right_can]
len_candidates = len(candidates)
for i in range(len_candidates):
print(getChildNodes(i,len_candidates))