在右侧,哪个数字比其他所有数字都小,能改写成长尾词吗?

2026-04-02 00:541阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1021个文字,预计阅读时间需要5分钟。

在右侧,哪个数字比其他所有数字都小,能改写成长尾词吗?

在右侧找到最远的较小数字原文:https://www.gee+plus.com/在右侧找到最远的较小数字原文:https://www+.+geesforgeks+.+org/find-最右边的数字/给定一个大小为N的数组arr[]。对于数组中的每个元素,任务是将该元素与N相加。

在右侧找到最远的较小数字原文:www.gee 在右侧找到最远的较小数字

原文:www . geesforgeks . org/find-最右边的小数字/

给定一个大小为 N 的数组 arr[] 。对于数组中的每个元素,任务是找到数组中最右边的元素的索引,它比当前元素小。如果没有这样的号码,则打印 -1 。

示例:

输入: arr[] = {3,1,5,2,4}输出: 3 -1 4 -1 -1arr[3]是 arr[0]右边最远的最小元素。arr[4]是 arr[2]右边最远的最小元素。对于其余的元素,它们的右边没有更小的元素。

输入: arr[] = {1,2,3,4,0}输出: 4 4 4 4 -1

方法:一种有效的方法是创建一个后缀 _min[] 数组,其中后缀 _min[i] 存储子数组arr[I…N–1]中的最小元素。现在对于任何元素 arr[i] ,二分搜索法可以在子阵列后缀 _ min[I+1…N–1]上使用,以找到 arr[i] 右侧最远的最小元素。

下面是上述方法的实现:

C++

// C++ implementation of the approach#include using namespace std;// Function to find the farthest// smaller number in the right sidevoid farthest_min(int a[], int n){    // To store minimum element    // in the range i to n    int suffix_min[n];    suffix_min[n - 1] = a[n - 1];    for (int i = n - 2; i >= 0; i--) {        suffix_min[i] = min(suffix_min[i + 1], a[i]);    }    for (int i = 0; i Java 语言(一种计算机语言,尤用于创建网站)

// Java implementation of the approachclass GFG {    // Function to find the farthest    // smaller number in the right side    static void farthest_min(int[] a, int n)    {        // To store minimum element        // in the range i to n        int[] suffix_min = new int[n];        suffix_min[n - 1] = a[n - 1];        for (int i = n - 2; i >= 0; i--) {            suffix_min[i]                = Math.min(suffix_min[i + 1], a[i]);        }        for (int i = 0; i Python 3

# Python3 implementation of the approach# Function to find the farthest# smaller number in the right sidedef farthest_min(a, n):    # To store minimum element    # in the range i to n    suffix_min = [0 for i in range(n)]    suffix_min[n - 1] = a[n - 1]    for i in range(n - 2, -1, -1):        suffix_min[i] = min(suffix_min[i + 1], a[i])    for i in range(n):        low = i + 1        high = n - 1        ans = -1        while (low <= high):            mid = (low + high) // 2            # If current element in the suffix_min            # is less than a[i] then move right            if (suffix_min[mid]                 ans = mid                low = mid + 1            else:                high = mid - 1        # Print the required answer        print(ans, end=" ")# Driver codea = [3, 1, 5, 2, 4]n = len(a)farthest_min(a, n)# This code is contributed by Mohit Kumar

C

// C# implementation of the approachusing System;class GFG {    // Function to find the farthest    // smaller number in the right side    static void farthest_min(int[] a, int n)    {        // To store minimum element        // in the range i to n        int[] suffix_min = new int[n];        suffix_min[n - 1] = a[n - 1];        for (int i = n - 2; i >= 0; i--) {            suffix_min[i]                = Math.Min(suffix_min[i + 1], a[i]);        }        for (int i = 0; i java 描述语言

Javascript

Output

3 -1 4 -1 -1

时间复杂度: O(N log(N) )辅助空间:* O(N)

在右侧,哪个数字比其他所有数字都小,能改写成长尾词吗?

本文共计1021个文字,预计阅读时间需要5分钟。

在右侧,哪个数字比其他所有数字都小,能改写成长尾词吗?

在右侧找到最远的较小数字原文:https://www.gee+plus.com/在右侧找到最远的较小数字原文:https://www+.+geesforgeks+.+org/find-最右边的数字/给定一个大小为N的数组arr[]。对于数组中的每个元素,任务是将该元素与N相加。

在右侧找到最远的较小数字原文:www.gee 在右侧找到最远的较小数字

原文:www . geesforgeks . org/find-最右边的小数字/

给定一个大小为 N 的数组 arr[] 。对于数组中的每个元素,任务是找到数组中最右边的元素的索引,它比当前元素小。如果没有这样的号码,则打印 -1 。

示例:

输入: arr[] = {3,1,5,2,4}输出: 3 -1 4 -1 -1arr[3]是 arr[0]右边最远的最小元素。arr[4]是 arr[2]右边最远的最小元素。对于其余的元素,它们的右边没有更小的元素。

输入: arr[] = {1,2,3,4,0}输出: 4 4 4 4 -1

方法:一种有效的方法是创建一个后缀 _min[] 数组,其中后缀 _min[i] 存储子数组arr[I…N–1]中的最小元素。现在对于任何元素 arr[i] ,二分搜索法可以在子阵列后缀 _ min[I+1…N–1]上使用,以找到 arr[i] 右侧最远的最小元素。

下面是上述方法的实现:

C++

// C++ implementation of the approach#include using namespace std;// Function to find the farthest// smaller number in the right sidevoid farthest_min(int a[], int n){    // To store minimum element    // in the range i to n    int suffix_min[n];    suffix_min[n - 1] = a[n - 1];    for (int i = n - 2; i >= 0; i--) {        suffix_min[i] = min(suffix_min[i + 1], a[i]);    }    for (int i = 0; i Java 语言(一种计算机语言,尤用于创建网站)

// Java implementation of the approachclass GFG {    // Function to find the farthest    // smaller number in the right side    static void farthest_min(int[] a, int n)    {        // To store minimum element        // in the range i to n        int[] suffix_min = new int[n];        suffix_min[n - 1] = a[n - 1];        for (int i = n - 2; i >= 0; i--) {            suffix_min[i]                = Math.min(suffix_min[i + 1], a[i]);        }        for (int i = 0; i Python 3

# Python3 implementation of the approach# Function to find the farthest# smaller number in the right sidedef farthest_min(a, n):    # To store minimum element    # in the range i to n    suffix_min = [0 for i in range(n)]    suffix_min[n - 1] = a[n - 1]    for i in range(n - 2, -1, -1):        suffix_min[i] = min(suffix_min[i + 1], a[i])    for i in range(n):        low = i + 1        high = n - 1        ans = -1        while (low <= high):            mid = (low + high) // 2            # If current element in the suffix_min            # is less than a[i] then move right            if (suffix_min[mid]                 ans = mid                low = mid + 1            else:                high = mid - 1        # Print the required answer        print(ans, end=" ")# Driver codea = [3, 1, 5, 2, 4]n = len(a)farthest_min(a, n)# This code is contributed by Mohit Kumar

C

// C# implementation of the approachusing System;class GFG {    // Function to find the farthest    // smaller number in the right side    static void farthest_min(int[] a, int n)    {        // To store minimum element        // in the range i to n        int[] suffix_min = new int[n];        suffix_min[n - 1] = a[n - 1];        for (int i = n - 2; i >= 0; i--) {            suffix_min[i]                = Math.Min(suffix_min[i + 1], a[i]);        }        for (int i = 0; i java 描述语言

Javascript

Output

3 -1 4 -1 -1

时间复杂度: O(N log(N) )辅助空间:* O(N)

在右侧,哪个数字比其他所有数字都小,能改写成长尾词吗?