Power Set – algorithmic and fast

class Solution: # @param A : list of integers # @return a list of list of integers def subsets(self, A):     if len(A) == 0:         return [[]]     h, t = A[0], A[1:]     subsets_excluding_h = self.subsets(t)     subsets_including_h = [sorted([h] + ss) for ss in subsets_excluding_h] […]

Maximum non-negative subarray

class Solution: # @param A : list of integers # @return a list of integers def maxset(self, A): i = 0 af = [] a1 = [] while i < len(A): if A[i] >= 0: a1.append(A[i]) else: af.append(a1) a1 = [] i += 1 # Append the last subarray, if any af.append(a1) # Find a […]

Painter’s Partition Problem

class Solution: # @param A : integer # @param B : integer # @param C : list of integers # @return an integer def partition(self, arr, n, k):     # One painter     if k==1:         return sum(arr[0:n])          # One Board to paint     if […]