活跃农民
- 积分
- 405
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-10-12
- 最后登录
- 1970-1-1
|
Sep 2
220:
Medium
bucket sort
358
# HARD
# Heap
# Input: s = "aaadbbcc", k = 2
# Output: "abacabcd"
# 1. find the frequency of each char in the input and store em in a heap
# ==> a: 3, b:2, c:2, d: 1
# 2. pop the heap till its empty
# ==>
# while heap is not empty
# pop the heap k times. since here k == 2, we popped a,b
# store the popped value to result, and a tmp
# after k pops:
# decrement each t-- in [tmp],
# if the frequency of t is still >0, push it back
# eg. =>
# 1st: a: 3, b:2, c:2, d: 1
# a,b
# 2nd: a:2, c:2, b:1, d:1
# a,c
# 3rd: a:1, b:1, c:1, d:1
# a,b
# 4th: c:1, d:1
# c,d
# result => a,b,a,c,a,b,c,d
294:
MEDIUM
# recursion
# recursive rule:
# s[i-1] == s[i] == "+" and flipped s[i-1:i+1] + rest of string are not flippable
316
# check if we have it in stack using set
if c not in appear:
# if current char has lower value and there is one more stack[-1]
# at the following string, then we have to pop the previous one
while stack and c< stack[-1] and i < lastOccur[stack[-1]]: |
|