예제 #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;
  }
  /**
   * Creates an integer random array with numbers in the interval [0; max].
   *
   * @param len length of the array.
   * @param max maximum random value.
   * @return the array
   */
  static int[] randomArray(int len, int max) {
    assert len >= 0;
    assert max >= 0;

    int[] result = new int[len];
    for (int i = 0; i < len; i++) result[i] = (int) (Math.random() * (max + 1));
    return result;
  }
예제 #3
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);
    }
  }
  public static int basetoNum(String a, int b) {
    int c = 0;
    int i = 0;

    for (int j = a.length() - 1; j >= 0; j--) {
      c += ((Character.getNumericValue(a.charAt(j))) * (Math.pow(b, i)));
      i++;
    }

    return c;
  }
    /**
     * Gives random available port in the given range.
     *
     * @param from if <=0 then default value 49152 is used
     * @param to   if <=0 then default value 65535 is used
     */
    protected int randomLocalPort(int from, int to){
        from = (from <=0) ? 49152 : from;
        to = (to <= 0) ? 65535 : to;


        while(true){
            int candidate = (int) ((Math.random() * (to-from)) + from);
            if(isFreePort(candidate)){
                return candidate;
            }
            System.out.println(String.format("Port %s is in use", candidate));
        }
    }
예제 #6
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];
  }
예제 #7
0
  private void scannerLoop() throws ParseException {
    Scanner reader = new Scanner(in);
    String line = reader.nextLine();
    while (!line.equals("startScheduling")) {
      String[] params = line.split("\\(|,( )*|\\)");
      switch (params[0]) {
        case "create":
          if (params.length == 4) {
            User temp = find(params[1]);
            if (temp == null) {
              User user =
                  new User(params[1], Integer.parseInt(params[2]), Boolean.parseBoolean(params[3]));
              users.add(user);
            } else {
              out.print("Error: user with this name already exist");
            }
          } else {
            out.print("Error: wrong arguments");
          }
          break;
        case "modify":
          if (params.length == 4) {
            User temp = find(params[1]);
            if (!(temp == null)) {
              temp.setName(params[1]);
              temp.setTimezone(Integer.parseInt(params[2]));
              temp.setStatus(Boolean.parseBoolean(params[3]));
            } else {
              out.print("Error: user with this name not found");
            }
          } else {
            out.print("Error: wrong arguments");
          }
          break;
        case "addEvent":
          if (params.length == 4) {
            User temp = find(params[1]);
            if (!(temp == null)) {
              Pattern pattern =
                  Pattern.compile(
                      "(0?[1-9]|[12][0-9]|3[01])\\.(0?[1-9]|1[012])\\.((19|20)\\d\\d)\\-([01]?[0-9]|2[0-3])\\:[0-5][0-9]\\:[0-5][0-9]");
              Matcher matcher = pattern.matcher(params[3]);
              if (matcher.matches()) {
                DateFormat df = new SimpleDateFormat("dd.MM.yyyy'-'HH:mm:ss");
                Date result = df.parse(params[3]);
                result.setTime(
                    result.getTime() + (defaultTimezone - temp.getTimezone()) * 3600 * 1000);
                Event event = new Event(result, params[2]);
                temp.addEvent(event);
              } else {
                out.print("Error: wrong dateformat");
              }
            } else {
              out.print("Error: user with this name not found");
            }
          } else {
            out.print("Error: wrong arguments");
          }
          break;
        case "addRandomTimeEvent":
          if (params.length == 5) {
            User temp = find(params[1]);
            if (!(temp == null)) {
              Pattern pattern =
                  Pattern.compile(
                      "(0?[1-9]|[12][0-9]|3[01])\\.(0?[1-9]|1[012])\\.((19|20)\\d\\d)\\-([01]?[0-9]|2[0-3])\\:[0-5][0-9]\\:[0-5][0-9]");
              Matcher matcherFrom = pattern.matcher(params[3]);
              Matcher matcherTo = pattern.matcher(params[4]);
              if (matcherFrom.matches() && matcherTo.matches()) {
                long dateFrom = Timestamp.valueOf(params[3]).getTime();
                long dateTo = Timestamp.valueOf(params[4]).getTime();
                long diff = dateFrom - dateTo + 1;

                Event event =
                    new Event(new Date(dateFrom + (long) (Math.random() * diff)), params[2]);
                temp.addEvent(event);
              } else {
                out.print("Error: wrong dateformat");
              }
            } else {
              out.print("Error: user with this name not found");
            }
          } else {
            out.print("Error: wrong arguments");
          }
          break;
        case "removeEvent":
          if (params.length == 3) {
            User temp = find(params[1]);
            if (!(temp == null)) {
              temp.removeEvent(params[2]);
            } else {
              out.print("Error: user with this name not found");
            }
          } else {
            out.print("Error: wrong arguments");
          }
          break;
        case "cloneEvent":
          if (params.length == 4) {
            User temp = find(params[1]);
            User tempTo = find(params[3]);
            if (!(temp == null) && !(tempTo == null)) {
              Event event = temp.findEvent(params[2]);
              tempTo.addEvent(event);
            } else {
              out.print("Error: user with this name not found");
            }
          } else {
            out.print("Error: wrong arguments");
          }
          break;
        case "showInfo":
          if (params.length == 2) {
            User temp = find(params[1]);
            if (!(temp == null)) {
              temp.showInfo();
            } else {
              out.print("Error: user with this name not found");
            }
          } else {
            out.print("Error: wrong arguments");
          }
          break;
      }
      line = reader.nextLine();
    }
  }
예제 #8
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();
  }