Skip to content

Commit

Permalink
Added codes for 22 and 23 June
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jun 23, 2024
1 parent 5f5e7cf commit 58828e0
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 0 deletions.
41 changes: 41 additions & 0 deletions GeeksForGeeks/June/22-6-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//{ Driver Code Starts
// Initial Template for Java

import java.io.*;
import java.util.*;
import java.util.regex.*;

class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
String S = read.readLine();
Solution ob = new Solution();
System.out.println(ob.ExtractNumber(S));
}
}
}
// } Driver Code Ends


// User function Template for Java

class Solution
{
long ExtractNumber(String sentence)
{
// code here
long result = -1;
String[] strArray = sentence.split(" ");
for(int i=0;i<strArray.length;i++)
{
if(Character.isDigit(strArray[i].charAt(0)))
{
if(!(strArray[i].contains("9")))
result = Math.max(result, Long.parseLong(strArray[i]));
}
}
return result;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/June/22-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
50 changes: 50 additions & 0 deletions GeeksForGeeks/June/23-6-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//{ Driver Code Starts
// Initial Template for Java

import java.io.*;
import java.util.*;

class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
String S = read.readLine();
Solution ob = new Solution();
ArrayList<Integer> result = ob.bracketNumbers(S);
for (int value : result) out.print(value + " ");
out.println();
}
out.close();
}
}
// } Driver Code Ends


// User function Template for Java

class Solution
{
ArrayList<Integer> bracketNumbers(String str)
{
// code here
ArrayList<Integer> list = new ArrayList<>();
Stack<Integer> st = new Stack<>();
int i = 1;
for(char ch : str.toCharArray())
{
if(ch=='(')
{
st.push(i++);
list.add(st.peek());
}
else if(!st.isEmpty() && ch == ')' )
{
list.add(st.pop());
}
}

return list;
}
};
2 changes: 2 additions & 0 deletions GeeksForGeeks/June/23-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(|str|)
Space complexity - O(|str|)
2 changes: 2 additions & 0 deletions LeetCode/June/22-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
30 changes: 30 additions & 0 deletions LeetCode/June/22-6-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public int numberOfSubarrays(int[] nums, int k) {
return numberOfSubarraysAtMost(nums, k) - numberOfSubarraysAtMost(nums, k - 1);
}

private int numberOfSubarraysAtMost(int[] nums, int k)
{
int ans = 0;

for (int l = 0, r = 0; r <= nums.length;)
{
if (k >= 0)
{
ans += r - l;
if (r == nums.length)
break;
if (nums[r] % 2 == 1)
--k;
++r;
}
else
{
if (nums[l] % 2 == 1)
++k;
++l;
}
}
return ans;
}
}
2 changes: 2 additions & 0 deletions LeetCode/June/23-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
36 changes: 36 additions & 0 deletions LeetCode/June/23-6-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public int longestSubarray(int[] nums, int limit) {
Deque<Integer> decQ = new LinkedList<>();
Deque<Integer> incQ = new LinkedList<>();
int ans = 0;
int left = 0;

for (int right = 0; right < nums.length; ++right) {
int num = nums[right];

while (!decQ.isEmpty() && num > decQ.peekLast()) {
decQ.pollLast();
}
decQ.addLast(num);

while (!incQ.isEmpty() && num < incQ.peekLast()) {
incQ.pollLast();
}
incQ.addLast(num);

while (decQ.peekFirst() - incQ.peekFirst() > limit) {
if (decQ.peekFirst() == nums[left]) {
decQ.pollFirst();
}
if (incQ.peekFirst() == nums[left]) {
incQ.pollFirst();
}
++left;
}

ans = Math.max(ans, right - left + 1);
}

return ans;
}
}

0 comments on commit 58828e0

Please sign in to comment.