注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
本帖最后由 tomzhawang 于 2023-2-2 11:13 编辑
Please give me rice :)
"""
You are using a browser to visit web pages.
e.g. page1 -> page2 -> page3 -> page4 -> page5 (single thread page view)
This gives a list like so:
[
[page1, page2],
[page2, page3],
[page3, page4],
[page4, page5],
]
Now we shuffle the list:
[
[page2, page3],
[page1, page2],
[page4, page5],
[page3, page4],
]
Return a pair of strings indicating the first and last page you visited:
["page1", "page2"]
FOLLOW UP:
There are some cases where you cannot determine what page you started with:
page1 -> page2 -> page3 -> page4 -> page5 -> page1
You could start at any of the pages
But there are also cases where you can determine where you started
page1 -> page2 -> page3 -> page4 -> page5 -> page1 -> page5 (single thread page view)
Input:
[
[page3, page4],
[page4, page5],
[page1, page2],
[page2, page3],
[page5, page1],
[page1, page5],
]
The only possible answer is ["page1", "page5"] (because the indegree of page 1 < outdegree of page1)
(and the indegree of page 2 >= outdegree of page 2)
Handle this case as well
"""
def firstAndLastPages(pageSequence):
if not pageSequence or not pageSequence[0]:
return ["", ""]
pageToNumInpages = {}
pageToNumOutpages = {}
for inpage, outpage in pageSequence:
pageToNumInpages[outpage] = pageToNumInpages.get(outpage, 0) + 1
pageToNumOutpages[inpage] = pageToNumOutpages.get(inpage, 0) + 1
firstPage = None
for page in pageToNumOutpages:
indegree = pageToNumInpages.get(page, 0)
outdegree = pageToNumOutpages.get(page, 0)
if (indegree == 0) or (outdegree > indegree):
firstPage = page
break
lastPage = None
for page in pageToNumInpages:
indegree = pageToNumInpages.get(page, 0)
outdegree = pageToN nextCol = col + dc
if 0 <= nextRow < R and 0 <= nextCol < C and grid[nextRow][nextCol] != 0 and (nextRow, nextCol) not in cellToParentInTheClosestPath:
# cellToParentInTheClosestPath[(nextRow, nextCol)] = (row, col)
minCostToNextCell = grid[nextRow][nextCol] + smallestDist
heappush(heap, (minCostToNextCell, nextRow, nextCol, row, col))
cellsInTheShortestPath = set()
if rowOfBottom != -1:
current = (rowOfBottom, colOfBottom)
while current != (None, None):
cellsInTheShortestPath.add(current)
current = cellToParentInTheClosestPath[current]
for row in range(R):
for col in range(C):
if (row, col) not in cellsInTheShortestPath:
grid[row][col] = 0
return grid
----
The System Design was to design a password login app. It should support lockout, changing passwords, logging in, and login history lookup.
----
The last coding question was a topological sort. |