Share leetcode 75 c++,

thanhtuyen668

New member
..

LeetCode 75 là một vấn đề kinh điển trong cuộc phỏng vấn mã hóa.Nó yêu cầu bạn tìm Subarray tiếp giáp với số tiền lớn nhất.

Giải pháp ngây thơ là lặp lại trên tất cả các subarrays và tìm một cái có số tiền lớn nhất.Tuy nhiên, giải pháp này có độ phức tạp về thời gian của O (n^2), quá chậm đối với các mảng lớn.

Một giải pháp hiệu quả hơn là sử dụng phương pháp lập trình động.Chúng ta có thể xác định chức năng sau:

`` `
MAX_SUBARRAY_SUM (i) = max (a , max_subarray_sum (i - 1) + a )
`` `

Hàm này trả về tổng tối đa của kết thúc Subarray tại Index I.Chúng ta có thể tính toán hàm này theo cách đệ quy, bắt đầu từ i = 0. Trường hợp cơ sở là khi I = 0, trong trường hợp đó tổng tối đa chỉ đơn giản là [0].

Khi chúng tôi đã tính toán hàm MAX_Subarray_Sum (i), chúng tôi có thể tìm thấy SubArray tối đa bằng cách tìm Chỉ mục I trong đó MAX_Subarray_Sum (i) được tối đa hóa.

Sau đây là mã C ++ cho giải pháp lập trình động cho LeetCode 75:

`` `C ++
#include <Istream>
#include <Vector>

sử dụng không gian tên STD;

int main () {
// Tạo một mảng đầu vào
vector <int> a = {-2, 1, -3, 4, -1, 2, 1, -5, 4};

// Tính tổng Subarray tối đa bằng cách sử dụng lập trình động
int max_subarray_sum = a [0];
for (int i = 1; i <a.size (); i ++) {
MAX_SUBARRAY_SUM = MAX (A , MAX_SUBARRAY_SUM + A );
}

// In tổng Subarray tối đa
cout << Max_Subarray_Sum << endl;

trả lại 0;
}
`` `

Giải pháp này có độ phức tạp về thời gian của O (N) và độ phức tạp không gian của O (1).

### Tài nguyên bổ sung

* [Giải pháp LeetCode 75] (https://leetcode.com/problems/maximum-ubarray/solution/)
* [Hướng dẫn lập trình động] (
)
* [Chuẩn bị phỏng vấn mã hóa] (Programming - InterviewBit)
=======================================
#LeetCode, #C++, #Programming, #Interview, #Problem-solving ### Leetcode 75 C++: Solution and Explanation

Leetcode 75 is a classic problem in the coding interview. It asks you to find the contiguous subarray with the largest sum.

The naive solution is to iterate over all subarrays and find the one with the largest sum. However, this solution has a time complexity of O(n^2), which is too slow for large arrays.

A more efficient solution is to use a dynamic programming approach. We can define the following function:

```
max_subarray_sum(i) = max(A, max_subarray_sum(i - 1) + A)
```

This function returns the maximum sum of the subarray ending at index i. We can compute this function recursively, starting from i = 0. The base case is when i = 0, in which case the maximum sum is simply A[0].

Once we have computed the function max_subarray_sum(i), we can find the maximum subarray by finding the index i where max_subarray_sum(i) is maximized.

The following is the C++ code for the dynamic programming solution to Leetcode 75:

```c++
#include <iostream>
#include <vector>

using namespace std;

int main() {
// Create an input array
vector<int> A = {-2, 1, -3, 4, -1, 2, 1, -5, 4};

// Compute the maximum subarray sum using dynamic programming
int max_subarray_sum = A[0];
for (int i = 1; i < A.size(); i++) {
max_subarray_sum = max(A, max_subarray_sum + A);
}

// Print the maximum subarray sum
cout << max_subarray_sum << endl;

return 0;
}
```

This solution has a time complexity of O(n) and a space complexity of O(1).

### Additional Resources

* [Leetcode 75 Solution](https://leetcode.com/problems/maximum-subarray/solution/)
* [Dynamic Programming Tutorial](https://www.youtube.com/watch?v=vnq36996-84)
* [Coding Interview Prep](https://www.interviewbit.com/courses/programming/)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top