-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
230dc59
commit 0a216c2
Showing
4 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//{ Driver Code Starts | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
class GFG { | ||
public static void main(String[] args) { | ||
|
||
// taking input using Scanner class | ||
Scanner sc = new Scanner(System.in); | ||
|
||
// taking total testcases | ||
int t = sc.nextInt(); | ||
|
||
while (t-- > 0) { | ||
|
||
// taking total number of stairs | ||
int n = sc.nextInt(); | ||
|
||
// creating an object of class DynamicProgramming | ||
Solution obj = new Solution(); | ||
|
||
// calling method of class countWays() | ||
// of class DynamicProgramming | ||
System.out.println(obj.countWays(n)); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
|
||
class Solution | ||
{ | ||
// Function to count number of ways to reach the nth stair | ||
// when order does not matter. | ||
Long countWays(int n) | ||
{ | ||
// your code here | ||
return (long) (1 + (n / 2)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(1) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Solution | ||
{ | ||
public String minRemoveToMakeValid(String s) | ||
{ | ||
Deque<Integer> stack = new ArrayDeque<>(); | ||
StringBuilder sb = new StringBuilder(s); | ||
|
||
for (int i = 0; i < s.length(); ++i) | ||
{ | ||
if (sb.charAt(i) == '(') | ||
{ | ||
stack.push(i); | ||
} | ||
else if (sb.charAt(i) == ')') | ||
{ | ||
if (stack.isEmpty()) | ||
sb.setCharAt(i, '#'); | ||
else | ||
stack.pop(); | ||
} | ||
} | ||
|
||
while (!stack.isEmpty()) | ||
sb.setCharAt(stack.pop(), '#'); | ||
|
||
return sb.toString().replaceAll("#", ""); | ||
} | ||
} |