Perbezaan Minimum Antara Nilai Terbesar dan Terkecil dalam Tiga Pergerakan

WBOY
Lepaskan: 2024-07-18 22:44:21
asal
1199 orang telah melayarinya

Minimum Difference Between Largest and Smallest Value in Three Moves

1509. Perbezaan Minimum Antara Nilai Terbesar dan Terkecil dalam Tiga Pergerakan

Sederhana

Anda diberi nombor tatasusunan integer.

Dalam satu langkah, anda boleh memilih satu elemen nombor dan menukarnya kepada sebarang nilai.

Kembali perbezaan minimum antara nilai terbesar dan terkecil nombor selepas melakukan paling banyak tiga gerakan.

Contoh 1:

  • Input: nombor = [5,3,2,4]
  • Output: 0
  • Penjelasan: Kita boleh membuat paling banyak 3 gerakan.
  In the first move, change 2 to 3. nums becomes [5,3,3,4].
  In the second move, change 4 to 3. nums becomes [5,3,3,3].
  In the third move, change 5 to 3. nums becomes [3,3,3,3].
  After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0.
Salin selepas log masuk

Contoh 2:

  • Input: nombor = [1,5,0,10,14]
  • Output: 1
  • Penjelasan: Kita boleh membuat paling banyak 3 gerakan.
  In the first move, change 5 to 0. nums becomes [1,0,0,10,14].
  In the second move, change 10 to 0. nums becomes [1,0,0,0,14].
  In the third move, change 14 to 1. nums becomes [1,0,0,0,1].
  After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1.
  It can be shown that there is no way to make the difference 0 in 3 moves.
Salin selepas log masuk

Contoh 3:

  • Input: nombor = [3,100,20]
  • Output: 0
  • Penjelasan: Kita boleh membuat paling banyak 3 gerakan.
  In the first move, change 100 to 7. nums becomes [3,7,20].
  In the second move, change 20 to 7. nums becomes [3,7,7].
  In the third move, change 3 to 7. nums becomes [7,7,7].
  After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0.
Salin selepas log masuk

Kekangan:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109

Penyelesaian:

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function minDifference($nums) {
        $n = count($nums);

        // If the array has 4 or fewer elements, the difference is zero because we can remove all but one element.
        if ($n <= 4) {
            return 0;
        }

        // Sort the array to facilitate the calculation of differences after removals.
        sort($nums);

        // We consider removing 0, 1, 2, or 3 elements from the start or the end.
        // Calculate the differences:
        // 1. Remove 3 from start: nums[n-1] - nums[3]
        // 2. Remove 2 from start, 1 from end: nums[n-2] - nums[2]
        // 3. Remove 1 from start, 2 from end: nums[n-3] - nums[1]
        // 4. Remove 3 from end: nums[n-4] - nums[0]
        $differences = [
            $nums[$n - 1] - $nums[3],
            $nums[$n - 2] - $nums[2],
            $nums[$n - 3] - $nums[1],
            $nums[$n - 4] - $nums[0]
        ];

        // Return the minimum difference.
        return min($differences);
    }
}
Salin selepas log masuk

Pautan Kenalan

  • LinkedIn
  • GitHub

Atas ialah kandungan terperinci Perbezaan Minimum Antara Nilai Terbesar dan Terkecil dalam Tiga Pergerakan. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan