前往
大廳
主題

[LeetCode]1441. Build an Array With Stack Operations

テリ君(福佬模式) | 2023-11-03 19:47:44 | 巴幣 2 | 人氣 95

說實在今天好像沒有難到可以被標為Medium
這個跟火車進站的概念挺像的
class Solution:
    def buildArray(self, target, n: int):
        res = []
        index = 0 # target's index
        curr = 1 # curr num
        while index < len(target):
            if target[index] == curr:
                res.append("Push")
                index += 1
            else:
                res.append("Push")
                res.append("Pop")
            curr += 1

        return res

sol = Solution()

print(sol.buildArray([1, 3], 3))
print(sol.buildArray([1, 2, 3], 3))
print(sol.buildArray([1, 2], 4))

創作回應

相關創作

更多創作