【每日一题20220311】最高利润

:man_mage:给定一个表示股票价格的列表,请编写一个函数,返回由其中最小值和最大值组成的新列表,从而可以获取最大利润。

示例:
输入: [1,2,3,4,5]
输出: [1,5]
解释:[1,2,3,4,5]中最小数是1,最大数是5

题目难度:简单
题目来源:CodeWars:The highest profit wins!

def solution(prices: list) -> list:
    # your code here

assert solution([1,2,3,4,5]) == [1, 5]
assert solution([2334454,5]) == [5, 2334454]
assert solution([1]) == [1, 1]
return [min(price), max(price)] if price else []

能麻烦问一下,后面的if else的作用是什么
我是小白,不太懂这个地方的含义

if … else 条件判断,等价于:

if price:  # 如果price列表不为空
    return [min(price), max(price)]
else:
    return []   
    def solution(prices: list) -> list:
        mi,ma = prices[0],prices[0]
        for i in prices:
            if i < mi:mi = i
            if i > ma:ma = i
        return [mi,ma]

好的,谢谢

class MinMax {
    public static int[] minMax(int[] arr) {
        // Your awesome code here
      int len = arr.length-1;
      quickSort(arr,0,len);
      return new int[]{arr[0],arr[len]};
    }
  
    static void quickSort(int s[], int l, int r)
    {
        if (l < r)
        {
            int i = l, j = r, x = s[l];
            while (i < j)
            {
                while(i < j && s[j] >= x) 
                    j--;
                if(i < j)
                    s[i++] = s[j];
                while(i < j && s[i] <= x)
                    i++;
                if(i < j)
                    s[j--] = s[i];
            }
            s[i] = x;
            quickSort(s, l, i - 1);
            quickSort(s, i + 1, r);
        }
    }
}
def solution(prices: list) -> list:
    return [min(prices), max(prices)]


assert solution([1, 2, 3, 4, 5]) == [1, 5]
assert solution([2334454, 5]) == [5, 2334454]
assert solution([1]) == [1, 1]
def solution(prices: list) -> list:
    # your code here
    return [min(prices), max(prices)] if prices else [ ]

assert solution([1, 2, 3, 4, 5]) == [1, 5]
assert solution([2334454, 5]) == [5, 2334454]
assert solution([1]) == [1, 1]
assert solution([]) ==[]
def solution(prices: list) -> list:
    # 0311 最高利润
    return [min(prices), max(prices)] if prices else []

assert solution([1,2,3,4,5]) == [1, 5]
assert solution([2334454,5]) == [5, 2334454]
assert solution([1]) == [1, 1]
assert solution([]) == []

def solution(prices: list) -> list:
    return [min(prices),max(prices)]
assert solution([1,2,3,4,5]) == [1, 5]
assert solution([2334454,5]) == [5, 2334454]
assert solution([1]) == [1, 1]