Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 更新和打印数组元素?_Arrays_Adhoc - Fatal编程技术网

Arrays 更新和打印数组元素?

Arrays 更新和打印数组元素?,arrays,adhoc,Arrays,Adhoc,给你一个由N个正整数组成的数组,A1,A2,…,an 另外,给出了形式:-i j:Update Ai=j的Q更新。1.≤ 我≤ N 执行所有更新,并在每次更新后报告阵列模式。因此,返回一个Q元素数组,其中第i个元素是执行第i次更新后的数组模式 注释 模式是数组中最常出现的元素 如果可以使用多种模式,请返回最小的模式 更新数组输入是通过一个Q*2数组进行的,其中每一行表示一次更新 比如说, A=[2, 2, 2, 3, 3] Updates= [ [1, 3] ]

给你一个由N个正整数组成的数组,A1,A2,…,an

另外,给出了形式:-i j:Update Ai=j的Q更新。1.≤ 我≤ N

执行所有更新,并在每次更新后报告阵列模式。因此,返回一个Q元素数组,其中第i个元素是执行第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; }
/**
*@input A:整数数组
*@input n1:整数数组的(A)长度
*@input B:2D整数数组
*@input n21:Integer数组的(B)行
*@input n22:Integer数组的(B)列
* 
*@输出整数数组。您需要malloc内存,并在len1中填充长度
*/
int*getMode(int*A、int n1、int**B、int n21、int n22、int*len1){
int*ans=(int*)malloc(sizeof(int));//数组的动态分配
int x=0,i,j;
对于(i=0;iA[idx1])
p=A[idx];
其他的
p=A[idx1];
ans[x++]=p;}
返回ans;}
我们如何解决这个问题?有人能提出一些答案吗?< /P> <代码>我的答案是C++。
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;
        }
int getModeHash(无序映射和映射) { int maxCount=0; int maxCountNum=int_MIN; for(auto it=map.begin();it!=map.end();+it) { 如果(最大计数秒) { 如果(maxCount==它->秒) { maxCountNum=min(maxCountNum,it->first); } 其他的 { maxCountNum=it->first; maxCount=it->second; } } } 返回maxCountNum; } 向量解决方案::getMode(向量A、向量B){ 无序地图; 矢量结果; //将数组元素计数存储在哈希表中,即.map 对于(int i=0;i >我的答案是C++ int getModeHash(无序映射和映射) { int maxCount=0; int maxCountNum=int_MIN; for(auto it=map.begin();it!=map.end();+it) { 如果(最大计数秒) { 如果(maxCount==它->秒) { maxCountNum=min(maxCountNum,it->first); } 其他的 { maxCountNum=it->first; maxCount=it->second; } } } 返回maxCountNum; } 向量解决方案::getMode(向量A、向量B){ 无序地图; 矢量结果; //将数组元素计数存储在哈希表中,即.map 对于(int i=0;i >我的答案是C++ int getModeHash(无序映射和映射) { int maxCount=0; int maxCountNum=int_MIN; for(auto it=map.begin();it!=map.end();+it) { 如果(最大计数秒) { 如果(maxCount==它->秒) { maxCountNum=min(maxCountNum,it->first); } 其他的 { maxCountNum=it->first; maxCount=it->second; }
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;
    }
}
class compare
{
    public:
    bool operator()(pair<int,int> p1,pair<int,int> p2)
    {
        if(p1.first==p2.first)
        return p1.second>p2.second;
        return p1.first<p2.first;
    }
};
vector<int> Solution::getMode(vector<int> &a, vector<vector<int> > &b) {
    map<int,int> m;// map store the exact count of elements
    int n=a.size();
    for(int i=0;i<n;i++)
    {
        m[a[i]]++;
    }
    vector<int> ans;

// take a max heap which store element count and its value
    priority_queue<pair<int,int> ,vector<pair<int,int> >,compare >pq;
    for(int i=0;i<n;i++)
    {
        pq.push(make_pair(m[a[i]],a[i]));
    }
    int i=0,q=b.size();
    while(i<q)
    {
        int index=b[i][0]-1;
        int val=b[i][1];
        int val1=a[index];
        a[index]=val;
        if(val!=val1)
        {
            m[val1]--;
        m[val]++;
        pq.push(make_pair(m[val1],val1));
        pq.push(make_pair(m[val],val));
        }
        while(true)
        {
            pair<int,int> t=pq.top();
            int cnt=t.first;int vall=t.second;
            if(m[vall]==cnt)
            {

         // when top of heap has cnt same as in map then this the answer for the ith query
                ans.push_back(vall);
                break;
            }
            else
            pq.pop();
        }
        i++;
    }
    return ans;
}
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;
}
// 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;
    }
}