当前位置:网站首页>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;
}
}
边栏推荐
- QSAR model establishment script based on pytoch and rdkit
- Hutool dynamically adds scheduled tasks
- Is pytorch difficult to learn? How to learn pytorch well?
- IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥
- Separable bonds and convertible bonds
- Appium automated testing framework
- [mathematical logic] propositional logic (judgment of the correctness of propositional logic reasoning | formal structure is eternal truth - equivalent calculus | deduction from premise - logical reas
- Write it down once Net travel management background CPU Explosion Analysis
- Makefile demo
- Without sxid, suid & sgid will be in danger- Shangwen network xUP Nange
猜你喜欢

Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)

Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis

IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥

pytorch难学吗?如何学好pytorch?

2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =

Leetcode: dynamic planning template

Interaction free shell programming

Recursion: depth first search

编译文件时报错:错误: 编码GBK的不可映射字符

【毕业季·进击的技术er】职场人的自白
随机推荐
动态规划:最长回文子串和子序列
Role of JS No
【学习笔记】seckill-秒杀项目--(11)项目总结
[mathematical logic] propositional logic (judgment of the correctness of propositional logic reasoning | formal structure is eternal truth - equivalent calculus | deduction from premise - logical reas
Cnopendata China Customs Statistics
Message queue addition failure
Application of I2C protocol of STM32F103 (read and write EEPROM)
China Mobile Internet of things oneos and onenet were selected in the list of 2021 Internet of things demonstration projects
【刷题篇】多数元素(超级水王问题)
How to execute a swift for in loop in one step- How can I do a Swift for-in loop with a step?
Ffmpeg download and installation tutorial and introduction
leetcode:297. 二叉树的序列化与反序列化
Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
Supervised pre training! Another exploration of text generation!
Practical operation of vim
CEPH Shangwen network xUP Nange that releases the power of data
动态规划:最长公共子串和最长公共子序列
Is it better to speculate in the short term or the medium and long term? Comparative analysis of differences
eth入门之DAPP
Hutool dynamically adds scheduled tasks