Skip to content

Commit 70f3293

Browse files
author
Sonia Mathew
committed
more
1 parent 52f9374 commit 70f3293

16 files changed

+1218
-229
lines changed

.idea/workspace.xml

+220-229
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BasicCalculatorIII.java

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import java.util.ArrayDeque;
2+
import java.util.Deque;
3+
4+
/**
5+
* Leetcode question
6+
*/
7+
8+
public class BasicCalculatorIII {
9+
public static void main(String[] args) {
10+
System.out.println(calculate("1 + 1")==2);
11+
System.out.println(calculate(" 6-4 / 2 ")==4);
12+
System.out.println(calculate("2*(5+5*2)/3+(6/2+8)")==21);
13+
System.out.println(calculate("(2+6* 3+5- (3*14/7+2)*5)+3")==-12);
14+
System.out.println(calculate("1-(-7)")==8);
15+
}
16+
public static int calculate(String s) {
17+
Deque<Long> operands = new ArrayDeque<>();
18+
Deque<Integer> signs = new ArrayDeque<>();
19+
//1 for +, -1 for -, 2 for * and 3 for /
20+
21+
int sign = 1;
22+
for(int i=0; i<s.length(); i++) {
23+
char c = s.charAt(i);
24+
if(Character.isDigit(c)) {
25+
long num = 0l;
26+
while(i < s.length() && Character.isDigit(s.charAt(i))) {
27+
num = num*10 + (s.charAt(i)-'0');
28+
i++;
29+
}
30+
i--;
31+
long res = operate(operands, sign, num);
32+
operands.push(res);
33+
} else if(c=='+') {
34+
sign = 1;
35+
} else if(c=='-') {
36+
sign = -1;
37+
} else if(c=='*') {
38+
sign = 2;
39+
} else if(c=='/') {
40+
sign = 3;
41+
} else if(c=='(') {
42+
signs.push(sign);
43+
operands.push(Long.MAX_VALUE);
44+
sign = 1;
45+
} else if(c==')') {
46+
long sum = 0;
47+
while(!operands.isEmpty() && operands.peek()!=Long.MAX_VALUE) {
48+
sum += operands.pop();
49+
}
50+
sign = signs.pop();
51+
operands.pop();
52+
long res = operate(operands, sign, sum);
53+
operands.push(res);
54+
}
55+
}
56+
int sum = 0;
57+
while (!operands.isEmpty()) {
58+
sum += operands.pop().intValue();
59+
}
60+
61+
return sum;
62+
}
63+
public static long operate(Deque<Long> operands, int sign, long num) {
64+
if(sign == 1) {
65+
return num;
66+
} else if(sign == -1) {
67+
return -num;
68+
} else if(sign == 2) {
69+
return operands.pop() * num;
70+
} else {
71+
return operands.pop()/num;
72+
}
73+
}
74+
}

src/DailyCoding225.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
/**
5+
* There are N prisoners standing in a circle, waiting to be executed. The executions are carried out starting with
6+
* the kth person, and removing every successive kth person going clockwise until there is no one left.
7+
*
8+
* Given N and k, write an algorithm to determine where a prisoner should stand in order to be the last survivor.
9+
*/
10+
public class DailyCoding225 {
11+
public static void main(String[] args) {
12+
System.out.println(lastStanding(5, 2) == 3);
13+
14+
}
15+
public static int lastStanding(int N, int k) {
16+
//Josephus problem
17+
if(N == 1) {
18+
return N;
19+
}
20+
//since we skipped k-1 people, we have to add them back so that the index is proper
21+
// since module returns from 0 to N-1, add 1 to get the right index
22+
return (lastStanding(N-1, k) + k-1)%N + 1;
23+
}
24+
}

src/DailyCoding9.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Given a list of integers, write a function that returns the largest sum of
3+
* non-adjacent numbers. Numbers can be 0 or negative.
4+
*
5+
* For example, [2, 4, 6, 2, 5] should return 13, since we pick 2, 6, and 5.
6+
* [5, 1, 1, 5] should return 10, since we pick 5 and 5.
7+
*
8+
* Follow-up: Can you do this in O(N) time and constant space?
9+
*/
10+
public class DailyCoding9 {
11+
public static void main(String[] args) {
12+
System.out.println(largestSum(new int[]{2,4,6,2,5}) == 13);
13+
System.out.println(largestSum(new int[]{5,1,1,5}) == 10);
14+
15+
System.out.println(largestSumOptimized(new int[]{2,4,6,2,5}) == 13);
16+
System.out.println(largestSumOptimized(new int[]{5,1,1,5}) == 10);
17+
}
18+
public static int largestSum(int[] arr) {
19+
int n = arr.length;
20+
int[][] dp = new int[n+1][n+1];
21+
for (int i=1; i<=n ; i++) {
22+
//if we don't choose the ith number
23+
dp[i][0] = Math.max(dp[i-1][1], dp[i-1][0]);
24+
25+
//if we choose the ith number
26+
dp[i][1] = dp[i-1][0]+arr[i-1];
27+
}
28+
29+
return Math.max(dp[n][0], dp[n][1]);
30+
}
31+
public static int largestSumOptimized(int[] arr) {
32+
int n = arr.length;
33+
34+
int prevIncluded = 0, prevExcluded = 0;
35+
for (int i=0; i<n; i++) {
36+
int currExcluded = Math.max(prevIncluded, prevExcluded);
37+
int currIncluded = arr[i] + prevExcluded;
38+
prevExcluded = currExcluded;
39+
prevIncluded = currIncluded;
40+
}
41+
return Math.max(prevExcluded, prevIncluded);
42+
}
43+
}

src/DailyCoding92.java

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.util.*;
2+
3+
/**
4+
* We're given a hashmap associating each courseId key with a list of courseIds values,
5+
* which represents that the prerequisites of courseId are courseIds. Return a sorted ordering
6+
* of courses such that we can finish all courses.
7+
*
8+
* Return null if there is no such ordering.
9+
*
10+
* Leetcode 207
11+
*/
12+
public class DailyCoding92 {
13+
public static void main(String[] args) {
14+
HashMap<String, List<String>> courses = new HashMap<>();
15+
16+
List<String> list1 = new ArrayList<>();
17+
list1.add("CSC100");
18+
list1.add("CSC200");
19+
20+
courses.put("CSC300", list1);
21+
22+
List<String> list2 = new ArrayList<>();
23+
list2.add("CSC100");
24+
courses.put("CSC200", list2);
25+
26+
courses.put("CSC100", new ArrayList<>());
27+
28+
List<String> ordering = getCourseOrdering(courses);
29+
30+
if (ordering!=null){
31+
Utility.printStr(ordering);
32+
}
33+
}
34+
35+
public static List<String> getCourseOrdering(HashMap<String, List<String>> courses) {
36+
HashMap<String, Set<String>> prerequisites = new HashMap<>();
37+
Deque<String> queue = new ArrayDeque<>();
38+
for (Map.Entry<String, List<String>> entry : courses.entrySet()) {
39+
if (entry.getValue().size()==0) {
40+
queue.add(entry.getKey());
41+
}
42+
for (String course : entry.getValue()) {
43+
if (!prerequisites.containsKey(course)) {
44+
prerequisites.put(course, new HashSet<>());
45+
}
46+
Set<String> set = prerequisites.get(course);
47+
set.add(entry.getKey());
48+
prerequisites.put(course, set);
49+
}
50+
}
51+
52+
List<String> result = new ArrayList<>();
53+
while (!queue.isEmpty()){
54+
String c = queue.poll();
55+
result.add(c);
56+
57+
Set<String> dep = prerequisites.getOrDefault(c, new HashSet<>());
58+
for (String s : dep) {
59+
List<String> cs = courses.get(s);
60+
cs.remove(c);
61+
if (cs.size()==0) {
62+
queue.add(s);
63+
} else {
64+
courses.put(s, cs);
65+
}
66+
67+
}
68+
}
69+
return result.size()!=courses.size() ? null : result;
70+
}
71+
72+
}

src/ExpressionEquivalence.java

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.ArrayDeque;
2+
import java.util.Deque;
3+
4+
/**
5+
* Check if two expressions are equivalent
6+
*
7+
* -(a+b+c) is same as -a-b-c
8+
* -(c+b+a) is same as -c-b-a
9+
* a-b-(c-d) is not same as a-b-c-d
10+
*
11+
* Each operand appears only once. Operands are lower case variables a-z.
12+
* Operators will be only + or -
13+
*/
14+
public class ExpressionEquivalence {
15+
public static void main(String[] args) {
16+
System.out.println(equals("-(a+b+c)", "-a-b-c"));
17+
System.out.println(equals("a-b-(c-d)", "a-b-c-d"));
18+
System.out.println(equals("a+((b)+(c))", "((b+c)+a)"));
19+
}
20+
public static boolean equals(String exp1, String exp2) {
21+
Deque<Integer> stack = new ArrayDeque<>(); // 1 for +, -1 for -
22+
23+
int sign = 1;
24+
int[] count = new int[26];
25+
for(char c : exp1.toCharArray()) {
26+
if (c=='+') {
27+
sign = 1;
28+
} else if (c=='-') {
29+
sign = -1;
30+
} else if (c=='(') {
31+
stack.push(sign);
32+
sign = 1;
33+
} else if (c==')') {
34+
sign = stack.pop();
35+
} else {
36+
int global = 1;
37+
if (!stack.isEmpty()) {
38+
global = stack.peek();
39+
}
40+
count[c-'a'] += sign * global;
41+
}
42+
}
43+
sign = 1;
44+
for (char c : exp2.toCharArray()) {
45+
if (c=='+') {
46+
sign = 1;
47+
} else if (c=='-') {
48+
sign = -1;
49+
} else if (c=='(') {
50+
stack.push(sign);
51+
sign = 1;
52+
} else if (c==')') {
53+
sign = stack.pop();
54+
} else {
55+
int global = 1;
56+
if (!stack.isEmpty()) {
57+
global = stack.peek();
58+
}
59+
count[c-'a'] -= sign*global;
60+
}
61+
}
62+
63+
for (int n: count) {
64+
if (n!=0) {
65+
return false;
66+
}
67+
}
68+
return true;
69+
}
70+
}

src/FindNode.java

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* A binary tree is level order sorted (Each level is sorted). Find a given node.
3+
* It is a full binary tree. The left and right edges are sorted.
4+
* Could you do better than O(n).
5+
*/
6+
public class FindNode {
7+
public static void main(String[] args) {
8+
TreeNode root = new TreeNode(1);
9+
10+
TreeNode left = new TreeNode(2);
11+
left.left = new TreeNode(8);
12+
left.right = new TreeNode(9);
13+
14+
TreeNode right = new TreeNode(3);
15+
right.left = new TreeNode(10);
16+
right.right = new TreeNode(11);
17+
18+
root.left = left;
19+
root.right = right;
20+
21+
System.out.println(findNode(root, 10));
22+
System.out.println(findNode(root, 6));
23+
}
24+
public static int findNode(TreeNode root, int val) {
25+
if (root == null) {
26+
return -1;
27+
}
28+
29+
//find the level in which the element may appear
30+
TreeNode tmp = root;
31+
int level = 0;
32+
while(tmp.left!=null && tmp.left.val <= val) {
33+
tmp = tmp.left;
34+
level++;
35+
}
36+
37+
//Number of nodes in the level
38+
int count = (int)Math.pow(2, level);
39+
40+
41+
int start = 0, end = count-1;
42+
while (start <= end) {
43+
int mid = (start + end)/2;
44+
int divPt = count/2; // this needs to be set everytime since we are traversing from root all over again
45+
int pos = mid;
46+
tmp = root;
47+
for (int l=0; l<level; l++) {
48+
int dir = pos/divPt;
49+
50+
if(dir == 0) {
51+
tmp = tmp.left;
52+
} else {
53+
tmp = tmp.right;
54+
pos -= divPt;
55+
}
56+
divPt /= 2;
57+
}
58+
59+
if (val == tmp.val) {
60+
break;
61+
} else if (val > tmp.val) {
62+
start = mid+1;
63+
} else {
64+
end = mid -1;
65+
}
66+
}
67+
return val == tmp.val ? val : -1;
68+
}
69+
}

0 commit comments

Comments
 (0)