#include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
int n = S.length();
int dp[n + 1];
memset(dp, 0x3f, sizeof dp);
dp[0] = 0;
for (int i = 0; i < n; i++) {
int digit = S[i] - '0';
for (int j = max(0, i - 2); j < i; j++) {
int sum = 0;
for (int k = j; k <= i; k++) {
sum += S[k] - '0';
}
if (sum % 3 == 0) {
int cost = digit;
for (int k = j; k <= i; k++) {
cost = min(cost, S[k] - '0');
//@Coding_000
}
dp[i + 1] = min(dp[i + 1], dp[j] + cost);
}
}
}
cout << dp[n] << endl;
return 0;
}
C++
INFOSYS EXAM ANS 10AM
Share @Coding_000✅
using namespace std;
int main() {
string S;
cin >> S;
int n = S.length();
int dp[n + 1];
memset(dp, 0x3f, sizeof dp);
dp[0] = 0;
for (int i = 0; i < n; i++) {
int digit = S[i] - '0';
for (int j = max(0, i - 2); j < i; j++) {
int sum = 0;
for (int k = j; k <= i; k++) {
sum += S[k] - '0';
}
if (sum % 3 == 0) {
int cost = digit;
for (int k = j; k <= i; k++) {
cost = min(cost, S[k] - '0');
//@Coding_000
}
dp[i + 1] = min(dp[i + 1], dp[j] + cost);
}
}
}
cout << dp[n] << endl;
return 0;
}
C++
INFOSYS EXAM ANS 10AM
Share @Coding_000✅
👍1
def shortest_subarray(colors, C):
n = len(colors)
color_count = [0] * (C+1)
left, right = 0, 0
min_length = float('inf')
count = 0
while right < n:
color_count[colors[right]] += 1
if color_count[colors[right]] == 1:
count += 1
while count == C:
min_length = min(min_length, right - left + 1)
color_count[colors[left]] -= 1
if color_count[colors[left]] == 0:
count -= 1
left += 1
right += 1
if min_length == float('inf'):
return -1
else:
return min_length
Python3 👆👆👆
n = len(colors)
color_count = [0] * (C+1)
left, right = 0, 0
min_length = float('inf')
count = 0
while right < n:
color_count[colors[right]] += 1
if color_count[colors[right]] == 1:
count += 1
while count == C:
min_length = min(min_length, right - left + 1)
color_count[colors[left]] -= 1
if color_count[colors[left]] == 0:
count -= 1
left += 1
right += 1
if min_length == float('inf'):
return -1
else:
return min_length
Python3 👆👆👆
👍1
Share our channel guys...fast...✅✅
👍1
Take screenshot of my channel and share to large groups if u need more answers❤️😍
👍2