给定一个长度为N的字符串S ,它由‘x’和‘.’组成。 .给定的字符串代表一排座位,其中‘x’和‘.’分别代表已占席位和未占席位。任务是最大限度地减少跳跃或跳跃的总数,以使所有乘员坐在一起,即彼此相邻,而在他们之间没有任何空位。
注意:由于跳跃次数可能非常大,请打印答案模10 9 + 7 。
例子:
Input: S = “. . . . x . . x x . . . x . .”
Output: 5
Explanation: Below are the required shuffling of occupants:
Step 1: Make the person at 5th seat jump 2 places to the 7th seat.
Step 2: Make the person at 13th seat jump 3 places to the 10th seat.
Therefore, total number of jumps required = 2 + 3 = 5.
Input: S = “x x x . . . . . . . . x x x x x x”
Output: 24
Explanation: Move the occupants from 1st, 2nd and 3rd position to the 9th, 10th, 11th positions respectively. Therefore, the total number of jumps required = (11 – 3) + (10 – 2) + (9 – 3) = 24.
方法:这个想法是使用贪心方法来解决这个问题。请注意,将元素移向人员中的中间元素或所有在场人员中的中心人员始终是最佳的。当我们将点移到中位数时,跳跃次数总是最少的。以下是步骤:
- 初始化一个向量位置来存储在场人员的索引。
- 找到向量position[]的中位数。现在将让所有其他人坐在这个人周围,因为这将提供所需的最少跳跃次数。
- 初始化一个变量ans来存储所需的最小跳转。
- 现在,遍历向量位置[]和为每一个索引i找到中值元素和更新ANS为:
ans= ans+ abs(position[i] – medianElement)
- 经过以上步骤,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
long long int MOD = 1e9 + 7;
// Function to find the minimum jumps
// required to make the whole group
// sit adjacently
int minJumps(string seats)
{
// Store the indexes
vector position;
// Stores the count of occupants
int count = 0;
// Length of the string
int len = seats.length();
// Traverse the seats
for (int i = 0; i < len; i++) {
// If current place is occupied
if (seats[i] == 'x') {
// Push the current position
// in the vector
position.push_back(i - count);
count++;
}
}
// Base Case:
if (count == len || count == 0)
return 0;
// The index of the median element
int med_index = (count - 1) / 2;
// The value of the median element
int med_val = position[med_index];
int ans = 0;
// Traverse the position[]
for (int i = 0;
i < position.size(); i++) {
// Update the ans
ans = (ans % MOD
+ abs(position[i]
- med_val)
% MOD)
% MOD;
}
// Return the final count
return ans % MOD;
}
// Driver Code
int main()
{
// Given arrange of seats
string S = "....x..xx...x..";
// Function Call
cout << minJumps(S);
return 0;
}
Java
// Java program for the
// above approach
import java.util.*;
class GFG{
static int MOD = (int)1e9 + 7;
// Function to find the minimum
// jumps required to make the
// whole group sit adjacently
static int minJumps(String seats)
{
// Store the indexes
Vector position =
new Vector<>();
// Stores the count of
// occupants
int count = 0;
// Length of the String
int len = seats.length();
// Traverse the seats
for (int i = 0; i < len; i++)
{
// If current place is occupied
if (seats.charAt(i) == 'x')
{
// Push the current position
// in the vector
position.add(i - count);
count++;
}
}
// Base Case:
if (count == len ||
count == 0)
return 0;
// The index of the median
// element
int med_index = (count - 1) / 2;
// The value of the median
// element
int med_val = position.get(med_index);
int ans = 0;
// Traverse the position[]
for (int i = 0;
i < position.size(); i++)
{
// Update the ans
ans = (ans % MOD +
Math.abs(position.get(i) -
med_val) % MOD) % MOD;
}
// Return the final count
return ans % MOD;
}
// Driver Code
public static void main(String[] args)
{
// Given arrange of seats
String S = "....x..xx...x..";
// Function Call
System.out.print(minJumps(S));
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program for the above approach
MOD = 10**9 + 7
# Function to find the minimum jumps
# required to make the whole group
# sit adjacently
def minJumps(seats):
# Store the indexes
position = []
# Stores the count of occupants
count = 0
# Length of the string
lenn = len(seats)
# Traverse the seats
for i in range(lenn):
# If current place is occupied
if (seats[i] == 'x'):
# Push the current position
# in the vector
position.append(i - count)
count += 1
# Base Case:
if (count == lenn or count == 0):
return 0
# The index of the median element
med_index = (count - 1) // 2
# The value of the median element
med_val = position[med_index]
ans = 0
# Traverse the position[]
for i in range(len(position)):
# Update the ans
ans = (ans % MOD +
abs(position[i] - med_val)
% MOD) % MOD
# Return the final count
return ans % MOD
# Driver Code
if __name__ == '__main__':
# Given arrange of seats
S = "....x..xx...x.."
# Function Call
print(minJumps(S))
# This code is contributed by mohit kumar 29
C#
// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG{
static int MOD = (int)1e9 + 7;
// Function to find the minimum
// jumps required to make the
// whole group sit adjacently
static int minJumps(String seats)
{
// Store the indexes
List position =
new List();
// Stores the count of
// occupants
int count = 0;
// Length of the String
int len = seats.Length;
// Traverse the seats
for (int i = 0; i < len; i++)
{
// If current place is
// occupied
if (seats[i] == 'x')
{
// Push the current
// position in the
// vector
position.Add(i - count);
count++;
}
}
// Base Case:
if (count == len ||
count == 0)
return 0;
// The index of the median
// element
int med_index = (count - 1) / 2;
// The value of the median
// element
int med_val = position[med_index];
int ans = 0;
// Traverse the position[]
for (int i = 0;
i < position.Count; i++)
{
// Update the ans
ans = (ans % MOD +
Math.Abs(position[i] -
med_val) % MOD) % MOD;
}
// Return the readonly
// count
return ans % MOD;
}
// Driver Code
public static void Main(String[] args)
{
// Given arrange of seats
String S = "....x..xx...x..";
// Function Call
Console.Write(minJumps(S));
}
}
// This code is contributed by Amit Katiyar
Javascript
5
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。