Amazon | OA 2019 | Optimal Utilization
Given 2 lists a and b. Each element is a pair of integers where the first integer represents the unique id and the second integer represents a value. Your task is to find an element from a and an element form b such that the sum of their values is less or equal to target and as close to target as possible. Return a list of ids of selected elements. If no pair is possible, return an empty list. Example 1: Input:
a = [[1, 2], [2, 4], [3, 6]]
b = [[1, 2]]
target = 7 Output: [[2, 1]] Explanation:
There are only three combinations [1, 1], [2, 1], and [3, 1], which have a total sum of 4, 6 and 8, respectively.
Since 6 is the largest sum that does not exceed 7, [2, 1] is the optimal pair.
Example 2: Input:
a = [[1, 3], [2, 5], [3, 7], [4, 10]]
b = [[1, 2], [2, 3], [3, 4], [4, 5]]
target = 10 Output: [[2, 4], [3, 2]] Explanation:
There are two pairs possible. Element with id = 2 from the list `a` has a value 5, and element with id = 4 from the list `b` also has a value 5.
Combined, they add up to 10. Similarily, element with id = 3 from `a` has a value 7, and element with id = 2 from `b` has a value 3.
These also add up to 10. Therefore, the optimal pairs are [2, 4] and [3, 2].
Example 3: Input:
a = [[1, 8], [2, 7], [3, 14]]
b = [[1, 5], [2, 10], [3, 14]]
target = 20 Output: [[3, 1]]
Example 4: Input:
a = [[1, 8], [2, 15], [3, 9]]
b = [[1, 8], [2, 11], [3, 12]]
target = 20 Output: [[1, 3], [3, 2]]
2-Pointers
Syntax side: Pay attention to how to print an array: System.out.println(Arrays.toString(int[] item));
import java.util.*;
/**
* https://leetcode.com/discuss/interview-question/373202
*/
public class OptimalUtilization {
public List<int[]> optimal(List<int[]> a, List<int[]> b, int target) {
if (a == null || a.isEmpty() || b == null || b.isEmpty()) {
return new ArrayList<int[]>();
} Collections.sort(a, (a1, a2) -> Integer.compare(a1[1], a2[1]));
Collections.sort(b, (b1, b2) -> Integer.compare(b1[1], b2[1]));
int m = a.size();
int n = b.size();
int i = 0;
int j = n - 1;
List<int[]> result = new ArrayList<>();
int max = Integer.MIN_VALUE;
while (i < m && j >= 0) {
int sum = a.get(i)[1] + b.get(j)[1];
if (sum <= target) {
// maybe duplicate ele
if (sum > max) {
result.clear();
max = sum;
result.add(new int[]{a.get(i)[0], b.get(j)[0]});
} else if (sum == max) {
result.add(new int[]{a.get(i)[0], b.get(j)[0]});
}
i++;
} else {
j--;
}
}
return result;
} public static void main(String[] args) {
OptimalUtilization sol = new OptimalUtilization();
List<int[]> aa = new ArrayList<>();
aa.add(new int[]{1, 8});
aa.add(new int[]{2, 15});
aa.add(new int[]{3, 9});
List<int[]> bb = new ArrayList<>();
bb.add(new int[]{1, 8});
bb.add(new int[]{2, 11});
bb.add(new int[]{3, 12});
List<int[]> res = sol.optimal(aa, bb, 20);
for (int[] item : res) {
System.out.println(Arrays.toString(item));
}
}
}
Amazon | OA 2019 | Optimal Utilization的更多相关文章
- Amazon OA
Remove Duplicates from unsorted array,它的错误在于9-10行k out of bound,改成下面这样就没问题了 public class removeDupli ...
- Anveshak: Placing Edge Servers In The Wild
Anveshak:在野外放置边缘服务器 本文为SIGCOMM 2018 Workshop (Mobile Edge Communications, MECOMM)论文. 笔者翻译了该论文.由于时间仓促 ...
- 云原生 - 体验Istio的完美入门之旅(一)
作者:justmine 头条号:大数据达摩院 微信公众号:大数据处理系统 创作不易,在满足创作共用版权协议的基础上可以转载,但请以超链接形式注明出处. 为了方便大家阅读,可以关注头条号或微信公众号,后 ...
- 云原生 - Why is istio(二)
出处:https://cizixs.com/2018/08/26/what-is-istio 创作不易,在满足创作共用版权协议的基础上可以转载,但请以超链接形式注明出处. 前言 随着微服务架构的流行, ...
- 2019年Amazon AWS-Solutions-Architect-Professional考试最新题库(AWS SAP题库)带考试模拟器
大家好,由于最近自己备考Amazon AWS-Solutions-Architect-Professional考试,购买了以下链接的题库,并通过了考试 https://www.kaoguti.gq/A ...
- Codeforces Gym-102219 2019 ICPC Malaysia National E. Optimal Slots(01背包+输出路径)
题意:给你一个体积为\(T\)的背包,有\(n\)个物品,每个物品的价值和体积都是是\(a_{i}\),求放哪几个物品使得总价值最大,输出它们,并且输出价值的最大值. 题解:其实就是一个01背包输出路 ...
- F#周报2019年第12期
新闻 Amazon.Lambda.RuntimeSupport发布 Forge 3.0架构 Blazor 0.9.0试验版发布 通过微软游戏栈实现更多应用 介绍ASP.NET Core中的gRPC M ...
- 一篇文章带你看懂AWS re:Invent 2018大会,揭秘Amazon Aurora
本文由云+社区发表 | 本文作者: 刘峰,腾讯云NewSQL数据库产品负责人.曾职于联想研究院,Teradata北京研发中心,从事数据库相关工作8年.2017年加入腾讯数据库产品中心,担任NewSQL ...
- 2019年微服务5大趋势,你pick哪个?
2018年对于微服务来说是非常重要的一年,这一年Service Mesh开始崭露头角,解决服务间复杂的通信问题,这一年很多国内互联网公司已经有了较为成熟的微服务实践案例,网易云主办的微服务实践沙龙中也 ...
随机推荐
- JS正则表达式提取数字
/** * [参数str] * @type {var String} * return 30 */ var str = "ren民BI30kuai" console.log(str ...
- static 关键字在java语言中的特性
一,将自己注入到一个静态变量中实现静态类,如下写法 以上方法的目的是要实现一个静态类,方便用类名获取对象实例,一般情况下调用普通方法需要对象实例.这对象要么new出来,要么spring的注入如下是 ...
- Linux 文件监控之谁动了我的奶酪
有时候配置文件会被莫名其妙的修改,但是找不到修改他的进程,从而无法抓住罪魁祸首,这时候就诞生了摄像头.呃呃,应该是audit 比如我要监控一个文件test.txt [root@hsun /]# aud ...
- 次小生成树(lca)
题目描述 原题来自:BeiJing 2010 组队赛 给定一张 N 个点 M 条边的无向图,求无向图的严格次小生成树. 设最小生成树的边权之和为 sum,严格次小生成树就是指边权之和大于 sum 的生 ...
- hak的使用
autohotkey简称ahk 它是一款轻量级的脚本语言文件,它可以干任何事情,如做dnf的连发脚本,类似按键精灵的自动化点击,按键自动打开文件一系列事情,文件需要按照ahk自己的语言,实现自定义的脚 ...
- 开发Electron可能用到的工具
nodejs:搭载谷歌v8内核的高性能的node环境npm:包管理工具webpack:模块打包器jQuery:js必备库Bootstrap:css必备库react:用于构建用户界面的库vue:构建数据 ...
- Centos7 Rsync怎么实现热备份笔记
应用场景:备份Web服务器相关目录下的数据文件,确保指定目录下的所有文件同步. 操作系统:Centos 7 服务器两台:web服务器 172.19.242.70 备份服务器 172.19.242.7 ...
- vue组件通信方式(多种方案)
一.Props传递数据 components |-Grandson1.vue //孙子1 |-Grandson2.vue //孙子2 |-Parent.vue //父亲 |-Grandson1.vue ...
- Pivotal Greenplum 6.0 新特性介绍
Pivotal Greenplum 6.0 新特性介绍 在1月12日举办的Greenplum开源有道智数未来技术研讨会上,Pivotal中国研发中心Greenplum 产品经理李阳向大家介绍了Pi ...
- BZOJ 2006: [NOI2010]超级钢琴 ST表+堆
开始想到了一个二分+主席树的 $O(n\log^2 n)$ 的做法. 能过,但是太无脑了. 看了一下题解,有一个 ST 表+堆的优美解法. 你发现肯定是选取前 k 大最优. 然后第一次选的话直接选固定 ...