czwartek, 25 października 2018

Codility FibFrogg

Idea: This is a search graph task. A used this explanation of the algorithm LINK.

In first step you check all possible single jumps from start point. This creates a new "row" of starting positions.
In next step you check all possible single jumps from every start position in the "row".
Sum of found new positions create another row.
You skip if you find a possible jump to the position which was "visited" previously, because there is a shorter or equal path to that position.
You stop when any jump will reach "end position" or you cant find a position to jump anymore. Code:

wtorek, 16 października 2018

Codility-CommonPrimeDivisors version by PrimeFactorization

It's much slower solution than by using Greatest Common Divisor, but it's different and still gets 100% on Codility platform

Codility-CommonPrimeDivisors version by GCD

It's tricky task. I used some help via internet.
Idea:  Greatest common divisor of A and B consists of EVERY prime divisor of A & B. Consists or contains. There is no such a number E that would be prime, and divisor of A and B that GCD(A,B) * E = A && GCD(A,B) * E =B

Once you have GCD, you divide a number by it. That number can be considered as a "row" of prime factors and their exponents. So dividing number by GCD will remove some factors or their exponents.

Then you create a new GCD value form divided number and old GCD value. And repeat dividing. Eventually GCD will become = 1. Then there are two possibilities:
(last) divided number = 1 number consisted only with prime divisors contained in original GCD
(last) divided number != 1 number consists of some more prime divisors than original GCD

piątek, 5 października 2018

Codility - CountNonDivisible

Idea:
Take an element.
Count amount divisors it has.
Non-divisors = N - amount_of_divisors


import static java.lang.Integer.max;
import java.util.Arrays;
class Solution {
  public int[] solution(int[] A) {
    final int N = A.length;
    final int MAX_VALUE_TBL = 2*50000;
    int[] r = new int[N];                     // result table
    int[] AS_AV = new int[MAX_VALUE_TBL + 1]; // number of cell with values

    int[] AS_AR = new int[MAX_VALUE_TBL + 1]; // results yet counted for values
    boolean[] b = new boolean[MAX_VALUE_TBL + 1]; // if value has been counted

    if (N == 1) return r;

    for (int i = 0; i < N; i++) {
      int v = A[i];
      AS_AV[v]++;
    }

    for (int i = 0; i < N; i++) {
      int cu_val = A[i];
      if (!b[cu_val]) {
        int am_div = getAmDivisors(cu_val, AS_AV);
        int am_all = N;
        r[i] = am_all - am_div;
        b[cu_val] = true;
        AS_AR[cu_val] = r[i];
      } else {
        r[i] = AS_AR[cu_val];
      }
    }
    return r;
  }

  private int getAmDivisors(int cu_val, int[] AS_AV) {
    int r = 0;
    int sqr = (int) Math.sqrt(cu_val);

    for (int divisor = sqr; divisor > 0; divisor--) {
      if (cu_val % divisor == 0) {
        r += AS_AV[divisor];
        if (divisor * divisor != cu_val) {
          r += AS_AV[cu_val / divisor];
        }
      }
    }
    return r;
  }
}