-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
103 lines (75 loc) · 2.53 KB
/
Solution.java
File metadata and controls
103 lines (75 loc) · 2.53 KB
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
package hackrank.algorithm.greedy.boardcut;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/**
* Cutting Boards Challenge
*
* @see https://www.hackerrank.com/challenges/board-cutting
*/
public class Solution {
public static void main(String[] args) {
for (Board board : readInput(System.in)) {
System.out.println(board.findMinCutCost() % 1_000_000_007);
}
}
public static List<Board> readInput(InputStream stream) {
Scanner scanner = new Scanner(stream);
int size = scanner.nextInt();
List<Board> boards = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
int height = scanner.nextInt();
int width = scanner.nextInt();
List<Long> costHeights = new ArrayList<>(height - 1);
for (int h = 0; h < height - 1; h++) {
costHeights.add(scanner.nextLong());
}
List<Long> costWidths = new ArrayList<>(width - 1);
for (int w = 0; w < width - 1; w++) {
costWidths.add(scanner.nextLong());
}
boards.add(new Board(costHeights, costWidths));
}
scanner.close();
return boards;
}
}
class Board {
private List<Long> costHeight;
private List<Long> costWidth;
Board(List<Long> costHeight, List<Long> costWidth) {
this.costHeight = costHeight;
this.costWidth = costWidth;
}
public long findMinCutCost() {
Collections.sort(costHeight, Collections.reverseOrder());
Collections.sort(costWidth, Collections.reverseOrder());
int h = 0;
int w = 0;
int segmentsWidth = 1;
int segmentsHeight = 1;
long cost = 0;
while (h < costHeight.size() || w < costWidth.size()) {
long costWidth = getCostWidth(w);
long costHeight = getCostHeight(h);
if (costWidth > costHeight) {
cost += costWidth * segmentsHeight;
segmentsWidth++;
w++;
} else {
cost += costHeight * segmentsWidth;
segmentsHeight++;
h++;
}
}
return cost;
}
public long getCostHeight(int index) {
return index >= 0 && index < costHeight.size() ? costHeight.get(index) : -1;
}
public long getCostWidth(int index) {
return index >= 0 && index < costWidth.size() ? costWidth.get(index) : -1;
}
}