Skip to content

Commit 922136d

Browse files
committed
feat: solve No.2844
1 parent 351313f commit 922136d

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# 2844. Minimum Operations to Make a Special Number
2+
3+
- Difficulty: Medium.
4+
- Related Topics: .
5+
- Similar Questions: Remove K Digits, Remove Digit From Number to Maximize Result.
6+
7+
## Problem
8+
9+
You are given a **0-indexed** string `num` representing a non-negative integer.
10+
11+
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`.
12+
13+
Return **the **minimum number of operations** required to make** `num` *special*.
14+
15+
An integer `x` is considered **special** if it is divisible by `25`.
16+
17+
 
18+
Example 1:
19+
20+
```
21+
Input: num = "2245047"
22+
Output: 2
23+
Explanation: Delete digits num[5] and num[6]. The resulting number is "22450" which is special since it is divisible by 25.
24+
It can be shown that 2 is the minimum number of operations required to get a special number.
25+
```
26+
27+
Example 2:
28+
29+
```
30+
Input: num = "2908305"
31+
Output: 3
32+
Explanation: Delete digits num[3], num[4], and num[6]. The resulting number is "2900" which is special since it is divisible by 25.
33+
It can be shown that 3 is the minimum number of operations required to get a special number.
34+
```
35+
36+
Example 3:
37+
38+
```
39+
Input: num = "10"
40+
Output: 1
41+
Explanation: Delete digit num[0]. The resulting number is "0" which is special since it is divisible by 25.
42+
It can be shown that 1 is the minimum number of operations required to get a special number.
43+
44+
```
45+
46+
 
47+
**Constraints:**
48+
49+
50+
51+
- `1 <= num.length <= 100`
52+
53+
- `num` only consists of digits `'0'` through `'9'`.
54+
55+
- `num` does not contain any leading zeros.
56+
57+
58+
59+
## Solution
60+
61+
```javascript
62+
/**
63+
* @param {string} num
64+
* @return {number}
65+
*/
66+
var minimumOperations = function(num) {
67+
var min = num.length;
68+
if (num.includes('0')) min = num.length - 1;
69+
var nums = ['25', '50', '75', '00'];
70+
for (var i = 0; i < nums.length; i++) {
71+
var m = 0;
72+
var count = 0;
73+
for (var j = num.length - 1; j >= 0; j--) {
74+
if (num[j] === nums[i][nums[i].length - 1 - m]) {
75+
m++;
76+
if (m === nums[i].length) {
77+
min = Math.min(min, count);
78+
}
79+
} else {
80+
count++;
81+
}
82+
}
83+
}
84+
return min;
85+
};
86+
```
87+
88+
**Explain:**
89+
90+
nope.
91+
92+
**Complexity:**
93+
94+
* Time complexity : O(n).
95+
* Space complexity : O(1).

0 commit comments

Comments
 (0)