- Date : 2022.02.27(월)
- Time : 20분
- A permutation perm of n + 1 integers of all the integers in the range [0, n] can be represented as a string s of length n where:
- s[i] == 'I' if perm[i] < perm[i + 1], and
- s[i] == 'D' if perm[i] > perm[i + 1].
- Given a string s, reconstruct the permutation perm and return it. If there are multiple valid permutations perm, return any of them.
- 1 <= s.length <= 10^5
- s[i] is either 'I' or 'D'.
- Input: s = "IDID"
- Output: [0,4,1,3,2]
def diStringMatch(self, s: str) -> List[int]:
answer = []
i,d = 0, len(s)
for x in range(len(s)):
if s[x] == "I":
answer.append(i)
i += 1
elif s[x] == "D":
answer.append(d)
d -= 1
if s[-1] == "I": answer.append(i)
else : answer.append(d)
return answer
: 심플한 문제지만 마지막에 헷갈리는 문제! 일단 s의 문자는 무조건 D 아니면 I로 이뤄져있다. I는 그 다음 위치의 숫자보다 작아야한다. D는 그 다음 위치의 숫자보다 커야한다. 그렇기 때문에 제일 최소 최대값으로 처음 값을 설정해주고 +1, -1를 진행해주었다. 여기까지는 쉬웠는데.. 분명 길이가 4인데 답은 5개의 숫자가 있는것이다. 그래서 마지막 숫자가 대체 뭔가 많이 생각했다. 근데 조건에서 보면 길이는 n+1이다. 마지막도 조건때문에 있는 것이다. I는 그 다음 위치보다 작아야 한다고 했다! 그렇기 때문에 마지막에 가상의 비교대상을 넣어줘야 하는 것이다. 이거때문에 한 5분 생각했다.