[Mock] Random 21
5723 ワード
2つ目の問題は時間が終わるとすぐに解けます…^^
1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
My Answer 1: Time Limit Exceeded (55 / 61 test cases passed.)
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
self.ans = 1
for i in range(len(nums)):
a = i
b = i
mi = nums[i]
ma = nums[i]
for j in range(i+1, len(nums)):
if ma < nums[j]:
ma = nums[j]
a = j
elif mi > nums[j]:
mi = nums[j]
b = j
if abs(nums[a]-nums[b]) <= limit:
m = a if a < b else b
self.ans = max(self.ans, abs(j-i)+1)
return self.ans
最低価格のa
と最高価格のb
を更新し、サブアレイの最大長を取得します.Solution 1: Accepted (Runtime: 700 ms - 61.58% / Memory Usage: 24.1 MB - 65.26%)
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
min_deque, max_deque = deque(), deque()
l = r = 0
ans = 0
while r < len(nums):
while min_deque and nums[r] <= nums[min_deque[-1]]:
min_deque.pop()
while max_deque and nums[r] >= nums[max_deque[-1]]:
max_deque.pop()
min_deque.append(r)
max_deque.append(r)
while nums[max_deque[0]] - nums[min_deque[0]] > limit:
l += 1
if l > min_deque[0]:
min_deque.popleft()
if l > max_deque[0]:
max_deque.popleft()
ans = max(ans, r - l + 1)
r += 1
return ans
2つのdequeを使用r
はサブアレイの開始値のようです...?ex)[8,2,4,7]
=>8から始まるsub~、2...min_deque[0]
は現在のウィンドウの最高値、min_deque[0]
は最高値です.nums[max_deque[0]] - nums[min_deque[0]]
がlimit
を離れるとpopleftex)
[8,2,4,7]
=>8, 4
不可同様、連続するサブアレイのみがハイライトになる可能性があります難しいですね.ううう
注1:https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/discuss/609771/JavaC%2B%2BPython-Deques-O(N)
注2:https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/discuss/609708/Python-Clean-Monotonic-Queue-solution-with-detail-explanation-O(N)
問題の解き方は前の239です.Sliding Window Maximumのような問題
https://leetcode.com/problems/sliding-window-maximum/
1376. Time Needed to Inform All Employees
A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company is the one with headID.
Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also, it is guaranteed that the subordination relationships have a tree structure.
The head of the company wants to inform all the company employees of an urgent piece of news. He will inform his direct subordinates, and they will inform their subordinates, and so on until all employees know about the urgent news.
The i-th employee needs informTime[i] minutes to inform all of his direct subordinates (i.e., After informTime[i] minutes, all his direct subordinates can start spreading the news).
Return the number of minutes needed to inform all the employees about the urgent news.
My Answer 1: Accepted (Runtime: 1400 ms - 46.18% / Memory Usage: 52.9 MB - 23.22%)
class Solution:
def numOfMinutes(self, n: int, headID: int, manager: List[int], informTime: List[int]) -> int:
ans = 0
dic = {headID:[]}
for i in range(len(manager)):
if manager[i] == -1:
continue
elif manager[i] not in dic:
dic[manager[i]] = [i]
else:
dic[manager[i]].append(i)
def func(ID):
if ID not in dic:
return 0
tmp = 0
for i in dic[ID]:
tmp = max(tmp, func(i))
return informTime[ID] + tmp
ans = func(headID)
return ans
時間が終わるとすぐに解けてしまう状況は….dic
、担当idをmanager
毎に記憶するheadID
から、再帰記憶informTime[ID]
によりtmp = max(tmp, func(i))
=>最長時間のマネージャ行値の追加本来max値ではなく、少し多めにしただけなのですが、ひとつ直せば良かったのですが…^^
ex)
1 -> 2 -> 3
&1 -> 4 -> 5
のように、1から複数の接続線があれば1
次の2, 4
は待ち時間が長いと思っていましたがReference
この問題について([Mock] Random 21), 我々は、より多くの情報をここで見つけました https://velog.io/@jsh5408/Mock-Random-21テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol