diff --git a/Domains/CompetitiveProgramming/Programs/C++/Jump_Game.cpp b/Domains/CompetitiveProgramming/Programs/C++/Jump_Game.cpp new file mode 100644 index 00000000..e5ac4464 --- /dev/null +++ b/Domains/CompetitiveProgramming/Programs/C++/Jump_Game.cpp @@ -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 +#include +#include +#include +using namespace std; + +class Solution { +public: + bool canJump(vector& 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; + } +}; diff --git a/Domains/CompetitiveProgramming/Programs/C++/Valid_Parentheses.cpp b/Domains/CompetitiveProgramming/Programs/C++/Valid_Parentheses.cpp new file mode 100644 index 00000000..98a31fd7 --- /dev/null +++ b/Domains/CompetitiveProgramming/Programs/C++/Valid_Parentheses.cpp @@ -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 +#include +#include +#include +using namespace std; + +class Solution { +public: + bool isValid(string s) { + stack 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 testCases = {"()", "()[]{}", "(]", "([])", "([)]"}; + for (auto &s : testCases) { + cout << s << " -> " << (sol.isValid(s) ? "Valid" : "Invalid") << endl; + } + return 0; +}