Skip to content

Commit 7a38142

Browse files
committed
Added 4 solutions
1 parent afb72b4 commit 7a38142

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int[] distributeCandies(int candies, int num_people) {
3+
int[] count = new int[num_people];
4+
int idx = 0;
5+
int currCount = 0;
6+
int startCandy = 1;
7+
8+
while (currCount < candies) {
9+
count[idx++] += currCount + startCandy > candies ? candies - currCount : startCandy;
10+
currCount += startCandy++;
11+
if (idx == num_people) {
12+
idx = 0;
13+
}
14+
}
15+
16+
return count;
17+
}
18+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
int count = 0;
3+
public int validSubarrays(int[] nums) {
4+
helper(nums, 0, 0, -1);
5+
return count;
6+
}
7+
8+
private void helper(int[] nums, int firstIdx, int idx, int left) {
9+
if (idx - firstIdx > 1 || idx > nums.length - 1) {
10+
return;
11+
}
12+
13+
for (int i = idx; i < nums.length; i++) {
14+
if (left == -1) {
15+
count++;
16+
helper(nums, i, i + 1, nums[i]);
17+
}
18+
else if (nums[i] < left) {
19+
return;
20+
}
21+
else {
22+
count++;
23+
helper(nums, firstIdx, i + 1, left);
24+
}
25+
}
26+
}
27+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
class Solution {
2+
public boolean parseBoolExpr(String expression) {
3+
char[] chars = expression.toCharArray();
4+
Stack<Character> operations = new Stack<>();
5+
Stack<Character> boolValues = new Stack<>();
6+
7+
for (int i = 0; i < chars.length; i++) {
8+
char c = chars[i];
9+
if (c == '!' || c == '&' || c == '|') {
10+
operations.push(c);
11+
}
12+
else if (c == '(') {
13+
boolValues.push('#');
14+
}
15+
else if (c == 't' || c == 'f') {
16+
boolValues.push(chars[i]);
17+
}
18+
else if (c == ',') {
19+
continue;
20+
}
21+
else {
22+
List<Character> list = new ArrayList<>();
23+
while (!boolValues.isEmpty()) {
24+
char temp = boolValues.pop();
25+
if (temp == '#') {
26+
break;
27+
}
28+
29+
list.add(temp);
30+
}
31+
32+
boolValues.push(performOperation(list, operations.pop()));
33+
}
34+
}
35+
36+
return boolValues.peek() == 't' ? true : false;
37+
}
38+
39+
private Character performOperation(List<Character> list, Character operation) {
40+
if (operation == '|') {
41+
return performOr(list);
42+
}
43+
else if (operation == '&') {
44+
return performAnd(list);
45+
}
46+
else {
47+
return list.get(0) == 't' ? 'f' : 't';
48+
}
49+
}
50+
51+
private Character performAnd(List<Character> list) {
52+
boolean val = getBooleanValue(list.get(0));
53+
for (int i = 1; i < list.size(); i++) {
54+
val &= getBooleanValue(list.get(i));
55+
}
56+
57+
return val ? 't' : 'f';
58+
}
59+
60+
private Character performOr(List<Character> list) {
61+
boolean val = getBooleanValue(list.get(0));
62+
for (int i = 1; i < list.size(); i++) {
63+
val |= getBooleanValue(list.get(i));
64+
}
65+
66+
return val ? 't' : 'f';
67+
}
68+
69+
private boolean getBooleanValue(Character character) {
70+
return character == 't' ? true : false;
71+
}
72+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode bstToGst(TreeNode root) {
12+
inorderReverse(root, new int[]{0});
13+
return root;
14+
}
15+
16+
private void inorderReverse(TreeNode root, int[] currVal) {
17+
if (root == null) {
18+
return;
19+
}
20+
21+
inorderReverse(root.right, currVal);
22+
currVal[0] += root.val;
23+
root.val = currVal[0];
24+
inorderReverse(root.left, currVal);
25+
}
26+
}

0 commit comments

Comments
 (0)