更新并打印数组元素?

时间:2015-09-09 14:43:04

标签: arrays adhoc

给出一组N个正整数,A1,A2,...,AN。

另外,给定形式的Q更新: - i j:更新Ai = j。 1≤i≤N。

在阵列的每个此类更新报告模式之后执行所有更新。因此,返回一个Q元素数组,其中第i个元素是执行了更新后的数组模式。

备注

  • 模式是阵列中最常出现的元素。
  • 如果可以使用多种模式,请返回最小的模式。
  • 通过Q * 2阵列更新阵列输入,其中每行代表更新。

例如,

A=[2, 2, 2, 3, 3]

Updates=    [ [1, 3] ]
            [ [5, 4] ]
            [ [2, 4] ]

A = [3, 2, 2, 3, 3] after 1st update.
3 is mode.

A = [3, 2, 2, 3, 4] after 2nd update.
2 and 3 both are mode. Return smaller i.e. 2.

A = [3, 4, 2, 3, 4] after 3rd update.
3 and 4 both are mode. Return smaller i.e. 3.

Return array [3, 2, 3].
Constraints 
1 ≤ N, Q ≤ 105 
1 ≤ j, Ai ≤ 109

使用简单数组我的实现有什么问题?

  /**
     * @input A : Integer array
     * @input n1 : Integer array's ( A ) length
     * @input B : 2D integer array 
     * @input n21 : Integer array's ( B ) rows
     * @input n22 : Integer array's ( B ) columns
     * 
     * @Output Integer array. You need to malloc memory, and fill the length in len1
     */
    int* getMode(int* A, int n1, int** B, int n21, int n22, int *len1) {
        int *ans = (int *) malloc (sizeof(int)); // dynamic allocation of array 
        int x = 0,i,j;
        for(i = 0; i < n1 ; i ++ )
            for(j = 0; j < n21; j++)
                A[B[j][0]] = B[j][1];
            // frequency calculate  

            int c[n1] ;

            for(i = 0; i < n1; i++)
                c[i] = 0;

            for(i = 0; i < n1; i++)
                c[A[i]]++;

            int mx = INT_MAX;
            int idx = -1, idx1 = -1;

            for(i = 0; i < n1; i++){
                if (mx  < A[i]){
                    idx1 = idx;
                    mx = A[i];
                    idx = i;
                }

            int p;
            if (A[idx]> A[idx1])
                p = A[idx];
            else
                p = A[idx1];

            ans[x++] = p; }

        return ans; }

我们如何解决?任何人都可以建议一些答案吗?

5 个答案:

答案 0 :(得分:2)

我在TreeMap中使用TreeMap,一个TreeMap用于按排序顺序存储模式,另一个TreeMap用于排序顺序的频率。

public class Solution {
    HashMap<Integer, Integer> map;
    TreeMap<Integer, TreeMap<Integer, Integer>> map2;
    public ArrayList<Integer> getMode(ArrayList<Integer> A, ArrayList<ArrayList<Integer>> B) {
        map = new HashMap<Integer, Integer>();
        map2 = new TreeMap<>();
        ArrayList<Integer> result = new ArrayList<Integer>();

        for(int i : A) {
            if(map.containsKey(i))
                map.put(i, map.get(i) +1);
            else
                map.put(i, 1);
        }
        for (Map.Entry m : map.entrySet()) {
            int freq = (int)m.getValue();
            TreeMap<Integer, Integer> x = new TreeMap<>();
            if (map2.containsKey(freq) == true) {
                x = map2.get(freq);
            }
            x.put((int)m.getKey(),1);
            map2.put(freq, x);
        }
        for(ArrayList<Integer> update : B){
            int index = update.get(0);
            int num = update.get(1);

            int toUpdate = A.get(index - 1);

            if(map.get(toUpdate) != null) {
                int freq = map.get(toUpdate);
                TreeMap<Integer, Integer> temp = map2.get(freq);
                if (temp.size() == 1) {
                    map2.remove(freq);
                }
                else {
                    temp.remove(toUpdate);
                    map2.put(freq, temp);
                }
                if (freq == 1) {
                    map.remove(toUpdate);
                }
                else {
                    map.put(toUpdate, freq - 1);
                    int z = freq-1;
                    TreeMap<Integer, Integer> tt = new TreeMap<>();
                    if (map2.containsKey(z)) {
                        tt = map2.get(z);
                    }
                    tt.put(toUpdate, 1);
                    map2.put(z, tt);
                }
            }
            A.set(index - 1, num);
            if(map.containsKey(num)) {
                int tt = map.get(num);
                TreeMap<Integer, Integer> w = map2.get(tt);
                if (w.size() == 1) {
                    map2.remove(tt);
                }
                else {
                    w.remove(num);
                    map2.put(tt, w);
                }
                map.put(num, tt+1);
                TreeMap<Integer, Integer> q = new TreeMap<>();
                if (map2.containsKey(tt+1)) {
                    q = map2.get(tt+1);
                }
                q.put(num,1);
                map2.put(tt+1, q);
            }
            else {
                map.put(num, 1);
                TreeMap<Integer, Integer> qq = new TreeMap<>();
                if (map2.containsKey(1)) {
                    qq = map2.get(1);
                }
                qq.put(num, 1);
                map2.put(1, qq);
            }
            int rr = (int)map2.lastKey();
             TreeMap<Integer, Integer> xyz = map2.get(rr);
            result.add((int)(xyz.firstKey()));
        }
        return result;
    }
}

答案 1 :(得分:1)

我在c ++中的回答:

   2.2.3 :001 > foo
   5.230000   0.020000   5.250000 (  5.274806)

答案 2 :(得分:1)

Java解决方案:每次更新后,更新元素并使用hashMap获取模式。 (虽然不是最佳)

public int[] getMode(int[] a, int[][] b) {
    int res[]=new int[b.length];
    int j=0;
    for(int i=0;i<b.length;i++)
    {
        int index=b[i][0];
        int element=b[i][1];
        a[index-1]=element;
        int m=mode(a);
        res[j]=m;
        j++;
    }
    return res;
}
public int mode(int[] a)
{
    HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
    int max=0,m=0;
    for(int i=0;i<a.length;i++)
    {
        if(hm.containsKey(a[i]))
        {
            hm.put(a[i],hm.get(a[i])+1);
        }
        else
        {
            hm.put(a[i],1);
        }
        if(hm.get(a[i])>max)
        {
            max=hm.get(a[i]);
            m=a[i];
        }
        else if(hm.get(a[i])==max)
        {
            m=(m<a[i])?m:a[i];
        }
    }
    return m;
}

答案 3 :(得分:0)

// this is classic implementation of LRU cache , I have used hash map and priority queue (heap to sort by frequency)    
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;

class Node implements Comparable<Node> {
    Integer key;
    Integer count;

    public Node(Integer key) {
        this.key = key;
        this.count = 1;

    }

    @Override
    public int compareTo(Node a) {
        return count != a.count ? a.count - count : key - a.key;
    }

}

public class Ninja1 {
    public static void main(String[] args) {
        Ninja1 n = new Ninja1();
        ArrayList<ArrayList<Integer>> b = new ArrayList<ArrayList<Integer>>();
        b.add(new ArrayList<Integer>(Arrays.asList(1, 3)));
        b.add(new ArrayList<Integer>(Arrays.asList(5, 4)));
        b.add(new ArrayList<Integer>(Arrays.asList(2, 4)));
        //b.add(new ArrayList<Integer>(Arrays.asList(1, 4)));
        System.out.println(n.getMode(new ArrayList<Integer>(Arrays.asList(2, 2, 2, 3, 3)), b));

    }

    public ArrayList<Integer> getMode(ArrayList<Integer> a, ArrayList<ArrayList<Integer>> b) {
        Queue<Node> q = new PriorityQueue<Node>();
        Map<Integer, Node> map = new HashMap<Integer, Node>();
        ArrayList<Integer> sol = new ArrayList<Integer>();

        Node temp = null;
        for (Integer i : a) {
            temp = map.get(i);
            if (temp == null) {
                Node n = new Node(i);
                map.put(i, n);
                q.add(n);
            } else {
                q.remove(temp);
                temp.count = temp.count + 1;
                q.add(temp);
            }
        }

        for (List<Integer> i : b) {
            if (a.get(i.get(0) - 1) != i.get(1)) {
                temp = map.get(a.get(i.get(0) - 1));
                q.remove(temp);

                if (temp.count != 1) {
                    temp.count = temp.count - 1;
                    q.add(temp);
                }

                temp = map.get(i.get(1));
                if (temp == null) {
                    Node n = new Node(i.get(1));
                    map.put(i.get(1), n);
                    q.add(n);
                } else {
                    q.remove(temp);
                    temp.count = temp.count + 1;
                    q.add(temp);
                }

            }
            sol.add(q.peek().key);
        }
        return sol;
    }
}

答案 4 :(得分:-2)

My answer is in C++
        int getModeHash(unordered_map<int, int> &map)
        {
            int maxCount = 0;
            int maxCountNum = INT_MIN;
            for ( auto it = map.begin(); it != map.end(); ++it)
            {
                if(maxCount <= it->second)
                {
                    if(maxCount == it->second)
                    {
                        maxCountNum = min(maxCountNum, it->first);
                    }
                    else
                    {
                        maxCountNum = it->first;
                        maxCount = it->second;
                    }
                }
            }
            return maxCountNum;
        }
        vector<int> Solution::getMode(vector<int> &A, vector<vector<int> > &B) {
           unordered_map<int, int> map;
           vector<int> result;

        //Store A array elements count in hash i.e.. map
           for(int i=0; i< A.size(); i++)
           {
               if(map.find(A[i]) == map.end())
               {
                   map[A[i]] = 0;
               }
               map[A[i]]++;
           }
           // now update hash map for each element in B, 
           //and get large count element
           for(int i=0; i < B.size(); i++)
           {
                map[A[B[i][0]-1]]--;
                if(map[A[B[i][0]-1]] == 0)
                {
                    map.erase(A[B[i][0]-1]);
                }
                A[B[i][0]-1] = B[i][1];
               if(map.find(A[B[i][0]-1]) == map.end())
               {
                   map[A[B[i][0]-1]] = 0;
               }
               map[A[B[i][0]-1]]++;     

               result.push_back(getModeHash(map));
           }
           return result;
        }