LeetCode 1081. Smallest Subsequence of Distinct Characters
考点 | 难度 |
---|---|
Greedy | Easy |
题目
Given a string s, return the
lexicographically smallest
subsequence
of s that contains all the distinct characters of s exactly once.
思路
对每个字母找到最后一次出现的index。用stack记录result,如果input里的一个character小于stack里的最后一个character,而且后续有stack里的最后一个character,pop最后一个character,加入input里的这个。
答案
class Solution(object):
def findKthPositive(self, arr, k):
left = 0
right = len(arr)
while left < right:
middle = (left + right) // 2
if arr[middle] - middle - 1 < k:
left = middle + 1
else:
right = middle
return left + k