Skip to content

Latest commit

 

History

History
200 lines (169 loc) · 4.99 KB

File metadata and controls

200 lines (169 loc) · 4.99 KB

中文文档

Description

You are given a 0-indexed integer array nums. For each index i (1 <= i <= nums.length - 2) the beauty of nums[i] equals:

  • 2, if nums[j] < nums[i] < nums[k], for all 0 <= j < i and for all i < k <= nums.length - 1.
  • 1, if nums[i - 1] < nums[i] < nums[i + 1], and the previous condition is not satisfied.
  • 0, if none of the previous conditions holds.

Return the sum of beauty of all nums[i] where 1 <= i <= nums.length - 2.

 

Example 1:

Input: nums = [1,2,3]
Output: 2
Explanation: For each index i in the range 1 <= i <= 1:
- The beauty of nums[1] equals 2.

Example 2:

Input: nums = [2,4,6,4]
Output: 1
Explanation: For each index i in the range 1 <= i <= 2:
- The beauty of nums[1] equals 1.
- The beauty of nums[2] equals 0.

Example 3:

Input: nums = [3,2,1]
Output: 0
Explanation: For each index i in the range 1 <= i <= 1:
- The beauty of nums[1] equals 0.

 

Constraints:

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

Solutions

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
}

...