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 62ade42

Browse files
author
C5141506
committed
Leetcode problem
1 parent 91f4c60 commit 62ade42

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package java_problem.array;
2+
3+
public class StrictlyIncreasing {
4+
public static void main(String[] args) {
5+
6+
int[] nums = new int[]{1,2,3,4, 3 ,5
7+
};
8+
boolean b = solution(nums);
9+
System.out.println(b);
10+
}
11+
12+
public static boolean solution(int[] sequence) {
13+
int n = sequence.length;
14+
if (n < 2) return true;
15+
int max = Integer.MIN_VALUE;
16+
int maxFalseCount = 0;
17+
for (int val : sequence) {
18+
if (val > max)
19+
max = val;
20+
else
21+
maxFalseCount++;
22+
}
23+
24+
int min = Integer.MAX_VALUE;
25+
int minFalseCount = 0;
26+
for (int i = n - 1; i >= 0; i--) {
27+
if (sequence[i] < min)
28+
min = sequence[i];
29+
else
30+
minFalseCount++;
31+
}
32+
return Math.min(minFalseCount,maxFalseCount)<=1;
33+
}
34+
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package java_problem.stack;
2+
3+
public class DailyTemperatures {
4+
public static void main(String args[]) {
5+
int[] tokens = new int[]{73,74,75,71,69,72,76,73};
6+
System.out.println(dailyTemperatures(tokens));
7+
}
8+
public static int[] dailyTemperatures(int[] temperatures) {
9+
int n=temperatures.length;
10+
if(n==1) return new int[]{0};
11+
int[] result=new int[n];
12+
int i=0;
13+
while(i<n-1){
14+
int j=i+1;
15+
boolean isHigher=false;
16+
int count=0;
17+
while(j<n){
18+
count++;
19+
if(temperatures[i]<temperatures[j]){
20+
isHigher=true;
21+
break;
22+
}
23+
j++;
24+
}
25+
if(isHigher){
26+
result[i]=count;
27+
}
28+
else{
29+
result[i]=0;
30+
}
31+
i++;
32+
}
33+
return result;
34+
}
35+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package java_problem.stack;
2+
3+
import java.util.Stack;
4+
5+
public class EvaluateReversePolishNotation {
6+
public static void main(String args[]) {
7+
String[] tokens = new String[]{"2", "1", "+", "3", "*"};
8+
System.out.println(evalRPN(tokens));
9+
}
10+
11+
public static int evalRPN(String[] tokens) {
12+
int n = tokens.length;
13+
Stack<Integer> st = new Stack<Integer>();
14+
int i = 0;
15+
while (i < n) {
16+
switch (tokens[i]) {
17+
case "+" -> {
18+
int val1 = st.pop();
19+
int val2 = st.pop();
20+
int result = val2 + val1;
21+
st.push(result);
22+
break;
23+
}
24+
case "-" -> {
25+
int val1 = st.pop();
26+
int val2 = st.pop();
27+
int result = val2 - val1;
28+
st.push(result);
29+
break;
30+
}
31+
case "/" -> {
32+
int val1 = st.pop();
33+
int val2 = st.pop();
34+
int result = val2 / val1;
35+
st.push(result);
36+
break;
37+
}
38+
case "*" -> {
39+
int val1 = st.pop();
40+
int val2 = st.pop();
41+
int result = val2 * val1;
42+
st.push(result);
43+
break;
44+
}
45+
default -> {
46+
int val = Integer.parseInt(tokens[i]);
47+
st.push(val);
48+
}
49+
}
50+
i++;
51+
}
52+
return st.pop();
53+
}
54+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package java_problem.string;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class PermutationInString {
7+
public static void main(String args[]) {
8+
System.out.println(checkInclusion("ab", "eidbooo"));
9+
}
10+
11+
public static boolean checkInclusion(String s1, String s2) {
12+
int n1 = s1.length();
13+
int n2 = s2.length();
14+
Map<Integer, Integer> map = new HashMap<>();
15+
for (int i = 0; i < n2; i++) {
16+
int c = (int) s2.charAt(i);
17+
if (map.containsKey(c)) {
18+
map.put(c, map.get(c) + 1);
19+
} else
20+
map.put(c, 1);
21+
}
22+
for (int i = 0; i < n1; i++) {
23+
int c = s1.charAt(i);
24+
if (map.containsKey(c) && map.get(c) > 0) {
25+
map.put(c, map.get(c) - 1);
26+
} else
27+
return false;
28+
}
29+
return true;
30+
}
31+
32+
33+
}

0 commit comments

Comments
(0)

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