Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int minimumOperations(string num) {
- if(num.length()==1)
- {
- return 1;
- }
- int cz=0;
- for(int i=0;i<num.length();i++)
- {
- if(num[i]=='0')
- {
- cz++;
- }
- }
- bool f=true;
- for(int i=0;i<num.length()-1;i++)
- {
- for(int j=i+1;j<num.length();j++)
- {
- string pair = string(1,num[i]) + string(1,num[j]);
- if (pair == "00" || pair == "25" || pair == "50" || pair == "75") {
- f=false;
- }
- }
- }
- if(f)
- {
- return num.length()-cz;
- }
- int n = num.size();
- int min_del = INT_MAX;
- min_del = min(min_del, n - 1);
- for (int i = 0; i < n - 1; i++) {
- for (int j = i + 1; j < n; j++) {
- string pair = string(1,num[i]) + string(1,num[j]);
- int deletions;
- if(i+1==j)
- {
- deletions = n-j-1;
- }
- else
- {
- deletions = n-i-2;
- }
- if (pair == "00" || pair == "25" || pair == "50" || pair == "75") {
- min_del = min(min_del, deletions);
- }
- }
- }
- return min_del;
- }
- };
- // 8040. Minimum Operations to Make a Special Number
- User Accepted: 6883
- User Tried: 9492
- Total Accepted: 7035
- Total Submissions: 22544
- Difficulty: Medium
- You are given a 0-indexed string num representing a non-negative integer.
- In one operation, you can pick any digit of num and delete it. Note that if you delete all the digits of num, num becomes 0.
- Return the minimum number of operations required to make num special.
- An integer x is considered special if it is divisible by 25.
Advertisement
Add Comment
Please, Sign In to add comment