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号)出售,收益最大。

     

     

  • 相关阅读:
    14-ESP8266 SDK开发基础入门篇--上位机串口控制 Wi-Fi输出PWM的占空比,调节LED亮度,8266程序编写
    Zookeeper之Curator(1)客户端基本的创建,删除,更新,查找操作api
    【1】基于quartz框架和Zookeeper实现集群化定时任务系统
    Spring源码学习之:ClassLoader学习(5)-自测
    java多线程:线程体往外抛出异常的处理机制实践
    MySQL详解--锁,事务
    ubuntu下访问支付宝官网,安装安全控件
    spring源码学习之:springAOP实现底层原理
    spring源码学习之:项目公共配置项解决方案
    java多线程:synchronized和lock比较浅析
  • 原文地址:https://www.cnblogs.com/idealing/p/3372868.html
Copyright © 2011-2022 走看看