首页 > 其他分享 >构建类问题 constructive problem 2007,

构建类问题 constructive problem 2007,

时间:2024-03-03 09:05:07浏览次数:28  
标签:map int changed constructive 2007 key problem original array

2007. Find Original Array From Doubled Array An integer array original is transformed into a doubled array changed by appending twice the value of every element in original, and then randomly shuffling the resulting array.

Given an array changed, return original if changed is a doubled array. If changed is not a doubled array, return an empty array. The elements in original may be returned in any order.

 Example 1:

Input: changed = [1,3,4,2,6,8]
Output: [1,3,4]
Explanation: One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].

Example 2:

Input: changed = [6,3,0,1]
Output: []
Explanation: changed is not a doubled array.

Example 3:

Input: changed = [1]
Output: []
Explanation: changed is not a doubled array.

Constraints:

  • 1 <= changed.length <= 105
  • 0 <= changed[i] <= 105
class Solution {
    /**
    思路: sort array + map
    1. sort array
    2. 统计每个元素出现次数 map
    3. 遍历array,从map中判定其二倍数的进行消减
         1.如果map不存在,证明该数字已经被作为二倍消减
         2.如果map存在,但是二倍值不存在,那么不合法直接返回
     */
    public int[] findOriginalArray(int[] changed) {
        // 边界条件
        if(changed.length % 2 == 1) return new int[]{};
        // sort 数组
        Arrays.sort(changed);
        // 统计各元素数量
        Map<Integer, Integer> map = new HashMap<>();
        for(int e : changed) map.put(e, map.getOrDefault(e, 0) + 1);

        int[] result = new int[changed.length / 2];
        int index = 0 ;
        //从头开始扫描
        for(int key : changed) {
            // 如果不存在,可能已经作为二倍的值消掉了
            if(!map.containsKey(key)) continue;
            // 将key从count中--
            reduceCount(map, key);
            // 如果二倍的key不存在,那么说明不合法
            if(!map.containsKey(key * 2)) return new int[]{};
            // 将key * 2 从count中消掉
            reduceCount(map, key * 2);
            result[index++] = key;
        }
        return result;
    }

    private void reduceCount(Map<Integer, Integer> map, int key) {
        map.put(key, map.get(key) - 1);
        if(map.get(key) == 0) map.remove(key);
    }
}

 

标签:map,int,changed,constructive,2007,key,problem,original,array
From: https://www.cnblogs.com/cynrjy/p/18049562

相关文章

  • Week 1 Problems
    T1今有一逻辑表达式\(F_0\)为:\[(p\rightarrowr)\rightarrow((q\rightarrowr)\rightarrow(p\lorq)\land\negr)\]其中的联结词运算优先级与命题逻辑合式公式完全相同。观察\(F_0\)的形式,完成以下两个题目(1)补全真值表pqr\(p\rightarrowr\)\(q\rightarrowr\)......
  • 洛谷题单指南-二分查找与二分答案-P3853 [TJOI2007] 路标设置
    原题链接:https://www.luogu.com.cn/problem/P3853题意解读:相邻路标的最大距离即空旷指数,空旷指数越小,用的路标越多,因此可以根据空旷指数将使用路标情况分成两类:路标数<=K,路标数>K,对空旷指数进行二分即可。解题思路:二分的判定条件为,给定空旷指数,计算需要的路标数只需遍历每两......
  • Codeforces 1446D1 Frequency Problem (Easy Version)
    考虑求出全局的众数\(A\)。那么有一个结论,就是答案区间的众数中绝对有\(A\)。考虑反证法,如果没有\(A\),\(A\)在序列中出现的个数一定\(\ge\)区间内众数的出现个数,所以可以一直往外扩展直到\(A\)出现的个数与区间内非\(A\)众数的个数持平,这样肯定更优。于是可以考虑钦......
  • Codeforces 1446D2 Frequency Problem (Hard Version)
    考虑求出全局的众数\(A\)。那么有一个结论,就是答案区间的众数中绝对有\(A\)。考虑反证法,如果没有\(A\),\(A\)在序列中出现的个数一定\(\ge\)区间内众数的出现个数,所以可以一直往外扩展直到\(A\)出现的个数与区间内非\(A\)众数的个数持平,这样肯定更优。于是可以考虑钦......
  • P1110 [ZJOI2007] 报表统计 题解
    考察点:STL的熟练运用。记:\(l_i\)表示序列中不超过\(a_i\)的最大数,\(r_i\)表示序列中超过\(a_i\)的最小数。开一个vectorinsert[N]存储\(a_i\)后面插入的所有数字。首先,我们使用一个multisets1来存储相邻元素的差的绝对值,然后再开一个multisets2来存储当前出......
  • cURL error 60: SSL certificate problem: unable to get local issuer certifica 解
    cURLerror60:SSLcertificateproblem:unabletogetlocalissuercertifica解决 无法获取本地颁发者证书 Windows版本1.到https://curl.haxx.se/ca/cacert.pem下载证书文件cacert.pem,将其保存到PHP安装路径下。2.编辑php.ini文件,删除curl.cainfo配置项前......
  • Yet Another Two Pieces Problem
    YetAnotherTwoPiecesProblemProblem你在原点\((0,0)\),你可以进行以下三种操作:花费\(1\)的代价,向上移动一单位长度。花费\(k\)的代价,向右移动\(k\)单位长度,需要保证不经过\(y=x\)。其中\(k\)属于给定的整数集合\(S\)。花费\(1\)的代价,使得横坐标与纵坐标......
  • 洛谷题单指南-贪心-P1094 [NOIP2007 普及组] 纪念品分组
    原题链接:https://www.luogu.com.cn/problem/P1094题意解读:贪心选择解题思路:贪心策略:将纪念品按价格由小到大排序,优先选择价格大的一直到超过分组价格上限,再选择价格小的直到超过价格上限,此为一组重复以上过程,直到所有数据都遍历到,采用一头一尾双指针即可。证明:如果最大价格......
  • POJ--3468 A Simple Problem with Integers(线段树/树状数组)
    记录11:032024-2-25http://poj.org/problem?id=1961线段树树状数组把区间增加转变为单点增加,利用两个树状数组\(c_0和c_1\)将”Clrd"转化为在树状数组\(c_0\)中,把位置l上的数加d在树状数组\(c_0\)中,把位置r+1上的数减d在树状数组\(c_1\)中,把位置l上的数......
  • 「COCI 2006-2007 #4」 ZBRKA
    题意概括题面很清楚,不多赘述了。分析设\(f_{i,j}\)表示已用前\(i\)个数,使数列出现\(j\)个逆序对的方案数。因为从小到大枚举\(i\),所以填\(i\)时前面所有的数都比它小,那么\(i\)每向前移动一位,就会增加一个逆序对。所以可以直接枚举每一个\(i,j\),再枚举插入\(i\)......