Code Monkey home page Code Monkey logo

Comments (3)

PisecesPeng avatar PisecesPeng commented on September 26, 2024

解题思路

  • 遍历数组, 记录最大最小值的差值(能被覆盖).

代码

private static int func(int[] ints) {
    int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
    int result = 0;
    for (int i = 0; i < ints.length; i++) {
        int v = ints[i];
        // 判定最大最小值
        if (min > v) {
            min = v;
            max = min;  // 最小值刷新时, 重制最大值
        } else if (max < v) {
            max = v;
            result = (result < (max - min)) ? (max - min) : result;  // 最大值刷新时, 重新判断最大利润
        }
    }
    return result;
}

from pisecespeng.record.me.

PisecesPeng avatar PisecesPeng commented on September 26, 2024

LeetCode题解

解题思路

  • 暴力法, 找出数组中两个数字的最大差值

代码

public static int func(int prices[]) {
    int maxprofit = 0;
    for (int i = 0; i < prices.length - 1; i++) {
        for (int j = i + 1; j < prices.length; j++) {
            int profit = prices[j] - prices[i];
            if (profit > maxprofit) {
                maxprofit = profit;
            }
        }
    }
    return maxprofit;
}

from pisecespeng.record.me.

PisecesPeng avatar PisecesPeng commented on September 26, 2024

LeetCode题解

解题思路

  • 遍历一次, 记录历史最低点价格, 且每天都计算以此价格卖出之后的收益.

代码

public static int func(int prices[]) {
    int minprice = Integer.MAX_VALUE;
    int maxprofit = 0;
    for (int i = 0; i < prices.length; i++) {
        if (prices[i] < minprice) {
            minprice = prices[i];
        } else if (prices[i] - minprice > maxprofit) {
            maxprofit = prices[i] - minprice;
        }
    }
    return maxprofit;
}

from pisecespeng.record.me.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.