HackerRank Fraudulent Activity Notifications
提出
3/8 Terminated due to timeout :(
code: python
import math
import os
import random
import re
import sys
import statistics
from collections import deque
#
# Complete the 'activityNotifications' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY expenditure
# 2. INTEGER d
#
def activityNotifications(expenditure, d):
# Write your code here
now = deque(expenditure:d) rest = deque(expenditured:) ans = 0
while(len(rest)):
median = statistics.median(now)
ans += 1
rest_p = rest.popleft()
now.popleft()
now.append(rest_p)
return ans
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0) d = int(first_multiple_input1) expenditure = list(map(int, input().rstrip().split()))
result = activityNotifications(expenditure, d)
fptr.write(str(result) + '\n')
fptr.close()
解答
code: python
import math
import os
import random
import re
import sys
from collections import defaultdict
#
# Complete the 'activityNotifications' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY expenditure
# 2. INTEGER d
#
def activityNotifications(expenditure, d):
dic = defaultdict(int)
def get_ith_value(idx):
s = 0
for i in range(201):
freq = 0
if i in dic:
s += freq
if s >= idx:
return i
result = 0
for i in range(n):
if i >= d:
if d%2 == 0:
if val >= 2 * ((get_ith_value(d//2) + get_ith_value(d//2 + 1))) // 2:
result += 1
else:
if val >= 2 * get_ith_value(d//2 + 1):
result += 1
if i >= d:
return result
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0) d = int(first_multiple_input1) expenditure = list(map(int, input().rstrip().split()))
result = activityNotifications(expenditure, d)
fptr.write(str(result) + '\n')
fptr.close()
メモ
https://www.youtube.com/watch?v=qF44vXnciGQ
提出
Terminated due to timeout :(
code: python
import math
import os
import random
import re
import sys
from collections import deque
import statistics
#
# Complete the 'activityNotifications' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY expenditure
# 2. INTEGER d
#
# 2 3 4 2 3 6 8 4 5
def activityNotifications(expenditure, d):
# Write your code here
q = deque()
ans = 0
for i in range(d):
for i in range(d, len(expenditure)):
# slow
# heapq? d=5 -> index=2 d=6 -> index=2,3
# median = statistics.median(q)
sq = sorted(q)
if expenditurei >= 2 * median: ans += 1
q.popleft()
return ans
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0) d = int(first_multiple_input1) expenditure = list(map(int, input().rstrip().split()))
result = activityNotifications(expenditure, d)
fptr.write(str(result) + '\n')
fptr.close()
code: python
import math
import os
import random
import re
import sys
from queue import deque
#
# Complete the 'activityNotifications' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY expenditure
# 2. INTEGER d
#
def activityNotifications(expenditure, d):
# Write your code here
que = deque()
for i in range(d):
for i in range(d+1, len(expenditure)):
median = calc_median(que)
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0) d = int(first_multiple_input1) expenditure = list(map(int, input().rstrip().split()))
result = activityNotifications(expenditure, d)
fptr.write(str(result) + '\n')
fptr.close()