“This is the 18th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”
describe
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.
Copy the code
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.
Copy the code
Note:
0 <= memory1, memory2 <= 2^31 - 1
Copy the code
parsing
Two integers memory1 and memory2 are given to represent the number of bits of memory available on two memory bars. An error program is currently running, consuming more memory per second. At the i-second (starting from 1) it is necessary to divide the i-bit memory space on the memory that has more space (divide the space from the first memory if two memory sticks have the same available memory). If neither of the two memory sticks has at least I bits of memory available, the program crashes.
Return an array containing [crashTime, memory1Crash, memory2Crash], where crashTime is the time (in seconds) at which the program crashed, Memory1crash and memory2Crash are the number of bits of memory left free on the first and second memory sticks, respectively.
If both memory sticks have the I bit space available for the I second, divide the space on one memory stick. The key is that each partition is carried out on the memory with more space; If the remaining space on the two memory sticks is equal, proceed on the first memory stick.
answer
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]
Copy the code
The results
Connected to the node in the linked list in the linked list 10 MB, less than 76.67% of Python online submissions for Incremental Memory Leak.Copy the code
Original link: leetcode.com/problems/in…
Your support is my biggest motivation