ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อหาเวลาที่ใช้ในการส่งสัญญาณไปถึงตำแหน่งทั้งหมดในสตริง
สำหรับสิ่งนี้ เราจะได้รับสตริงที่มี 'x' และ 'o' สัญญาณมาจาก 'x' และเดินทางทั้งสองทิศทางโดยเปลี่ยนค่าหนึ่ง 'o' ในหนึ่งหน่วยเวลา งานของเราคือการคำนวณเวลาที่สมบูรณ์ในการแปลงสตริงทั้งหมดให้เป็น 'x'
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
//calculating the total required time
int findMaximumDuration(string s, int n) {
int right = 0, left = 0;
int count = 0, maximumLength = INT_MIN;
s = s + '1';
for (int i = 0; i <= n; i++) {
if (s[i] == 'o')
count++;
else {
if (count > maximumLength) {
right = 0;
left = 0;
if (s[i] == 'x')
right = 1;
if (((i - count) > 0) && (s[i - count - 1] == 'x'))
left = 1;
count = ceil((double)count / (right + left));
maximumLength = max(maximumLength, count);
}
count = 0;
}
}
return maximumLength;
}
int main() {
string str = "xooxoooxxoooxoooxooxooox";
int length = str.size();
cout << findMaximumDuration(str, length);
return 0;
} ผลลัพธ์
2