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的更多相关文章

  1. Amazon OA

    Remove Duplicates from unsorted array,它的错误在于9-10行k out of bound,改成下面这样就没问题了 public class removeDupli ...

  2. Anveshak: Placing Edge Servers In The Wild

    Anveshak:在野外放置边缘服务器 本文为SIGCOMM 2018 Workshop (Mobile Edge Communications, MECOMM)论文. 笔者翻译了该论文.由于时间仓促 ...

  3. 云原生 - 体验Istio的完美入门之旅(一)

    作者:justmine 头条号:大数据达摩院 微信公众号:大数据处理系统 创作不易,在满足创作共用版权协议的基础上可以转载,但请以超链接形式注明出处. 为了方便大家阅读,可以关注头条号或微信公众号,后 ...

  4. 云原生 - Why is istio(二)

    出处:https://cizixs.com/2018/08/26/what-is-istio 创作不易,在满足创作共用版权协议的基础上可以转载,但请以超链接形式注明出处. 前言 随着微服务架构的流行, ...

  5. 2019年Amazon AWS-Solutions-Architect-Professional考试最新题库(AWS SAP题库)带考试模拟器

    大家好,由于最近自己备考Amazon AWS-Solutions-Architect-Professional考试,购买了以下链接的题库,并通过了考试 https://www.kaoguti.gq/A ...

  6. Codeforces Gym-102219 2019 ICPC Malaysia National E. Optimal Slots(01背包+输出路径)

    题意:给你一个体积为\(T\)的背包,有\(n\)个物品,每个物品的价值和体积都是是\(a_{i}\),求放哪几个物品使得总价值最大,输出它们,并且输出价值的最大值. 题解:其实就是一个01背包输出路 ...

  7. F#周报2019年第12期

    新闻 Amazon.Lambda.RuntimeSupport发布 Forge 3.0架构 Blazor 0.9.0试验版发布 通过微软游戏栈实现更多应用 介绍ASP.NET Core中的gRPC M ...

  8. 一篇文章带你看懂AWS re:Invent 2018大会,揭秘Amazon Aurora

    本文由云+社区发表 | 本文作者: 刘峰,腾讯云NewSQL数据库产品负责人.曾职于联想研究院,Teradata北京研发中心,从事数据库相关工作8年.2017年加入腾讯数据库产品中心,担任NewSQL ...

  9. 2019年微服务5大趋势,你pick哪个?

    2018年对于微服务来说是非常重要的一年,这一年Service Mesh开始崭露头角,解决服务间复杂的通信问题,这一年很多国内互联网公司已经有了较为成熟的微服务实践案例,网易云主办的微服务实践沙龙中也 ...

随机推荐

  1. 利用ViewStub实现布局懒惰加载

    这个问题也是头条面试官问的,本身没什么难度,但以前确实没仔细研究过. 1.使用介绍 ViewStub是一种不可见的尺寸为0的View,用来实现布局资源的懒加载.当ViewStub被设置为用户可见或其  ...

  2. 22.centos7基础学习与积累-008-系统调优及安全设置

    从头开始积累centos7系统运用 大牛博客:https://blog.51cto.com/yangrong/p5 1.关闭selinux功能: SELinux(Securety-EnhancedLi ...

  3. 安装nginx环境(含lua)时遇到报错ngx_http_lua_common.h:20:20: error: luajit.h: No such file or directory的解决

    下面是安装nginx+lua环境时使用的相关模块及版本,ngx_devel_kit和lua-nginx-module模块用的都是github上最新的模块.并进行了LuaJIT的安装. #Install ...

  4. vue响应式原理解析

    # Vue响应式原理解析 首先定义了四个核心的js文件 - 1. observer.js 观察者函数,用来设置data的get和set函数,并且把watcher存放在dep中 - 2. watcher ...

  5. jq function return value

    所有 JS  函数 都会返回值 假如 没有 return  则返回 undefined

  6. HDU - 5823:color II (状压DP 反演DP)

    题意:给定连通图,求出连通图的所有子图的颜色数. 一个图的颜色数,指最少的颜色数,给图染色,使得有边相邻的点之间颜色不同. 思路:首先想法是DFS枚举,然后计算颜色,发现对于给定图,求颜色不会求? 毕 ...

  7. HDU3109: Worms(字符串变换类 DP)

    pro:开始有一个字母虫,然后字母虫在每一天可以选择自己身上的部分字母变换,变换规则形如A->BC. 现状给定最终字母虫的字符串,求最少用了多少天. 如有规则A->BC,B->AC, ...

  8. django 进行语言的国际化及在后台进行中英文切换

    项目的部署地为: 中国大陆与美国东海岸, 两个地区的服务器数据不进行同步, 中国地区的服务器页面展示中文, 美国地区的服务器页面展示成英文, 项目后台使用python编程语言进行开发, 并结合djan ...

  9. java项目部署到LIINUX

    天领导给个任务,把java项目部署到liunx服务器上.现记录步骤,方便以后查看.项目部署服务器步骤:服务器信息:弹性IP地址:xx.xx.xxx.xx账号:root密码:cjw@100 数据库信息: ...

  10. 02-Flutter移动电商实战-建立项目和编写入口文件

    环境搭建请参考之前写的一篇文章:Flutter_初体验_创建第一个应用 1.创建项目 采用AndroidStudio构建本项目,FIle>New>New Flutter Project… ...