Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Domains/CompetitiveProgramming/Programs/C++/Jump_Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Problem: Jump Game
Platform: LeetCode
Problem Code/Number: 55
Difficulty: Medium
Link: https://leetcode.com/problems/jump-game/

Problem Statement:
You are given an integer array nums. You are initially positioned at the array's first index,
and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.

Approach:
Use a greedy approach to track the farthest index you can reach:
- Initialize a variable 'farthest' to 0.
- Traverse the array:
- If the current index i is greater than 'farthest', return false (stuck, cannot move forward).
- Update 'farthest' as max(farthest, i + nums[i]).
- If 'farthest' >= last index, return true.
- If the loop ends, return true.

Time Complexity: O(n)
Space Complexity: O(1)

Contributor: alisha1510
*/

#include <iostream>
#include <stack>
#include <vector>
#include <string>
using namespace std;

class Solution {
public:
bool canJump(vector<int>& nums) {
int farthest = 0;
int n = nums.size();

for (int i = 0; i < n; i++) {
if (i > farthest) return false;
farthest = max(farthest, i + nums[i]);
if (farthest >= n - 1) return true;
}

return true;
}
};
64 changes: 64 additions & 0 deletions Domains/CompetitiveProgramming/Programs/C++/Valid_Parentheses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Problem: Valid Parentheses
Platform: LeetCode
Problem Code/Number: 20
Difficulty: Easy
Link: https://leetcode.com/problems/valid-parentheses/

Problem Statement:
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']',
determine if the input string is valid.
An input string is valid if:
1. Open brackets must be closed by the same type of brackets.
2. Open brackets must be closed in the correct order.
3. Every close bracket has a corresponding open bracket of the same type.

Approach:
Use a stack to store opening brackets. Traverse the string:
- Push opening brackets onto the stack.
- For closing brackets, check if the top of the stack matches the corresponding opening bracket.
- If it matches, pop the stack; otherwise, the string is invalid.
- The string is valid if the stack is empty at the end.

Time Complexity: O(n)
Space Complexity: O(n)

Contributor: alisha1510
*/

#include <iostream>
#include <stack>
#include <vector>
#include <string>
using namespace std;

class Solution {
public:
bool isValid(string s) {
stack<char> st;
for (char c : s) {
if (c == '(' || c == '{' || c == '[') {
st.push(c);
} else {
if (st.empty()) return false;
char top = st.top();
if ((c == ')' && top != '(') ||
(c == '}' && top != '{') ||
(c == ']' && top != '[')) {
return false;
}
st.pop();
}
}
return st.empty();
}
};

int main() {
Solution sol;
vector<string> testCases = {"()", "()[]{}", "(]", "([])", "([)]"};
for (auto &s : testCases) {
cout << s << " -> " << (sol.isValid(s) ? "Valid" : "Invalid") << endl;
}
return 0;
}
Loading