2016.07/28 hedviginc phone interview


  • cid:53:privileges:topics:read

    Given: A score, N
    Given scores {1, 2, 3, 6}

    N = 7

    {1, 1, 1, 1, 1, 1, 1}
    {1, 2, 2, 2}
    {3, 2, 2}
    {1, 6}
    {6, 1}
    {3, 1, 2, 1} etc

    number of such combinations

    static int count = 0;
    int numOfCombinations(int[] scores, int N)
    {
    numOfCombinations(scores,N);
    }
    helper(int[] scores, int N) //heper(scores, 3)
    {
    for(int i = 0; i < scores.length;i++){
    if(N == 0){
    count++;
    }
    if(N < 0){
    return;
    }
    helper(scores,N-scores[i]); //i = 3, N = 3 helper(scores, -3)
    }
    }


    Given: An Array of integers
    Required: Find the min distance between any two elements from the given array

    Eg:
    {5, 2, 3, 4, 3, 7, 8, 3}
    0 1 2 3 4 5 6 7

    a = 3, b = 8

    2 - 6 = 4
    4 - 6 = 2
    7 - 6 = 1

    min distance = 1

    int minDistance(int[] array, int a, int b)
    {
    if(a == b){
    return 0;
    }
    ArrayList lista = new ArrayList();
    ArrayList listb = new ArrayList();
    for(int i = 0; i < array.length; i++){
    if(array[i] == a){
    lista.add(i);
    }
    if(array[i] == b){
    listb.add(i);
    }
    }
    int minDistance = array.length;
    for(int j = 0; j < lista.size(); j++){
    for(int k = 0; k < listb.size(); k++){
    minDistance = Math.min(Math.abs(lista.get(j)-listb.get(k)),minDistance);
    }
    }
    return minDistance;
    }


登录后回复
 

与 BitTiger Community 的连接断开,我们正在尝试重连,请耐心等待