知乎 0916 iOS A 卷

笔试三道题,不用动脑子。

1 堆排序

import heapq

class Solution:
    def viewFrames(self , views: List[int], columnCount: int) -> List[List[int]]:
        result = []
        h = [[0, i] for i in range(columnCount)]
        heapq.heapify(h)

        for i in views:
            t = heapq.heappop(h)
            result.append([t[1] * 100, t[0], 100, i])
            heapq.heappush(h, [t[0] + i, t[1]])

        return result

2 计数排序

class Solution:
    def bestSell(self , dealFlow: List[int], minPrice: int, maxPrice: int) -> int:
        # write code here
        t = [0 for _ in range(400000)]
        for i in dealFlow:
            t[i] += 1
        count = 0
        index = -1
        for i in range(minPrice, maxPrice + 1):
            if t[i] > count or (t[i] != 0 and t[i] == count and i > index):
                count = t[i]
                index = i
        return index

3 DFS

class Solution:
    def dfs(self, grid: List[List[int]], x: int, y: int) -> int:
        if x < 0 or x >= len(grid):
            return 0
        if y < 0 or y >= len(grid[0]):
            return 0
        if grid[x][y] == 0:
            return 0
        result = grid[x][y]
        grid[x][y] = 0
        result += self.dfs(grid, x - 1, y)
        result += self.dfs(grid, x + 1, y)
        result += self.dfs(grid, x, y + 1)
        result += self.dfs(grid, x, y - 1)
        return result
    def bigestMine(self , grid: List[List[int]]) -> int:
        result = 0
        for x in range(len(grid)):
            for y in range(len(grid[x])):
                result = max(result, self.dfs(grid, x, y))
        return result

#笔试经验#
全部评论
知乎0916 Android B卷两题也不用动脑子
点赞 回复 分享
发布于 2023-09-16 15:04 云南

相关推荐

把实习生当正职使昨天第一天就加班,晚上连口饭都没吃上,以后日子咋过,我不想干了
码农索隆:实习不怕忙,就怕干的活重复且没难度,要干就干那种有深度有难度的任务,这样才能快速的提升
实习吐槽大会
点赞 评论 收藏
分享
LemontreeN:有的兄弟有的我今天一天面了五场,4个二面一个hr面
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
评论
点赞
3
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务