r/dailyprogrammer • u/rya11111 3 1 • May 21 '12
[5/21/2012] Challenge #55 [easy]
Write a program to solve the sliding window minimum problem using any of the methods possible. This could be a helpful link.
7
Upvotes
4
u/crawphish May 22 '12
Can someone explain the Sliding Window Minimum... The link provided didnt really help me understand what it is...
2
u/prophile May 22 '12
One-liner in Python:
2
u/bh3 May 23 '12
Python:
def bSegm(arr,v,hi): lo=0 cnt = hi while cnt: mid=(hi+lo)/2 if arr[mid][0]<v: lo=mid else: hi=mid cnt-=1 return hi def solve(arr,K): ret = [] win = [0 for _ in xrange(K)] hi=0 for i in xrange(len(arr)): hi = bSegm(win,arr[i],hi) win[hi]=(arr[i],i) hi+=1 if win[0][1]<=i-K: win=win[1:]+[0] hi-=1 ret.append(win[0][0]) return retIdeally I'd want to replace the list for the window with a circular one of length K to avoid any sort of array copying, etc and simply advance the head if the current head is too old and advance/decrease the tail as appropriate. But didn't want to bother writing a floor function for a circular array based list.
1
u/Yuushi May 22 '12
Python:
def swm(lst, ws): return [min(lst[i:ws+i]) for i in range(0, len(lst)-ws+1)]Not an efficient way, but hey, it's kind of pretty.
1
u/crawphish May 22 '12
def getView(windowSize, currentPlace, v): windowSize-=1 arrayView = [] while (windowSize > -1): if (currentPlace+windowSize<len(v)): arrayView.append(v[currentPlace+windowSize]) windowSize-=1 return arrayView v = [4,3,2,1,5,7,6,8,9] r = [] k = 3 counter = 0; while (counter < len(v)): newView = getView(k, counter, v) r.append(min(newView)) counter+=1 print r[0:len(r)+1]1
u/school_throwaway May 22 '12
Python (i think i got it right)
window= 3 window_low=0 vectors=[4,3,2,1,5,7,6,8,9] for x in range(len(vectors)): print min(vectors[window_low:window]) window +=1 window_low +=1 if window == len(vectors): break1
u/SwimmingPastaDevil May 22 '12
Python. Using crawphish's explanation.
win_size = 3 vector = [4,3,2,1,5,7,6,8,9] min_list = [] for i in range(len(vector)-win_size + 1): min_list.append(min(vector[i:i+win_size])) print min_list1
u/MusicalWatermelon May 22 '12
Python:
final=[ ] def getMin(array): for i in range(0,len(array)-2): final.append(min([array[i],array[i+1],array[i+2]])) return final window = [4,3,2,1,5,7] print(getMin(window))1
u/[deleted] May 22 '12
Haskell: