[HackerRank][Python3] Maximum Subarray Sum
2018. 5. 18. 03:22 |
프로그래밍/HackerRank
Problem :
https://www.hackerrank.com/challenges/maximum-subarray-sum/problem
My Solution :
#!/usr/bin/env python3 def maximumSum(a, m): s = 0 pfs = [] for i in range(len(a)): s = (s + a[i]%m)%m if s == m - 1: return s pfs.append((i, s)) pfs = sorted(pfs, key=lambda x: x[1]) min_d = m max_s = 0 for i in range(len(pfs) - 1): if 0 < pfs[i+1][1] - pfs[i][1] < min_d and pfs[i][0] > pfs[i+1][0]: min_d = pfs[i+1][1] - pfs[i][1] max_s = pfs[i][1] - pfs[i+1][1] + m return max(max_s, pfs[-1][1]) q = int(input()) for _ in range(q): n, m = map(int, input().strip().split()) a = list(map(int, input().strip().split())) print(maximumSum(a, m))
'프로그래밍 > HackerRank' 카테고리의 다른 글
[HackerRank][Python3] Big Sorting (0) | 2018.05.29 |
---|---|
[HackerRank][Python3] CamelCase (0) | 2018.05.27 |
[HackerRank][Python3] Journey to the Moon (0) | 2018.05.25 |
[HackerRank][Python3] Hackerland Radio Transmitters (0) | 2018.05.23 |
[HackerRank][Python3] Sherlock and Cost (0) | 2018.05.21 |
[HackerRank][Python3] Equal (0) | 2018.05.16 |
[HackerRank][Python3] The Coin Change Problem (0) | 2018.05.15 |
[HackerRank][Python3] Super Reduced String (2) | 2018.05.11 |
최근에 달린 댓글 최근에 달린 댓글