📜  Stream 中所有元素到达时按降序排列

📅  最后修改于: 2021-09-06 06:42:17             🧑  作者: Mango

给定一个数字流arr ,任务是在流中每个元素到达时按降序找到它们的排名。

例子:

天真的方法:

  1. 数组的第一个元素将始终具有排名 1。
  2. 遍历数组,如果该元素在前面的元素中最大,则其秩为 1。
  3. 如果元素不大于先前比较的元素。那么这个元素的排名将是它之前的更大元素的数量。
    例如,假设它大于 2 个先前的元素,则其等级为 3。

下面是上述方法的实现:

C++
// C++ program to rank of all elements
// in a Stream in descending order
// when they arrive
#include 
using namespace std;
     
    // FindRank function to find rank
        void FindRank(int arr[], int length)
    {
        // Rank of first element is always 1
        cout << "1" <<  " ";
         
        // Iterate over array
        for (int i = 1; i < length; i++)
        {
            // As element let say its rank is 1
            int rank = 1;
     
            // Element is compared
            // with previous elements
            for (int j = 0; j < i; j++)
            {
                // If greater than previous
                // than rank is incremented
                if(arr[j] > arr[i])
                    rank++;
                 
            }
             
            // print rank
            cout <<  rank  << " ";
        }
    }
 
 
 
 
// Driver code
int main() {
     
        // array named arr
        int arr[] = {88, 14, 69, 30, 29, 89};
     
        // length of arr
        int len = sizeof(arr)/sizeof(arr[0]);
     
        FindRank(arr, len);
         
    return 0;
}
 
// This code is contributed by AnkitRai01


Java
// Java program to rank of all elements
// in a Stream in descending order
// when they arrive
import java.util.*;
 
class GFG{
     
    // FindRank function to find rank
    static void FindRank(int arr[], int length)
    {
        // Rank of first element is always 1
        System.out.print("1" + " ");
         
        // Iterate over array
        for (int i = 1; i < arr.length; i++)
        {
            // As element let say its rank is 1
            int rank = 1;
     
            // Element is compared
            // with previous elements
            for (int j = 0; j < i; j++)
            {
                // If greater than previous
                // than rank is incremented
                if(arr[j] > arr[i])
                    rank++;
                 
            }
             
            // print rank
            System.out.print(rank + " ");
        }
    }
 
    // Driver code
    public static void main(String args[]){
         
        // array named arr
        int arr[] = {88, 14, 69, 30, 29, 89};
     
        // length of arr
        int len = arr.length;
     
        FindRank(arr, len);
    }
}
 
// This code is contributed by AbhiThakur


Python3
# Python program to rank of all elements
# in a Stream in descending order
# when they arrive
 
# FindRank function to find rank
def FindRank(arr, length):
     
    # Rank of first element is always 1
    print(1, end =" ")
     
    # Iterate over array
    for i in range(1, length):
         
        # As element let say its rank is 1
        rank = 1
 
        # Element is compared
        # with previous elements
        for j in range(0, i):
             
            # If greater than previous
            # than rank is incremented
            if(arr[j] > arr[i]):
                rank = rank + 1
                 
        # print rank
        print(rank, end =" ")
         
         
# Driver code
if __name__ == '__main__':
     
    # array named arr
    arr = [88, 14, 69, 30, 29, 89]
 
    # length of arr
    length = len(arr)
 
    FindRank(arr, length)


C#
// C# program to rank of all elements
// in a Stream in descending order
// when they arrive
using System;
 
class GFG{
     
    // FindRank function to find rank
    static void FindRank(int[] arr, int length)
    {
        // Rank of first element is always 1
        Console.Write("1" + " ");
         
        // Iterate over array
        for (int i = 1; i < arr.Length; i++)
        {
            // As element let say its rank is 1
            int rank = 1;
     
            // Element is compared
            // with previous elements
            for (int j = 0; j < i; j++)
            {
                // If greater than previous
                // than rank is incremented
                if(arr[j] > arr[i])
                    rank++;
                 
            }
             
            // print rank
            Console.Write(rank + " ");
        }
    }
 
    // Driver code
    public static void Main(){
         
        // array named arr
        int[] arr = {88, 14, 69, 30, 29, 89};
     
        // length of arr
        int len = arr.Length;
     
        FindRank(arr, len);
    }
}
 
// This code is contributed by AbhiThakur


Javascript


Python3
# Python program to rank of all elements
# in a Stream in descending order
# when they arrive
 
# import of bisect to use bisect.insort()
import bisect
 
# FindRank function to find rank
def FindRank(arr, length):
    # rank list to store values of
    # array in ascending order
    rank = []
 
    first = arr[0]
    rank.append(first)
    # Rank of first element is always 1
    print(1, end =" ")
     
    # Iterate over remaining array
    for i in range(1, length):
        val = arr[i]
         
        # element inserted in the rank list
        # using the binary method
         
        bisect.insort(rank, val)
         
        # To find rank of that element
         
        # length of rank array - index of element
        # in rank array
        eleRank = len(rank)-rank.index(val)
         
        # print of rank of element of array
        print(eleRank, end =" ")
 
# Driver code
if __name__ == '__main__':
     
    # array named arr
    arr = [88, 14, 69, 30, 29, 89]
 
    # lenght of arr
    length = len(arr)
 
    FindRank(arr, length)


输出:
1 2 2 3 4 1

时间复杂度: O(N 2 ) ,其中 N 是数组的长度。
空间复杂度: O(1)
高效的方法:这个想法是使用二分搜索来找到数字的排名。它将给出大于给定数字的数字计数。
下面是上述方法的实现:

蟒蛇3

# Python program to rank of all elements
# in a Stream in descending order
# when they arrive
 
# import of bisect to use bisect.insort()
import bisect
 
# FindRank function to find rank
def FindRank(arr, length):
    # rank list to store values of
    # array in ascending order
    rank = []
 
    first = arr[0]
    rank.append(first)
    # Rank of first element is always 1
    print(1, end =" ")
     
    # Iterate over remaining array
    for i in range(1, length):
        val = arr[i]
         
        # element inserted in the rank list
        # using the binary method
         
        bisect.insort(rank, val)
         
        # To find rank of that element
         
        # length of rank array - index of element
        # in rank array
        eleRank = len(rank)-rank.index(val)
         
        # print of rank of element of array
        print(eleRank, end =" ")
 
# Driver code
if __name__ == '__main__':
     
    # array named arr
    arr = [88, 14, 69, 30, 29, 89]
 
    # lenght of arr
    length = len(arr)
 
    FindRank(arr, length)
输出:
1 2 2 3 4 1

时间复杂度: O(N * log N),其中 N 是数组的长度。
空间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live