일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- 파이썬
- SW Expert Academy
- Baekjoon
- Kaggle
- 프로그래머스 파이썬
- Docker
- 우분투
- 맥북
- 캐치카페
- ubuntu
- ChatGPT
- 편스토랑
- 데이콘
- leetcode
- 더현대서울 맛집
- 코로나19
- gs25
- 편스토랑 우승상품
- 백준
- hackerrank
- 프로그래머스
- Real or Not? NLP with Disaster Tweets
- 금융문자분석경진대회
- programmers
- 자연어처리
- dacon
- AI 경진대회
- Git
- PYTHON
- github
- Today
- Total
솜씨좋은장씨
[HackerRank] Arrays: Array Manipulation (Python) 본문
Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in your array.
For example, the length of your array of zeros n = 10. Your list of queries is as follows:
a b k
1 5 3
4 8 7
6 9 1
Add the values of k between the indices a and b inclusive:
index-> 1 2 3 4 5 6 7 8 9 10
[0,0,0, 0, 0,0,0,0,0, 0]
[3,3,3, 3, 3,0,0,0,0, 0]
[3,3,3,10,10,7,7,7,0, 0]
[3,3,3,10,10,8,8,8,1, 0]
The largest value is 10 after all operations are performed.
Function Description
Complete the function arrayManipulation in the editor below. It must return an integer, the maximum value in the resulting array.
arrayManipulation has the following parameters:
- n - the number of elements in your array
- queries - a two dimensional array of queries where each queries[i] contains three integers, a, b, and k.
Input Format
The first line contains two space-separated integers n and m, the size of the array and the number of operations.
Each of the next m lines contains three space-separated integers a, b and k, the left index, right index and summand.
Constraints
- 3 <= n <= 10^7
- 1<= m <= 2*10^5
- 1 <= a <= b <= n
- 0 <= k <= 10^9
Output Format
Return the integer maximum value in the finished array.
Sample Input
5 3
1 2 100
2 5 100
3 4 100
Sample Output
200
Explanation
After the first update list will be 100 100 0 0 0.
After the second update list will be 100 200 100 100 100.
After the third update list will be 100 200 200 200 100.
The required answer will be 200.
첫번째 시도
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the arrayManipulation function below.
def arrayManipulation(n, queries):
arr = [0] * n
for query in queries:
for j in range(query[0]-1, query[1]):
arr[j] = arr[j] + query[2]
return max(arr)
결과
시간초과의 결과가 나왔습니다.
아무래도 반복문을 사용하면 반복되어야하는 숫자도 큰데 거기에 이중 반복문까지 사용하여 그런 것 같습니다.
두번째 시도
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the arrayManipulation function below.
def arrayManipulation(n, queries):
arr = [0] * (n + 1)
for query in queries:
front = query[0] -1
end = query[1]
arr[front] = arr[front] + query[2]
arr[end] = arr[end] + (-1 * query[2])
answer = -1
total = 0
for i in range(len(arr)):
total = total + arr[i]
if answer < total :
answer = total
return answer
결과
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[HackerRank] Diagonal Difference (Python) (0) | 2020.03.15 |
---|---|
[HackerRank] Dictionaries and Hashmaps: Two Strings (Python) (0) | 2020.03.14 |
[leetCode] 58. Length of Last Word (Python) (2) | 2020.03.12 |
[HackerRank] Arrays: Minimum Swaps 2 (Python) (0) | 2020.03.11 |
[HackerRank] Sorting: Bubble Sort (Python) (0) | 2020.03.10 |