문제
You are given integers K, M and a non-empty array A consisting of N integers. Every element of the array is not greater than M.
You should divide this array into K blocks of consecutive elements. The size of the block is any integer between 0 and N. Every element of the array should belong to some block.
The sum of the block from X to Y equals A[X] + A[X + 1] + ... + A[Y]. The sum of empty block equals 0.
The large sum is the maximal sum of any block.
For example, you are given integers K = 3, M = 5 and array A such that:
A[0] = 2
A[1] = 1
A[2] = 5
A[3] = 1
A[4] = 2
A[5] = 2
A[6] = 2
The array can be divided, for example, into the following blocks:
[2, 1, 5, 1, 2, 2, 2], [], [] with a large sum of 15;
[2], [1, 5, 1, 2], [2, 2] with a large sum of 9;
[2, 1, 5], [], [1, 2, 2, 2] with a large sum of 8;
[2, 1], [5, 1], [2, 2, 2] with a large sum of 6.
The goal is to minimize the large sum. In the above example, 6 is the minimal large sum.
Write a function:
class Solution { public int solution(int K, int M, int[] A); }
that, given integers K, M and a non-empty array A consisting of N integers, returns the minimal large sum.
For example, given K = 3, M = 5 and array A such that:
A[0] = 2
A[1] = 1
A[2] = 5
A[3] = 1
A[4] = 2
A[5] = 2
A[6] = 2
the function should return 6, as explained above.
Assume that:
N and K are integers within the range [1..100,000];
M is an integer within the range [0..10,000];
each element of array A is an integer within the range [0..M].
Complexity:
expected worst-case time complexity is O(N*log(N+M));
expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).
정답
class Solution {
public int solution(int K, int M, int[] A) {
int divideVal = A.length / K;
int cnt = 0;
int sum = 0;
int largeSum =0;
int arrayCnt = 0;
for(int i=0; i
int maxRead = divideVal*(i+1);
if( (i+1) == K ){
maxRead = K;
}
for(int j = divideVal*i ; j < maxRead ; j++){
sum = sum + A[j];
}
if(sum > largeSum || largeSum == 0){
largeSum =sum;
sum =0;
}
}
return largeSum;
}
}
'프로그래밍 > JAVA' 카테고리의 다른 글
자바 (JAVA) 힙 메모리에 관하여 (1) | 2020.12.27 |
---|---|
자바 JVM (RUNTIME DATA AREAS) (0) | 2020.04.01 |
Java의 Collections (List, Set, Map) 이해 (0) | 2020.03.14 |
Iterator 와 Enumeration 차이 (0) | 2020.03.14 |
Hashtable, HashMap, ConcurrentHashMap 비교 (0) | 2020.02.14 |
댓글