Skip to content

Latest commit

 

History

History
208 lines (171 loc) · 5.19 KB

File metadata and controls

208 lines (171 loc) · 5.19 KB

English Version

题目描述

给你一个下标从 0 开始的整数数组 nums 。对于每个下标 i1 <= i <= nums.length - 2),nums[i]美丽值 等于:

  • 2,对于所有 0 <= j < ii < k <= nums.length - 1 ,满足 nums[j] < nums[i] < nums[k]
  • 1,如果满足 nums[i - 1] < nums[i] < nums[i + 1] ,且不满足前面的条件
  • 0,如果上述条件全部不满足

返回符合 1 <= i <= nums.length - 2 的所有 nums[i] 美丽值的总和

 

示例 1:

输入:nums = [1,2,3]
输出:2
解释:对于每个符合范围 1 <= i <= 1 的下标 i :
- nums[1] 的美丽值等于 2

示例 2:

输入:nums = [2,4,6,4]
输出:1
解释:对于每个符合范围 1 <= i <= 2 的下标 i :
- nums[1] 的美丽值等于 1
- nums[2] 的美丽值等于 0

示例 3:

输入:nums = [3,2,1]
输出:0
解释:对于每个符合范围 1 <= i <= 1 的下标 i :
- nums[1] 的美丽值等于 0

 

提示:

  • 3 <= nums.length <= 105
  • 1 <= nums[i] <= 105

解法

Python3

class Solution:
    def sumOfBeauties(self, nums: List[int]) -> int:
        n = len(nums)
        lmx = [0] * n
        for i in range(1, n):
            lmx[i] = max(lmx[i - 1], nums[i - 1])
        rmi = [100001] * n
        for i in range(n - 2, -1, -1):
            rmi[i] = min(rmi[i + 1], nums[i + 1])
        ans = 0
        for i in range(1, n - 1):
            if lmx[i] < nums[i] < rmi[i]:
                ans += 2
            elif nums[i - 1] < nums[i] < nums[i + 1]:
                ans += 1
        return ans

Java

class Solution {
    public int sumOfBeauties(int[] nums) {
        int n = nums.length;
        int[] lmx = new int[n];
        int[] rmi = new int[n];
        rmi[n - 1] = 100001;
        for (int i = 1; i < n; ++i) {
            lmx[i] = Math.max(lmx[i - 1], nums[i - 1]);
        }
        for (int i = n - 2; i >= 0; --i) {
            rmi[i] = Math.min(rmi[i + 1], nums[i + 1]);
        }
        int ans = 0;
        for (int i = 1; i < n - 1; ++i) {
            if (lmx[i] < nums[i] && nums[i] < rmi[i]) {
                ans += 2;
            } else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {
                ans += 1;
            }
        }
        return ans;
    }
}

**TypeScript

function sumOfBeauties(nums: number[]): number {
    let n = nums.length;
    let prefix = new Array(n),
        postfix = new Array(n);
    prefix[0] = nums[0];
    postfix[n - 1] = nums[n - 1];
    for (let i = 1, j = n - 2; i < n; ++i, --j) {
        prefix[i] = Math.max(nums[i], prefix[i - 1]);
        postfix[j] = Math.min(nums[j], postfix[j + 1]);
    }
    let ans = 0;
    for (let i = 1; i < n - 1; ++i) {
        if (prefix[i - 1] < nums[i] && nums[i] < postfix[i + 1]) {
            ans += 2;
        } else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {
            ans += 1;
        }
    }
    return ans;
}

C++

class Solution {
public:
    int sumOfBeauties(vector<int>& nums) {
        int n = nums.size();
        vector<int> lmx(n);
        vector<int> rmi(n, 100001);
        for (int i = 1; i < n; ++i) lmx[i] = max(lmx[i - 1], nums[i - 1]);
        for (int i = n - 2; i >= 0; --i) rmi[i] = min(rmi[i + 1], nums[i + 1]);
        int ans = 0;
        for (int i = 1; i < n - 1; ++i) {
            if (lmx[i] < nums[i] && nums[i] < rmi[i])
                ans += 2;
            else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1])
                ans += 1;
        }
        return ans;
    }
};

Go

func sumOfBeauties(nums []int) int {
	n := len(nums)
	lmx := make([]int, n)
	rmi := make([]int, n)
	rmi[n-1] = 100001
	for i := 1; i < n; i++ {
		lmx[i] = max(lmx[i-1], nums[i-1])
	}
	for i := n - 2; i >= 0; i-- {
		rmi[i] = min(rmi[i+1], nums[i+1])
	}
	ans := 0
	for i := 1; i < n-1; i++ {
		if lmx[i] < nums[i] && nums[i] < rmi[i] {
			ans += 2
		} else if nums[i-1] < nums[i] && nums[i] < nums[i+1] {
			ans += 1
		}
	}
	return ans
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

...