Exp 2
Exp 2
Experiment 2
Student Name: Aakanksha Sharma UID: 22BCS16783
Branch: BE-CSE Section/Group: 632 /A
Semester: 5th Date of Performance: 27/01/25
Subject Name: AP LAB-2 Subject Code:22CSP-314
Problem 1
1. Aim: Jump game II.
2. Objectives: You are given a 0-indexed array of integers nums of length n. You are initially
positioned at nums[0]. Each element nums[i] represents the maximum length of a forward
jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j]
where: 0 <= j <= nums[i] and i + j < n.
Return the minimum number of jumps to reach nums[n - 1]. The test cases are
generated such that you can reach nums[n - 1].
3. Algorithm:
➢ Increment jumps.
int jumps = 0;
int currentEnd = 0;
int farthest = 0; // Farthest index reachable overall
5. Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Problem 2
1. Aim: Simplify path.
2. Objective: You are given an absolute path for a Unix-style file system, which always
begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical
path.
3. Algorithm:
Step1: Initialize Variables:
4. Code:
class Solution {
public:
string ans;
istringstream iss(path);
vector<string> stack;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
for (string dir; getline(iss, dir, '/');) {
continue;
if (dir == "..") {
if (!stack.empty())
stack.pop_back();
} else {
stack.push_back(dir);
}}
ans += "/" + s;
};
5. Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Learning outcomes:
• Learn how to split a string into parts (tokens) using delimiters (e.g., / in file
paths).
• Gain practice in constructing a new string based on conditions, which is a useful skill for
various string-related problems.
• Learn to apply the greedy approach and stack-based data structure to solve real-world
problems like file path normalization.
• Learn how to efficiently simplify a path by using a stack to manage valid directories and
navigate between directories with .. .