今天有N个面试者需要面试,公司安排了两个面试的城市A和B,每一个面试者都有到A城市的开销costA和到B城市的开销costB。公司需要将面试者均分成两拨,使得total cost最小。
样例
输入: cost = [[5,4],[3,6],[1,8],[3,9]]
输出: 14
说明: 第一个和第二个人去B城市,剩下的去A城市
说明
题目要求去A的人数和去B的人数相等。
注意事项
N是偶数
2<=N<=1e5
答案确保在int范围内
1<=costA,costB <=1e6
//贪心的思想:a[0]-a[1]按照该差值进行排序
public int TotalCost(List<List<Integer>> cost) {
// write your code here
int n = cost.size();
int totalB = 0;
int[] diff = new int[n];
for (int i = 0; i < n; i++) {
totalB += cost[i][1];
diff[i] = cost[i][0] - cost[i][1];
}
Arrays.sort(diff);
int totalDiff = 0;
for (int i = 0; i < n/2; i++) {
totalDiff += diff[i];
}
return totalB + totalDiff;
}
给定一个正整数,返回相应的列标题,如Excel表中所示。
样例
样例1
输入: 28
输出: “AB”
样例2
输入: 29
输出: “AC”
注意事项
1 -> A
2 -> B
3 -> C
…
26 -> Z
27 -> AA
28 -> AB
//进制转换问题;进展转换的话,n%我们想要转的进制,
然后n/=
public String convertToTitle(int n) {
// write your code here
StringBuilder str = new StringBuilder();
while (n > 0) {
n--;
str.append ( (char) ( (n % 26) + 'A'));
n /= 26;
}
return str.reverse().toString();
}

965

被折叠的 条评论
为什么被折叠?



