Skip to content

Commit d9a645f

Browse files
committed
Added Hard Ones ✅
1 parent a68ae62 commit d9a645f

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed

Hard/Calculator.class

1.25 KB
Binary file not shown.

Hard/Calculator.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import java.util.*;
2+
3+
class Calculator {
4+
public static void main(String[] args) {
5+
Scanner s = new Scanner(System.in);
6+
String str = s.nextLine();
7+
System.out.println(calculate(str));
8+
}
9+
10+
public static int calculate(String s) {
11+
if(s == null) return 0;
12+
13+
int result = 0;
14+
int sign = 1;
15+
int num = 0;
16+
17+
Stack<Integer> stack = new Stack<Integer>();
18+
stack.push(sign);
19+
20+
for(int i = 0; i < s.length(); i++) {
21+
char c = s.charAt(i);
22+
23+
if(c >= '0' && c <= '9') {
24+
num = num * 10 + (c - '0');
25+
26+
} else if(c == '+' || c == '-') {
27+
result += sign * num;
28+
sign = stack.peek() * (c == '+' ? 1: -1);
29+
num = 0;
30+
31+
} else if(c == '(') {
32+
stack.push(sign);
33+
34+
} else if(c == ')') {
35+
stack.pop();
36+
}
37+
}
38+
39+
result += sign * num;
40+
return result;
41+
}
42+
public int calculateEditoral(String s) {
43+
44+
Stack<Integer> stack = new Stack<Integer>();
45+
int operand = 0;
46+
int result = 0; // For the on-going result
47+
int sign = 1; // 1 means positive, -1 means negative
48+
49+
for (int i = 0; i < s.length(); i++) {
50+
51+
char ch = s.charAt(i);
52+
if (Character.isDigit(ch)) {
53+
54+
// Forming operand, since it could be more than one digit
55+
operand = 10 * operand + (int) (ch - '0');
56+
57+
} else if (ch == '+') {
58+
59+
// Evaluate the expression to the left,
60+
// with result, sign, operand
61+
result += sign * operand;
62+
63+
// Save the recently encountered '+' sign
64+
sign = 1;
65+
66+
// Reset operand
67+
operand = 0;
68+
69+
} else if (ch == '-') {
70+
71+
result += sign * operand;
72+
sign = -1;
73+
operand = 0;
74+
75+
} else if (ch == '(') {
76+
77+
// Push the result and sign on to the stack, for later
78+
// We push the result first, then sign
79+
stack.push(result);
80+
stack.push(sign);
81+
82+
// Reset operand and result, as if new evaluation begins for the new sub-expression
83+
sign = 1;
84+
result = 0;
85+
86+
} else if (ch == ')') {
87+
88+
// Evaluate the expression to the left
89+
// with result, sign and operand
90+
result += sign * operand;
91+
92+
// ')' marks end of expression within a set of parenthesis
93+
// Its result is multiplied with sign on top of stack
94+
// as stack.pop() is the sign before the parenthesis
95+
result *= stack.pop();
96+
97+
// Then add to the next operand on the top.
98+
// as stack.pop() is the result calculated before this parenthesis
99+
// (operand on stack) + (sign on stack * (result from parenthesis))
100+
result += stack.pop();
101+
102+
// Reset the operand
103+
operand = 0;
104+
}
105+
}
106+
return result + (sign * operand);
107+
}
108+
109+
}
110+

Hard/InsertInterval.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.util.*;
2+
class InsertInterval{
3+
public int[][] insert(int[][] intervals, int[] newInterval) {
4+
5+
if ((intervals.length == 0) || (intervals[0].length == 0))
6+
return new int[][]{newInterval};
7+
8+
List<int[]> ints = new ArrayList<>(Arrays.asList(intervals));
9+
10+
ints.add(searchInsertPosition(intervals, newInterval), newInterval);
11+
12+
return mergeIntervals(ints).toArray(new int[][]{{0}});
13+
}
14+
15+
// LC 35 Search Insert Position
16+
private int searchInsertPosition(int[][] intervals, int[] newInterval) {
17+
int target = newInterval[0];
18+
19+
if (target <= intervals[0][0])
20+
return 0;
21+
22+
if (target >= intervals[intervals.length - 1][0])
23+
return intervals.length;
24+
25+
int low = 0;
26+
int high = intervals.length - 1;
27+
28+
while (low <= high) {
29+
int mid = (low + high) / 2;
30+
31+
if (intervals[mid][0] == target)
32+
return mid;
33+
34+
if (intervals[mid][0] > target)
35+
high = mid - 1;
36+
else
37+
low = mid + 1;
38+
}
39+
return low;
40+
}
41+
42+
// LC 56 Merge Intervals
43+
private List<int[]> mergeIntervals(List<int[]> ints) {
44+
45+
List<int[]> merged = new ArrayList<>();
46+
int[] lastInt = ints.get(0);
47+
merged.add(lastInt);
48+
49+
for (int i = 1; i < ints.size(); i++) {
50+
int[] currInt = ints.get(i);
51+
52+
if (currInt[0] <= lastInt[1]) {
53+
lastInt[1] = Math.max(lastInt[1], currInt[1]);
54+
55+
} else {
56+
merged.add(currInt);
57+
lastInt = currInt;
58+
}
59+
}
60+
return merged;
61+
}
62+
}

Hard/JumpGameII.class

1001 Bytes
Binary file not shown.

Hard/JumpGameII.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class JumpGameII {
2+
public static void main(String[] args) {
3+
int[] a = {2,3,1,1,4};
4+
System.out.println(jump(a));
5+
}
6+
public static int jump(int[] A) {
7+
//This is an implicit bfs solution. i == curEnd means you visited all the items on the current level. Incrementing jumps++ is like incrementing the level you are on. And curEnd = max is like getting the queue size (level size) for the next level you are traversing.
8+
9+
int jumps = 0,
10+
curEnd = 0,
11+
max = 0;
12+
13+
//i < A.length - 1 BECAUSE The last number doesn't need to be traversed. The question just needs to reach to the last number, then it ends and then outputs the result.
14+
15+
for (int i = 0; i < A.length - 1; i++) {
16+
17+
max = Math.max(max, i + A[i]);
18+
//System.out.println("MAX "+max + " "+i+ " " +A[i]);
19+
20+
if (i == curEnd) {
21+
jumps++;
22+
curEnd = max;
23+
//System.out.println("JUMPS "+jumps+" " +curEnd);
24+
}
25+
}
26+
27+
return jumps;
28+
}
29+
}

0 commit comments

Comments
 (0)