Exemplo n.º 1
0
  private static int[] candy(int[] ratings) {
    int[] candy = new int[ratings.length];
    Arrays.fill(candy, 1);

    // increasing
    for (int i = 1; i < ratings.length; i++) {
      if (ratings[i] > ratings[i - 1]) {
        candy[i] = candy[i - 1] + 1;
      }
    }

    // decreasing
    for (int j = ratings.length - 2; j >= 0; j--) {
      if (ratings[j] > ratings[j + 1]) {
        if (((j - 1 >= 0)) && (ratings[j - 1] < ratings[j])) {
          candy[j] = Math.max(candy[j], candy[j - 1] + 1);
        } else {
          candy[j] = candy[j + 1] + 1;
        }
      }
    }

    // int sum = 0;
    // for(int k=0; k<candy.length; k++) sum+=candy[k];

    return candy;
  }
Exemplo n.º 2
0
  public static int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
    if (triangle == null) return 0;

    int[] sum = new int[triangle.size() + 1];

    for (int i = triangle.size() - 1; i >= 0; i--) {
      for (int j = 0; j < triangle.get(i).size(); j++) {
        List currentList = triangle.get(i);
        sum[j] = (Integer) (currentList.get(j)) + Math.min(sum[j], sum[j + 1]);
      }

      for (int x = 0; x < sum.length; x++) System.out.print(sum[x] + " ");
      System.out.println();
    }

    return sum[0];
  }