-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathreplace-question-marks-in-string-to-minimize-its-value.py
120 lines (109 loc) · 3.24 KB
/
replace-question-marks-in-string-to-minimize-its-value.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Time: O(n + 26 * log(26))
# Space: O(26)
# greedy, counting sort, prefix sum
class Solution(object):
def minimizeStringValue(self, s):
"""
:type s: str
:rtype: str
"""
def counting_sort(cnt):
for i in xrange(len(cnt)):
for _ in xrange(cnt[i]):
yield i
def fill(cnt):
result = [0]*26
a = [(x, i) for i, x in enumerate(cnt)]
a.sort()
total = s.count('?')
curr = 0
for i in xrange(len(a)-1):
if curr+(a[i+1][0]-a[i][0])*(i+1) > total:
break
curr += (a[i+1][0]-a[i][0])*(i+1)
else:
i = len(a)-1
q, r = divmod(total-curr, i+1)
for j in xrange(i+1):
result[a[j][1]] = (a[i][0]-a[j][0])+q
cnt2 = [0]*26
for j in xrange(i+1):
cnt2[a[j][1]] += 1
it = counting_sort(cnt2)
for _ in xrange(r):
result[next(it)] += 1
return result
cnt = [0]*26
for x in s:
if x == '?':
continue
cnt[ord(x)-ord('a')] += 1
it = counting_sort(fill(cnt))
result = list(s)
for i in xrange(len(result)):
if result[i] != '?':
continue
result[i] = chr(ord('a')+next(it))
return "".join(result)
# Time: O(n + n * log(26))
# Space: O(26)
import heapq
# greedy, counting sort, heap
class Solution2(object):
def minimizeStringValue(self, s):
"""
:type s: str
:rtype: str
"""
def counting_sort(cnt):
for i in xrange(len(cnt)):
for _ in xrange(cnt[i]):
yield i
cnt = [0]*26
for x in s:
if x == '?':
continue
cnt[ord(x)-ord('a')] += 1
min_heap = [(x, i) for i, x in enumerate(cnt)]
heapq.heapify(min_heap)
cnt2 = [0]*26
for _ in xrange(s.count('?')):
c, i = heapq.heappop(min_heap)
heapq.heappush(min_heap, (c+1, i))
cnt2[i] += 1
it = counting_sort(cnt2)
result = list(s)
for i in xrange(len(result)):
if result[i] != '?':
continue
result[i] = chr(ord('a')+next(it))
return "".join(result)
# Time: O(n + n * 26)
# Space: O(26)
# greedy, counting sort
class Solution3(object):
def minimizeStringValue(self, s):
"""
:type s: str
:rtype: str
"""
def counting_sort(cnt):
for i in xrange(len(cnt)):
for _ in xrange(cnt[i]):
yield i
cnt = [0]*26
for x in s:
if x == '?':
continue
cnt[ord(x)-ord('a')] += 1
cnt2 = [0]*26
for _ in xrange(s.count('?')):
i = min(xrange(len(cnt)), key=lambda x: cnt[x]+cnt2[x])
cnt2[i] += 1
it = counting_sort(cnt2)
result = list(s)
for i in xrange(len(result)):
if result[i] != '?':
continue
result[i] = chr(ord('a')+next(it))
return "".join(result)