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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 總和大于給定值的最小子數組 > 原文: [https://www.geeksforgeeks.org/minimum-length-subarray-sum-greater-given-value/](https://www.geeksforgeeks.org/minimum-length-subarray-sum-greater-given-value/) 給定一個整數數組和一個數字 x,找到總和大于給定值的最小子數組。 ``` Examples: arr[] = {1, 4, 45, 6, 0, 19} x = 51 Output: 3 Minimum length subarray is {4, 45, 6} arr[] = {1, 10, 5, 2, 7} x = 9 Output: 1 Minimum length subarray is {10} arr[] = {1, 11, 100, 1, 0, 200, 3, 2, 1, 250} x = 280 Output: 4 Minimum length subarray is {100, 1, 0, 200} arr[] = {1, 2, 4} x = 8 Output : Not Possible Whole array sum is smaller than 8. ``` **簡單解決方案**是使用兩個嵌套循環。 外循環選擇一個開始元素,內循環將所有元素(在當前開始的右側)視為結束元素。 只要當前開始和結束之間的元素總數超過給定數目,如果當前長度小于到目前為止的最小長度,則更新結果。 以下是簡單方法的實現。 ## C ``` # include <iostream> using namespace std; // Returns length of smallest subarray with sum greater than x. // If there is no subarray with given sum, then returns n+1 int smallestSubWithSum(int arr[], int n, int x) { ????//? Initilize length of smallest subarray as n+1 ?????int min_len = n + 1; ?????// Pick every element as starting point ?????for (int start=0; start<n; start++) ?????{ ??????????// Initialize sum starting with current start ??????????int curr_sum = arr[start]; ??????????// If first element itself is greater ??????????if (curr_sum > x) return 1; ??????????// Try different ending points for curremt start ??????????for (int end=start+1; end<n; end++) ??????????{ ??????????????// add last element to current sum ??????????????curr_sum += arr[end]; ??????????????// If sum becomes more than x and length of ??????????????// this subarray is smaller than current smallest ??????????????// length, update the smallest length (or result) ??????????????if (curr_sum > x && (end - start + 1) < min_len) ?????????????????min_len = (end - start + 1); ??????????} ?????} ?????return min_len; } /* Driver program to test above function */ int main() { ????int arr1[] = {1, 4, 45, 6, 10, 19}; ????int x = 51; ????int n1 = sizeof(arr1)/sizeof(arr1[0]); ????int res1 = smallestSubWithSum(arr1, n1, x); ????(res1 == n1+1)? cout << "Not possible\n" : ????????????????????cout << res1 << endl; ????int arr2[] = {1, 10, 5, 2, 7}; ????int n2 = sizeof(arr2)/sizeof(arr2[0]); ????x? = 9; ????int res2 = smallestSubWithSum(arr2, n2, x); ????(res2 == n2+1)? cout << "Not possible\n" : ????????????????????cout << res2 << endl; ????int arr3[] = {1, 11, 100, 1, 0, 200, 3, 2, 1, 250}; ????int n3 = sizeof(arr3)/sizeof(arr3[0]); ????x? = 280; ????int res3 = smallestSubWithSum(arr3, n3, x); ????(res3 == n3+1)? cout << "Not possible\n" : ????????????????????cout << res3 << endl; ????return 0; } ``` ## Java ```java class SmallestSubArraySum { ????// Returns length of smallest subarray with sum greater than x. ????// If there is no subarray with given sum, then returns n+1 ????static int smallestSubWithSum(int arr[], int n, int x) ????{ ????????//? Initilize length of smallest subarray as n+1 ????????int min_len = n + 1; ????????// Pick every element as starting point ????????for (int start = 0; start < n; start++) ????????{ ????????????// Initialize sum starting with current start ????????????int curr_sum = arr[start]; ????????????// If first element itself is greater ????????????if (curr_sum > x) ????????????????return 1; ????????????// Try different ending points for curremt start ????????????for (int end = start + 1; end < n; end++) ????????????{ ????????????????// add last element to current sum ????????????????curr_sum += arr[end]; ????????????????// If sum becomes more than x and length of ????????????????// this subarray is smaller than current smallest ????????????????// length, update the smallest length (or result) ????????????????if (curr_sum > x && (end - start + 1) < min_len) ????????????????????min_len = (end - start + 1); ????????????} ????????} ????????return min_len; ????} ????// Driver program to test above functions ????public static void main(String[] args) ????{ ????????int arr1[] = {1, 4, 45, 6, 10, 19}; ????????int x = 51; ????????int n1 = arr1.length; ????????int res1 = smallestSubWithSum(arr1, n1, x); ????????if (res1 == n1+1) ???????????System.out.println("Not Possible"); ????????else ???????????System.out.println(res1); ????????int arr2[] = {1, 10, 5, 2, 7}; ????????int n2 = arr2.length; ????????x = 9; ????????int res2 = smallestSubWithSum(arr2, n2, x); ????????if (res2 == n2+1) ???????????System.out.println("Not Possible"); ????????else ???????????System.out.println(res2); ????????int arr3[] = {1, 11, 100, 1, 0, 200, 3, 2, 1, 250}; ????????int n3 = arr3.length; ????????x = 280; ????????int res3 = smallestSubWithSum(arr3, n3, x); ????????if (res3 == n3+1) ???????????System.out.println("Not Possible"); ????????else ???????????System.out.println(res3); ????} } // This code has been contributed by Mayank Jaiswal ``` ## Python3 ```py # Python3 program to find Smallest? # subarray with sum greater? # than a given value # Returns length of smallest subarray # with sum greater than x. If there # is no subarray with given sum,? # then returns n+1 def smallestSubWithSum(arr, n, x): ????# Initilize length of smallest ????# subarray as n+1 ????min_len = n + 1 ????# Pick every element as starting point ????for start in range(0,n): ????????# Initialize sum starting? ????????# with current start ????????curr_sum = arr[start] ????????# If first element itself is greater ????????if (curr_sum > x): ????????????return 1 ????????# Try different ending points ????????# for curremt start ????????for end in range(start+1,n): ????????????# add last element to current sum ????????????curr_sum += arr[end] ????????????# If sum becomes more than x? ????????????# and length of this subarray ????????????# is smaller than current smallest ????????????# length, update the smallest? ????????????# length (or result) ????????????if curr_sum > x and (end - start + 1) < min_len: ????????????????min_len = (end - start + 1) ????return min_len; # Driver program to test above function */ arr1 = [1, 4, 45, 6, 10, 19] x = 51 n1 = len(arr1) res1 = smallestSubWithSum(arr1, n1, x); if res1 == n1+1: ????print("Not possible") else: ????print(res1) arr2 = [1, 10, 5, 2, 7] n2 = len(arr2) x = 9 res2 = smallestSubWithSum(arr2, n2, x); if res2 == n2+1: ????print("Not possible") else: ????print(res2) arr3 = [1, 11, 100, 1, 0, 200, 3, 2, 1, 250] n3 = len(arr3) x = 280 res3 = smallestSubWithSum(arr3, n3, x) if res3 == n3+1: ????print("Not possible")? else: ????print(res3) # This code is contributed by Smitha Dinesh Semwal ``` ## C# ```cs // C# program to find Smallest? // subarray with sum greater? // than a given value using System; class GFG { ????// Returns length of smallest ????// subarray with sum greater? ????// than x. If there is no? ????// subarray with given sum, ????// then returns n+1 ????static int smallestSubWithSum(int []arr,? ??????????????????????????????????int n, int x) ????{ ????????// Initilize length of? ????????// smallest subarray as n+1 ????????int min_len = n + 1; ????????// Pick every element ????????// as starting point ????????for (int start = 0; start < n; start++) ????????{ ????????????// Initialize sum starting ????????????// with current start ????????????int curr_sum = arr[start]; ????????????// If first element ????????????// itself is greater ????????????if (curr_sum > x) ????????????????return 1; ????????????// Try different ending? ????????????// points for curremt start ????????????for (int end = start + 1;? ?????????????????????end < n; end++) ????????????{ ????????????????// add last element ????????????????// to current sum ????????????????curr_sum += arr[end]; ????????????????// If sum becomes more than ????????????????// x and length of this? ????????????????// subarray is smaller than ????????????????// current smallest length, ????????????????// update the smallest? ????????????????// length (or result) ????????????????if (curr_sum > x &&? ????????????????????????(end - start + 1) < min_len) ????????????????????min_len = (end - start + 1); ????????????} ????????} ????????return min_len; ????} ????// Driver Code ????static public void Main () ????{ ????????int []arr1 = {1, 4, 45,? ??????????????????????6, 10, 19}; ????????int x = 51; ????????int n1 = arr1.Length; ????????int res1 = smallestSubWithSum(arr1,? ??????????????????????????????????????n1, x); ????????if (res1 == n1 + 1) ????????Console.WriteLine("Not Possible"); ????????else ????????Console.WriteLine(res1); ????????int []arr2 = {1, 10, 5, 2, 7}; ????????int n2 = arr2.Length; ????????x = 9; ????????int res2 = smallestSubWithSum(arr2,? ??????????????????????????????????????n2, x); ????????if (res2 == n2 + 1) ????????Console.WriteLine("Not Possible"); ????????else ????????Console.WriteLine(res2); ????????int []arr3 = {1, 11, 100, 1, 0,? ??????????????????????200, 3, 2, 1, 250}; ????????int n3 = arr3.Length; ????????x = 280; ????????int res3 = smallestSubWithSum(arr3,? ??????????????????????????????????????n3, x); ????????if (res3 == n3 + 1) ????????Console.WriteLine("Not Possible"); ????????else ????????Console.WriteLine(res3); ????} } // This code is contributed by ajit ``` ## PHP ```php <?php // Returns length of smallest? // subarray with sum greater? // than x. If there is no? // subarray with given sum,? // then returns n+1 function smallestSubWithSum($arr, $n, $x) { ????// Initilize length of? ????// smallest subarray as n+1 ????$min_len = $n + 1; ????// Pick every element? ????// as starting point ????for ($start = 0; $start < $n; $start++) ????{ ????????// Initialize sum starting ????????// with current start ????????$curr_sum = $arr[$start]; ????????// If first element? ????????// itself is greater ????????if ($curr_sum > $x) return 1; ????????// Try different ending? ????????// points for curremt start ????????for ($end= $start + 1; $end < $n; $end++) ????????{ ????????????// add last element ????????????// to current sum ????????????$curr_sum += $arr[$end]; ????????????// If sum becomes more than? ????????????// x and length of this subarray? ????????????// is smaller than current? ????????????// smallest length, update the? ????????????// smallest length (or result) ????????????if ($curr_sum > $x &&? ???????????????????($end - $start + 1) < $min_len) ????????????????$min_len = ($end - $start + 1); ????????} ????} ????return $min_len; } // Driver Code $arr1 = array (1, 4, 45,? ???????????????6, 10, 19); $x = 51; $n1 = sizeof($arr1); $res1 = smallestSubWithSum($arr1, $n1, $x); if (($res1 == $n1 + 1) == true) ????echo "Not possible\n" ; else ????echo $res1 , "\n"; $arr2 = array(1, 10, 5, 2, 7); $n2 = sizeof($arr2); $x = 9; $res2 = smallestSubWithSum($arr2, $n2, $x); if (($res2 == $n2 + 1) == true)? ????echo "Not possible\n" ; else ????echo $res2 , "\n"; $arr3 = array (1, 11, 100, 1, 0, ???????????????200, 3, 2, 1, 250); $n3 = sizeof($arr3); $x = 280; $res3 = smallestSubWithSum($arr3, $n3, $x); if (($res3 == $n3 + 1) == true) ????echo "Not possible\n" ; else ????echo $res3 , "\n"; // This code is contributed by ajit ?> ``` **輸出**: ``` 3 1 4 ``` **時間復雜度**:上述方法的時間復雜度顯然為 `O(n^2)`。 **有效解決方案**:可以使用[此](https://www.geeksforgeeks.org/find-subarray-with-given-sum/)帖子中的想法,在 **`O(n)`時間**中解決此問題。 感謝 Ankit 和 Nitin 提出這個優化的解決方案。 ## C++ ```cpp // O(n) solution for finding smallest subarray with sum // greater than x #include <iostream> using namespace std; // Returns length of smallest subarray with sum greater than x. // If there is no subarray with given sum, then returns n+1 int smallestSubWithSum(int arr[], int n, int x) { ????// Initialize current sum and minimum length ????int curr_sum = 0, min_len = n+1; ????// Initialize starting and ending indexes ????int start = 0, end = 0; ????while (end < n) ????{ ????????// Keep adding array elements while current sum ????????// is smaller than x ????????while (curr_sum <= x && end < n) ????????????curr_sum += arr[end++]; ????????// If current sum becomes greater than x. ????????while (curr_sum > x && start < n) ????????{ ????????????// Update minimum length if needed ????????????if (end - start < min_len) ????????????????min_len = end - start; ????????????// remove starting elements ????????????curr_sum -= arr[start++]; ????????} ????} ????return min_len; } /* Driver program to test above function */ int main() { ????int arr1[] = {1, 4, 45, 6, 10, 19}; ????int x = 51; ????int n1 = sizeof(arr1)/sizeof(arr1[0]); ????int res1 = smallestSubWithSum(arr1, n1, x); ????(res1 == n1+1)? cout << "Not possible\n" : ????????????????????cout << res1 << endl; ????int arr2[] = {1, 10, 5, 2, 7}; ????int n2 = sizeof(arr2)/sizeof(arr2[0]); ????x? = 9; ????int res2 = smallestSubWithSum(arr2, n2, x); ????(res2 == n2+1)? cout << "Not possible\n" : ????????????????????cout << res2 << endl; ????int arr3[] = {1, 11, 100, 1, 0, 200, 3, 2, 1, 250}; ????int n3 = sizeof(arr3)/sizeof(arr3[0]); ????x? = 280; ????int res3 = smallestSubWithSum(arr3, n3, x); ????(res3 == n3+1)? cout << "Not possible\n" : ????????????????????cout << res3 << endl; ????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>

                              哎呀哎呀视频在线观看