[InterviewBit] Seats

Seats

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
long long mod = 10000003;
int Solution::seats(string A) {
int n = A.size();
if(n == 1) return 0;
vector<long long> dp(n), dpp(n);
for(long long i = 0, now = 0; i < n; i++) {
if(i) dp[i] = dp[i - 1];
if(A[i] == 'x') now += 1;
else dp[i] += now;
}
for(long long i = n - 1, now = 0; i >= 0; i--) {
if(i + 1 != n) dpp[i] = dpp[i + 1];
if(A[i] == 'x') now += 1;
else dpp[i] += now;
}
long long res = LLONG_MAX;
for(int i = 1; i < n; i++) res = min(res, dp[i-1] + dpp[i]);
return res % mod;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/07/PS/interviewbit/seats/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.