<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 排序二進制數組所需的最小相鄰交換 > 原文: [https://www.geeksforgeeks.org/minimum-swaps-required-sort-binary-array/](https://www.geeksforgeeks.org/minimum-swaps-required-sort-binary-array/) 給定一個二進制數組,任務是使用最少交換對該二進制數組進行排序。 我們只允許交換相鄰的元素 **示例**: ``` Input : [0, 0, 1, 0, 1, 0, 1, 1] Output : 3 1st swap : [0, 0, 1, 0, 0, 1, 1, 1] 2nd swap : [0, 0, 0, 1, 0, 1, 1, 1] 3rd swap : [0, 0, 0, 0, 1, 1, 1, 1] Input : Array = [0, 1, 0, 1, 0] Output : 3 ``` [### 推薦:請首先在 IDE 上嘗試您的方法,然后查看解決方案](https://ide.geeksforgeeks.org/) **方法**: 這可以通過在每個 1 的右側找到零個數并將它們相加來完成。 為了對數組進行排序,每個數組總是必須在其右側的每個零處執行交換操作。 因此,數組中特定 1 的交換操作總數為其右側的零數目。 找出每一個右邊的零的數目,即交換的數目,并將它們全部相加以獲得交換的總數。 **實現**: ## C++ ```cpp // C++ code to find minimum number of // swaps to sort a binary array #include <bits/stdc++.h> using namespace std; // Function to find minimum swaps to // sort an array of 0s and 1s. int findMinSwaps(int arr[], int n) { ????// Array to store count of zeroes ????int noOfZeroes[n]; ????memset(noOfZeroes, 0, sizeof(noOfZeroes)); ????int i, count = 0; ????// Count number of zeroes ????// on right side of every one. ????noOfZeroes[n - 1] = 1 - arr[n - 1]; ????for (i = n - 2; i >= 0; i--) { ????????noOfZeroes[i] = noOfZeroes[i + 1]; ????????if (arr[i] == 0) ????????????noOfZeroes[i]++; ????} ????// Count total number of swaps by adding number ????// of zeroes on right side of every one. ????for (i = 0; i < n; i++) { ????????if (arr[i] == 1) ????????????count += noOfZeroes[i]; ????} ????return count; } // Driver code int main() { ????int arr[] = { 0, 0, 1, 0, 1, 0, 1, 1 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????cout << findMinSwaps(arr, n); ????return 0; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看