【leetcode】1395. Count Number of Teams

题目如下:

There are n soldiers standing in a line. Each soldier is assigned a unique rating value.

You have to form a team of 3 soldiers amongst them under the following rules:

  • Choose 3 soldiers with index (ijk) with rating (rating[i]rating[j]rating[k]).
  • A team is valid if:  (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).

Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).

Example 1:

Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).

Example 2:

Input: rating = [2,1,3]
Output: 0
Explanation: We can‘t form any team given the conditions.

Example 3:

Input: rating = [1,2,3,4]
Output: 4

Constraints:

  • n == rating.length
  • 1 <= n <= 200
  • 1 <= rating[i] <= 10^5

解题思路:这个题目不难。假设把rating[i]放在三个人的中间,我们只需要分别求出rating[i]左边比其大和比其小的元素的个数,以及分别求出rating[i]右边比其大和比其小的元素的个数,最后左边小的个数*右边大的个数+左边大的个数*右边小的个数,即为rating[i]放在三个人的中间时可以组成的排列数的总数。

代码如下:

class Solution(object):
    def numTeams(self, rating):
        """
        :type rating: List[int]
        :rtype: int
        """
        res = 0
        for i in range(1,len(rating)-1):
            left_small = 0
            left_great = 0
            for j in range(i):
                if rating[i] > rating[j]:
                    left_small += 1
                elif rating[i] < rating[j]:
                    left_great += 1

            right_small = 0
            right_great = 0
            for j in range(i+1,len(rating)):
                if rating[i] > rating[j]:
                    right_small += 1
                elif rating[i] < rating[j]:
                    right_great += 1

            res += left_small * right_great
            res += left_great * right_small
            #print rating[i],left_small,left_great,right_small,right_great
        return res

原文地址:https://www.cnblogs.com/seyjs/p/12631498.html

时间: 2024-08-07 10:07:32

【leetcode】1395. Count Number of Teams的相关文章

【leetcode】204 - Count Primes

Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity

【LeetCode】327. Count of Range Sum

题目: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the sum of the elements in nums between indices i and  j (i ≤ j), inclusive. Note: A naive algorithm of O(n2) is tr

【LeetCode】204. Count Primes 解题小结

题目: Description: Count the number of prime numbers less than a non-negative number, n. 这个题目有提示,计算素数的方法应该不用多说. class Solution { public: int countPrimes(int n) { vector<bool> isPrime; for (int i = 0; i < n; ++i){ isPrime.push_back(true); } for (int

【LeetCode】222. Count Complete Tree Nodes

Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last le

【leetcode】Valid Triangle Number

题目: Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. Example 1: Input: [2,2,3,4] Output: 3 Explanation: Valid c

【LeetCode】009 Palindrome Number

题目:LeetCode 009 Palindrome Number 题意:判断一个整数是否为回文数,不要用额外空间 思路:我不会不用额外空间的方法,需要利用一个长度为20以内的字符串.将整数先写入一个字符串,然后判断首位字符是否相等即可. 代码如下: 1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 string s = to_string(x); 5 int len = s.size(); 6 for(int i = 0;

【Leetcode】Super Ugly Number

题目链接:https://leetcode.com/problems/super-ugly-number/ 题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13

【leetcode】1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix

题目如下: Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the four neighbours of it if they exist (Flip is changing 1 to 0 and 0 to 1). A pair of cells are called neighboors if they share one edge. Return the min

【leetcode】1347. Minimum Number of Steps to Make Two Strings Anagram

题目如下: Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character. Return the minimum number of steps to make t an anagram of s. An Anagram of a string is a string that contains the same c