Manan AP 3.3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment No. : 3.3

Student Name: Manan UID: 21BCS9258


Branch: BE CSE Section: 21BCS_CC_647(A)
Subject Name: Advanced Semester: Sixth
Programming Lab-2
Date of Performance: 11/04/2024 Subject Code: 21CSH-351

Aim: To demonstrate the concept of Dynamic Programming.

Objective: To learn about the fundamentals of Dynamic Programming approaches.

Problem Statements:

i. You are climbing a staircase. It takes n steps to reach the top.


Each time you can either climb 1 or 2 steps. In how many distinct ways can you
climb to the top?

ii. Given an integer array nums, find the subarray with the largest sum, and
return its sum.

Input/Apparatus Used:

Hardware requirements: Minimum 384MB RAM, 100 GB hard Disk,


processor with 2.1 MHz
Software requirements: LeetCode
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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;

for (int i = 2; i <= n; ++i)


dp[i] = dp[i - 1] + dp[i - 2];

return dp[n];
}
};

ii. Maximum Subarray:

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:

ii. Maximum Subarray:

Learning outcomes:

i. Gained a clear understanding of the fundamental data structures.


ii. Learnt about the problem solving using Dynamic Programming method.
iii. Learnt how to implement various approaches of Dynamic Programming.

You might also like