贪心算法(6)
来源:互联网 发布:先导者之爱知重生 编辑:程序博客网 时间:2024/05/22 11:42
原题:
/** * Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue. * <p> * Note: * The number of people is less than 1,100. * <p> * Example * <p> * Input: * [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]] * <p> * Output: * [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
答案:
public class QueueReconstructionByHeight { public static void main(String[] args) throws Exception { int[][] A = {{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}}; int[][] r = new QueueReconstructionByHeight().reconstructQueue(A); for (int[] i : r) { System.out.println(i[0] + " " + i[1]); } } public int[][] reconstructQueue(int[][] people) { Arrays.sort(people, ((o1, o2) -> (o2[0] - o1[0] == 0) ? o1[1] - o2[1] : o2[0] - o1[0])); LinkedList<int[]> list = new LinkedList<>(); for (int[] p : people) list.add(p[1], p); int[][] result = new int[people.length][2]; for (int i = 0, l = list.size(); i < l; i++) result[i] = list.get(i); return result; }}
阅读全文
0 0
- 贪心算法(6)
- 【贪心算法(一)】贪心算法基础
- 贪心算法(1)
- 贪心算法(2)
- watcher(贪心算法)
- 贪心算法(一)
- 加油站(贪心算法)
- 贪心算法(上)
- 贪心算法(下)
- 贪心算法(2)
- 贪心算法(2)
- 贪心算法(1)
- 贪心算法(一)
- 贪心算法(上)
- 贪心算法(1)
- 贪心算法(2)
- 贪心算法(3)
- 贪心算法(4)
- Deep Learning-TensorFlow TFLearn 快速搭建深度学习模型Alxnet
- SolrCloud搜索引擎集群介绍
- Java:浅谈InputStream的close方法
- Y must be a vector or a character array
- javascript表达正则验证公共函数 检查元素,改成统一
- 贪心算法(6)
- MiniGui核心库编译配置选项
- 大疆M100 Onboard SDK Linux系统下飞行示例简记
- 引水入城(dfs的做法)洛谷p1514
- Struts 2中<s:subset>标签的用法
- Oracle快速替换UNDO表空间方法
- Codeforces Round #441 (Div. 1, by Moscow Team Olympiad) E. Delivery Club
- lnmp 继承环境安装后,使用navicat 链接mysql 10038问题
- THOUGHTWORK'S HOMEWORK 2018YEAR