Пример #1
0
  public static void main(String[] args) {

    InputStream inputStream = System.in;
    InputReader in = new InputReader(inputStream);
    Solution solver = new Solution();
    solver.solve(in);
  }
Пример #2
0
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    Solution solution = new Solution();

    int N = scanner.nextInt();
    parent = new int[N];

    for (int i = 0; i < N; i++) {
      parent[i] = i;
    }

    int M = scanner.nextInt();

    for (int i = 0; i < M; i++) {
      int x = scanner.nextInt();
      int y = scanner.nextInt();
      solution.UNION(x, y);
    }

    Set<Integer> set = new HashSet<Integer>();
    for (int i = 0; i < N; i++) {
      // IMPORTANT: need to use solution.FIND here because parent[i] might not refer to root yet
      set.add(solution.FIND(parent[i]));
    }

    System.out.println(set.size());

    scanner.close();
  }
Пример #3
0
  public static void main(String[] args) {
    Solution solution = new Solution();

    Scanner scanner = new Scanner(System.in);

    ListNode head1 = solution.buildList(scanner.nextLine());
    ListNode head2 = solution.buildList(scanner.nextLine());

    ListNode cur = head1;
    while (cur != null && head2 != null) {
      ListNode next1 = cur.next;
      ListNode next2 = head2.next;

      cur.next = head2;
      head2.next = next1;

      head2 = next2;
      cur = next1;
    }

    solution.printList(head1);
    solution.printList(head2);

    scanner.close();
  }
Пример #4
0
 public static void main(String args[]) {
   Solution sol = new Solution();
   int n = 8;
   ArrayList<String[]> ret = sol.solveNQueens(n);
   for (String[] e : ret) {
     System.out.println(Arrays.toString(e));
   }
 }
Пример #5
0
 public static void main(String[] args) {
   char[][] t = {
     {'0', '0', '1', '1', '0', '1'},
     {'0', '1', '0', '0', '0', '0'},
     {'1', '0', '1', '0', '1', '0'}
   };
   Solution sol = new Solution();
   System.out.println(sol.maximalRectangle(t));
 }
Пример #6
0
  /** @param individual */
  void updateReference(Solution individual) {
    for (int n = 0; n < problem_.getNumberOfObjectives(); n++) {
      if (individual.getObjective(n) < z_[n]) {
        z_[n] = individual.getObjective(n);

        indArray_[n] = individual;
      }
    }
  } // updateReference
Пример #7
0
  double fitnessFunction(Solution individual, double[] lambda) {
    double fitness;
    fitness = 0.0;

    if (functionType_.equals("_TCHE1")) {
      double maxFun = -1.0e+30;

      for (int n = 0; n < problem_.getNumberOfObjectives(); n++) {
        double diff = Math.abs(individual.getObjective(n) - z_[n]);

        double feval;
        if (lambda[n] == 0) {
          feval = 0.0001 * diff;
        } else {
          feval = diff * lambda[n];
        }
        if (feval > maxFun) {
          maxFun = feval;
        }
      } // for

      fitness = maxFun;
    } // if
    else if (functionType_.equals("_AGG")) {
      double sum = 0.0;
      for (int n = 0; n < problem_.getNumberOfObjectives(); n++) {
        sum += (lambda[n]) * individual.getObjective(n);
      }

      fitness = sum;

    } else if (functionType_.equals("_PBI")) {
      double d1, d2, nl;
      double theta = 5.0;

      d1 = d2 = nl = 0.0;

      for (int i = 0; i < problem_.getNumberOfObjectives(); i++) {
        d1 += (individual.getObjective(i) - z_[i]) * lambda[i];
        nl += Math.pow(lambda[i], 2.0);
      }
      nl = Math.sqrt(nl);
      d1 = Math.abs(d1) / nl;

      for (int i = 0; i < problem_.getNumberOfObjectives(); i++) {
        d2 += Math.pow((individual.getObjective(i) - z_[i]) - d1 * (lambda[i] / nl), 2.0);
      }
      d2 = Math.sqrt(d2);

      fitness = (d1 + theta * d2);
    } else {
      System.out.println("MOEAD.fitnessFunction: unknown type " + functionType_);
      System.exit(-1);
    }
    return fitness;
  } // fitnessEvaluation
Пример #8
0
  public static void main(String[] args) throws IOException {
    InputHandler ih = new InputHandler(args[0]);
    Solution sol = new Solution();

    ArrayList<int[]> inIntList = ih.getDataAsIntList();
    for (int i = 0; i < inIntList.size(); i++) {
      int n = inIntList.get(i)[0];
      System.out.println(sol.isPalindrome(n));
    }
  }
Пример #9
0
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    String[] words = {"oath", "pea", "eat", "rain"};
    char[][] board = {
      {'o', 'a', 'a', 'n'}, {'e', 't', 'a', 'e'}, {'i', 'h', 'k', 'r'}, {'i', 'f', 'l', 'v'}
    };

    Solution s = new Solution();
    List<String> result = s.findWords(board, words);
    for (int i = 0; i < result.size(); i++) System.out.println(result.get(i));
  }
Пример #10
0
 public static void main(String args[]) {
   // int num[] = {1,2};
   int num[] = {3, 1, 3, 5, 1, 1};
   // int num[] = {1, 1, 1,3, 3, 5};
   int target = 8;
   // int num[] =
   // {14,6,25,9,30,20,33,34,28,30,16,12,31,9,9,12,34,16,25,32,8,7,30,12,33,20,21,29,24,17,27,34,11,17,30,6,32,21,27,17,16,8,24,12,12,28,11,33,10,32,22,13,34,18,12};
   // int target = 27;
   Solution s = new Solution();
   ArrayList<ArrayList<Integer>> ret = s.combinationSum2(num, target);
   for (ArrayList<Integer> e : ret) {
     System.out.println(e);
   }
 }
Пример #11
0
  public static void main(String[] args) {
    Solution s = new Solution();
    char[][] grid;
    String[] strs;

    strs =
        new String[] {
          "11111011111111101011",
          "01111111111110111110",
          "10111001101111111111",
          "11110111111111111111",
          "10011111111111111111",
          "10111111011101110111",
          "01111111111101101111",
          "11111111111101111011",
          "11111111110111111111",
          "11111111111111111111",
          "01111111011111111111",
          "11111111111111111111",
          "11111111111111111111",
          "11111011111110111111",
          "10111110111011110111",
          "11111111111101111110",
          "11111111111110111100",
          "11111111111111111111",
          "11111111111111111111",
          "11111111111111111111"
        };
    grid = new char[strs.length][];
    for (int i = 0; i < strs.length; i++) {
      grid[i] = strs[i].toCharArray();
    }
    System.out.println(s.numIslands(grid));

    strs = new String[] {"1"};
    grid = new char[strs.length][];
    for (int i = 0; i < strs.length; i++) {
      grid[i] = strs[i].toCharArray();
    }
    System.out.println(s.numIslands(grid));

    strs = new String[] {"11"};
    grid = new char[strs.length][];
    for (int i = 0; i < strs.length; i++) {
      grid[i] = strs[i].toCharArray();
    }
    System.out.println(s.numIslands(grid));
  }
Пример #12
0
  public static void main(String[] args) {
    TreeLinkNode e = new TreeLinkNode(1);
    e.left = new TreeLinkNode(1);
    e.right = new TreeLinkNode(2);

    e.left.left = new TreeLinkNode(3);
    e.left.right = new TreeLinkNode(4);

    e.right.left = new TreeLinkNode(5);
    e.right.right = new TreeLinkNode(6);

    Solution s = new Solution();
    s.connect(e);
    s.printList(e.left);
    s.printList(e.left.left);
  }
Пример #13
0
 public static void main(String[] args) {
   ArrayList<Integer> num = new ArrayList<Integer>();
   // 1,2,3,4,5,6,8,9,10,7
   num.add(1);
   num.add(2);
   num.add(3);
   num.add(4);
   num.add(5);
   num.add(6);
   num.add(8);
   num.add(9);
   num.add(10);
   num.add(7);
   Solution s = new Solution();
   int res = s.kthLargestElement(10, num);
   System.out.println(res);
 }
Пример #14
0
 @Test
 public void testGetOutputSameTime() {
   Map<Integer, List<Integer>> map = new TreeMap<>();
   map.put(2, new LinkedList<Integer>(Arrays.asList(new Integer[] {1, 2, 3})));
   String actual = Solution.getOutput(map);
   String expected = "1 2 3";
   Assert.assertEquals(expected, actual);
 }
Пример #15
0
 public static HashMap<String, Float> ottieniTagUtenteConRelativoPunteggioSorted()
     throws IOException {
   HashMap<String, String> unsortedMap = new HashMap<>();
   HashMap<String, String> temp = new HashMap<>();
   HashMap<String, Float> result = new HashMap<>();
   CSVReader reader = new CSVReader(new FileReader("Output.csv"));
   String[] nextLine;
   while ((nextLine = reader.readNext()) != null) {
     if (nextLine[2].equals("tag")) {
       unsortedMap.put(nextLine[0], nextLine[3]);
     }
   }
   temp = (HashMap<String, String>) Solution.sortByValues(unsortedMap);
   for (String s : temp.keySet()) {
     result.put(s, Float.parseFloat(temp.get(s)));
   }
   result = (HashMap<String, Float>) Solution.sortByValues(result);
   System.out.println(result);
   return result;
 }
Пример #16
0
 @Test
 public void testGetOutput2() {
   Map<Integer, List<Integer>> map = new TreeMap<>();
   map.put(9, new LinkedList<Integer>(Arrays.asList(new Integer[] {1})));
   map.put(6, new LinkedList<Integer>(Arrays.asList(new Integer[] {2})));
   map.put(11, new LinkedList<Integer>(Arrays.asList(new Integer[] {3})));
   map.put(4, new LinkedList<Integer>(Arrays.asList(new Integer[] {4})));
   map.put(7, new LinkedList<Integer>(Arrays.asList(new Integer[] {5})));
   String actual = Solution.getOutput(map);
   String expected = "4 2 5 1 3";
   Assert.assertEquals(expected, actual);
 }
 @Test
 public void test1() {
   Solution sol = new IntegerBreak().new Solution();
   assertEquals(1, sol.integerBreak(2));
   assertEquals(2, sol.integerBreak(3));
   assertEquals(4, sol.integerBreak(4));
   assertEquals(6, sol.integerBreak(5));
   assertEquals(9, sol.integerBreak(6));
   assertEquals(12, sol.integerBreak(7));
   assertEquals(18, sol.integerBreak(8));
   assertEquals(27, sol.integerBreak(9));
   assertEquals(36, sol.integerBreak(10));
 }
 public static void main(String[] args) {
   Solution tool = new Solution();
   tool.init();
   tool.solve();
 }
Пример #19
0
 public static void main(String[] args) {
   Solution sol = new Solution();
   ArrayList<ArrayList<String>> re = sol.partition("aab");
   System.out.println(re);
 }
Пример #20
0
 public static void main(String[] args) {
   Solution testSolution = new Solution();
   boolean testBoolean = testSolution.canPermutePalindrome("as");
   System.out.println(testBoolean);
 }
Пример #21
0
 private static void test(int x) {
   Solution s = new Solution();
   System.out.printf("%d: %d %f", x, s.sqrt(x), Math.sqrt(x));
   System.out.println();
 }
Пример #22
0
 public static void main(String args[]) {
   Solution s = new Solution();
   System.out.println(s.trailingZeroes(92));
 }
Пример #23
0
 public static void main(String args[]) {
   Solution sol = new Solution();
   int A[] = {2};
   int elem = 3;
   System.out.println(sol.removeElement(A, elem));
 }