如何找到右侧最远的较小数字?

2026-06-09 06:503阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

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

如何找到右侧最远的较小数字?

在右侧找到最靠近右边的最小数字原文:https://www.geeplus.com/在右侧找到最靠近右边的最小数字原文:https://www+.geesforgeks+.org/find-%E6%9C%80%E5%8F%B3%E8%BE%B9%E7%9A%84%E5%B0%8F%E6%95%B0%E5%AD%97/%E7%BB%99%E5%AE%9A%E4%B8%80%E4%B8%AA%E5%A4%A7%E5%B0%8F%E4%B8%BA+N+%E7%9A%84%E6%95%B0%E7%BB%84+arr%5B%5D+%E3%80%82%E5%AF%B9%E4%BA%8E%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%AF%8F%E4%B8%AA%E5%85%83%E7%B4%A0%EF%BC%8C%E4%BB%BB%E5%8A%A1%E2%80%9D

在右侧找到最远的较小数字原文: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)

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

如何找到右侧最远的较小数字?

在右侧找到最靠近右边的最小数字原文:https://www.geeplus.com/在右侧找到最靠近右边的最小数字原文:https://www+.geesforgeks+.org/find-%E6%9C%80%E5%8F%B3%E8%BE%B9%E7%9A%84%E5%B0%8F%E6%95%B0%E5%AD%97/%E7%BB%99%E5%AE%9A%E4%B8%80%E4%B8%AA%E5%A4%A7%E5%B0%8F%E4%B8%BA+N+%E7%9A%84%E6%95%B0%E7%BB%84+arr%5B%5D+%E3%80%82%E5%AF%B9%E4%BA%8E%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%AF%8F%E4%B8%AA%E5%85%83%E7%B4%A0%EF%BC%8C%E4%BB%BB%E5%8A%A1%E2%80%9D

在右侧找到最远的较小数字原文: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)