Skip to content

Commit 3403ed0

Browse files
committed
daily
1 parent 8fb9f1f commit 3403ed0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

my-submissions/m1415.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
# _pm_dfs stands for premature dfs
3+
def _pm_dfs(self, rem: int, n: int, output: List[str]) -> str :
4+
if not n :
5+
return ''.join(output)
6+
7+
pot_per_path = 2 ** (n - 1)
8+
(options := ['a', 'b', 'c']).remove(output[-1])
9+
10+
if rem <= pot_per_path :
11+
output.append(options[0])
12+
return self._pm_dfs(rem, n - 1, output)
13+
output.append(options[1])
14+
return self._pm_dfs(rem - pot_per_path, n - 1, output)
15+
16+
def getHappyString(self, n: int, k: int) -> str:
17+
n -= 1
18+
pot_n_min_1 = 2 ** n
19+
20+
match (k - 1) // pot_n_min_1 :
21+
case 0 :
22+
return self._pm_dfs(k, n, ['a'])
23+
case 1 :
24+
return self._pm_dfs(k - pot_n_min_1, n, ['b'])
25+
case 2 :
26+
return self._pm_dfs(k - 2 * pot_n_min_1, n, ['c'])
27+
case _ :
28+
return ''

0 commit comments

Comments
 (0)