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开始崭露头角,解决服务间复杂的通信问题,这一年很多国内互联网公司已经有了较为成熟的微服务实践案例,网易云主办的微服务实践沙龙中也 ...
随机推荐
- jquery对象转成dom对象
jQuery库本质上还是JavaScript代码,它只是对JavaScript语言进行包装处理,为的是提供更好更方便快捷的DOM处理与开发中经常使用的功能.我们使用jQuery的同时也能混合Java ...
- CentOS7下的AIDE入侵检测配置
一.AIDE的概念 AIDE:Advanced Intrusion Detection Environment,是一款入侵检测工具,主要用途是检查文档的完整性.AIDE在本地构造了一个基准的数据库,一 ...
- 【转载】Fiddler 抓包工具使用指北: 弱网络环境模拟限速测试流程
一:为什么要做弱网络测试? 实际的客户现场可能网络不稳定或者网速低,恶劣的网络环境会导致出现一些bug,影响用户体验甚至某些服务不可用.而公司内部的研发环境网络通常比较顺畅,难以复现这种bug.要解决 ...
- npm run build打包时修改的路径
- [Luogu 3794]签到题IV
Description 题库链接 给定长度为 \(n\) 的序列 \(A\).求有多少子段 \([l,r]\) 满足 \[ \left(\gcd_{l\leq i\leq r}A_i\right) \ ...
- linux /lib64/libc.so.6: version `GLIBC_2.17′ not found
使用root权限安装Glances,需要用到glibc,安装失败后所有命令都不好用了,执行回报“/lib64/libc.so.6: version `GLIBC_2.17′ not found ”的错 ...
- [Javascript] Keyword 'in' to check prop exists on Object
function addTo80(n ) { + n; } function memoizedAddTo80 (fn) { let cache = {}; return (n) => { /*k ...
- am335x system upgrade set/get current cpufreq(二十一)
1 Scope of Document This document describes am335x cpufreq technology insider. 2 Requireme ...
- nginx配置ssl加密(单/双向认证、部分https)
nginx下配置ssl本来是很简单的,无论是去认证中心买SSL安全证书还是自签署证书,但最近公司OA的一个需求,得以有个机会实际折腾一番.一开始采用的是全站加密,所有访问http:80的请求强制转换( ...
- Dart和JavaScript对比小结
作为一名web前端来入门dart,新语言和我们熟悉的js有所差异,写dart的过程中容易受到原有思维的影响,这里把dart和js做一个对比总结,方便查找和熟悉. 变量声明 var 关键字 dart和j ...