Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f91f1e0

Browse files
MMT continued 2
1 parent b086170 commit f91f1e0

File tree

4 files changed

+221
-0
lines changed

4 files changed

+221
-0
lines changed

‎MakeMyTrip/LRUCache.java‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// { Driver Code Starts
2+
import java.io.*;
3+
import java.util.*;
4+
import java.lang.*;
5+
6+
public class LRUDesign {
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader read =
10+
new BufferedReader(new InputStreamReader(System.in));
11+
12+
int t = Integer.parseInt(read.readLine());
13+
14+
while (t-- > 0) {
15+
16+
int capacity = Integer.parseInt(read.readLine());
17+
int queries = Integer.parseInt(read.readLine());
18+
LRUCache cache = new LRUCache(capacity);
19+
String str[] = read.readLine().trim().split(" ");
20+
int len = str.length;
21+
int itr = 0;
22+
23+
for (int i = 0; (i < queries) && (itr < len); i++) {
24+
String queryType = str[itr++];
25+
if (queryType.equals("SET")) {
26+
int key = Integer.parseInt(str[itr++]);
27+
int value = Integer.parseInt(str[itr++]);
28+
cache.set(key, value);
29+
}
30+
if (queryType.equals("GET")) {
31+
int key = Integer.parseInt(str[itr++]);
32+
System.out.print(cache.get(key) + " ");
33+
}
34+
}
35+
System.out.println();
36+
}
37+
}
38+
}
39+
// } Driver Code Ends
40+
41+
42+
// design the class in the most optimal way
43+
class LRUCache
44+
{
45+
Map<Integer,Integer> map ;
46+
int capacity ;
47+
48+
public LRUCache(int N) {
49+
50+
this.map = new LinkedHashMap<Integer,Integer>();
51+
this.capacity = N;
52+
}
53+
54+
public int get(int x) {
55+
int key = x;
56+
if(!map.containsKey(key)) return -1;
57+
int value = map.get(key);
58+
if(map.size()>1) {
59+
map.remove(key);
60+
map.put(key,value);
61+
}
62+
return value;
63+
}
64+
65+
public void set(int x, int y) {
66+
int key = x;
67+
int value = y;
68+
if(!map.containsKey(key)) {
69+
if(map.size()==capacity) {
70+
int firstKey = map.keySet().iterator().next();
71+
map.remove(firstKey);
72+
}
73+
map.put(key,value);
74+
} else {
75+
map.remove(key);
76+
map.put(key,value);
77+
}
78+
}
79+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*package whatever //do not write package name here */
2+
3+
import java.util.*;
4+
import java.lang.*;
5+
import java.io.*;
6+
7+
class GFG {
8+
public static void main (String[] args) {
9+
//code
10+
Scanner sc = new Scanner(System.in);
11+
int t = sc.nextInt();
12+
while(t-->0){
13+
int n = sc.nextInt();
14+
int[] arr = new int[n];
15+
for(int i=0;i<n;i++) arr[i] = sc.nextInt();
16+
boolean[] bool = new boolean[10];
17+
for(int i:arr){
18+
char[] carr = (i+"").toCharArray();
19+
for(char c:carr){
20+
bool[c-'0'] = true;
21+
}
22+
23+
}
24+
for(int k=0;k<10;k++){
25+
if(bool[k]) System.out.print(k+" ");
26+
}
27+
System.out.println();
28+
}
29+
}
30+
}

‎MakeMyTrip/specialStack.java‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// { Driver Code Starts
2+
import java.util.Scanner;
3+
import java.util.Stack;
4+
class SpeStack{
5+
public static void main(String[] args){
6+
Scanner sc=new Scanner(System.in);
7+
int t=sc.nextInt();
8+
while(t-->0){
9+
int n=sc.nextInt();
10+
Stack<Integer> s=new Stack<>();
11+
GfG g=new GfG();
12+
while(!g.isFull(s,n)){
13+
g.push(sc.nextInt(),s);
14+
}
15+
System.out.println(g.min(s));
16+
}
17+
}
18+
}// } Driver Code Ends
19+
20+
/*Complete the function(s) below*/
21+
class GfG{
22+
public int minVal = Integer.MAX_VALUE;
23+
public void push(int a,Stack<Integer> s)
24+
{
25+
//add code here.
26+
minVal = Math.min(minVal,a);
27+
s.push(a);
28+
}
29+
public int pop(Stack<Integer> s)
30+
{
31+
//add code here.
32+
int top = s.pop();
33+
if(top==minVal){
34+
Stack<Integer> s2 = new Stack<>();
35+
minVal = Integer.MAX_VALUE;
36+
while(!s.isEmpty()){
37+
int c = s.pop();
38+
minVal = Math.min(minVal,c);
39+
s2.push(c);
40+
}
41+
while(!s2.isEmpty()) s.push(s2.pop());
42+
}
43+
return top;
44+
}
45+
public int min(Stack<Integer> s)
46+
{
47+
//add code here.
48+
return minVal;
49+
}
50+
public boolean isFull(Stack<Integer>s, int n)
51+
{
52+
//add code here.
53+
return s.size()==n;
54+
}
55+
public boolean isEmpty(Stack<Integer>s)
56+
{
57+
//add code here.
58+
return s.isEmpty();
59+
}
60+
}

‎MakeMyTrip/thief_jumps.java‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
import java.util.*;
3+
import java.lang.*;
4+
import java.io.*;
5+
6+
class GFG {
7+
public static void main (String[] args) {
8+
//code
9+
GFG gfg = new GFG();
10+
Scanner sc = new Scanner(System.in);
11+
12+
int testCases, x, y, n, count, h[], t;
13+
testCases = sc.nextInt();
14+
15+
for(int i = 0; i < testCases; i++)
16+
{
17+
x = sc.nextInt();
18+
y = sc.nextInt();
19+
n = sc.nextInt();
20+
h = new int[n];
21+
count = 0;
22+
23+
for(int j = 0; j < n; j++)
24+
{
25+
h[j] = sc.nextInt();
26+
}
27+
28+
for(int j = 0; j < n; j++)
29+
{
30+
if(h[j] <= x)
31+
{
32+
count++;
33+
}
34+
else
35+
{
36+
t = h[j];
37+
38+
while(t > x)
39+
{
40+
count++;
41+
t = t - (x - y);
42+
}
43+
count++;
44+
}
45+
}
46+
47+
System.out.println(count);
48+
}
49+
50+
}
51+
52+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /