Counting Valleys

def countingValleys(n, s): valleys = 0 hikes = 0 for i in s: if i==’D’: if hikes==0: valleys += 1 hikes -= 1 else: hikes += 1 return valleys Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike, he took exactly  steps. For […]

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] […]


class Solution: # @param A : string # @return an integer def atoi(self, A): mx = (2**31)-1 mn = -(2**31) s = ” for x in A: if x.isdigit() or (A.index(x)==0 and x in (‘-‘, ‘+’)): s += x else: break if (s and s[0].isdigit()) or (len(s)>1 and s[0] in (‘+’, ‘-‘)): if 0 <= […]

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 […]

Now and 10,000 years forward

Ever wondered, if your ancestors left a giant big-ass clock running from past thousands of years, evolutionary studies would be much easy and truncated by substantial unnecessary research? Life as we know, is moving forward in time. Life is a matter continuum, we wear the matter and leave it. Our forefathers were great people, they […]

In a binary world

I think sadness is overrated and so is happiness for that matter. Sadness is rather just a note that descends, a converging infinite sequence like Euler’s number. Music and mathematics make human life possible. They complement each other but are mutually exclusive, together they form art. You see a highway with a sunset, and you […]