zoukankan      html  css  js  c++  java
  • Amazon Hiring Campus 2013

    Let's assume that there is a simple market for beans. Every day there is a published bean price in the market. Traders can buy or sell at the published price. There is a trader who time travelled to future and brought back the price information for a number of days in the future. If you have this information and you are allowed to buy and sell many times. How do you make the maximum profit? The price information will be given as an array of numbers. Each number is for a day’s trading price. The numbers are all integers to simplify the problem. You will need to return the index of the buy-in point and sell-out point for maximum profit.

    Rules:

    1) The input line length less than 1000, and the trading price length less than 100;

    2) The trading price is positive integer;

    3) The trading prices are delimited  by ' '(single space);

    4) Please make sure every buying and selling period shortest. especially, please ouput '-' if all the trading prices are the same or no trading point;

    Sample Input and Output:

    Input 1

    1 3 5 4 2 8 10

    Output 1

    1 3 5 7

    To make the maximum profit, you should buy at $1 and sell at $5, and then buy at $5 and sell it at $10. so the output is "1 3 5 7".

    Input 2 

    1 1 1 3 5 4 2 2 2 8 10

    Ouput 2

    3 5 9 11 

     

    ///////////////////////////// 
    //C Sample 
    //////////////////////////// 
    #include <stdio.h> 
    #include <string.h> 
    
    void calculateAndPrint(int array[], int length){ 
    //Your Code is here 
    
      int low = 0,//最低购入点
    hight = 0,//最高出售点
    profit= 0;//是否有交易 while(low + 1 < length) { //寻找最小购入点
    while(low + 1 < length&&array[low] >= array[low + 1])
    ++low ; //出售不能早于购入(由上循环可以,如果下一点不为空的话则会比low处值大因此可以作为hight起点)
    hight
    = low + 1; //等待最大出售点
    while(hight + 1 < length&&array[hight] < array[hight + 1]) ++hight; //判断是否有交易
    if(low < hight&&hight + 1 <= length){ printf("%d %d ",low + 1,hight + 1); //重新寻求最大增区间
    low
    = hight + 1; //至此,则有交易进行
    profit
    = 1; } } if(!profit) //Code Over printf("-"); } int splitAndConvert(char* strings,int array[]){ char*tokenPtr = strtok(strings," "); int i=0; while(tokenPtr!=NULL){ array[i] = atoi(tokenPtr); i++; tokenPtr=strtok(NULL," "); } return i; } int main(){ char line[1000] = {0} ; while(gets(line)){ int array[100] = {0}; int length = splitAndConvert(line,array); if(length==0){ break; } calculateAndPrint(array, length); printf(" "); } return 0; }

    这道题,解题的思路就是寻找单调增区间:这样才能使收益最大。

    如:

    Input 1

    1 3 5 4 2 8 10

    Output 1

    1 3 5 7

    则,在第一个1处(1号)购入,在第一个5处(3号)出售,收益最大;然后在第一个2处(5号)购入,在第一个10处(7号)出售,收益最大。

     

     

  • 相关阅读:
    两种常用图像识别迁移学习方法
    学习备忘
    图像处理代码举例(C++、MATLAB、OpenCV)
    Java 读取clob字段的几种方法
    Oracle 查询表注释以及字段注释
    Oracle 查询表的索引包含的字段
    Oracle 获取表的主键、外键以及唯一约束条件
    Oracle列信息表 all_tab_columns中的data_length和data_precision字段区别
    Oracle 表结构、索引以及分区信息查询
    Java中的String,StringBuilder,StringBuffer三者的区别
  • 原文地址:https://www.cnblogs.com/idealing/p/3372868.html
Copyright © 2011-2022 走看看