1. 程式人生 > >英偉達 2019校園招聘Web Services-2018.09.04(python版)

英偉達 2019校園招聘Web Services-2018.09.04(python版)

1-1.png

1-2.png

1-3.png

結果應該輸出1.

思路:約瑟夫環問題

# coding:utf-8
def circle(n, m):
    a = [None] * 1001
    # 死亡人數
    dead = 0
    # 報數
    count = 0
    res = 0
    # 將每個人的位置儲存到a中
    for i in range(1, n + 1):
        a[i] = i

    for i in range(1, 10000):
        if i > n:
            i = i % n
        if a[i] == 0: # 表示此人已死,不計
            continue
        # 活著就記數
        if a[i] > 0:
            count += 1
        # 到了該殺人的m點且不為最後一人
        if m == count and dead != n - 1:
            count = 0 # 新的一輪
            a[i] = 0 # 死亡置0
            dead += 1
        # 只剩最後一人就輸出到結果中,終止迴圈
        elif m == count and dead == n - 1:
            res = a[i]
            break

    return res

print circle(4, 1)

2-1.png

2-2

2-3.png

暫時沒有思路

3-1.png

3-2.png

思路:可以參考leetcode 675.為高爾夫比賽砍樹,遇到障礙就跳過去,這裡我使用的是BFS來解。參考別人的答案。

# coding:utf-8
from collections import deque

class Solution(object):
    '''
    def cutOffTree(self, forest, start, end):

        if not forest or not forest[0]:
            return -1

        # cut the tree in reverse order of tree's height
        # treeList = []
        # for i in xrange(len(forest)):
            # for j in xrange(len(forest[0])):
                # if forest[i][j] > 1:
                    # treeList.append([forest[i][j], [i, j]])
        # treeList.sort()

        totalSteps = self.minStep(forest, start, end)
        # k = 0
        # while k < len(treeList) - 1:
            # step = self.minStep(forest, treeList[k][1], treeList[k + 1][1])
            # if step == -1:
                # return -1
            # totalSteps += step
            # k += 1

        return totalSteps
        '''
    # 傳入平面圖,開始位置和結束位置
    def minStep(self, forest, start, end):
        directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
        step = 0
        m, n = len(forest), len(forest[0])
        visited = [[False for x in xrange(n)] for _ in xrange(m)]
        visited[start[0]][start[1]] = True
        queue = deque()
        queue.append(start)

        while queue:
            size = len(queue)
            for i in xrange(size):
                curr = queue.popleft()
                if curr == end:
                    return step
                for direction in directions:
                    nextRow = curr[0] + direction[0]
                    nextCol = curr[1] + direction[1]
                    # 需要判定當前點位是否出界,或者該點是否被訪問,遇到障礙時(==1)跳過,不做處理。
                    if nextRow < 0 or nextRow >= m or nextCol < 0 or nextCol >= n or visited[nextRow][nextCol] or \
                            forest[nextRow][nextCol] == 1:
                        continue
                    queue.append([nextRow, nextCol])
                    visited[nextRow][nextCol] = True
            step += 1

        # here, it means we can never find the destination
        return -1

s = Solution()
print s.minStep([[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], [0,4], [4, 4])

leetcode原題需要上面的函式。