結果

問題 No.927 Second Permutation
ユーザー simansiman
提出日時 2020-08-11 11:56:34
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 522 bytes
コンパイル時間 125 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 19,840 KB
最終ジャッジ日時 2024-04-17 17:12:00
合計ジャッジ時間 7,809 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
12,032 KB
testcase_01 AC 98 ms
12,288 KB
testcase_02 AC 97 ms
12,032 KB
testcase_03 AC 95 ms
12,032 KB
testcase_04 AC 97 ms
12,288 KB
testcase_05 AC 90 ms
12,160 KB
testcase_06 AC 98 ms
12,032 KB
testcase_07 AC 91 ms
12,160 KB
testcase_08 RE -
testcase_09 AC 107 ms
14,720 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:39: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

X = gets.to_i
D = X.to_s.size

$values = []

def dfs(stack, depth, counter)
  if depth == D
    $values << stack.join.to_i
    return
  end

  9.downto(0) do |n|
    return if $values.size >= 2

    next if depth == 0 && n == 0
    next if counter[n] == 0

    counter[n] -= 1
    stack << n

    dfs(stack, depth + 1, counter)

    counter[n] += 1
    stack.pop
  end
end

counter = Hash.new(0)

X.digits.each do |x|
  counter[x] += 1
end

dfs([], 0, counter)

if $values.size >= 2
  puts $values.last
else
  puts -1
end
0