結果

問題 No.22 括弧の対応
ユーザー n3k2t1n3k2t1
提出日時 2019-07-10 22:38:24
言語 JavaScript
(node v21.7.1)
結果
AC  
実行時間 72 ms / 5,000 ms
コード長 628 bytes
コンパイル時間 63 ms
コンパイル使用メモリ 5,212 KB
実行使用メモリ 42,444 KB
最終ジャッジ日時 2023-09-27 13:39:12
合計ジャッジ時間 2,565 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
42,196 KB
testcase_01 AC 70 ms
42,328 KB
testcase_02 AC 71 ms
42,148 KB
testcase_03 AC 72 ms
42,372 KB
testcase_04 AC 70 ms
42,280 KB
testcase_05 AC 70 ms
42,444 KB
testcase_06 AC 68 ms
42,164 KB
testcase_07 AC 68 ms
42,356 KB
testcase_08 AC 67 ms
42,364 KB
testcase_09 AC 71 ms
42,192 KB
testcase_10 AC 67 ms
42,368 KB
testcase_11 AC 65 ms
42,224 KB
testcase_12 AC 65 ms
42,264 KB
testcase_13 AC 65 ms
42,216 KB
testcase_14 AC 63 ms
42,216 KB
testcase_15 AC 68 ms
42,196 KB
testcase_16 AC 65 ms
42,396 KB
testcase_17 AC 65 ms
42,172 KB
testcase_18 AC 65 ms
42,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

const lines = require('fs')
  .readFileSync('/dev/stdin', 'utf-8')
  .trim().split('\n').values();

const [N, K] = lines.next().value.split(' ').map(x => Number(x));
const S = lines.next().value;

const bracket = [];
const position = [];
let ans;

bracket.push(S[0]);
position.push(0);

for (let i = 1; i < N; i++) {
  if (S[i] === ')' && bracket[bracket.length - 1] === '(') {
    bracket.pop();
    let p = position.pop();
    if (p === K - 1) {
      ans = i + 1;
      break;
    }
    if (i === K - 1) {
      ans = p + 1;
      break;
    }
  } else {
    bracket.push(S[i]);
    position.push(i);
  }
}

console.log(ans);
0