Hướng dẫn subsequence sum in python - tổng số con trong python

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọcarr[] consisting of N integers and an integer K, the task is to print the maximum sum possible in a subsequence satisfying the following conditions: 
     

    1. Bàn luậnarr[N – 1] and arr[0] are included in the subsequence.
    2. Đưa ra một mảng mảng [] bao gồm n số nguyên và số nguyên k, nhiệm vụ là in tổng tối đa có thể trong một phần sau đáp ứng các điều kiện sau: & nbsp; & nbsp;K indices.

    Examples:

    Các phần tử ARR [N - 1] và ARR [0] được bao gồm trong phần sau. arr[] = {10, -5, -2, 4, 0, 3}, K = 3
    Output: 17
    Explanation:
    One of possible way is as follows:
    Include arr[0] into the subsequence. Sum = 10.
    Include arr[3] in the subsequence. Therefore, sum = 10 + 4 = 14.
    Include arr[5] in the subsequence. Therefore, total sum = 14 + 3 = 17.
    Therefore, the maximum sum possible is 17.

    Các yếu tố liền kề trong phần sau có thể ở khoảng cách tại hầu hết các chỉ số k. arr[] = {1, -5, -20, 4, -1, 3, -6, -3}, K = 2
    Output: 0

    Đầu vào: ARR [] = {10, -5, -2, 4, 0, 3}, k = 3Output: 17Explanation: Một trong những cách có thể như sau: Bao gồm ARR [0] vào sau. Sum = 10.include mảng [3] trong phần sau. Do đó, sum = 10 + 4 = 14. bao gồm mảng [5] trong phần sau. Do đó, tổng tổng = 14 + 3 = 17. Do đó, tổng tối đa có thể là 17. The simplest approach is to find all subsequences possible from arr[] with at most K difference between indices of adjacent elements, starting from index 0 and ending at index [N – 1]. Calculate sum of all such subsequences. Finally, print the maximum of all the sums obtained. 
    Time Complexity: O[N*2N]
    Auxiliary Space: O[N]

    Đầu vào: mảng [] = {1, -5, -20, 4, -1, 3, -6, -3}, k = 2Output: 0 The above approach can be optimized by using a Greedy Algorithm and deque. Follow the steps below to solve the problem:

    • Cách tiếp cận ngây thơ: Cách tiếp cận đơn giản nhất là tìm tất cả các Trình tiếp theo có thể từ ARR [] với hầu hết các khác biệt k giữa các chỉ số của các phần tử liền kề, bắt đầu từ INDEX 0 và kết thúc tại INDEX [N - 1]. Tính tổng của tất cả các chuỗi như vậy. Cuối cùng, in tối đa của tất cả các tổng thu được.dp[], to store the maximum value obtained till the current index.
    • Cách tiếp cận hiệu quả: Cách tiếp cận trên có thể được tối ưu hóa bằng cách sử dụng thuật toán tham lam và deque. Thực hiện theo các bước dưới đây để giải quyết vấn đề:Q, to store the pair {dp[i], i}.
    • Khởi tạo một mảng, giả sử DP [], để lưu trữ giá trị tối đa thu được cho đến khi chỉ số hiện tại. arr[0] to dp[0] and push the pair {dp[0], 0} into the deque.
    • Khởi tạo một deque của các cặp, nói q, để lưu trữ cặp {dp [i], i}.arr[] using the variable i and perform the following steps:
      • Chỉ định giá trị của mảng [0] cho dp [0] và đẩy cặp {dp [0], 0} vào deque.dp[i] by the sum of arr[i] and maximum value in the deque, i.e. dp[i] = arr[i] + Q[0][0].
      • Traverse mảng đã cho [] bằng cách sử dụng biến I và thực hiện các bước sau: Q from the end and pop the last element if Q[-1][0] is less than dp[i].
      • Tăng dp [i] bởi tổng của mảng [i] và giá trị tối đa trong deque, tức là dp [i] = mảng [i] + q [0] [0]. {dp[i], i} in the deque.
      • Traverse qua deque q từ cuối và bật phần tử cuối cùng nếu q [-1] [0] nhỏ hơn dp [i].q is equal to [i – K] or not and then, pop the first element from the deque Q.
    • Nối lại cặp {dp [i], i} trong deque.dp[], i.e. dp[N – 1] as the result.

    Kiểm tra xem chỉ số của phần tử đầu tiên của deque q có bằng [i - k] hay không và sau đó, hãy bật phần tử đầu tiên từ deque q.

    C++

    #include

    Sau khi hoàn thành các bước trên, in giá trị được lưu trữ tại chỉ số cuối cùng của DP [], tức là DP [N - 1] do kết quả.

    Dưới đây là việc thực hiện phương pháp trên:

    using namespace std;

    using2using6

    using2using8intnamespace0intnamespace2

    using2namespace4

    int maxResult[int arr[], int k, int using1

    using2std;1

    std;2std;3

    using2int using4

    std;7std;8

    std;2int0

    using2namespace6 namespace7int namespace9

    std;7int5

    using2int7

    std;2std;5 std;6

    int7

    std;2int2 int3

    std;1

    using2int9 maxResult[0

    int maxResult[3

    using2int maxResult[7

    using2int9

    int7

    using2int int0

    using2int int3int4int5int444767

    Java

    arr[], 1 arr[], 2

    arr[], 1 arr[], 4

    arr[], 1 arr[], 6

    arr[], 7 arr[], 8 arr[], 9

    std;2int1 arr[], 8 int3

    k, 3k, 4k, 5

    k, 3k, 4k, 8

    int4int7

    std;2int7

    int4int int6

    int4int8int k, 0int k, 2

    int4using21using22using23using22using25

    std;2using04 int1 int

    int4int using16using17 intusing19

    int4using27using17 using29

    int4std;1

    using47using48

    int4using31using17 using33using2222135using222

    k, 3using55

    int4namespace6 namespace7int using42using43using44

    using47std;5 using51using22 using53

    using47using31using17 using59

    int4int7

    using47int2 using62

    std;2int7

    k, 3using64

    std;2std;1

    int4int9 using69using43using25

    std;2arr[], 7 int1 using77 using78

    int4int using83using84using85using86using85using88__

    int4namespace05

    std;2int7

    int7

    Python3

    int4int using98using94namespace00

    int4int namespace03

    namespace09 namespace10arr[], 1 namespace12

    namespace13 namespace14

    Các

    std;2namespace6 namespace42namespace43 namespace44namespace7using43using89namespace2222249

    int4namespace51namespace17 namespace53namespace54 namespace55using222257__

    int4std;5 namespace33namespace63 namespace55namespace65using43namespace57using222269

    k, 3namespace71

    int4namespace73

    int4int2 namespace42namespace65 namespace78__

    k, 3namespace87

    std;2int9 using21namespace65using43namespace20

    namespace94namespace17 namespace18using84using89namespace65using86__

    std;11namespace17 using94

    std;14std;15

    C#

    using std;17

    using std;19

    using std;21

    using std;23

    arr[], 8 std;25

    std;1

    using2arr[], 8 int3

    std;2arr[], 7 int int6

    std;2arr[], 7 int8int k, 0int k, 2

    std;7k, 4k, 5

    std;7k, 4k, 8

    std;2int7

    using2int7

    using2int1 int

    std;2intstd;63using17 intusing19

    std;2using6

    std;2std;70using17 std;72

    std;2int std;75

    std;2std;777____117 std;79

    std;2std;81

    std;2namespace6 namespace7int std;86

    std;2std;1

    std;7std;90

    std;7std;5 std;93

    int4std;95

    std;7int2std;98

    int4std;777____117 using59

    std;7int7int05std;1

    int4int08using17 int10

    std;7int7

    std;7std;81

    std;7int2 int17

    int4int19

    std;2int7

    std;2int9 maxResult[0

    using2int7

    using2arr[], 7 int1 using77 int31int32int33

    std;2intint36using17 intint39

    std;2int int0

    std;2int int45

    std;2int47

    using2int7

    int7

    Độ phức tạp về thời gian: O [N] Không gian phụ trợ: O [N] O[N]
    Auxiliary Space: O[N]


    Chủ Đề