-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjamesAndTheMenus.java
More file actions
65 lines (54 loc) · 1.9 KB
/
jamesAndTheMenus.java
File metadata and controls
65 lines (54 loc) · 1.9 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
// time = O(n * m), space = O(m)
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] prices = new int[n][m];
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
prices [i][j] = sc.nextInt();
}
}
// find max value
int[] maxPrices = new int[m];
for (int j = 0; j < m; j++) {
int maxPrice = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
if (prices[i][j] > maxPrice) {
maxPrice = prices[i][j];
}
}
maxPrices[j] = maxPrice;
}
// Count good prices and compute the average price for each menu
int bestMenuIndex = -1;
int maxGoodPrices = -1;
double highestAveragePrice = -1.0;
for(int i=0; i<n; i++){
int goodPriceCount = 0;
double totalPrice = 0.0;
for (int j = 0; j < m; j++) {
totalPrice += prices[i][j];
if (prices[i][j] == maxPrices[j]) {
goodPriceCount++;
}
}
double averagePrice = totalPrice / m;
// finding best menu
if (goodPriceCount > maxGoodPrices) {
bestMenuIndex = i;
maxGoodPrices = goodPriceCount;
highestAveragePrice = averagePrice;
} else if (goodPriceCount == maxGoodPrices && averagePrice > highestAveragePrice) {
bestMenuIndex = i;
highestAveragePrice = averagePrice;
}
}
// Output the result (1-based index)
System.out.println(bestMenuIndex + 1); sc.close();
}
}