-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_cost_to_reach_every_position.java
More file actions
46 lines (39 loc) · 1.4 KB
/
Copy pathMinimum_cost_to_reach_every_position.java
File metadata and controls
46 lines (39 loc) · 1.4 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
public class Minimum_cost_to_reach_every_position{
public int[] minCosts(int[] cost) {
int n = cost.length;
int[] answer = new int[n];
for (int i = 0; i < n; i++) {
answer[i] = cost[i];
for (int j = 0; j < i; j++) {
if (answer[j] < answer[i]) {
answer[i] = answer[j];
}
}
}
return answer;
}
public static void main(String[] args) {
Minimum_cost_to_reach_every_position solution = new Minimum_cost_to_reach_every_position();
int[] cost1 = {5, 3, 4, 1, 3, 2};
int[] result1 = solution.minCosts(cost1);
System.out.println("Test case 1:");
System.out.println("Input: [5, 3, 4, 1, 3, 2]");
System.out.print("Output: [");
for (int i = 0; i < result1.length; i++) {
System.out.print(result1[i]);
if (i < result1.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
int[] expected1 = {5, 3, 3, 1, 1, 1};
boolean isCorrect = true;
for (int i = 0; i < result1.length; i++) {
if (result1[i] != expected1[i]) {
isCorrect = false;
break;
}
}
System.out.println("Test case 1 " + (isCorrect ? "passed" : "failed"));
}
}