华为OD机试真题 - 最大股票收益

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] prices = in.nextLine().split(" ");
        int[] pricesTemp = changeToRMB(prices);
        System.out.println(getResult(pricesTemp));
    }

    public static int getResult(int[] prices) {
        int sum = 0;
        int price = 0;
        for (int i = 0; i < prices.length; i++) {
            if (isPeak(prices, i) &amp;&amp; price != 0)
                sum += prices[i] - price;
            if (isValley(prices, i))
                price = prices[i];
        }
        return sum;
    }

    public static int[] changeToRMB(String[] prices) {
        int[] pricesTemp = new int[prices.length];
        for (int i = 0; i < prices.length; i++) {
            if (prices[i].endsWith(&quot;Y&quot;))
                pricesTemp[i] = Integer.parseInt(prices[i].substring(0, prices[i].length() - 1));
            else
                pricesTemp[i] = Integer.parseInt(prices[i].substring(0, prices[i].length() - 1)) * 7;
        }
        return pricesTemp;
    }

    public static boolean isPeak(int[] prices, int i) {
        if (i == 0 &amp;&amp; prices[i + 1] < prices[i])
            return true;
        else if (i == 0)
            return false;
        if (i == prices.length - 1 &amp;&amp; prices[i - 1] < prices[i])
            return true;
        else if (i == prices.length - 1)
            return false;
        if (prices[i - 1] < prices[i] &amp;&amp; prices[i + 1] < prices[i])
            return true;
        return false;
    }

    public static boolean isValley(int[] prices, int i) {
        if (i == 0 &amp;&amp; prices[i + 1] > prices[i])
            return true;
        else if (i == 0)
            return false;
        if (i == prices.length - 1 &amp;&amp; prices[i - 1] > prices[i])
            return true;
        else if (i == prices.length - 1)
            return false;
        if (prices[i - 1] > prices[i] &amp;&amp; prices[i + 1] > prices[i])
            return true;
        return false;
    }
全部评论

相关推荐

Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务