0% found this document useful (0 votes)
2 views5 pages

Exp 2

The document outlines two programming problems from a computer science lab: Jump Game II and Simplify Path. For Jump Game II, the goal is to determine the minimum number of jumps needed to reach the last index of an array based on jump lengths. The Simplify Path problem involves transforming a Unix-style absolute path into its simplified canonical form using a stack to manage valid directories.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Exp 2

The document outlines two programming problems from a computer science lab: Jump Game II and Simplify Path. For Jump Game II, the goal is to determine the minimum number of jumps needed to reach the last index of an array based on jump lengths. The Simplify Path problem involves transforming a Unix-style absolute path into its simplified canonical form using a stack to manage valid directories.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

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:

Step 1: Initialize Variables:

• n = nums.size(): Get the size of the array.

• If n == 1, return 0 because no jumps are needed.

• Initialize jumps = 0 (stores the count of jumps).

• Initialize currentEnd = 0 (boundary of the current jump).

• Initialize farthest = 0 (farthest index that can be reached).

Step 2: Traverse the Array (0 to n-2):

• For each index i, update farthest = max(farthest, i + nums[i]) to


determine the farthest index we can reach.

• If i reaches currentEnd, it means we must jump to go further.

➢ Increment jumps.

➢ Update currentEnd = farthest.

➢ If currentEnd >= n - 1, break the loop as we can reach the last


index.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Step 3: Return jumps as the minimum number of jumps required.


4. Code:
class Solution {
public:
int jump(vector<int>& nums) {
int n = nums.size();
if (n == 1) return 0; // If there's only one element, no jumps are needed

int jumps = 0;
int currentEnd = 0;
int farthest = 0; // Farthest index reachable overall

for (int i = 0; i < n - 1; i++) {


// Update the farthest index reachable from the current position
farthest = max(farthest, i + nums[i]);

// If we reach the end of the current range


if (i == currentEnd) {
jumps++; // Increment jump count
currentEnd = farthest;
if (currentEnd >= n - 1) break;
}
}
return jumps;
}
};

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:

ans = "": To store the simplified path.

stack = []: To keep track of valid directories.

Step 2: Tokenize the Path:

Use getline() to split the input path by /.

Step 3: Process Each Token:

If the token is empty or . , skip it.

If the token is .. , pop from the stack (if not empty).

Otherwise, push the token (valid directory) onto the stack.

Step 4: Build the Simplified Path:

Concatenate each directory from the stack with /.

If the stack is empty, return /.

Step 5: Return the Result:

Return the final simplified path.

4. Code:

class Solution {

public:

string simplifyPath(string path) {

string ans;

istringstream iss(path);

vector<string> stack;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
for (string dir; getline(iss, dir, '/');) {

if (dir.empty() || dir == ".")

continue;

if (dir == "..") {

if (!stack.empty())

stack.pop_back();

} else {

stack.push_back(dir);

}}

for (const string& s : stack)

ans += "/" + s;

return ans.empty() ? "/" : ans;

};

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 .. .

You might also like