chore(Java): add celebrity problem and sliding window maximum (#281)

pull/288/head
priyanshu jain 2021-05-05 22:06:49 +05:30 committed by GitHub
parent 4d6a7cfa87
commit e40e475d15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 145 additions and 0 deletions

View File

@ -53,6 +53,8 @@
1. [Balanced Parenthesis](stacks/balanced-paranthesis.java)
2. [Stack](stacks/stack.java)
3. [The Stock Span Problem](stacks/the-stock-span-problem.java)
4. [Celebrity Problem](stacks/celebrity-problem.java)
5. [Sliding Window Maximum](stacks/sliding-window-maximum.java)
## Strings

View File

@ -0,0 +1,78 @@
//Problem Statement
// 1. You are given a number n, representing the number of people in a party.
// 2. You are given n strings of n length containing 0's and 1's
// 3. If there is a '1' in ith row, jth spot, then person i knows about person j.
// 4. A celebrity is defined as somebody who knows no other person than himself but everybody else knows him.
// 5. If there is a celebrity print it's index otherwise print "none".
//solution
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
// write your code here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[][] arr = new int[n][n];
for (int j = 0; j < n; j++) {
String line = br.readLine();
for (int k = 0; k < n; k++) {
arr[j][k] = line.charAt(k) - '0';
}
}
findCelebrity(arr);
}
public static void findCelebrity(int[][] arr) {
// if a celebrity is there print it's index (not position), if there is not then print "none"
Stack < Integer > st = new Stack < > ();
for (int i = 0; i < arr.length; i++) {
st.push(i);
}
while (st.size() > 1) {
int i = st.pop();
int j = st.pop();
if (arr[i][j] == 1) {
st.push(j);
} else {
st.push(i);
}
}
int pot = st.pop();
boolean flag = true;
for (int i = 0; i < arr.length; i++) {
if (i != pot) {
if (arr[i][pot] == 0 || arr[pot][i] == 1) {
flag = false;
break;
}
}
}
if (flag) {
System.out.println(pot);
} else {
System.out.println("none");
}
}
}
//Test Case:-
//Input
// 4
// 0000
// 1011
// 1101
// 1110
//Output
//0

View File

@ -0,0 +1,65 @@
//Statement
// 1. You are given a number n, representing the size of array a.
// 2. You are given n numbers, representing the elements of array a.
// 3. You are given a number k, representing the size of window.
// 4. You are required to find and print the maximum element in every window of size k.
// e.g.
// for the array [2 9 3 8 1 7 12 6 14 4 32 0 7 19 8 12 6] and k = 4, the answer is [9 9 8 12 12 14 14 32 32 32 32 19 19 19]
//solution
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = Integer.parseInt(br.readLine());
}
int k = Integer.parseInt(br.readLine());
// code
//nge is next Greater Element
// nge begin
int[] nge = new int[arr.length];
Stack<Integer> st = new Stack<>();
st.push(arr.length - 1);
nge[arr.length - 1] = arr.length;
for(int i = arr.length - 2; i >= 0; i--){
while(st.size() > 0 && arr[i] >= arr[st.peek()]){
st.pop();
}
if(st.size() == 0){
nge[i] = arr.length;
} else {
nge[i] = st.peek();
}
st.push(i);
}
// nge end
int j = 0;
for(int i = 0; i <= arr.length - k; i++){
if(j < i){
j = i;
}
while(nge[j] < i + k){
j = nge[j];
}
System.out.println(arr[j]);
}
}
}