Gaurav Keswani
BAN USERI don't think it can get simpler than this
from collections import Counter
def schedule(tasks, n):
#Gives us the tasks in the descending order of frequency
tasks = Counter(tasks)
tasks = sorted(tasks.items(), key=lambda x: x[1], reverse=True)
#Keeps track of the last time a task was performed
check_last = {}
i = 0
while len(tasks) != 0:
current_task = findNextPossibleTask(tasks, check_last, i)
if current_task:
tasks.remove(current_task)
print(current_task[0])
remaining_count = current_task[1] - 1
check_last[current_task[0]] = i
#If we still have more of the current task to complete, add it to
#the end of the list
if remaining_count > 0:
tasks.append((current_task[0], remaining_count))
else:
print("_")
i += 1
def findNextPossibleTask(tasks, check_last, i):
for task in tasks:
current_task = task[0]
last_time_of_task = check_last.get(current_task, -n-1)
#If we have satisfied the criteria of waiting for time n, we can now
#do this task
if i - last_time_of_task > n:
return task
return None
from collections import defaultdict
buckets = defaultdict(list)
words = ["star", "rats", "ice", "cie", "arts"]
for i in xrange(len(words)):
sort = ''.join(sorted(words[i]))
buckets[sort].append(words[i])
public static int depth(Node root){
if(root == null){
return 0;
}
return 1 + Math.max(depth(root.left), depth(root.right));
}
public static int depth(Node root){
if(root == null){
return 0;
}
return 1 + Math.max(depth(root.left), depth(root.right));
}
array = [ 1, 0, 2, 0, 0, 3, 4 ]
left = 0
right = len(array) - 1
while left < right:
#If you find a non zero element while coming from the right hand side
if array[right] > 0:
#Look for an element that is 0 from the left hand side
while (left < right) and array[left] != 0:
left = left + 1
#If you find such an element, swap them
if array[left] == 0:
array[left], array[right] = array[right], array[left]
left += 1
right -= 1
#Number of non zero elements is the first index of 0
print(array.index(0))
class Interval:
lower = None
upper = None
def __init__(self, lower, upper):
self.lower = lower
self.upper = upper
#Checks if the two intervals overlap with each other
def isOverlapping(self, ob):
if (ob.lower > self.lower and ob.lower < self.upper) \
or (ob.upper > self.lower and ob.upper < self.upper) :
return True
return False
#Pass in a set of interval elements to be merged
def merge(self, interval):
lowerBound = min(self.lower, interval.lower)
upperBound = max(self.upper, interval.upper)
return Interval(lowerBound, upperBound)
#Find the correct position of the new interval
def findIntervalPosition(interval, arr1):
for i in xrange(len(arr1)-1,-1,-1):
if interval.lower > arr1[i].lower:
return i+1;
#Merge all elements with which this new interval overlaps with
def mergeNewInterval(arr1, index):
lower = index - 1
upper = index + 1
while lower >= 0 or upper < len(arr1):
if lower >= 0:
if arr1[index].isOverlapping(arr1[lower]):
newInterval = arr1[index].merge(arr1[lower])
arr1.pop(index)
arr1.pop(lower)
arr1.insert(lower, newInterval)
index = index - 1
lower = lower - 1
upper = upper - 1
else:
lower = -1 #As soon as one lower element does not overlap, no other element
#below it will overlap. So no need to check
if upper < len(arr1):
if arr1[index].isOverlapping(arr1[upper]):
newInterval = arr1[index].merge(arr1[upper])
arr1.pop(upper)
arr1.pop(index)
arr1.insert(index, newInterval)
else:
upper = len(arr1) + 1
def main():
arr1 = [Interval(3,11), Interval(17,25), Interval(58,73)]
arr2 = [Interval(6,18), Interval(40,47)]
for interval in arr2:
#Insert the element into the right position in arr1
index = findIntervalPosition(interval, arr1)
arr1.insert(index, interval)
mergeNewInterval(arr1, index)
for interval in arr1:
print("({0}-{1})".format(interval.lower, interval.upper))
if __name__ == "__main__":
main()
Repwilliamchansen95, Computer Scientist at 247quickbookshelp
I am working as a manager in Lionel Kiddie City company. I really enjoy my job. I like to play ...
- Gaurav Keswani February 27, 2016