Manan AP 3.3
Manan AP 3.3
Manan AP 3.3
Problem Statements:
ii. Given an integer array nums, find the subarray with the largest sum, and
return its sum.
Input/Apparatus Used:
Program Code:
i. Climbing Stairs:
class Solution {
public:
int climbStairs(int n) {
// dp[i] := the number of ways to climb to the i-th stair
vector<int> dp(n + 1);
dp[0] = 1;
dp[1] = 1;
return dp[n];
}
};
class Solution {
public:
int maxSubArray(vector<int>& nums) {
// dp[i] := the maximum sum subarray ending in i
vector<int> dp(nums.size());
dp[0] = nums[0];
for (int i = 1; i < nums.size(); ++i)
dp[i] = max(nums[i], dp[i - 1] + nums[i]);
return ranges::max(dp);
}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Result/Output:
i. Climbing Stairs:
Learning outcomes: