当前位置:网站首页>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));
}
}
边栏推荐
猜你喜欢

C language course design student information management system (big homework)

高阶-二叉搜索树详解

高阶-二叉平衡树

VS2019如何永久配置本地OpenCV4.5.5使用

ManageEngine卓豪助您符合ISO 20000标准(四)

SystemVerilog learning-10-validation quantification and coverage
![[self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage](/img/54/f187e22ad69f3985d30376bad1fa03.png)
[self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
![[unity shader ablation effect _ case sharing]](/img/e3/464f1cf426e8c03ce3d538ed9cf4bc.png)
[unity shader ablation effect _ case sharing]

Lxml module (data extraction)

Detailed steps for installing redis on Windows system
随机推荐
Uniapp tree level selector
C语言课设职工信息管理系统(大作业)
第五章 输入/输出(I/O)管理
Comment imprimer le tableau original
C#如何打印输出原版数组
Requests module (requests)
做技术,自信不可或缺
sci-hub如何使用
mysql约束学习笔记
How did ManageEngine Zhuohao achieve the goal of being selected into Gartner Magic Quadrant for four consecutive years?
【LeetCode】Day91-存在重复元素
json模块
【Unity Shader 消融效果_案例分享】
[self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
JMM详解
根据输入画有向图
C语言课设工资管理系统(大作业)
SQL语句
10-golang运算符
Pol8901 LVDS to Mipi DSI supports rotating image processing chip