给定一个由N 个元素组成的数组arr[] (0 ≤ arr[i] ≤ 1000)。任务是找到只包含丑数的子数组的最大长度。丑数是唯一质因数为2 、 3或5 的数。
序列1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …..显示了前几个丑陋的数字。按照惯例,包括 1。
例子:
Input: arr[] = {1, 2, 7, 9, 120, 810, 374}
Output: 3
The Longest possible sub-array of ugly number sis {9, 120, 810}
Input: arr[] = {109, 480, 320, 142, 121, 1}
Output: 2
方法:
- 取一个 unordered_set,将所有小于 1000 的丑数插入集合中。
- 使用两个名为current_max和max_so_far 的变量遍历数组。
- 检查每个元素是否存在于集合中。
- 如果发现一个丑陋的数字,则增加 current_max 并将其与 max_so_far 进行比较。
- 如果current_max > max_so_far则max_so_far = current_max 。
- 每次发现非丑元素时,重置current_max = 0 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to get the nth ugly number
unsigned uglyNumber(int n)
{
// To store ugly numbers
int ugly[n];
int i2 = 0, i3 = 0, i5 = 0;
int next_multiple_of_2 = 2;
int next_multiple_of_3 = 3;
int next_multiple_of_5 = 5;
int next_ugly_no = 1;
ugly[0] = 1;
for (int i = 1; i < n; i++) {
next_ugly_no = min(next_multiple_of_2,
min(next_multiple_of_3,
next_multiple_of_5));
ugly[i] = next_ugly_no;
if (next_ugly_no == next_multiple_of_2) {
i2 = i2 + 1;
next_multiple_of_2 = ugly[i2] * 2;
}
if (next_ugly_no == next_multiple_of_3) {
i3 = i3 + 1;
next_multiple_of_3 = ugly[i3] * 3;
}
if (next_ugly_no == next_multiple_of_5) {
i5 = i5 + 1;
next_multiple_of_5 = ugly[i5] * 5;
}
}
return next_ugly_no;
}
// Function to return the length of the
// maximum sub-array of ugly numbers
int maxUglySubarray(int arr[], int n)
{
unordered_set s;
int i = 1;
// Insert ugly numbers in set
// which are less than 1000
while (1) {
int next_ugly_number = uglyNumber(i);
if (next_ugly_number > 1000)
break;
s.insert(next_ugly_number);
i++;
}
int current_max = 0, max_so_far = 0;
for (int i = 0; i < n; i++) {
// Check if element is non ugly
if (s.find(arr[i]) == s.end())
current_max = 0;
// If element is ugly, than update
// current_max and max_so_far accordingly
else {
current_max++;
max_so_far = max(current_max, max_so_far);
}
}
return max_so_far;
}
// Driver code
int main()
{
int arr[] = { 1, 0, 6, 7, 320, 800, 100, 648 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxUglySubarray(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to get the nth ugly number
static int uglyNumber(int n)
{
// To store ugly numbers
int []ugly = new int[n];
int i2 = 0, i3 = 0, i5 = 0;
int next_multiple_of_2 = 2;
int next_multiple_of_3 = 3;
int next_multiple_of_5 = 5;
int next_ugly_no = 1;
ugly[0] = 1;
for (int i = 1; i < n; i++)
{
next_ugly_no = Math.min(next_multiple_of_2,
Math.min(next_multiple_of_3,
next_multiple_of_5));
ugly[i] = next_ugly_no;
if (next_ugly_no == next_multiple_of_2)
{
i2 = i2 + 1;
next_multiple_of_2 = ugly[i2] * 2;
}
if (next_ugly_no == next_multiple_of_3)
{
i3 = i3 + 1;
next_multiple_of_3 = ugly[i3] * 3;
}
if (next_ugly_no == next_multiple_of_5)
{
i5 = i5 + 1;
next_multiple_of_5 = ugly[i5] * 5;
}
}
return next_ugly_no;
}
// Function to return the length of the
// maximum sub-array of ugly numbers
static int maxUglySubarray(int arr[], int n)
{
HashSet s = new HashSet<>();
int i = 1;
// Insert ugly numbers in set
// which are less than 1000
while (true)
{
int next_ugly_number = uglyNumber(i);
if (next_ugly_number > 1000)
break;
s.add(next_ugly_number);
i++;
}
int current_max = 0, max_so_far = 0;
for (i = 0; i < n; i++)
{
// Check if element is non ugly
if (!s.contains(arr[i]))
current_max = 0;
// If element is ugly, than update
// current_max and max_so_far accordingly
else
{
current_max++;
max_so_far = Math.max(current_max,
max_so_far);
}
}
return max_so_far;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 0, 6, 7, 320, 800, 100, 648 };
int n = arr.length;
System.out.println(maxUglySubarray(arr, n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python 3 implementation of the approach
# Function to get the nth ugly number
def uglyNumber(n):
# To store ugly numbers
ugly = [None for i in range(n)]
i2 = 0
i3 = 0
i5 = 0
next_multiple_of_2 = 2
next_multiple_of_3 = 3
next_multiple_of_5 = 5
next_ugly_no = 1
ugly[0] = 1
for i in range(1, n, 1):
next_ugly_no = min(next_multiple_of_2,
min(next_multiple_of_3,
next_multiple_of_5))
ugly[i] = next_ugly_no
if (next_ugly_no == next_multiple_of_2):
i2 = i2 + 1
next_multiple_of_2 = ugly[i2] * 2
if (next_ugly_no == next_multiple_of_3):
i3 = i3 + 1
next_multiple_of_3 = ugly[i3] * 3
if (next_ugly_no == next_multiple_of_5):
i5 = i5 + 1
next_multiple_of_5 = ugly[i5] * 5
return next_ugly_no
# Function to return the length of the
# maximum sub-array of ugly numbers
def maxUglySubarray(arr, n):
s = set()
i = 1
# Insert ugly numbers in set
# which are less than 1000
while (1):
next_ugly_number = uglyNumber(i)
if (next_ugly_number >= 1000):
break
s.add(next_ugly_number)
i += 1
current_max = 0
max_so_far = 0
for i in range(n):
# Check if element is non ugly
if (arr[i] not in s):
current_max = 0
# If element is ugly, than update
# current_max and max_so_far accordingly
else:
current_max += 1
max_so_far = max(current_max,
max_so_far)
return max_so_far
# Driver code
if __name__ == '__main__':
arr = [1, 0, 6, 7, 320, 800, 100, 648]
n = len(arr)
print(maxUglySubarray(arr, n))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to get the nth ugly number
static int uglyNumber(int n)
{
// To store ugly numbers
int []ugly = new int[n];
int i2 = 0, i3 = 0, i5 = 0;
int next_multiple_of_2 = 2;
int next_multiple_of_3 = 3;
int next_multiple_of_5 = 5;
int next_ugly_no = 1;
ugly[0] = 1;
for (int i = 1; i < n; i++)
{
next_ugly_no = Math.Min(next_multiple_of_2,
Math.Min(next_multiple_of_3,
next_multiple_of_5));
ugly[i] = next_ugly_no;
if (next_ugly_no == next_multiple_of_2)
{
i2 = i2 + 1;
next_multiple_of_2 = ugly[i2] * 2;
}
if (next_ugly_no == next_multiple_of_3)
{
i3 = i3 + 1;
next_multiple_of_3 = ugly[i3] * 3;
}
if (next_ugly_no == next_multiple_of_5)
{
i5 = i5 + 1;
next_multiple_of_5 = ugly[i5] * 5;
}
}
return next_ugly_no;
}
// Function to return the length of the
// maximum sub-array of ugly numbers
static int maxUglySubarray(int []arr, int n)
{
HashSet s = new HashSet();
int i = 1;
// Insert ugly numbers in set
// which are less than 1000
while (true)
{
int next_ugly_number = uglyNumber(i);
if (next_ugly_number > 1000)
break;
s.Add(next_ugly_number);
i++;
}
int current_max = 0, max_so_far = 0;
for (i = 0; i < n; i++)
{
// Check if element is non ugly
if (!s.Contains(arr[i]))
current_max = 0;
// If element is ugly, than update
// current_max and max_so_far accordingly
else
{
current_max++;
max_so_far = Math.Max(current_max,
max_so_far);
}
}
return max_so_far;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 0, 6, 7, 320, 800, 100, 648 };
int n = arr.Length;
Console.WriteLine(maxUglySubarray(arr, n));
}
}
// This code is contributed by Princi Singh
Javascript
输出:
4
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。