Skip to content

Commit f45815d

Browse files
committed
Handle negative exponents properly
1 parent 4191192 commit f45815d

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/combinatorics/bracket_sequences.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,15 @@ string kth_balanced2(int n, int k) {
206206
}
207207

208208
string ans;
209-
int depth = 0;
209+
int shift, depth = 0;
210+
210211
stack<char> st;
211212
for (int i = 0; i < 2*n; i++) {
213+
212214
// '('
213-
if (depth + 1 <= n) {
214-
int cnt = d[2*n-i-1][depth+1] << ((2*n-i-1-depth-1) / 2);
215+
shift = ((2*n-i-1-depth-1) / 2);
216+
if (shift >= 0 && depth + 1 <= n) {
217+
int cnt = d[2*n-i-1][depth+1] << shift;
215218
if (cnt >= k) {
216219
ans += '(';
217220
st.push('(');
@@ -222,8 +225,9 @@ string kth_balanced2(int n, int k) {
222225
}
223226

224227
// ')'
225-
if (depth && st.top() == '(') {
226-
int cnt = d[2*n-i-1][depth-1] << ((2*n-i-1-depth+1) / 2);
228+
shift = ((2*n-i-1-depth+1) / 2);
229+
if (shift >= 0 && depth && st.top() == '(') {
230+
int cnt = d[2*n-i-1][depth-1] << shift;
227231
if (cnt >= k) {
228232
ans += ')';
229233
st.pop();
@@ -234,8 +238,9 @@ string kth_balanced2(int n, int k) {
234238
}
235239
236240
// '['
237-
if (depth + 1 <= n) {
238-
int cnt = d[2*n-i-1][depth+1] << ((2*n-i-1-depth-1) / 2);
241+
shift = ((2*n-i-1-depth-1) / 2);
242+
if (shift >= 0 && depth + 1 <= n) {
243+
int cnt = d[2*n-i-1][depth+1] << shift;
239244
if (cnt >= k) {
240245
ans += '[';
241246
st.push('[');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy