当前位置:网站首页>HW(OD)岗面试题
HW(OD)岗面试题
2022-07-01 06:25:00 【ydfind】
把题目中的稀世珍品 换成 股票的概念就很好理解了,你若知道明天会涨,哪今天买入,明天卖出。明天的时候,你若知道后天会涨,则明天也买入,后台卖出…依次类推,必然获得最大收益
/**
* HW二面 面试官 出的题目
*/
public class MainTest {
// 假设你是一名古董商人,近期市场上出现了一件稀世珍品,
/**
*
* 一只股票,你能预知它今后N天每一天的市场价格(给定一个数组,下标为第i天,值为第i天价格),
* 你希望通过买卖来赚取利润,请计算下你能获得的最大利润。
*/
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));
}
}
边栏推荐
- Make: g++: command not found
- Record MySQL troubleshooting caused by disk sector damage
- sci-hub如何使用
- [ManageEngine] how to realize network automatic operation and maintenance
- mysql约束学习笔记
- 【ManageEngine卓豪】移动终端管理解决方案,助力中州航空产业数字化转型
- SystemVerilog learning-10-validation quantification and coverage
- 请求模块(requests)
- HCM Beginner (III) - quickly enter pa70 and pa71 to browse employee information PA10
- async 与 await
猜你喜欢
随机推荐
【ManageEngine】如何实现网络自动化运维
HCM Beginner (IV) - time
微信公众号内嵌跳转微信小程序方案总结
伪装请求头库: anti-useragent
数据库产生死锁了请问一下有没有解决办法
mysql数据类型学习笔记
地宮取寶(記憶化深搜)
Free trial of self-developed software noisecreater1.1
C#如何打印輸出原版數組
MongoDB:一、MongoDB是什么?MongoDB的优缺点
C语言课设物业费管理系统(大作业)
虚幻 简单的屏幕雨滴后处理效果
How did ManageEngine Zhuohao achieve the goal of being selected into Gartner Magic Quadrant for four consecutive years?
Although pycharm is marked with red in the run-time search path, it does not affect the execution of the program
Movable mechanical wall clock
做技术,自信不可或缺
Design of sales management system for C language course (big homework)
C language course design student information management system (big homework)
[ManageEngine Zhuohao] use unified terminal management to help "Eurex group" digital transformation
ManageEngine卓豪助您符合ISO 20000标准(四)