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开始崭露头角,解决服务间复杂的通信问题,这一年很多国内互联网公司已经有了较为成熟的微服务实践案例,网易云主办的微服务实践沙龙中也 ...
随机推荐
- JWT生成token及过期处理方案
业务场景 在前后分离场景下,越来越多的项目使用token作为接口的安全机制,APP端或者WEB端(使用VUE.REACTJS等构建)使用token与后端接口交互,以达到安全的目的.本文结合stacko ...
- 使用SSH配置git服务器免密提交
1. 生成SSH 1.1下载 下载工具 puttygen.exe ,当然其他工具请自行搜索. 下载地址: 下载地址1 百度网盘 (提取码: if8g)https://pan.baidu.com/s ...
- 26.centos7基础学习与积累-012-文件和目录的属性
从头开始积累centos7系统运用 大牛博客:https://blog.51cto.com/yangrong/p5 1.文件的属性(文件的信息描述): [root@python01 ~]# ls -l ...
- Python语言防坑小技巧
Python语言防坑小技巧 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.赋值即定义 1>.运行以下代码会出现报错 #!/usr/bin/env python #_*_ ...
- Kotlin反射在属性上的应用实战
继续研究Kotlin反射相关的东东,看代码: 接下来则遍历函数,来调一下,比如咱们先来调用一下带有2个参数的method方法,可以这样写: 其实在调用实例方法时的第一个参数永远都是要调用的那个实例,也 ...
- 项目Beta冲刺(团队)——凡事预则立
项目Beta冲刺(团队)--凡事预则立 格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺(团队) 团队名称:为了交项目干杯 作业目标:Beta冲刺前对冲刺阶段的总体规划 ...
- 【AirTest自学】AirTest工具介绍和入门学习(一)
==================================================================================================== ...
- python - 对接微信支付(PC)和 注意点
注:本文仅提供 pc 端微信扫码支付(模式一)的示例代码. 关于对接过程中遇到的问题总结在本文最下方. 参考: 官方文档, https://blog.csdn.net/lm_is_dc/arti ...
- js中回调函数,promise 以及 async/await 的对比用法 对比!!!
在编程项目中,我们常需要用到回调的做法来实现部分功能,那么在js中我们有哪些方法来实现回调的? 方法1:回调函数 首先要定义这个函数,然后才能利用回调函数来调用! login: function (f ...
- WinDbg的工作空间---Work Space
一.什么是工作空间 Windbg把和调试相关的所有配置称为workspace.WinDbg使用工作空间来描述和存储调试项目的属性.参数及调试器设置等信息.工作空间与vc中的项目文件很相似.退出wind ...