当前位置:网站首页>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)
- 嵌入式系统
- 三说 拷贝构造之禁用
- HW(OD)岗面试题
- Record MySQL troubleshooting caused by disk sector damage
- Free trial of self-developed software noisecreater1.1
- 第五章 输入/输出(I/O)管理
- DSBridge
- 【自动化运维】自动化运维平台有什么用
- What is a port scanning tool? What is the use of port scanning tools
猜你喜欢

B-树系列

数据库产生死锁了请问一下有没有解决办法

High order binary search tree
![[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

High order binary balanced tree

On siem

【#Unity Shader#Amplify Shader Editor(ASE)_第九篇】

C语言课设工资管理系统(大作业)
![[unity shader amplify shader editor (ASE) Chapter 9]](/img/f5/f0f6786406e149187e71c8e12cde0d.png)
[unity shader amplify shader editor (ASE) Chapter 9]

C语言课设学生考勤系统(大作业)
随机推荐
Detailed steps for installing redis on Windows system
【#Unity Shader#Amplify Shader Editor(ASE)_第九篇】
阿里OSS Postman Invalid according to Policy: Policy Condition failed: [“starts-with“, “$key“, “test/“]
第五章 输入/输出(I/O)管理
JSON module
C语言课设学生考勤系统(大作业)
High order binary search tree
C语言课设工资管理系统(大作业)
Uniapp tree level selector
高阶-二叉平衡树
Application of IT service management (ITSM) in Higher Education
HCM Beginner (III) - quickly enter pa70 and pa71 to browse employee information PA10
[unity shader custom material panel part I]
[unity shader amplify shader editor (ASE) Chapter 9]
RestTemplate使用
@Propagation property of transactional requires_ New in-depth understanding
软件工程领域的名词描述
SQL语句
Pol8901 LVDS to Mipi DSI supports rotating image processing chip
三分钟带你快速了解网站开发的整个流程