当前位置:网站首页>Leecode swipe questions and record LCP 18 breakfast combination
Leecode swipe questions and record LCP 18 breakfast combination
2022-07-03 04:01:00 【Why is there a bug list】
topic
Xiaokou chooses a breakfast stand in the autumn market , One dimensional integer array staple The price of each staple food was recorded , One dimensional integer array drinks The price of each drink is recorded in . Small bucks plan to choose a staple food and a drink , And it doesn't cost more than x element . Please return the total number of purchase options for small deduction .
Be careful : The answer needs to be 1e9 + 7 (1000000007) Take the mold for the bottom , Such as : The initial result of calculation is :1000000008, Please return 1
Example 1:
Input :staple = [10,20,5], drinks = [5,5,2], x = 15
Output :6
explain : Little button 6 A purchase plan , The corresponding subscripts of the selected staple food and beverage in the array are respectively :
The first 1 Kind of plan :staple[0] + drinks[0] = 10 + 5 = 15;
The first 2 Kind of plan :staple[0] + drinks[1] = 10 + 5 = 15;
The first 3 Kind of plan :staple[0] + drinks[2] = 10 + 2 = 12;
The first 4 Kind of plan :staple[2] + drinks[0] = 5 + 5 = 10;
The first 5 Kind of plan :staple[2] + drinks[1] = 5 + 5 = 10;
The first 6 Kind of plan :staple[2] + drinks[2] = 5 + 2 = 7.
Example 2:
Input :staple = [2,1,1], drinks = [8,9,5,1], x = 9
Output :8
explain : Little button 8 A purchase plan , The corresponding subscripts of the selected staple food and beverage in the array are respectively :
The first 1 Kind of plan :staple[0] + drinks[2] = 2 + 5 = 7;
The first 2 Kind of plan :staple[0] + drinks[3] = 2 + 1 = 3;
The first 3 Kind of plan :staple[1] + drinks[0] = 1 + 8 = 9;
The first 4 Kind of plan :staple[1] + drinks[2] = 1 + 5 = 6;
The first 5 Kind of plan :staple[1] + drinks[3] = 1 + 1 = 2;
The first 6 Kind of plan :staple[2] + drinks[0] = 1 + 8 = 9;
The first 7 Kind of plan :staple[2] + drinks[2] = 1 + 5 = 6;
The first 8 Kind of plan :staple[2] + drinks[3] = 1 + 1 = 2;
Tips :
1 <= staple.length <= 10^5
1 <= drinks.length <= 10^5
1 <= staple[i],drinks[i] <= 10^5
1 <= x <= 2*10^5
answer
public class Solution {
public int breakfastNumber(int[] staple, int[] drinks, int x)
{
Arrays.sort(staple);
Arrays.sort(drinks);
int count = 0;
int j = drinks.length - 1;
for(int i = 0;i < staple.length; i++)
{
if(staple[i] >= x) break;
for(;j >= 0; j--)
{
if(staple[i] + drinks[j] <= x)
{
count = (count + j + 1) % 1000000007;
break;
}
}
}
return count;
}
}
边栏推荐
- nodejs基础:浅聊url和querystring模块
- 【刷题篇】 找出第 K 小的数对距离
- [DRM] simple analysis of DRM bridge driver call process
- [brush questions] connected with rainwater (one dimension)
- 2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
- [mathematical logic] propositional logic (equivalent calculus | idempotent law | exchange law | combination law | distribution law | De Morgan law | absorption rate | zero law | identity | exclusion l
- 基于Pytorch和RDKit的QSAR模型建立脚本
- js中#号的作用
- Recursion: quick sort, merge sort and heap sort
- eth入门之DAPP
猜你喜欢
Nodejs Foundation: shallow chat URL and querystring module
105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode
【刷题篇】多数元素(超级水王问题)
In Net 6 project using startup cs
How to download pytorch? Where can I download pytorch?
[Apple Photo Album push] IMessage group anchor local push
Without sxid, suid & sgid will be in danger- Shangwen network xUP Nange
What is pytorch? Is pytorch a software?
JS native common knowledge
Deep dive kotlin synergy (19): flow overview
随机推荐
Commands related to the startup of redis under Linux server (installation and configuration)
[home push IMessage] software installation virtual host rental tothebuddy delay
Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)
【学习笔记】seckill-秒杀项目--(11)项目总结
Social phobia of contemporary young people (III)
Appium自动化测试框架
Arlo's thinking about himself
[mathematical logic] predicate logic (first-order predicate logic formula | example)
中移物联网OneOS与OneNET入选《2021年物联网示范项目名单》
How to execute a swift for in loop in one step- How can I do a Swift for-in loop with a step?
Makefile demo
Hutool dynamically adds scheduled tasks
JS native common knowledge
[mathematical logic] propositional logic (equivalent calculus | idempotent law | exchange law | combination law | distribution law | De Morgan law | absorption rate | zero law | identity | exclusion l
2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =
动态规划:最长回文子串和子序列
[Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
[Blue Bridge Road -- bug free code] interpretation of some codes of matrix keyboard
Shardingsphere dynamic data source
[DRM] simple analysis of DRM bridge driver call process