leetcode 1860. Incremental Memory Leak(python)

語言: CN / TW / HK

「這是我參與11月更文挑戰的第18天,活動詳情檢視:2021最後一次更文挑戰

描述

You are given two integers memory1 and memory2 representing the available memory in bits on two memory sticks. There is currently a faulty program running that consumes an increasing amount of memory every second.

At the ith second (starting from 1), i bits of memory are allocated to the stick with more available memory (or from the first memory stick if both have the same available memory). If neither stick has at least i bits of available memory, the program crashes.

Return an array containing [crashTime, memory1crash, memory2crash], where crashTime is the time (in seconds) when the program crashed and memory1crash and memory2crash are the available bits of memory in the first and second sticks respectively.

Example 1:

Input: memory1 = 2, memory2 = 2
Output: [3,1,0]
Explanation: The memory is allocated as follows:
- At the 1st second, 1 bit of memory is allocated to stick 1. The first stick now has 1 bit of available memory.
- At the 2nd second, 2 bits of memory are allocated to stick 2. The second stick now has 0 bits of available memory.
- At the 3rd second, the program crashes. The sticks have 1 and 0 bits available respectively.

Example 2:

Input: memory1 = 8, memory2 = 11
Output: [6,0,4]
Explanation: The memory is allocated as follows:
- At the 1st second, 1 bit of memory is allocated to stick 2. The second stick now has 10 bit of available memory.
- At the 2nd second, 2 bits of memory are allocated to stick 2. The second stick now has 8 bits of available memory.
- At the 3rd second, 3 bits of memory are allocated to stick 1. The first stick now has 5 bits of available memory.
- At the 4th second, 4 bits of memory are allocated to stick 2. The second stick now has 4 bits of available memory.
- At the 5th second, 5 bits of memory are allocated to stick 1. The first stick now has 0 bits of available memory.
- At the 6th second, the program crashes. The sticks have 0 and 4 bits available respectively.

Note:

0 <= memory1, memory2 <= 2^31 - 1

解析

根據題意, 給出兩個整數 memory1 和 memory2,分別表示兩個記憶體條上的可用記憶體位數。 當前有一個錯誤的程式正在執行,每秒消耗的記憶體量越來越大。在第 i 秒(從 1 開始)需要在有更多空間的記憶體條上劃分 i 位的記憶體空間(如果兩個記憶體條有相同的可用記憶體,則從第一個記憶體條中劃分空間)。 如果兩個記憶體條都沒有至少有 i 位可用記憶體,程式就會崩潰。

返回一個包含 [crashTime, memory1crash, memory2crash] 的陣列,其中 crashTime 是程式崩潰的時間(以秒為單位),而 memory1crash 和 memory2crash 分別是第一個和第二個記憶體條中剩下的可用記憶體位數。

這個題很簡單,就直接模仿題意進行寫程式碼即可,如果兩個記憶體條都有可供第 i 秒使用的大小為 i 位的空間,那就去某一個記憶體條上劃分空間。關鍵就在於每次劃分記憶體在有更多空間的記憶體條上進行;如果兩個記憶體條上剩餘的空間相等,在第一個記憶體條上進行。

解答

class Solution(object):
    def memLeak(self, memory1, memory2):
        """
        :type memory1: int
        :type memory2: int
        :rtype: List[int]
        """
        i = 1
        while max(memory1, memory2)>=i:
            if memory1>=memory2:
                memory1 -= i
            else:
                memory2 -= i
            i += 1
        return [i, memory1, memory2]

執行結果

Runtime: 424 ms, faster than 40.00% of Python online submissions for Incremental Memory Leak.
Memory Usage: 13.4 MB, less than 76.67% of Python online submissions for Incremental Memory Leak.

原題連結:https://leetcode.com/problems/incremental-memory-leak/

您的支援是我最大的動力