-
Notifications
You must be signed in to change notification settings - Fork 2
/
14. Binary search algorithm. MinMaxDivision.swift
112 lines (93 loc) · 3.9 KB
/
14. Binary search algorithm. MinMaxDivision.swift
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import Foundation
import Glibc
// Solution @ Sergey Leschev, Belarusian State University
// 14. Binary search algorithm. MinMaxDivision.
// 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.
// Write an efficient algorithm for the following assumptions:
// 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].
public func solution(_ K: Int, _ M: Int, _ A: inout [Int]) -> Int {
var maxElement = A.first!
var prefixSums = [A.first!]
var result = 0
var handledLeftIndex = 0
var handledLeftSum = 0
var blocksCount = 0
var maxCurrentBlockSum = 0
for i in 1..<A.count {
let element = A[i]
prefixSums.append(prefixSums.last! + element)
maxElement = max(element, maxElement)
}
let totalSum = prefixSums.last!
var minTargetBlockSum = Int((Double(totalSum) / Double(K)).rounded(.up))
minTargetBlockSum = max(minTargetBlockSum, maxElement)
var maxTargetBlockSum = totalSum
var targetBlockSum = (maxTargetBlockSum + minTargetBlockSum) / 2
while blocksCount != K && minTargetBlockSum <= maxTargetBlockSum {
var leftIndex = handledLeftIndex
var rightIndex = prefixSums.count - 1
var position: Int?
while leftIndex <= rightIndex {
let mid = (leftIndex + rightIndex) / 2
let totalLeftSum = prefixSums[mid]
let currentBlockSum = totalLeftSum - handledLeftSum
if currentBlockSum > targetBlockSum {
rightIndex = mid - 1
} else {
leftIndex = mid + 1
position = mid
maxCurrentBlockSum = max(maxCurrentBlockSum, currentBlockSum)
}
}
handledLeftIndex = position!
handledLeftSum = prefixSums[position!]
blocksCount += 1
let tooSmallBlockSize = (totalSum - handledLeftSum) > (K - blocksCount) * targetBlockSum
if blocksCount == K || tooSmallBlockSize {
let delta = totalSum - handledLeftSum
if delta > 0 {
minTargetBlockSum = targetBlockSum + 1
} else {
maxTargetBlockSum = targetBlockSum - 1
result = maxCurrentBlockSum
}
targetBlockSum = (maxTargetBlockSum + minTargetBlockSum) / 2
handledLeftIndex = 0
handledLeftSum = 0
blocksCount = 0
maxCurrentBlockSum = 0
}
}
return result
}