Created
March 8, 2020 02:35
-
-
Save aboSamoor/0efda6141353969f7b893ba8ef54ada6 to your computer and use it in GitHub Desktop.
leetcode-prob960
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from functools import lru_cache | |
def CompareTwoColumns(x, y): | |
return np.all(y >= x) | |
__CACHE = {} | |
def Solve(prefix, suffix): | |
global __CACHE | |
key = (prefix.tobytes(), suffix.tobytes()) | |
if key in __CACHE: | |
return __CACHE[key] | |
if suffix.size == 0: | |
return prefix | |
solution1 = Solve(prefix, suffix[:, 1:]) | |
solution2 = prefix | |
if prefix.size > 0: | |
if CompareTwoColumns(prefix[:, -1], suffix[:, 0]): | |
new_prefix = np.concatenate([prefix, suffix[:, :1]], axis=1) | |
solution2 = Solve(new_prefix, suffix[:, 1:]) | |
else: | |
new_prefix = np.concatenate([prefix, suffix[:, :1]], axis=1) | |
solution2 = Solve(new_prefix, suffix[:, 1:]) | |
if solution1.shape[1] > solution2.shape[1]: | |
final = solution1 | |
else: | |
final = solution2 | |
__CACHE[key] = final | |
return final | |
class Solution: | |
def minDeletionSize(self, A: List[str]) -> int: | |
global __CACHE | |
__CACHE = {} | |
X = np.array([list(row) for row in A]) | |
solution = Solve(np.empty(shape=(X.shape[0], 0)), X) | |
return X.shape[1] - solution.shape[1] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment