# Rabin–Karp algorithm

I know it have been a long while that I do not update my website. Even missed the entire May…

Actually, I am confused about my future path during these two months. I got a bunch of offers from different places. However, I don’t even know where should I go ultimately.

So I just try to learn some new stuff as I can to kill the time…

Rabin-Karp is a kind of string searching algorithm which created by Richard M. Karp and Michael O. Rabin. It uses the rolling hash to find an exact match of pattern in a given text. Of course, it is also able to match for multiple patterns.

```def search(pattern, text, mod):
# Let d be the number of characters in the input set
d = len(set(list(text)))

# Length of pattern
l_p = len(pattern)

# Length of text
l_t = len(text)

p = 0
t = 0
h = 1

# Let us calculate the hash value of the pattern
# hash value for pattern(p) = Σ(v * dm-1) mod 13
#                           = ((3 * 102) + (4 * 101) + (4 * 100)) mod 13
#                           = 344 mod 13
#                           = 6
for i in range(l_p - 1):
h = (h * d) % mod

# Calculate hash value for pattern and text
for i in range(l_p):
p = (d * p + ord(pattern[i])) % mod
t = (d * t + ord(text[i])) % mod

# Find the match
for i in range(l_t - l_p + 1):
if p == t:
for j in range(l_p):
if text[i+j] != pattern[j]:
break

j += 1
if j == l_p:
print("Pattern is found at position: " + str(i+1))

if i < l_t - l_p:
t = (d*(t-ord(text[i])*h) + ord(text[i+l_p])) % mod

if t < 0:
t += mod

text = "ABCCCDCCDDAEFG"
pattern = "CDD"
search(pattern, text, 13)```

# Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

```class Solution:
def generateParenthesis(self, n: int) -> List[str]:
ret = []

# @functools.lru_cache(None)
def dfs(curr, l, r):
if l == n and r == n:
ret.append(curr)

if r > l: return
if l < n: dfs(curr + "(", l + 1, r)
if r < n: dfs(curr + ")", l, r + 1)

dfs('', 0, 0)

return ret```

# Is a Balanced Binary Tree

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the left and right subtrees of every node differ in height by no more than 1.

This problem is an easy-level at Leetcode. I probably did it more than five times, once and once again. Just like a muscle memory.

However, I found an interesting solution today, which literally changed my mind about Python…

Here is the code:

```# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

class Solution:
def isBalanced(self, root: TreeNode, h = 1) -> bool:
if not root: return h
l = self.isBalanced(root.left, h + 1)
r = self.isBalanced(root.right, h + 1)
return abs(l - r) <= 1 and max(l, r)```

I am very confused at the last line, the max(l, r) part.

I thought that max(l, r) should be converted as a bool value even it returns a integer type value, because as the second component of the operation AND, max(l, r) should represent as a bool variable.

Following by my worst idea, I supposed that the function isBalanced would return either 1 (True) or 0 (False). However, I found a crazy truth after experiments, that Python executor actually return a integer value (the maximum value of l and r) if abs(l – r) <= 1 is matched.

So, it really makes sense. Gain new knowledge of Python 🙂

# How to buy and sell stock at the best time

• Best Time to Buy and Sell Stock I
• Best Time to Buy and Sell Stock II
• Best Time to Buy and Sell Stock III
• Best Time to Buy and Sell Stock IV
• Best Time to Buy and Sell Stock with cooldown
• Best Time to Buy and Sell Stock with transaction fee

Best Time to Buy and Sell Stock I
Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

```class Solution:
def maxProfit(self, prices: List[int]) -> int:

for price in prices:
sell = max(sell, buy + price)

return sell```

Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note that you may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

```class Solution:
def maxProfit(self, prices: List[int]) -> int:

for price in prices:

it tmp > 0:
profit += tmp

return profit```

Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note that you may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

```class Solution:
def maxProfit(self, prices: List[int]) -> int:

for price in prices:
sell_1 = max(sell_1, buy_1 + price)

sell_2 = max(sell_2, buy_2 + price)

return sell_2```

Best Time to Buy and Sell Stock IV
Say you have an array for which the i-th element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most k transactions.

Note that you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

```class Solution:
def maxProfit(self, k: int, prices: List[int]) -> int:
if k > len(prices) >> 1:
return sum(prices[i+1] - prices[i] for i in range(len(prices) - 1) if prices[i+1] > prices[i])

cash, asset = [-0x7777777] * (k + 1), [0] * (k + 1)

for price in prices:
for i in range(1, k+1):
cash[i] = max(cash[i], sell[i-1] - price)
asset[i] = max(asset[i], cash[i] + price)

return asset[k]```

Best Time to Buy and Sell Stock with Cooldown

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

• You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
• After you sell your stock, you cannot buy stock on the next day. (ie, cooldown 1 day)
```class Solution:
def maxProfit(self, prices: List[int]) -> int:
if not prices:
return 0

for price in prices:
prev_sell = sell
sell = max(prev_sell, prev_buy + price)

return sell```

Best Time to Buy and Sell Stock with Transaction Fee
You are given an array of integers `prices`, for which the `i`-th element is the price of a given stock on day `i`; and a non-negative integer `fee` representing a transaction fee.

You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.)

Return the maximum profit you can make.

```class Solution:
def maxProfit(self, prices: List[int], fee: int) -> int:

for p in prices[1:]:
sell = max(sell, buy + p - fee)

return sell```

# Word Break

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

Note:

• The same word in the dictionary may be reused multiple times in the segmentation.
• You may assume the dictionary does not contain duplicate words.

Example 1:

```Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because `"leetcode"` can be segmented as `"leet code"`.
```

Example 2:

```Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because `"`applepenapple`"` can be segmented as `"`apple pen apple`"`.
Note that you are allowed to reuse a dictionary word.
```

Example 3:

```Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false```

Recently, I am going to pick up my leetcode skills.

This problem that I still remember It token me more than two days to consider, but this time it was aced in 5 minutes, as well as just in nearly 1 line core code.

It seems like practising is really useful!

```from functools import lru_cache

class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
@lru_cache(None)
def dfs(s):
return True if not s else any(dfs(s[len(word):]) for word in wordDict if s.startswith(word))

return dfs(s)```

# Max Increase to Keep City Skyline

In a 2 dimensional array `grid`, each value `grid[i][j]` represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts can be different for different buildings). Height 0 is considered to be a building as well.

At the end, the “skyline” when viewed from all four directions of the grid, i.e. top, bottom, left, and right, must be the same as the skyline of the original grid. A city’s skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. See the following example.

What is the maximum total sum that the height of the buildings can be increased?

```Example:
Input: grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
Output: 35
Explanation:
The grid is:
[ [3, 0, 8, 4],
[2, 4, 5, 7],
[9, 2, 6, 3],
[0, 3, 1, 0] ]

The skyline viewed from top or bottom is: [9, 4, 8, 7]
The skyline viewed from left or right is: [8, 7, 9, 3]

The grid after increasing the height of buildings without affecting skylines is:

gridNew = [ [8, 4, 8, 7],
[7, 4, 7, 7],
[9, 4, 8, 7],
[3, 3, 3, 3] ]

```

Notes:

• `1 < grid.length = grid[0].length <= 50`.
• All heights `grid[i][j]` are in the range `[0, 100]`.
• All buildings in `grid[i][j]` occupy the entire grid cell: that is, they are a `1 x 1 x grid[i][j]` rectangular prism.
```class Solution:
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
lr_view = [max(line) for line in grid]
tb_view = [max(line) for line in zip(*grid)]

result = 0

for i in range(len(grid)):
for j in range(len(grid[0])):
diff = min(tb_view[j], lr_view[i]) - grid[i][j]
result += max(0, diff)

return result```

# My Calendar I

Implement a `MyCalendar` class to store your events. A new event can be added if adding the event will not cause a double booking.

Your class will have the method, `book(int start, int end)`. Formally, this represents a booking on the half open interval `[start, end)`, the range of real numbers `x` such that `start <= x < end`.

double booking happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)

For each call to the method `MyCalendar.book`, return `true` if the event can be added to the calendar successfully without causing a double booking. Otherwise, return `false` and do not add the event to the calendar.Your class will be called like this: `MyCalendar cal = new MyCalendar();``MyCalendar.book(start, end)`

```Example 1:
MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(15, 25); // returns false
MyCalendar.book(20, 30); // returns true
Explanation:
The first event can be booked.  The second can't because time 15 is already booked by another event.
The third event can be booked, as the first event takes every time less than 20, but not including 20.
```

Note:

• The number of calls to `MyCalendar.book` per test case will be at most `1000`.
• In calls to `MyCalendar.book(start, end)``start` and `end` are integers in the range `[0, 10^9]`.
```import bisect

class MyCalendar:

def __init__(self):
self.ints = []

def book(self, start: int, end: int) -> bool:
idx = bisect.bisect_left(self.ints, (start, end))

is_left_valid = idx == 0 or self.ints[idx - 1][1] <= start
is_right_valid = idx == len(self.ints) or end <= self.ints[idx][0]

if is_left_valid and is_right_valid:
self.ints.insert(idx, (start, end))
return True
return False```

# Find the Shortest Superstring

Given an array A of strings, find any smallest string that contains each string in `A` as a substring.

We may assume that no string in `A` is substring of another string in `A`.

```Example 1:
Input: ["alex","loves","leetcode"]
Output: "alexlovesleetcode"
Explanation: All permutations of "alex","loves","leetcode" would also be accepted.
```
```Example 2:
Input: ["catg","ctaagt","gcta","ttca","atgcatc"]
Output: "gctaagttcatgcatc"```

Note:
`1 <= A.length <= 12`
`1 <= A[i].length <= 20`

Solution 1 (Naive and TLE):
At the very first, I did this solution that naively using DFS to search each possible solution, and finally we can pick up the shortest one. It works on small test set but got TLE on large sets.

```from functools import lru_cache

class Solution:
@lru_cache(None)
def combinateStrings(self, str1, str2):
for i in range(len(str2), -1, -1):
if str1.endswith(str2[:i]):
return str1 + str2[i:]

def shortestSuperstring(self, A):

def func(current_list, result, results):
if not current_list:
results.append(result)

for s in current_list:
current_list.remove(s)
func(current_list, self.combinateStrings(result, s), results)
func(current_list, self.combinateStrings(s, result), results)
current_list.append(s)

results = []

func(A, "", results)

return min(results, key= lambda x: len(x))```

# Best Time to Buy and Sell Stock with Transaction Fee

Your are given an array of integers `prices`, for which the `i`-th element is the price of a given stock on day `i`; and a non-negative integer `fee` representing a transaction fee.

You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.)

Return the maximum profit you can make.

```Example 1:

Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
Output: 8
Explanation: The maximum profit can be achieved by:
Buying at prices[0] = 1Selling at prices[3] = 8Buying at prices[4] = 4Selling at prices[5] = 9The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.```

Note:
`0 < prices.length <= 50000`.
`0 < prices[i] < 50000`.
`0 <= fee < 50000`.

```class Solution:
def maxProfit(self, prices: List[int], fee: int) -> int:
sell = 0

for price in prices:
# Not buy or pay current price and fee

# Not sell or get current price
sell = max(sell, buy + price)

return sell```

# Reverse Pairs

Given an array `nums`, we call `(i, j)` an important reverse pair if `i < j` and `nums[i] > 2*nums[j]`.

You need to return the number of important reverse pairs in the given array.

Example1:

```Input: [1,3,2,3,1]
Output: 2```

Example2:

```Input: [2,4,3,5,1]
Output: 3```

Note:

1. The length of the given array will not exceed `50,000`.
2. All the numbers in the input array are in the range of 32-bit integer.
```# Solution 1: Using Binary Index Tree, which has O(nlogn) time complexity.

class BIT():
def __init__(self, n):
self.n = n + 1
self.sums = [0] * self.n

def update(self, i, delta):
while i < self.n:
self.sums[i] += delta
i += i & (-i)

def query(self, i):
res = 0
while i > 0:
res += self.sums[i]
i -= i & (-i)
return res

class Solution:
def reversePairs(self, nums: List[int]) -> int:
sorted_nums = sorted(list(set(nums + [x * 2 for x in nums])))
tree = BIT(len(sorted_nums))

res = 0
ranks = {}

for i, n in enumerate(sorted_nums):
ranks[n] = i + 1

for n in nums[::-1]:
res += tree.query(ranks[n] - 1)
tree.update(ranks[n * 2], 1)

return res```
```# Solution 2: At this time, we are going to use bisect. Got inspiration from Merge Sort (Divide and Conquer).

class Solution:
def reversePairs(self, nums: List[int]) -> int:
ranks = list()
ans = 0

for n in reverse(nums):
ans += bisect.bisect_left(ranks, n)
bisect.insort_left(ranks, n * 2)

return ans
```