<aside> 💡
check!
</aside>
class NestedIterator:
def __init__(self, nestedList: [NestedInteger]):
self._integers = []
self._position = -1
def flatten_list(nested_list):
for integer in nested_list:
if integer.isInteger():
self._integers.append(integer.getInteger())
else:
flatten_list(integer.getList())
flatten_list(nestedList)
def next(self) -> int:
self._position += 1
return self._integers[self._position]
def hasNext(self) -> bool:
return (self._position + 1) < len(self._integers)
<aside> 💡
check
</aside>
class Solution:
def simplifyPath(self, path: str) -> str:
stack = []
for portion in path.split("/"):
if portion == "..":
if stack:
stack.pop()
elif portion == "." or not portion:
continue
else:
stack.append(portion)
final_str = "/" + "/".join(stack)
return final_str
<aside> 💡
check
</aside>
class Table:
def __init__(self, name, columns):
self.name = name
self.row_inserted = 0
self.rows = {}
class SQL:
def __init__(self, names: List[str], columns: List[int]):
self.tables = {}
for name in names:
self.tables[name] = Table(name, columns)
def insertRow(self, name: str, row: List[str]) -> None:
self.tables[name].row_inserted +=1
#Initialize he row
self.tables[name].rows[self.tables[name].row_inserted]=row
def deleteRow(self, name: str, rowId: int) -> None:
del self.tables[name].rows[rowId]
def selectCell(self, name: str, rowId: int, columnId: int) -> str:
return self.tables[name].rows[rowId][columnId-1]
["SQL", "insertRow", "selectCell", "insertRow", "deleteRow", "selectCell"]
[[["one", "two", "three"], [2, 3, 1]], ["two", ["first", "second", "third"]], ["two", 1, 3], ["two", ["fourth", "fifth", "sixth"]], ["two", 1], ["two", 2, 2]]
Output
[null, null, "third", null, null, "fifth"]
Explanation
SQL sql = new SQL(["one", "two", "three"], [2, 3, 1]); // creates three tables.
sql.insertRow("two", ["first", "second", "third"]); // adds a row to the table "two". Its id is 1.
sql.selectCell("two", 1, 3); // return "third", finds the value of the third column in the row with id 1 of the table "two".
sql.insertRow("two", ["fourth", "fifth", "sixth"]); // adds another row to the table "two". Its id is 2.
sql.deleteRow("two", 1); // deletes the first row of the table "two". Note that the second row will still have the id 2.
sql.selectCell("two", 2, 2); // return "fifth", finds the value of the second column in the row with id 2 of the table "two".
class Excel:
def __init__(self, height: int, width: str):
ncols = ord(width[0]) - ord('A') + 1
self.dep = {}
self.mat = [[0 for _ in range (ncols)] for _ in range (height)]
def set(self, row: int, column: str, val: int) -> None:
col = ord(column[0]) - ord('A')
self.mat [row -1][col] = val
if (row-1, col) in self.dep:
del self.dep [(row-1, col)]
self.recalc()
def get(self, row: int, column: str) -> int:
col = ord(column[0])-ord('A')
return self.mat[row-1][col]
def sum(self, row: int, column: str, numbers: List[str]) -> int:
col = ord(column[0])-ord('A')
self.dep [(row-1, col)] = []
for number in numbers:
if ":" in number:
cells = self.getCells(number)
self.dep[(row - 1, col)].extend(cells)
else:
r, c = self.cell2rowCol(number)
self.dep[(row - 1, col)].append((r, c))
self.recalc()
return self.get(row, column)
def cell2rowCol(self, cell:str):
row = int(cell[1:])-1
col = ord(cell[0])-ord('A')
return row, col
def getCells(self, range_str:str):
start, end = range_str.split(':')
srow, scol = self.cell2rowCol(start)
erow, ecol = self.cell2rowCol (end)
cells = [(r, c) for r in range(srow, erow + 1) for c in range(scol, ecol + 1)]
return cells
def dfs (self, row, col):
if (row, col) not in self.dep:
return self.mat[row][col]
total = 0
for r, c in self.dep[(row, col)]:
total += self.dfs (r, c)
return total
def recalc (self):
for row, col in self.dep:
value = self.dfs(row, col)
self.mat[row][col] = value
# Your Excel object will be instantiated and called as such:
# obj = Excel(height, width)
# obj.set(row,column,val)
# param_2 = obj.get(row,column)
# param_3 = obj.sum(row,column,numbers)
<aside> 💡
check
</aside>
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
dp = [1] * len(nums)
for i in range (1, len(nums)):
for j in range (i):
if nums[i]>nums[j]:
dp[i] = max(dp[i], dp[j]+1)
return max(dp)