Skip to content

Commit eab0800

Browse files
committed
풀이: 릿코드.1689.Partitioning Into Minimum Number Of Deci-Binary Numbers
?: 순회를 이용해 풀이
1 parent c8cdf04 commit eab0800

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
2+
3+
[링크](https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/description/)
4+
5+
| 난이도 |
6+
| :----: |
7+
| Medium |
8+
9+
## 설계
10+
11+
### 시간 복잡도
12+
13+
문자열의 길이를 N이라 하자.
14+
15+
순회하며 각 문자중 가장 큰 숫자를 찾는 경우 순회에 (N)의 시간 복잡도를 사용한다.
16+
17+
### 공간 복잡도
18+
19+
순회에 O(1) 의 공간 복잡도를 사용한다.
20+
21+
### 순회
22+
23+
| 내 코드 (ms) | 시간 복잡도 | 공간 복잡도 |
24+
| :----------: | :---------: | :---------: |
25+
| 0 | O(N) | O(1) |
26+
27+
0, 1로만 이루어진 숫자를 최소한의 갯수로 더해서 N을 만들어야 한다.
28+
29+
이 때 각 자리수별로 존재하는 숫자가 1로 이루어진 숫자를 더할 수 있다.
30+
31+
이를 반복할 때 결국 각 자리수에 존재하는 가장 큰 숫자만큼 1로 이루어진 숫자를 더해야 한다.
32+
33+
```cpp
34+
int minPartitions(string n) {
35+
int answer = 0;
36+
for (char& c : n) {
37+
answer = max(answer, c - '0');
38+
}
39+
return answer;
40+
}
41+
```
42+
43+
## 고생한 점
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <algorithm>
2+
#include <climits>
3+
#include <cmath>
4+
#include <cstring>
5+
#include <functional>
6+
#include <iostream>
7+
#include <map>
8+
#include <numeric>
9+
#include <queue>
10+
#include <set>
11+
#include <stack>
12+
#include <string>
13+
#include <unordered_map>
14+
#include <unordered_set>
15+
#include <vector>
16+
17+
using namespace std;
18+
19+
// one pass
20+
// time : O(N)
21+
// space : O(1)
22+
class Solution {
23+
public:
24+
int minPartitions(string n) {
25+
int answer = 0;
26+
for (char& c : n) {
27+
answer = max(answer, c - '0');
28+
}
29+
return answer;
30+
}
31+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Input: n = "32"
2+
Output: 3
3+
4+
===
5+
6+
Input: n = "82734"
7+
Output: 8
8+
9+
===
10+
11+
Input: n = "27346209830709182346"
12+
Output: 9

0 commit comments

Comments
 (0)