【问题标题】:How to minimize the sum of the elements in the final array after performing k number of operation执行k次操作后如何最小化最终数组中元素的总和
【发布时间】:2019-12-29 15:16:37
【问题描述】:

这个问题是在我的面试编码中提出的,有 12 个测试用例,我只能通过其中的 8 个。 minSum(List values, int k) 方法需要完成。 示例:nums = [10,20,7] 和 k=4 这个数组的答案是 5+5+4=14。每个操作都包括从数组中删除元素,将其除以 2 并将结果的上限插入回数组中。

我的解决方案是这样的:

if (num != null) {
        Collections.sort(num);

        while (k != 0) {
            for (int i = num.size() - 1; i >= 0 && k != 0; i--) {

                int number = (int) Math.ceil((double) num.get(i) / 2);

                num.set(i, number);
                k--;
            }
        }
    }
    return getMinSumOfArray(num);

测试用例:nums[2], k= 1 那么答案是 1 如果 nums[2,3], k = 1 那么答案是 4

这些是简单的测试用例,但有大约 100000 个随机数数组和大约 1000000 个操作步骤。

我失败的测试用例:

[1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 12, 12, 12, 12, 13, 14, 16, 17, 18, 19, 20, 20, 21, 22, 22, 22, 22, 22, 23, 23, 23, 23, 25, 25, 25, 26, 26, 27, 27, 27, 27, 28, 28, 29, 29, 30, 30, 30, 31, 31, 33, 33, 33, 33, 34, 34, 34, 34, 35, 35, 36, 37, 37, 37, 38, 38, 38, 39, 39, 40, 42, 43, 43, 44, 44, 44, 45, 45, 45, 45, 45, 46, 46, 47, 47, 48, 48, 49, 50, 50, 52, 53, 54, 55, 56, 57, 58, 58, 59, 60, 60, 61, 62, 64, 64, 64, 64, 65, 65, 65, 66, 67, 67, 68, 69, 69, 69, 69, 70, 71, 71, 73, 73, 73, 74, 74, 74, 74, 75, 76, 77, 77, 77, 78, 78, 80, 81, 81, 81, 81, 82, 83, 83, 84, 84, 85, 85, 85, 86, 86, 86, 88, 88, 88, 89, 89, 90, 91, 92, 92, 93, 93, 93, 95, 96, 96, 96, 96, 97, 97, 97, 97, 97, 98, 98, 98, 98, 99, 100, 100, 101, 101, 101, 102, 102, 103, 103, 104, 105, 106, 106, 106, 106, 107, 107, 107, 108, 108, 108, 109, 109, 110, 111, 111, 113, 113, 114, 115, 115, 117, 117, 117, 117, 119, 119, 119, 119, 119, 120, 120, 120, 120, 121, 121, 121, 121, 122, 122, 122, 123, 123, 123, 125, 125, 126, 128, 129, 130, 130, 130, 130, 131, 131, 133, 134, 134, 134, 135, 135, 135, 135, 135, 136, 136, 137, 138, 139, 139, 140, 140, 143, 143, 144, 145, 145, 146, 146, 146, 147, 147, 148, 148, 148, 149, 149, 149, 150, 150, 151, 151, 151, 152, 152, 152, 152, 152, 153, 153, 154, 154, 154, 154, 155, 157, 157, 158, 159, 159, 159, 159, 160, 161, 162, 162, 163, 164, 164, 164, 165, 165, 167, 167, 168, 168, 169, 170, 171, 171, 171, 171, 172, 172, 172, 173, 174, 175, 175, 176, 176, 176, 177, 179, 179, 179, 179, 180, 181, 181, 181, 182, 183, 183, 184, 184, 184, 184, 186, 187, 188, 188, 189, 189, 189, 190, 190, 190, 190, 191, 192, 192, 192, 192, 192, 193, 193, 193, 194, 194, 194, 194, 196, 196, 197, 197, 197, 200, 200, 201, 201, 202, 203, 203, 205, 205, 206, 207, 207, 207, 208, 208, 208, 210, 210, 210, 210, 211, 212, 212, 212, 213, 213, 213, 214, 214, 214, 215, 216, 216, 216, 217, 218, 218, 218, 218, 220, 220, 221, 221, 222, 222, 222, 222, 222, 223, 223, 224, 224, 224, 224, 225, 225, 225, 226, 227, 228, 228, 230, 231, 232, 232, 232, 233, 235, 235, 235, 235, 236, 236, 237, 238, 238, 238, 238, 239, 240, 240, 241, 243, 244, 244, 245, 245, 245, 246, 246, 246, 246, 247, 248, 248, 249, 249, 249, 250, 250, 251, 251, 251, 252, 252, 252, 253, 254, 255, 255, 256, 256, 256, 257, 257, 257, 258, 258, 259, 259, 259, 260, 261, 261, 261, 262, 262, 262, 263, 263, 263, 264, 265, 266, 267, 267, 267, 268, 269, 269, 270, 270, 271, 271, 271, 272, 272, 273, 273, 273, 274, 275, 275, 276, 276, 277, 279, 279, 279, 281, 281, 281, 281, 282, 282, 282, 283, 284, 285, 285, 286, 286, 288, 288, 288, 288, 288, 289, 290, 290, 291, 291, 291, 292, 293, 293, 294, 295, 295, 295, 297, 297, 297, 297, 297, 298, 299, 300, 300, 301, 301, 301, 301, 303, 303, 303, 304, 304, 305, 305, 306, 306, 306, 307, 307, 307, 307, 308, 308, 309, 309, 309, 309, 310, 311, 311, 311, 312, 312, 312, 313, 313, 314, 315, 315, 315, 316, 317, 318, 318, 318, 318, 319, 321, 322, 322, 323, 323, 324, 324, 324, 325, 325, 325, 325, 325, 326, 326, 326, 326, 326, 327, 327, 327, 327, 327, 329, 329, 329, 329, 329, 329, 330, 331, 332, 332, 332, 333, 333, 333, 333, 333, 334, 334, 335, 335, 335, 335, 336, 336, 337, 338, 338, 339, 339, 340, 340, 341, 341, 342, 342, 342, 342, 343, 343, 344, 345, 345, 345, 346, 349, 350, 351, 351, 351, 351, 352, 352, 353, 353, 354, 354, 356, 356, 356, 356, 356, 357, 358, 358, 358, 359, 359, 360, 361, 361, 361, 361, 363, 363, 364, 364, 365, 365, 366, 366, 366, 366, 367, 368, 368, 369, 370, 370, 370, 371, 372, 372, 372, 372, 373, 374, 375, 375, 376, 376, 376, 376, 377, 377, 377, 377, 378, 378, 378, 379, 380, 380, 380, 381, 381, 381, 381, 382, 382, 382, 384, 384, 384, 386, 386, 387, 387, 389, 390, 390, 391, 391, 391, 392, 392, 393, 393, 394, 394, 394, 394, 394, 394, 395, 395, 395, 396, 396, 396, 396, 396, 397, 397, 398, 398, 398, 398, 399, 400, 402, 402, 403, 404, 404, 404, 405, 406, 406, 407, 407, 407, 409, 409, 410, 411, 413, 413, 413, 413, 414, 414, 415, 416, 416, 416, 417, 417, 418, 418, 418, 419, 419, 419, 419, 420, 421, 421, 421, 421, 422, 423, 423, 424, 425, 426, 426, 428, 429, 429, 429, 429, 429, 429, 429, 430, 430, 430, 431, 431, 432, 432, 432, 433, 433, 434, 436, 437, 437, 437, 438, 439, 439, 441, 441, 441, 442, 442, 443, 444, 445, 445, 445, 446, 446, 447, 447, 448, 450, 450, 450, 451, 451, 452, 452, 453, 453, 453, 454, 454, 455, 455, 456, 456, 458, 459, 459, 460, 461, 461, 462, 463, 463, 463, 464, 464, 465, 465, 467, 467, 467, 469, 469, 471, 472, 472, 473, 473, 473, 474, 474, 474, 476, 476, 476, 477, 477, 477, 478, 479, 479, 480, 480, 480, 480, 480, 481, 481, 481, 481, 481, 482, 482, 483, 484, 485, 486, 487, 488, 489, 489, 490, 491, 491, 493, 493, 493, 494, 494, 494, 495, 495, 495, 496, 496, 497, 497, 497, 498, 499, 500, 500, 500, 500]

k= 565

【问题讨论】:

  • 这个算法的问题是确定下一个最大的减半数。

标签: java arrays list


【解决方案1】:

根据我的测试,这似乎非常快。

算法如下:

  1. 将最大元素初始化为MAX_INTEGER。第一次通过这个触发 排序。

  2. 继续处理并应用测试,直到当前值小于 超过最大值。

  3. 然后resort,重置index并重新分配一个新的max

  4. 继续直到应用所有测试

这很有效,因为所有值都会被处理,直到当前值小于最大值。那么将测试应用于较小的值是没有意义的,是时候采取措施了。

k = 0 or 1entries = 0 or 1的边框也已经过测试。

    public static long minimalSum(List<Integer> data, int ktests) {
        if (data.size() == 0) {
            return 0;
        }

        int index = 0;
        int max = Integer.MAX_VALUE;
        while (ktests > 0) {
            int currentValue = data.get(index);
            if (currentValue <= max) {
                data.sort(Comparator.reverseOrder());
                index = 0;
                max = data.get(0) / 2;
                continue;
            }
            ktests--;

            int roundUp = currentValue & 1;
            currentValue /= 2;
            currentValue += roundUp;

            data.set(index, currentValue);
            if (index + 1 < data.size()) {
                index++;
            }
        }
        return data.stream().mapToLong(a->a).sum();
    }

【讨论】:

  • 漂亮的解决方案,无需使用任何额外空间。太棒了
  • Collections.sort(data, Comparator.reverseOrder()); 可以替换为data.sort(Comparator.reverseOrder())data.stream().collect(Collectors.summingLong(a -&gt; a)); 可以替换为data.stream().mapToLong(a -&gt; a).sum()
  • 我进行了建议的更改。谢谢!
  • 我花了一段时间才明白这一点。本质上,如果排序列表中的第一个数字仍然大于第二个,您会继续使用。一旦第二个数字大于第一个数字,您就会“处理”那个数字。我所说的“过程”是指“一半和 ceil”。
  • 是的。但我将保证在再次排序之前执行至少 1 次测试。那是因为一旦我按降序排序,我将 max 设置为第一个元素/2。然后将索引重置为0并继续循环结束。所以这一切都重新开始并重复,直到所有操作都执行完毕。
【解决方案2】:

如果输入是nums = [5, 16], k = 4,您的代码将对这两个数字应用运算,每个运算 2 次:

[5, 16][5, 8][3, 8][3, 4][2, 4]     sum = 6

问题是它应该这样进行:

[5, 16][5, 8][5, 4][3, 4][3, 2]     sum = 5

如您所见,该操作应该对第一个元素应用 1 次,对第二个元素应用 3 次。

我会让你重新思考你的逻辑来解决这个问题。

提示:不要对数组进行排序,而是构建一个数字到频率的映射,这样您就可以一次批量应用操作frequency。使用TreeMap,这样您就可以在每次迭代中快速找到最大的数字。


更新

好的,这是一个解决方案,按照上面给出的提示:

static long getMinSumOfArray(List<Integer> num, int k) {
    TreeMap<Integer, Long> map = num.stream().collect(Collectors.groupingBy(
            Function.identity(), TreeMap::new, Collectors.counting()));
    for (int remain = k, applyCount; remain > 0; remain -= applyCount) {
        Entry<Integer, Long> maxEntry = map.lastEntry();
        if ((applyCount = Math.min(maxEntry.getValue().intValue(), remain)) == maxEntry.getValue())
            map.remove(maxEntry.getKey());
        else
            map.put(maxEntry.getKey(), maxEntry.getValue() - applyCount);
        map.merge((maxEntry.getKey() + 1) / 2, (long) applyCount, Long::sum);
    }
    //System.out.println(map.entrySet().stream().map(e -> e.getValue() == 1 ? e.getKey().toString() : e.getKey() + "×" + e.getValue()).collect(Collectors.joining(" + ")));
    return map.entrySet().stream().mapToLong(e -> e.getKey() * e.getValue()).sum();
}

测试

// From question
System.out.println(getMinSumOfArray(Arrays.asList(10,20,7), 4));
System.out.println(getMinSumOfArray(Arrays.asList(2,3), 1));
System.out.println(getMinSumOfArray(Arrays.asList(1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 12, 12, 12, 12, 13, 14, 16, 17, 18, 19, 20, 20, 21, 22, 22, 22, 22, 22, 23, 23, 23, 23, 25, 25, 25, 26, 26, 27, 27, 27, 27, 28, 28, 29, 29, 30, 30, 30, 31, 31, 33, 33, 33, 33, 34, 34, 34, 34, 35, 35, 36, 37, 37, 37, 38, 38, 38, 39, 39, 40, 42, 43, 43, 44, 44, 44, 45, 45, 45, 45, 45, 46, 46, 47, 47, 48, 48, 49, 50, 50, 52, 53, 54, 55, 56, 57, 58, 58, 59, 60, 60, 61, 62, 64, 64, 64, 64, 65, 65, 65, 66, 67, 67, 68, 69, 69, 69, 69, 70, 71, 71, 73, 73, 73, 74, 74, 74, 74, 75, 76, 77, 77, 77, 78, 78, 80, 81, 81, 81, 81, 82, 83, 83, 84, 84, 85, 85, 85, 86, 86, 86, 88, 88, 88, 89, 89, 90, 91, 92, 92, 93, 93, 93, 95, 96, 96, 96, 96, 97, 97, 97, 97, 97, 98, 98, 98, 98, 99, 100, 100, 101, 101, 101, 102, 102, 103, 103, 104, 105, 106, 106, 106, 106, 107, 107, 107, 108, 108, 108, 109, 109, 110, 111, 111, 113, 113, 114, 115, 115, 117, 117, 117, 117, 119, 119, 119, 119, 119, 120, 120, 120, 120, 121, 121, 121, 121, 122, 122, 122, 123, 123, 123, 125, 125, 126, 128, 129, 130, 130, 130, 130, 131, 131, 133, 134, 134, 134, 135, 135, 135, 135, 135, 136, 136, 137, 138, 139, 139, 140, 140, 143, 143, 144, 145, 145, 146, 146, 146, 147, 147, 148, 148, 148, 149, 149, 149, 150, 150, 151, 151, 151, 152, 152, 152, 152, 152, 153, 153, 154, 154, 154, 154, 155, 157, 157, 158, 159, 159, 159, 159, 160, 161, 162, 162, 163, 164, 164, 164, 165, 165, 167, 167, 168, 168, 169, 170, 171, 171, 171, 171, 172, 172, 172, 173, 174, 175, 175, 176, 176, 176, 177, 179, 179, 179, 179, 180, 181, 181, 181, 182, 183, 183, 184, 184, 184, 184, 186, 187, 188, 188, 189, 189, 189, 190, 190, 190, 190, 191, 192, 192, 192, 192, 192, 193, 193, 193, 194, 194, 194, 194, 196, 196, 197, 197, 197, 200, 200, 201, 201, 202, 203, 203, 205, 205, 206, 207, 207, 207, 208, 208, 208, 210, 210, 210, 210, 211, 212, 212, 212, 213, 213, 213, 214, 214, 214, 215, 216, 216, 216, 217, 218, 218, 218, 218, 220, 220, 221, 221, 222, 222, 222, 222, 222, 223, 223, 224, 224, 224, 224, 225, 225, 225, 226, 227, 228, 228, 230, 231, 232, 232, 232, 233, 235, 235, 235, 235, 236, 236, 237, 238, 238, 238, 238, 239, 240, 240, 241, 243, 244, 244, 245, 245, 245, 246, 246, 246, 246, 247, 248, 248, 249, 249, 249, 250, 250, 251, 251, 251, 252, 252, 252, 253, 254, 255, 255, 256, 256, 256, 257, 257, 257, 258, 258, 259, 259, 259, 260, 261, 261, 261, 262, 262, 262, 263, 263, 263, 264, 265, 266, 267, 267, 267, 268, 269, 269, 270, 270, 271, 271, 271, 272, 272, 273, 273, 273, 274, 275, 275, 276, 276, 277, 279, 279, 279, 281, 281, 281, 281, 282, 282, 282, 283, 284, 285, 285, 286, 286, 288, 288, 288, 288, 288, 289, 290, 290, 291, 291, 291, 292, 293, 293, 294, 295, 295, 295, 297, 297, 297, 297, 297, 298, 299, 300, 300, 301, 301, 301, 301, 303, 303, 303, 304, 304, 305, 305, 306, 306, 306, 307, 307, 307, 307, 308, 308, 309, 309, 309, 309, 310, 311, 311, 311, 312, 312, 312, 313, 313, 314, 315, 315, 315, 316, 317, 318, 318, 318, 318, 319, 321, 322, 322, 323, 323, 324, 324, 324, 325, 325, 325, 325, 325, 326, 326, 326, 326, 326, 327, 327, 327, 327, 327, 329, 329, 329, 329, 329, 329, 330, 331, 332, 332, 332, 333, 333, 333, 333, 333, 334, 334, 335, 335, 335, 335, 336, 336, 337, 338, 338, 339, 339, 340, 340, 341, 341, 342, 342, 342, 342, 343, 343, 344, 345, 345, 345, 346, 349, 350, 351, 351, 351, 351, 352, 352, 353, 353, 354, 354, 356, 356, 356, 356, 356, 357, 358, 358, 358, 359, 359, 360, 361, 361, 361, 361, 363, 363, 364, 364, 365, 365, 366, 366, 366, 366, 367, 368, 368, 369, 370, 370, 370, 371, 372, 372, 372, 372, 373, 374, 375, 375, 376, 376, 376, 376, 377, 377, 377, 377, 378, 378, 378, 379, 380, 380, 380, 381, 381, 381, 381, 382, 382, 382, 384, 384, 384, 386, 386, 387, 387, 389, 390, 390, 391, 391, 391, 392, 392, 393, 393, 394, 394, 394, 394, 394, 394, 395, 395, 395, 396, 396, 396, 396, 396, 397, 397, 398, 398, 398, 398, 399, 400, 402, 402, 403, 404, 404, 404, 405, 406, 406, 407, 407, 407, 409, 409, 410, 411, 413, 413, 413, 413, 414, 414, 415, 416, 416, 416, 417, 417, 418, 418, 418, 419, 419, 419, 419, 420, 421, 421, 421, 421, 422, 423, 423, 424, 425, 426, 426, 428, 429, 429, 429, 429, 429, 429, 429, 430, 430, 430, 431, 431, 432, 432, 432, 433, 433, 434, 436, 437, 437, 437, 438, 439, 439, 441, 441, 441, 442, 442, 443, 444, 445, 445, 445, 446, 446, 447, 447, 448, 450, 450, 450, 451, 451, 452, 452, 453, 453, 453, 454, 454, 455, 455, 456, 456, 458, 459, 459, 460, 461, 461, 462, 463, 463, 463, 464, 464, 465, 465, 467, 467, 467, 469, 469, 471, 472, 472, 473, 473, 473, 474, 474, 474, 476, 476, 476, 477, 477, 477, 478, 479, 479, 480, 480, 480, 480, 480, 481, 481, 481, 481, 481, 482, 482, 483, 484, 485, 486, 487, 488, 489, 489, 490, 491, 491, 493, 493, 493, 494, 494, 494, 495, 495, 495, 496, 496, 497, 497, 497, 498, 499, 500, 500, 500, 500), 565));

// From answer and comments
System.out.println(getMinSumOfArray(Arrays.asList(5, 16), 4));
System.out.println(getMinSumOfArray(Arrays.asList(7,1024), 6));

打印语句未注释的输出

4 + 5×2
14
2×2
4
1 + 2×3 + 3×3 + 4×2 + 5 + 6×3 + 7×3 + 8×4 + 9×4 + 10×2 + 12×4 + 13 + 14 + 16 + 17 + 18 + 19 + 20×2 + 21 + 22×5 + 23×4 + 25×3 + 26×2 + 27×4 + 28×2 + 29×2 + 30×3 + 31×2 + 33×4 + 34×4 + 35×2 + 36 + 37×3 + 38×3 + 39×2 + 40 + 42 + 43×2 + 44×3 + 45×5 + 46×2 + 47×2 + 48×2 + 49 + 50×2 + 52 + 53 + 54 + 55 + 56 + 57 + 58×2 + 59 + 60×2 + 61 + 62 + 64×4 + 65×3 + 66 + 67×2 + 68 + 69×4 + 70 + 71×2 + 73×3 + 74×4 + 75 + 76 + 77×3 + 78×2 + 80 + 81×4 + 82 + 83×2 + 84×2 + 85×3 + 86×3 + 88×3 + 89×2 + 90 + 91 + 92×2 + 93×3 + 95 + 96×4 + 97×5 + 98×4 + 99 + 100×2 + 101×3 + 102×2 + 103×2 + 104 + 105 + 106×4 + 107×3 + 108×3 + 109×2 + 110 + 111×2 + 113×2 + 114 + 115×2 + 117×4 + 119×5 + 120×4 + 121×13 + 122×10 + 123×15 + 124×14 + 125×16 + 126×7 + 127×2 + 128×6 + 129×6 + 130×8 + 131×8 + 132×4 + 133×3 + 134×7 + 135×9 + 136×7 + 137×5 + 138×5 + 139×3 + 140×5 + 141×7 + 142×2 + 143×6 + 144×6 + 145×5 + 146×7 + 147×5 + 148×6 + 149×9 + 150×5 + 151×7 + 152×10 + 153×7 + 154×10 + 155×6 + 156×6 + 157×5 + 158×5 + 159×9 + 160×2 + 161×4 + 162×7 + 163×11 + 164×8 + 165×9 + 166×4 + 167×9 + 168×8 + 169×4 + 170×5 + 171×10 + 172×6 + 173×5 + 174 + 175×4 + 176×9 + 177×5 + 178×5 + 179×8 + 180×4 + 181×7 + 182×5 + 183×8 + 184×7 + 185×4 + 186×6 + 187×3 + 188×8 + 189×10 + 190×8 + 191×8 + 192×8 + 193×5 + 194×6 + 195×3 + 196×7 + 197×11 + 198×8 + 199×6 + 200×4 + 201×4 + 202×5 + 203×5 + 204×3 + 205×5 + 206×2 + 207×9 + 208×7 + 209×5 + 210×9 + 211×6 + 212×6 + 213×6 + 214×4 + 215×11 + 216×8 + 217×4 + 218×5 + 219×4 + 220×4 + 221×7 + 222×7 + 223×7 + 224×7 + 225×6 + 226×5 + 227×6 + 228×6 + 229 + 230×4 + 231×4 + 232×8 + 233×3 + 234×3 + 235×6 + 236×5 + 237×7 + 238×7 + 239×5 + 240×9 + 241
149373
2 + 3
5
7 + 16
23

更新 2

您需要执行大约 100000 个随机数数组和大约 1000000 个操作步骤。

为了对解决方案进行压力测试,这里是生成 100000 个介于 1 和 100000(含)之间的随机数,并应用操作 1000000 次的代码。

压力测试

List<Integer> maxRandom = new Random().ints(100000, 1, 100001).boxed().collect(Collectors.toList());
System.out.println(getMinSumOfArray(maxRandom, 1000000));

样本输出

1 + 2×2 + 4 + 6 + 7 + 8 + 10 + 11 + 12×2 + 14×2 + 15 + 16 + 18×2 + 22 + 23 + 24 + 25×2052 + 26×4084 + 27×4065 + 28×4060 + 29×4163 + 30×4066 + 31×4088 + 32×4101 + 33×4104 + 34×4076 + 35×4004 + 36×4139 + 37×4191 + 38×4079 + 39×4007 + 40×4137 + 41×4060 + 42×4136 + 43×4131 + 44×4019 + 45×4215 + 46×4123 + 47×4064 + 48×4110 + 49×3706
3720663

在我的机器上运行大约 0.13 秒。

【讨论】:

  • @userDT 然后写Map.Entry
  • 我喜欢你的解决方案 +1
【解决方案3】:

这是我的解决方案,涉及 2 个队列。

算法是这样的

  • 对输入进行排序,放入q1

  • 当 k > 0 时,通过窥视 q1 或 q2 来获取下一个最大值,将其减半并推到 q2 上

注意:q2 是我总是推送到的队列,所以如果 q1 为空,我会交换 q1 和 q2 的指针。

public int divideLargestBy2(List<Integer> input, int k) {
    if (input.isEmpty()) return 0;

    input.sort(Comparator.reverseOrder());
    Queue<Integer> q1 = new LinkedList<>(input);
    Queue<Integer> q2 = new LinkedList<>();
    while (k > 0) {
        Integer largest = (q2.isEmpty() || q1.peek() > q2.peek()) ? q1.poll() : q2.poll();

        if (largest == 1L) return input.size();

        if (q1.isEmpty()) {
            Queue<Integer> temp = q1;
            q1 = q2;
            q2 = temp;
        }

        q2.add((largest + 1) / 2);
        k--;
    }

    return q1.stream().mapToInt(Integer::intValue).sum() + q2.stream().mapToInt(Integer::intValue).sum();
}

【讨论】:

    【解决方案4】:

    更新

    Max Heap 是解决此类问题的正确数据结构,因为它始终将最大元素保持在堆的顶部。在 Java 中,我们可以使用 PriorityQueue 来创建它

    这是一个运行非常高效的PriorityQueue 实现

    long sum(List<Integer> data, int k) {
        PriorityQueue<Integer> pQueue = new PriorityQueue<>(Comparator.reverseOrder());
        pQueue.addAll(data);
        while (k-- > 0) {
            pQueue.add((int) Math.ceil(pQueue.poll() / 2d));
        }
        return pQueue.stream().mapToInt(a -> a).sum();
    }
    

    尝试使用100000 数字和1000000 操作步骤。跑得很快。试试看。

    以前的方法

    注意:效率不高 - 尤其是大数字时

    在开始该过程之前,您需要按降序对列表进行排序。

    现在标记两个数字,第一个和第二个

    第一个是max,第二个是next max。我们利用它来发挥我们的优势。

    这是我尝试过的解决方案:

    List<Integer> num = Arrays.asList(10, 20, 7);
    int total = 0;
    int k = 4;
    
    // If list contains only 1 entry
    if (num.size() == 1) {
        while (k > 0) {
            total = total + (int) Math.ceil(num.get(0) / 2d);
            k--;
        }
        System.out.println(total);
        return;
    }
    
    // sort in descending order first
    num.sort(Collections.reverseOrder());
    
    int i = 0;
    int firstMaxIndex = 0;
    int secondMaxIndex = 1;
    while (k > 0) {
        k--;
        int result = (int) Math.ceil(num.get(i) / 2d);
        num.set(i, result);
        if (num.get(secondMaxIndex) > result) {
            // second item is larger than first now.
            // put the first item in right position
            int maxNum = num.get(firstMaxIndex);
            int j;
            for (j = secondMaxIndex; j < num.size(); j++) {
                if (num.get(j) > maxNum) {
                    num.set(j - 1, num.get(j));
                } else {
                    break;
                }
            }
            num.set(j - 1, maxNum);
        }
    }
    for (int n : num) {
        total = total + n;
    }
    System.out.println(total);
    

    此代码不会不必要地对完整列表进行排序,并且仅在第一项小于第二项时将值放在正确的位置。

    【讨论】:

    • 每个数字只应用一次操作,这肯定是不够的,例如对于问题中显示的第一个示例,nums = [10,20,7] 和 k=4,该操作必须对中间数字应用两次。
    • @Andreas 更新了我的解决方案,让我知道您的想法。谢谢。
    • 我得到的结果是 149373。
    • 没有通过所有的测试用例。
    • @SunilDabburi 我更多地指的是最坏的情况,请参阅 stress test 添加到我的答案末尾。您的代码独立处理每个值,当您将值减半并移动它时,通常必须移动数组的一半。必须将 50000 个数字移动 1000000 次对性能没有好处。
    猜你喜欢
    • 2020-12-23
    • 2021-01-16
    • 2019-01-12
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    相关资源
    最近更新 更多