diff --git a/Code Testcase Test Result Test Result 5. Longest Palindromic Substring.py b/Code Testcase Test Result Test Result 5. Longest Palindromic Substring.py new file mode 100644 index 000000000000..e31ee37ae96a --- /dev/null +++ b/Code Testcase Test Result Test Result 5. Longest Palindromic Substring.py @@ -0,0 +1,22 @@ +def longestPalindrome(self, s): + longest_palindrom = '' + dp = [[0]*len(s) for _ in range(len(s))] + #filling out the diagonal by 1 + for i in range(len(s)): + dp[i][i] = True + longest_palindrom = s[i] + + # filling the dp table + for i in range(len(s)-1,-1,-1): + # j starts from the i location : to only work on the upper side of the diagonal + for j in range(i+1,len(s)): + if s[i] == s[j]: #if the chars mathces + # if len slicied sub_string is just one letter if the characters are equal, we can say they are palindomr dp[i][j] =True + #if the slicied sub_string is longer than 1, then we should check if the inner string is also palindrom (check dp[i+1][j-1] is True) + if j-i ==1 or dp[i+1][j-1] is True: + dp[i][j] = True + # we also need to keep track of the maximum palindrom sequence + if len(longest_palindrom) < len(s[i:j+1]): + longest_palindrom = s[i:j+1] + + return longest_palindrom diff --git a/data_structures/Design Movie Rental System.py b/data_structures/Design Movie Rental System.py new file mode 100644 index 000000000000..2e6941bda5cd --- /dev/null +++ b/data_structures/Design Movie Rental System.py @@ -0,0 +1,41 @@ +class MovieRentingSystem(object): + + def __init__(self, n, entries): + """ + :type n: int + :type entries: List[List[int]] + """ + self.avail = defaultdict(dict) + self.prices = defaultdict(dict) + self.rented = [] + + for s, m, p in entries: + self.avail[m][s] = p + self.prices[m][s] = p + + + def search(self, movie): + shop_inventory = self.avail[movie] + if len(shop_inventory) == 0: + return [] + + return [m[0] for m in sorted(shop_inventory.items(), key=lambda(k,v): (v,k))][:5] + + + def rent(self, shop, movie): + price = self.avail[movie][shop] + self.avail[movie].pop(shop) + + self.rented.append([price, shop, movie]) + + + def drop(self, shop, movie): + price = self.prices[movie][shop] + + self.rented.remove([price, shop, movie]) + self.avail[movie][shop] = price + + + def report(self): + sorted_output = sorted(self.rented)[:5] + return map(lambda x: x[-2:], sorted_output) diff --git a/maths/maxBottlesDrunk.py b/maths/maxBottlesDrunk.py new file mode 100644 index 000000000000..35cf0a01fa98 --- /dev/null +++ b/maths/maxBottlesDrunk.py @@ -0,0 +1,8 @@ +class Solution(object): + def maxBottlesDrunk(self, numBottles, x): + ans = numBottles + while numBottles >= x: + numBottles -= x - 1 + x += 1 + ans += 1 + return ans