Exemplo n.º 1
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];
  }
Exemplo n.º 2
0
  /**
   * Read block from file.
   *
   * @param file - File to read.
   * @param off - Marker position in file to start read from if {@code -1} read last blockSz bytes.
   * @param blockSz - Maximum number of chars to read.
   * @param lastModified - File last modification time.
   * @return Read file block.
   * @throws IOException In case of error.
   */
  public static VisorFileBlock readBlock(File file, long off, int blockSz, long lastModified)
      throws IOException {
    RandomAccessFile raf = null;

    try {
      long fSz = file.length();
      long fLastModified = file.lastModified();

      long pos = off >= 0 ? off : Math.max(fSz - blockSz, 0);

      // Try read more that file length.
      if (fLastModified == lastModified && fSz != 0 && pos >= fSz)
        throw new IOException(
            "Trying to read file block with wrong offset: " + pos + " while file size: " + fSz);

      if (fSz == 0)
        return new VisorFileBlock(file.getPath(), pos, fLastModified, 0, false, EMPTY_FILE_BUF);
      else {
        int toRead = Math.min(blockSz, (int) (fSz - pos));

        byte[] buf = new byte[toRead];

        raf = new RandomAccessFile(file, "r");

        raf.seek(pos);

        int cntRead = raf.read(buf, 0, toRead);

        if (cntRead != toRead)
          throw new IOException(
              "Count of requested and actually read bytes does not match [cntRead="
                  + cntRead
                  + ", toRead="
                  + toRead
                  + ']');

        boolean zipped = buf.length > 512;

        return new VisorFileBlock(
            file.getPath(), pos, fSz, fLastModified, zipped, zipped ? zipBytes(buf) : buf);
      }
    } finally {
      U.close(raf, null);
    }
  }
Exemplo n.º 3
0
  public static void main(String[] args) throws IOException {
    int i, j, k, a, b, c, d, e;
    String s, s1, s2;
    String[] t;

    boolean done = false;
    TreeMap<String, ArrayList<String>> genre = new TreeMap<>();
    TreeMap<String, ArrayList<String>> author = new TreeMap<>();
    ArrayList<String> list = new ArrayList<>();
    ArrayList<String> temp = new ArrayList<>();
    ArrayList<String> temp2 = new ArrayList<>();
    ArrayList<String> purchased = new ArrayList<>();

    Scanner input = new Scanner(new File("alsopurchased.dat"));
    PrintWriter output = new PrintWriter(new File("alsopurchased.out"));

    for (i = 0; i < 100; i++) {
      s = input.nextLine().trim();
      t = s.split("[*]");
      if (genre.get(t[2]) == null) {
        list = new ArrayList<String>();
        list.add(t[0]);
        genre.put(t[2], list);
      } else {
        list = genre.get(t[2]);
        list.add(t[0]);
        genre.put(t[2], list);
      }

      if (author.get(t[1]) == null) {
        list = new ArrayList<String>();
        list.add(t[0]);
        author.put(t[1], list);

      } else {
        list = author.get(t[1]);
        list.add(t[0]);
        author.put(t[1], list);
      }
    }

    //	out.println(genre+"\n\n");
    //	out.println(author);
    s = input.nextLine().trim();
    a = Integer.parseInt(s);

    for (b = 0; b < a; b++) {
      s = input.nextLine().trim();
      temp = new ArrayList<String>();
      temp2 = new ArrayList<String>();
      purchased = new ArrayList<String>();

      out.println(s);
      output.println(s);
      out.println("Customers also purchased:");
      output.println("Customers also purchased:");

      for (String ss : author.keySet()) {
        list = author.get(ss);
        if (list.contains(s)) {
          temp = author.get(ss);
          break;
        }
      }

      temp.remove(s);
      Collections.sort(temp);
      // out.println("   --"+temp);

      for (String ss : genre.keySet()) {
        list = genre.get(ss);
        if (list.contains(s)) {
          temp2 = genre.get(ss);
          break;
        }
      }

      temp2.remove(s);
      Collections.sort(temp2);
      // out.println("    --"+temp2);
      c = temp.size();
      if (c >= 3) {
        for (d = 0; d < 3; d++) {
          purchased.add(temp.get(d));
          // out.println(temp.get(d));
          // output.println(temp.get(d));

        }
      } else {
        for (d = 0; d < temp.size(); d++) {
          purchased.add(temp.get(d));
          // out.println(temp.get(d));
          // output.println(temp.get(d));
        }
        // out.println(" -- temp2.size()"+temp2.size());
        e = Math.min(3 - temp.size(), temp2.size());
        // e=3-temp.size();
        // out.println("C,E == "+c+","+e);
        for (d = 0; d < e; d++) {
          if (!purchased.contains(temp2.get(d))) purchased.add(temp2.get(d));

          // out.println(temp2.get(d));
          // output.println(temp2.get(d));
        }
      }
      for (String ss : purchased) {
        out.println(ss);
        output.println(ss);
      }

      out.println();
      output.println();
    }

    input.close();
    output.close();
  }