当前位置:网站首页>Interview questions for HW (OD) post
Interview questions for HW (OD) post
2022-07-01 06:28:00 【ydfind】
The rare treasures in the title Switch to The concept of stock is well understood , If you knew it would rise tomorrow , Which buy today , Sell... Tomorrow . Tomorrow , If you know it will rise the day after tomorrow , Then buy tomorrow , Backstage sales … By analogy , Must get the maximum benefit
/**
* HW Two sides interviewer The problem is
*/
public class MainTest {
// Suppose you are an antique dealer , Recently, a rare treasure has appeared on the market ,
/**
*
* A stock , You can predict its future N The market price of each day ( Given an array , The subscript is no i God , The value is No i Day price ),
* You want to make a profit by buying and selling , Please calculate the maximum profit you can make .
*/
public int getMaxProfit(int[] prices) {
int last = prices[0];
int profit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > last) {
profit += prices[i] - last;
}
last = prices[i];
}
return profit;
}
@Test
public void testExample1() {
int[] arr = new int[]{1, 3, 1, 10, 100, 1000};
Assert.assertEquals(1001, getMaxProfit(arr));
arr = new int[]{2, 4, 5, 2, 9, 7, 8, 10, 8};
Assert.assertEquals(13, getMaxProfit(arr));
}
}
边栏推荐
- [postgraduate entrance examination advanced mathematics Wu Zhongxiang +880 version for personal use] advanced mathematics Chapter II Basic Stage mind map
- 三说 拷贝构造之禁用
- Free trial of self-developed software noisecreater1.1
- Discrimination between left and right limits of derivatives and left and right derivatives
- 数据库对象:视图学习记录
- 考研目录链接
- H5网页判断是否安装了某个APP,安装则跳转未安装则下载的方案总结
- [ManageEngine Zhuohao] the role of LAN monitoring
- 图片服务器项目测试
- json模块
猜你喜欢
随机推荐
lxml模块(数据提取)
C语言课设学生信息管理系统(大作业)
端口扫描工具对企业有什么帮助?
async 与 await
SQL学习笔记2
RestTemplate使用
C语言课设物业费管理系统(大作业)
【ManageEngine卓豪】助力黄石爱康医院实现智能批量化网络设备配置管理
虚幻 简单的屏幕雨滴后处理效果
Promise
考研目录链接
libpng12.so. 0: cannot open shared object file: no such file or directory
Design of sales management system for C language course (big homework)
【ManageEngine卓豪】用统一终端管理助“欧力士集团”数字化转型
[postgraduate entrance examination advanced mathematics Wu Zhongxiang +880 version for personal use] advanced mathematics Chapter II Basic Stage mind map
[summary of knowledge points] chi square distribution, t distribution, F distribution
【ManageEngine卓豪】局域网监控的作用
SQL语言的学习记录一
[ManageEngine] how to realize network automatic operation and maintenance
@Propagation property of transactional requires_ New in-depth understanding




![[unity shader amplify shader editor (ASE) Chapter 9]](/img/f5/f0f6786406e149187e71c8e12cde0d.png)




