読者です 読者をやめる 読者になる 読者になる

TopCoder SRM 695 Div1 Easy: BearPasswordLexic

もうだめ

コード

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;

public class BearPasswordLexic {
  private String gen(int lenght, int isa) {
    String s = "";
    for (int i = 0; i < lenght; i++) {
      if (isa % 2 == 1) s += 'a';
      else s += 'b';
    }
    return s;
  }

  public String findPassword(int[] x) {
    final int N = x.length;
    int[] y = new int[N];

    ArrayList<Integer> list = new ArrayList<>();
    for (int n = N - 1; n >= 0; n--) {
      while (y[n] < x[n]) {
        list.add(n + 1);
        for (int i = n; i >= 0; i--) y[i] += (n + 1 - i);
      }
    }

    for (int i = 0; i < N; i++) {
      if (x[i] != y[i]) return "";
    }

    Collections.sort(list);
    ArrayDeque<Integer> deque = new ArrayDeque<>(list);
    String s = "";
    int isa = 1;
    while ((!deque.isEmpty())) {
      if (isa % 2 == 1) {
        s += gen(deque.pollLast(), isa++);
      } else {
        s += gen(deque.pollFirst(), isa++);
      }
    }
    if (s.length() != N) return "";
    return s;
  }
}