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. configure生成makefile的配置项说明

    一般Linux软件使用configure来检测系统生成makefile文件之后可使用make来编译安装软件. configure的配置选项有哪些呢?现简单收集如下,不断更新中. 以gcc -v为例,可 ...

  2. python爬取数据分析

    一.python爬虫使用的模块 1.import requests 2.from bs4 import BeautifulSoup 3.pandas 数据分析高级接口模块 二. 爬取数据在第一个请求中 ...

  3. eclipse项目结构目录

    文章:eclipse web 项目目录结构 地址:https://blog.csdn.net/Alan_Wdd/article/details/90514928 eclipse web 项目目录结构 ...

  4. 记一次对上传对jsp限制的绕过

    当访问网站任何.jsp后缀的文件时都会显示如下图所示或者session timeout等提示, 并且网站防护会,对上传大马和一句话会被查杀. 解决方法: 利用jspx包含,利用jspx包含图片或者cs ...

  5. Unity进阶之:MVC编程思想

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  6. python开发笔记-变长字典Series的使用

    Series的基本特征: 1.类似一维数组的对象 2.由数据和索引组成 import pandas as pd >>> aSer=pd.Series([1,2.0,'a']) > ...

  7. Spark数据倾斜解决方案(转)

    本文转发自技术世界,原文链接 http://www.jasongj.com/spark/skew/ Spark性能优化之道——解决Spark数据倾斜(Data Skew)的N种姿势  发表于 2017 ...

  8. 百度OCR文字识别-Android安全校验

    本文转载自好基友upuptop:https://blog.csdn.net/pyfysf/article/details/86438769 效果图: 如下为文章正文: 百度OCR接口使用总结:之前总结 ...

  9. LeetCode 1099. Two Sum Less Than K

    原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/ 题目: Given an array A of integers and inte ...

  10. linux 出错 “INFO: task java: xxx blocked for more than 120 seconds.” 的3种解决方案

    1 问题描述 最近搭建的一个linux最小系统在运行到241秒时在控制台自动打印如下图信息,并且以后每隔120秒打印一次. 仔细阅读打印信息发现关键信息是“hung_task_timeout_secs ...