Skip to content

Commit

Permalink
Added codes for 13 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 13, 2024
1 parent 5042fb4 commit 2e8f603
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
43 changes: 43 additions & 0 deletions GeeksForGeeks/April/13-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//{ 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));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
Long X = Long.parseLong(read.readLine());

Solution ob = new Solution();
System.out.println(ob.reversedBits(X));
}
}
}
// } Driver Code Ends


// User function Template for Java

class Solution
{
static Long reversedBits(Long x)
{
// code here
StringBuilder sb=new StringBuilder("");
while(x>0)
{
sb.append((char)(x%2+48));
x=x/2;
}

int n=32-sb.length();

for(int i=0;i<n;i++)
sb.append((char)(48));

return Long.parseLong(sb.toString(),2);
}
};
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/13-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(logx)
Space complexity - O(1)
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions LeetCode/April/13-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(m*n)
Space complexity - O(n)
41 changes: 41 additions & 0 deletions LeetCode/April/13-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution
{
public int maximalRectangle(char[][] matrix)
{
if (matrix.length == 0)
return 0;

int ans = 0;
int[] hist = new int[matrix[0].length];

for (char[] row : matrix)
{
for (int i = 0; i < row.length; ++i)
hist[i] = row[i] == '0' ? 0 : hist[i] + 1;

ans = Math.max(ans, largestRectangleArea(hist));
}

return ans;
}

private int largestRectangleArea(int[] heights)
{
int ans = 0;
Deque<Integer> stack = new ArrayDeque<>();

for (int i = 0; i <= heights.length; ++i)
{
while (!stack.isEmpty() && (i == heights.length || heights[stack.peek()] > heights[i]))
{
int h = heights[stack.pop()];
int w = stack.isEmpty() ? i : i - stack.peek() - 1;
ans = Math.max(ans, h * w);
}

stack.push(i);
}

return ans;
}
}

0 comments on commit 2e8f603

Please sign in to comment.