当前位置:网站首页>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));
}
}
边栏推荐
猜你喜欢
![阿里OSS Postman Invalid according to Policy: Policy Condition failed: [“starts-with“, “$key“, “test/“]](/img/3c/7684b7c594f7871471f89007294703.png)
阿里OSS Postman Invalid according to Policy: Policy Condition failed: [“starts-with“, “$key“, “test/“]

Promise

High order binary search tree

C语言课设学生考勤系统(大作业)

自开发软件NoiseCreater1.1版本免费试用

Promise

异常检测方法梳理,看这篇就够了!

sci-hub如何使用

【Unity Shader 描边效果_案例分享第一篇】

To sort out the anomaly detection methods, just read this article!
随机推荐
Projects and dependencies in ABP learning solutions
伪装请求头库: anti-useragent
VS2019如何永久配置本地OpenCV4.5.5使用
[unity shader custom material panel part I]
Functions of switch configuration software
[file system] how to run squashfs on UBI
Teach you how to implement a deep learning framework
Code power is full of quantitative learning | how to find a suitable financial announcement in the financial report
自开发软件NoiseCreater1.1版本免费试用
To sort out the anomaly detection methods, just read this article!
SystemVerilog learning-08-random constraints and thread control
Excel visualization
端口扫描工具是什么?端口扫描工具有什么用
【#Unity Shader#自定义材质面板_第二篇】
C language course is provided with employee information management system (large operation)
On siem
Pychart configuring jupyter
1034 Head of a Gang
Golang panic recover custom exception handling
【自动化运维】自动化运维平台有什么用