给定一个大小为N的数组arr[] ,任务是为每个数组索引找到最小的缺失非负整数,直到给定数组的该索引。
例子:
Input: arr[] = {1, 3, 0, 2}
Output: 0 0 2 4
Explanation:
Smallest missing non-negative integer from index 0 to 0 is 0.
Smallest missing non-negative integer from index 0 to 1 is 0.
Smallest missing non-negative integer from index 0 to 2 is 2.
Smallest missing non-negative integer from index 0 to 3 is 4.
Input: arr[] = {0, 1, 2, 3, 5}
Output: 1 2 3 4 4
方法:这个问题可以使用Hashing来解决。请按照以下步骤解决问题:
- 初始化一个变量,比如smNonNeg来存储给定数组的起始索引和当前索引之间的最小缺失非负整数。
- 初始化一个数组,比如hash[N]来检查smNonNeg 是否存在于起始索引和当前索引之间。
- 遍历给定的数组并检查hash[smNonNeg] 是否等于0 。如果发现为真,则打印smNonNeg的值。
- 否则,当hash[smNonNeg]不等于0 时,增加smNonNeg的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print the smallest
// missing non-negative integer
// up to every array indices
void smlstNonNeg(int arr[], int N)
{
// Stores the smallest missing
// non-negative integers between
// start index to current index
int smNonNeg = 0;
// Store the boolean value to check
// smNonNeg present between start
// index to each index of the array
bool hash[N + 1] = { 0 };
// Traverse the array
for (int i = 0; i < N; i++) {
// Since output always lies
// in the range[0, N - 1]
if (arr[i] >= 0 and arr[i] < N) {
hash[arr[i]] = true;
}
// Check if smNonNeg is
// present between start index
// and current index or not
while (hash[smNonNeg]) {
smNonNeg++;
}
// Print smallest missing
// non-negative integer
cout << smNonNeg << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 0, 1, 2, 3, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
smlstNonNeg(arr, N);
}
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.Arrays;
class GFG{
// Function to print the smallest
// missing non-negative integer
// up to every array indices
static void smlstNonNeg(int arr[], int N)
{
// Stores the smallest missing
// non-negative integers between
// start index to current index
int smNonNeg = 0;
// Store the boolean value to check
// smNonNeg present between start
// index to each index of the array
Boolean[] hash = new Boolean[N + 1];
Arrays.fill(hash, false);
// Traverse the array
for(int i = 0; i < N; i++)
{
// Since output always lies
// in the range[0, N - 1]
if (arr[i] >= 0 && arr[i] < N)
{
hash[arr[i]] = true;
}
// Check if smNonNeg is
// present between start index
// and current index or not
while (hash[smNonNeg])
{
smNonNeg++;
}
// Print smallest missing
// non-negative integer
System.out.print(smNonNeg + " ");
}
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 0, 1, 2, 3, 5 };
int N = arr.length;
smlstNonNeg(arr, N);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program to implement
# the above approach
# Function to prthe smallest
# missing non-negative integer
# up to every array indices
def smlstNonNeg(arr, N):
# Stores the smallest missing
# non-negative integers between
# start index to current index
smNonNeg = 0
# Store the boolean value to check
# smNonNeg present between start
# index to each index of the array
hash = [0] * (N + 1)
# Traverse the array
for i in range(N):
# Since output always lies
# in the range[0, N - 1]
if (arr[i] >= 0 and arr[i] < N):
hash[arr[i]] = True
# Check if smNonNeg is
# present between start index
# and current index or not
while (hash[smNonNeg]):
smNonNeg += 1
# Print smallest missing
# non-negative integer
print(smNonNeg, end = " ")
# Driver Code
if __name__ == '__main__':
arr = [ 0, 1, 2, 3, 5 ]
N = len(arr)
smlstNonNeg(arr, N)
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to print the smallest
// missing non-negative integer
// up to every array indices
static void smlstNonNeg(int[] arr, int N)
{
// Stores the smallest missing
// non-negative integers between
// start index to current index
int smNonNeg = 0;
// Store the boolean value to check
// smNonNeg present between start
// index to each index of the array
bool[] hash = new bool[N + 1];
for(int i = 0; i < N; i++)
{
hash[i] = false;
}
// Traverse the array
for(int i = 0; i < N; i++)
{
// Since output always lies
// in the range[0, N - 1]
if (arr[i] >= 0 && arr[i] < N)
{
hash[arr[i]] = true;
}
// Check if smNonNeg is
// present between start index
// and current index or not
while (hash[smNonNeg])
{
smNonNeg++;
}
// Print smallest missing
// non-negative integer
Console.Write(smNonNeg + " ");
}
}
// Driver Code
public static void Main ()
{
int[] arr = { 0, 1, 2, 3, 5 };
int N = arr.Length;
smlstNonNeg(arr, N);
}
}
// This code is contributed by code_hunt
Javascript
输出:
1 2 3 4 4
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。