<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 排除某些元素的最大子數組總和 > 原文: [https://www.geeksforgeeks.org/maximum-subarray-sum-excluding-certain-elements/](https://www.geeksforgeeks.org/maximum-subarray-sum-excluding-certain-elements/) 給定 n 個整數的 A 數組和 m 個整數的 B 數組,找到數組 A 的最大連續子數組總和,以使該子數組中不存在數組 B 的任何元素 **示例**: > 輸入:A = {1,7,-10,6,2},B = {5,6,7,1} > 輸出:2 > **說明** A 不允許在數組 B 中存在任何元素。 > 滿足此條件的最大和子數組為 **{2}** ,因為唯一允許的子數組為:{-10}和{2}。 最大和子數組為{2},總計為 2 > > 輸入:A = {3,4,5,-4,6},B = {1,8,5} > 輸出:7 > > **說明** > 滿足此要求的最大和子數組為 **{3,4}** ,因為唯一允許的子數組為{3},{4},{3、4},{- 4},{6},{-4、6}。 最大和子數組為{3,4},總計為 7 **方法 1(O(n * m)方法)**: 我們可以使用 [Kadane 算法](https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/)解決此問題。 由于我們不希望數組 B 的任何元素成為 A 的任何子數組的一部分,因此我們需要對經典的 Kadane 算法進行一些修改。 每當我們考慮 Kadane 算法中的元素時,我們要么擴展當前子數組,要么開始一個新的子數組。 ``` curr_max = max(a[i], curr_max+a[i]); if (curr_max < 0) curr_max = 0 ``` 現在,在這個問題中,當我們考慮任何元素時,我們通過在數組 B 中線性搜索[進行檢查,如果該元素存在于 B 中,則將 curr_max 設置為零,這意味著在該索引處我們考慮的所有子數組 到這一點將結束,并且不會進一步擴展,因為無法形成其他連續數組,即](https://www.geeksforgeeks.org/linear-search/) > 如果 B 中存在 A <sub>i</sub> ,則無法進一步擴展 A 中從 0 到(i – 1)的所有子數組,因為第 i <sup>個</sup>元素永遠不會包含在任何子數組中 如果數組 A 的當前元素不是數組 B 的一部分,我們將繼續使用 Kadane 算法,并跟蹤 max_so_far。 ## C++ ```cpp // C++ Program to find max subarray // sum excluding some elements #include <bits/stdc++.h> using namespace std; // Function to check the element? // present in array B bool isPresent(int B[], int m, int x) { ????for (int i = 0; i < m; i++) ????????if (B[i] == x) ????????????return true; ????return false; } // Utility function for findMaxSubarraySum() // with the following parameters? // A => Array A, // B => Array B, // n => Number of elements in Array A, // m => Number of elements in Array B? int findMaxSubarraySumUtil(int A[], int B[], ????????????????????????int n, int m) { ????// set max_so_far to INT_MIN ????int max_so_far = INT_MIN, curr_max = 0; ????for (int i = 0; i < n; i++) { ????????// if the element is present in B,? ????????// set current max to 0 and move to? ????????// the next element */ ????????if (isPresent(B, m, A[i])) { ????????????curr_max = 0; ????????????continue; ????????} ????????// Proceed as in Kadane's Algorithm? ????????curr_max = max(A[i], curr_max + A[i]); ????????max_so_far = max(max_so_far, curr_max); ????} ????return max_so_far; } // Wrapper for findMaxSubarraySumUtil()? void findMaxSubarraySum(int A[], int B[], ????????????????????????int n, int m) { ????int maxSubarraySum = findMaxSubarraySumUtil(A, B, ????????????????????????????????????????????????n, m); ????// This case will occour when all elements ????// of A are present in B, thus ????// no subarray can be formed? ????if (maxSubarraySum == INT_MIN) { ????????cout << "Maximum Subarray Sum cant be found" ?????????????<< endl; ????} ????else { ????????cout << "The Maximum Subarray Sum = " ????????????<< maxSubarraySum << endl; ????} } // Driver Code int main() { ????int A[] = { 3, 4, 5, -4, 6 }; ????int B[] = { 1, 8, 5 }; ????int n = sizeof(A) / sizeof(A[0]); ????int m = sizeof(B) / sizeof(B[0]); ????// Calling function ????findMaxSubarraySum(A, B, n, m); ????return 0; } ``` ## Java ```java // Java Program to find max subarray // sum excluding some elements import java.io.*; class GFG { ????// Function to check the element? ????// present in array B ????static boolean isPresent(int B[], ????????????????????????????int m, ????????????????????????????int x) ????{ ????????for (int i = 0; i < m; i++) ????????????if (B[i] == x) ????????????????return true; ????????return false; ????} ????// Utility function for findMaxSubarraySum() ????// with the following parameters ????// A => Array A, ????// B => Array B, ????// n => Number of elements in Array A, ????// m => Number of elements in Array B ????static int findMaxSubarraySumUtil(int A[], int B[], ??????????????????????????????????????int n, int m) ????{ ????????// set max_so_far to INT_MIN ????????int max_so_far = -2147483648, curr_max = 0; ????????for (int i = 0; i < n; i++) { ????????????// if the element is present in B, ????????????// set current max to 0 and move to ????????????// the next element ????????????if (isPresent(B, m, A[i])) { ????????????????curr_max = 0; ????????????????continue; ????????????} ????????????// Proceed as in Kadane's Algorithm ????????????curr_max = Math.max(A[i], curr_max + A[i]); ????????????max_so_far = Math.max(max_so_far, curr_max); ????????} ????????return max_so_far; ????} ????// Wrapper for findMaxSubarraySumUtil() ????static void findMaxSubarraySum(int A[], int B[], ???????????????????????????????????int n, int m) ????{ ????????int maxSubarraySum = findMaxSubarraySumUtil(A, B, ????????????????????????????????????????????????????n, m); ????????// This case will occour when all ????????// elements of A are present ????????// in B, thus no subarray can be formed ????????if (maxSubarraySum == -2147483648) { ????????????????????????System.out.println("Maximum Subarray Sum" ????????????????????????????????????????+ " " + "can't be found"); ????????} ????????else { ????????????System.out.println("The Maximum Subarray Sum = " ????????????????????????????????+ maxSubarraySum); ????????} ????} ????// Driver code ????public static void main(String[] args) ????{ ????????int A[] = { 3, 4, 5, -4, 6 }; ????????int B[] = { 1, 8, 5 }; ????????int n = A.length; ????????int m = B.length; ????????// Calling Function ????????findMaxSubarraySum(A, B, n, m); ????} } // This code is contributed by Ajit. ``` ## Python3 ```py # Python Program to find max? # subarray sum excluding some # elements? # Function to check the element? # present in array B? INT_MIN = -2147483648 def isPresent(B, m, x): ????for i in range(0, m): ????????if B[i] == x: ????????????return True ????return False # Utility function for findMaxSubarraySum()? # with the following parameters? # A => Array A,? # B => Array B,? # n => Number of elements in Array A,? # m => Number of elements in Array B? def findMaxSubarraySumUtil(A, B, n, m):? ????#set max_so_far to INT_MIN ????max_so_far = INT_MIN ????curr_max = 0 ????for i in range(0, n): ????????if isPresent(B, m, A[i]) == True: ????????????curr_max = 0 ????????????continue ????????# Proceed as in Kadane's Algorithm ????????curr_max = max(A[i], curr_max + A[i]) ????????max_so_far = max(max_so_far, curr_max) ????return max_so_far # Wrapper for findMaxSubarraySumUtil() def findMaxSubarraySum(A, B, n, m): ????maxSubarraySum = findMaxSubarraySumUtil(A, B, n, m) ????# This case will occour when all?? ????# elements of A are present? ????# in B, thus no subarray can be? ????# formed? ????if maxSubarraySum == INT_MIN: ????????print('Maximum Subarray Sum cant be found') ????else: ????????print('The Maximum Subarray Sum =',? ????????????????????????????maxSubarraySum) # Driver code A = [3, 4, 5, -4, 6 ] B = [ 1, 8, 5 ] n = len(A) m = len(B) # Calling function? findMaxSubarraySum(A, B, n, m) # This code is contributed # by sahil shelangia ``` ## C# ```cs // C# Program to find max subarray // sum excluding some elements using System; class GFG { ????// Function to check the element? ????// present in array B ????static bool isPresent(int[] B, int m,? ???????????????????????????????????int x) ????{ ????????for (int i = 0; i < m; i++) ????????????if (B[i] == x) ????????????????return true; ????????return false; ????} ????// Utility function for findMaxSubarraySum() ????// with the following parameters ????// A => Array A, ????// B => Array B, ????// n => Number of elements in Array A, ????// m => Number of elements in Array B ????static int findMaxSubarraySumUtil(int[] A, int[] B, ??????????????????????????????????????int n, int m) ????{ ????????// set max_so_far to INT_MIN ????????int max_so_far = -2147483648, curr_max = 0; ????????for (int i = 0; i < n; i++) { ????????????// if the element is present in B, ????????????// set current max to 0 and move to ????????????// the next element ????????????if (isPresent(B, m, A[i])) { ????????????????curr_max = 0; ????????????????continue; ????????????} ????????????// Proceed as in Kadane's Algorithm ????????????curr_max = Math.Max(A[i], curr_max + A[i]); ????????????max_so_far = Math.Max(max_so_far, curr_max); ????????} ????????return max_so_far; ????} ????// Wrapper for findMaxSubarraySumUtil() ????static void findMaxSubarraySum(int[] A, int[] B, ????????????????????????????????????int n, int m) ????{ ????????int maxSubarraySum = findMaxSubarraySumUtil(A, B, ????????????????????????????????????????????????????n, m); ????????// This case will occour when all ????????// elements of A are present ????????// in B, thus no subarray can be formed ????????if (maxSubarraySum == -2147483648) ????????{ ????????????Console.Write("Maximum Subarray Sum" ???????????????????????????+ " " + "can't be found"); ????????} ????????else? ????????{ ????????????Console.Write("The Maximum Subarray Sum = " ???????????????????????????+ maxSubarraySum); ????????} ????} ????// Driver Code ????static public void Main () ????{ ????????int[] A = {3, 4, 5, -4, 6}; ????????int[] B = {1, 8, 5}; ????????int n = A.Length; ????????int m = B.Length; ????????// Calling Function ????????findMaxSubarraySum(A, B, n, m); ????} } // This code is contributed by Shrikant13\. ``` ## PHP ```php <?php // PHP Program to find max subarray // sum excluding some elements // Function to check the element? // present in array B function isPresent($B, $m, $x) { ????for ($i = 0; $i < $m; $i++) ????????if ($B[$i] == $x) ????????????return true; ????return false; } // Utility function for? // findMaxSubarraySum()? // with the following? // parameters? // A => Array A, // B => Array B, // n => Number of elements //????? in Array A, // m => Number of elements? //????? in Array B? function findMaxSubarraySumUtil($A, $B, ????????????????????????????????$n, $m) { ????// set max_so_far ????// to INT_MIN ????$max_so_far = PHP_INT_MIN; ????$curr_max = 0; ????for ($i = 0; $i < $n; $i++)? ????{ ????????// if the element is present? ????????// in B, set current max to? ????????// 0 and move to the next? ????????// element? ????????if (isPresent($B, $m, $A[$i])) ????????{ ????????????$curr_max = 0; ????????????continue; ????????} ????????// Proceed as in ????????// Kadane's Algorithm? ????????$curr_max = max($A[$i],? ????????$curr_max + $A[$i]); ????????$max_so_far = max($max_so_far,? ??????????????????????????$curr_max); ????} ????return $max_so_far; } // Wrapper for? // findMaxSubarraySumUtil()? function findMaxSubarraySum($A, $B, ????????????????????????????$n, $m) { ????$maxSubarraySum = findMaxSubarraySumUtil($A, $B, ?????????????????????????????????????????????$n, $m); ????// This case will occour? ????// when all elements of? ????// A are present in ????// B, thus no subarray? ????// can be formed? ????if ($maxSubarraySum == PHP_INT_MIN)? ????{ ????????echo ("Maximum Subarray " .? ????????????"Sum cant be found\n"); ????} ????else? ????{ ????????echo ("The Maximum Subarray Sum = ".? ?????????????????????$maxSubarraySum. "\n"); ????} } // Driver Code $A = array(3, 4, 5, -4, 6); $B = array(1, 8, 5); $n = count($A); $m = count($B); // Calling function findMaxSubarraySum($A, $B, $n, $m); // This code is contributed by? // Manish Shaw(manishshaw1) ?> ``` **輸出**: ``` The Maximum Subarray Sum = 7 ``` 這種方法的時間復雜度為 O(n * m) **方法 2(O((n + m)* log(m))方法)** 該方法背后的主要思想與方法 1 完全相同。 使用 [**二分搜索**](http://www.geeksforgeeks.org/binary-search/) ,可以更快地使用數組 B 中的數組 A 的元素。 ## C++ ``` // C++ Program to find max subarray? // sum excluding some elements #include <bits/stdc++.h> using namespace std; // Utility function for findMaxSubarraySum() // with the following parameters? // A => Array A, // B => Array B, // n => Number of elements in Array A, // m => Number of elements in Array B? int findMaxSubarraySumUtil(int A[], int B[], ???????????????????????????int n, int m) { ????// set max_so_far to INT_MIN ????int max_so_far = INT_MIN, curr_max = 0; ????for (int i = 0; i < n; i++) { ????????// if the element is present in B,? ????????// set current max to 0 and move to ????????// the next element? ????????if (binary_search(B, B + m, A[i])) { ????????????curr_max = 0; ????????????continue; ????????} ????????// Proceed as in Kadane's Algorithm? ????????curr_max = max(A[i], curr_max + A[i]); ????????max_so_far = max(max_so_far, curr_max); ????} ????return max_so_far; } // Wrapper for findMaxSubarraySumUtil() void findMaxSubarraySum(int A[], int B[],? ????????????????????????int n, int m) { ????// sort array B to apply Binary Search ????sort(B, B + m); ????int maxSubarraySum = findMaxSubarraySumUtil(A, B,? ????????????????????????????????????????????????n, m); ????// This case will occour when all elements ????// of A are present in B, thus no subarray? ????// can be formed? ????if (maxSubarraySum == INT_MIN) { ????????cout << "Maximum subarray sum cant be found" ????????????<< endl; ????} ????else { ????????cout << "The Maximum subarray sum = " ?????????????<< maxSubarraySum << endl; ????} } // Driver Code int main() { ????int A[] = { 3, 4, 5, -4, 6 }; ????int B[] = { 1, 8, 5 }; ????int n = sizeof(A) / sizeof(A[0]); ????int m = sizeof(B) / sizeof(B[0]); ????// Calling fucntion ????findMaxSubarraySum(A, B, n, m); ????return 0; } ``` ## Java ```java // Java Program to find max subarray? // sum excluding some elements import java.util.*; class GFG? { ????// Utility function for findMaxSubarraySum() ????// with the following parameters? ????// A => Array A, ????// B => Array B, ????// n => Number of elements in Array A, ????// m => Number of elements in Array B? ????static int findMaxSubarraySumUtil(int A[], int B[], ????????????????????????????????????????int n, int m)? ????{ ????????// set max_so_far to INT_MIN ????????int max_so_far = Integer.MIN_VALUE, curr_max = 0; ????????for (int i = 0; i < n; i++)? ????????{ ????????????// if the element is present in B,? ????????????// set current max to 0 and move to ????????????// the next element? ????????????if (Arrays.binarySearch(B, A[i]) >= 0)? ????????????{ ????????????????curr_max = 0; ????????????????continue; ????????????} ????????????// Proceed as in Kadane's Algorithm? ????????????curr_max = Math.max(A[i], curr_max + A[i]); ????????????max_so_far = Math.max(max_so_far, curr_max); ????????} ????????return max_so_far; ????} ????// Wrapper for findMaxSubarraySumUtil() ????static void findMaxSubarraySum(int A[], int B[], ????????????????????????????????????????int n, int m)? ????{ ????????// sort array B to apply Binary Search ????????Arrays.sort(B); ????????int maxSubarraySum = findMaxSubarraySumUtil(A, B, ????????????????n, m); ????????// This case will occour when all elements ????????// of A are present in B, thus no subarray? ????????// can be formed? ????????if (maxSubarraySum == Integer.MIN_VALUE)? ????????{ ????????????System.out.println("Maximum subarray sum cant be found"); ????????}? ????????else? ????????{ ????????????System.out.println("The Maximum subarray sum = " ????????????????????+ maxSubarraySum); ????????} ????} ????// Driver Code ????public static void main(String[] args)? ????{ ????????int A[] = {3, 4, 5, -4, 6}; ????????int B[] = {1, 8, 5}; ????????int n = A.length; ????????int m = B.length; ????????// Calling fucntion ????????findMaxSubarraySum(A, B, n, m); ????} } // This code has been contributed by 29AjayKumar ``` **輸出**: ``` The Maximum subarray sum = 7 ``` 此方法的時間復雜度為 O(nlog(m)+ mlog(m))或 O((n + m)log(m))。 注意:mlog(m)因子歸因于對數組 B 的排序。 * * * * * *
                  <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>

                              哎呀哎呀视频在线观看