Skip to content

Commit 0bf67a9

Browse files
committed
Modified 6 solutions
1 parent 348741d commit 0bf67a9

6 files changed

+144
-181
lines changed

Medium/Basic Calculator II.java

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
11
class Solution {
2-
public int calculate(String s) {
3-
Stack<Integer> stack = new Stack<>();
4-
int num = 0;
5-
char sign = '+';
6-
7-
for (int i=0; i<s.length(); i++) {
8-
char c = s.charAt(i);
9-
if (Character.isDigit(c)) {
10-
num = num * 10 + Character.getNumericValue(c);
11-
}
12-
13-
if (!Character.isDigit(c) && c != ' ' || i == s.length() - 1){
14-
if (sign == '-') {
15-
stack.push(-num);
16-
}
17-
else if (sign == '+') {
18-
stack.push(num);
19-
}
20-
else if (sign == '*') {
21-
stack.push(stack.pop() * num);
22-
}
23-
else {
24-
stack.push(stack.pop() / num);
25-
}
26-
27-
sign = c;
28-
num = 0;
29-
}
2+
public int calculate(String s) {
3+
Stack<Integer> stack = new Stack<>();
4+
int num = 0;
5+
char sign = '+';
6+
for (int i = 0; i < s.length(); i++) {
7+
char c = s.charAt(i);
8+
if (Character.isDigit(c)) {
9+
num = num * 10 + Character.getNumericValue(c);
10+
}
11+
if (!Character.isDigit(c) && c != ' ' || i == s.length() - 1) {
12+
if (sign == '-') {
13+
stack.push(-num);
3014
}
31-
32-
int res = 0;
33-
while (!stack.isEmpty()) {
34-
res += stack.pop();
15+
else if (sign == '+') {
16+
stack.push(num);
3517
}
36-
37-
return res;
18+
else if (sign == '*') {
19+
stack.push(stack.pop() * num);
20+
}
21+
else {
22+
stack.push(stack.pop() / num);
23+
}
24+
sign = c;
25+
num = 0;
26+
}
27+
}
28+
int res = 0;
29+
while (!stack.isEmpty()) {
30+
res += stack.pop();
3831
}
32+
return res;
33+
}
3934
}

Medium/Boats to Save People.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
class Solution {
2-
public int numRescueBoats(int[] people, int limit) {
3-
Arrays.sort(people);
4-
int count = 0;
5-
int start = 0;
6-
int end = people.length-1;
7-
8-
while (end >= start) {
9-
if (people[start] + people[end] <= limit) {
10-
start++;
11-
}
12-
13-
end--;
14-
count++;
15-
}
16-
17-
return count;
2+
public int numRescueBoats(int[] people, int limit) {
3+
Arrays.sort(people);
4+
int count = 0;
5+
int start = 0;
6+
int end = people.length - 1;
7+
while (end >= start) {
8+
if (people[start] + people[end] <= limit) {
9+
start++;
10+
}
11+
end--;
12+
count++;
1813
}
14+
return count;
15+
}
1916
}

Medium/Delete Node in a BST.java

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,40 @@
44
* int val;
55
* TreeNode left;
66
* TreeNode right;
7-
* TreeNode(int x) { val = x; }
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
814
* }
915
*/
1016
class Solution {
11-
public TreeNode deleteNode(TreeNode root, int key) {
12-
if (root == null) {
13-
return root;
14-
}
15-
16-
if (key < root.val) {
17-
root.left = deleteNode(root.left, key);
18-
}
19-
else if (key > root.val) {
20-
root.right = deleteNode(root.right, key);
21-
}
22-
else {
23-
if (root.left == null || root.right == null) {
24-
TreeNode temp = root.left == null ? root.right : root.left;
25-
26-
if (temp == null) {
27-
return null;
28-
}
29-
else {
30-
return temp;
31-
}
32-
}
33-
else {
34-
TreeNode rightSuccessor = getRightSuccessor(root);
35-
root.val = rightSuccessor.val;
36-
root.right = deleteNode(root.right, rightSuccessor.val);
37-
}
38-
}
39-
40-
return root;
17+
public TreeNode deleteNode(TreeNode root, int key) {
18+
if (root == null) {
19+
return null;
4120
}
42-
43-
private TreeNode getRightSuccessor(TreeNode node) {
44-
if (node == null) {
45-
return null;
46-
}
47-
48-
TreeNode temp = node.right;
49-
if (temp != null) {
50-
while (temp.left != null) {
51-
temp = temp.left;
52-
}
53-
}
54-
21+
if (root.val > key) {
22+
root.left = deleteNode(root.left, key);
23+
}
24+
else if (root.val < key) {
25+
root.right = deleteNode(root.right, key);
26+
}
27+
else {
28+
if (root.left == null || root.right == null) {
29+
TreeNode temp = root.left == null ? root.right : root.left;
5530
return temp;
31+
}
32+
else {
33+
TreeNode inorderSuccessor = root.right;
34+
while (inorderSuccessor.left != null) {
35+
inorderSuccessor = inorderSuccessor.left;
36+
}
37+
root.val = inorderSuccessor.val;
38+
root.right = deleteNode(root.right, inorderSuccessor.val);
39+
}
5640
}
41+
return root;
42+
}
5743
}

Medium/Linked List Cycle II.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,26 @@
1010
* }
1111
*/
1212
public class Solution {
13-
public ListNode detectCycle(ListNode head) {
14-
ListNode slow = head;
15-
ListNode fast = head;
16-
17-
while (fast!=null && fast.next!=null){
18-
fast = fast.next.next;
19-
slow = slow.next;
20-
21-
if (fast == slow){
22-
ListNode slow2 = head;
23-
while (slow2 != slow){
24-
slow = slow.next;
25-
slow2 = slow2.next;
26-
}
27-
return slow;
28-
}
29-
}
30-
return null;
13+
public ListNode detectCycle(ListNode head) {
14+
ListNode slow = head;
15+
ListNode fast = head;
16+
boolean cycle = false;
17+
while (fast != null && fast.next != null) {
18+
slow = slow.next;
19+
fast = fast.next.next;
20+
if (slow == fast) {
21+
cycle = true;
22+
break;
23+
}
3124
}
25+
if (!cycle) {
26+
return null;
27+
}
28+
slow = head;
29+
while (slow != fast) {
30+
slow = slow.next;
31+
fast = fast.next;
32+
}
33+
return slow;
34+
}
3235
}
Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
class Solution {
2-
public int longestWPI(int[] hours) {
3-
int ans = 0;
4-
int[] preComputed = new int[hours.length + 1];
5-
6-
for (int i = 1; i < preComputed.length; i++) {
7-
if (hours[i - 1] > 8) {
8-
ans = 1;
9-
preComputed[i] = preComputed[i - 1] + 1;
10-
}
11-
else {
12-
preComputed[i] = preComputed[i - 1] - 1;
13-
}
14-
}
15-
16-
for(int i = 0; i <= hours.length; i++){
17-
for(int j = hours.length; j > i; j--){
18-
if(preComputed[j]-preComputed[i] > 0){
19-
ans = Math.max(ans, j - i);
20-
break;
21-
}
22-
}
23-
}
24-
25-
return ans;
2+
public int longestWPI(int[] hours) {
3+
int sum = 0;
4+
int maxInterval = 0;
5+
boolean greaterThanEightFound = false;
6+
Map<Integer, Integer> map = new HashMap<>();
7+
for (int i = 0; i < hours.length; i++) {
8+
sum += hours[i] > 8 ? 1 : -1;
9+
greaterThanEightFound = hours[i] > 8 ? true : greaterThanEightFound;
10+
map.putIfAbsent(sum, i);
11+
if (sum >= 1) {
12+
maxInterval = Math.max(maxInterval, i + 1);
13+
}
14+
else if (map.containsKey(sum - 1)) {
15+
maxInterval = Math.max(maxInterval, i - map.get(sum - 1));
16+
}
2617
}
18+
return maxInterval == 0 ? (greaterThanEightFound ? 1 : 0) : maxInterval;
19+
}
2720
}

Medium/Rotate List.java

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,43 @@
33
* public class ListNode {
44
* int val;
55
* ListNode next;
6-
* ListNode(int x) { val = x; }
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
79
* }
810
*/
911
class Solution {
10-
11-
public void rotateArr(int[] arr) {
12-
int temp = arr[0];
13-
for (int i=1;i<arr.length;i++) {
14-
int t = arr[i];
15-
arr[i] = temp;
16-
temp = t;
17-
}
18-
19-
arr[0] = temp;
12+
public ListNode rotateRight(ListNode head, int k) {
13+
if (head == null || head.next == null || k == 0) {
14+
return head;
2015
}
21-
22-
public ListNode rotateRight(ListNode head, int k) {
23-
if (head == null || head.next == null || k == 0) return head;
24-
int l = 0;
25-
ListNode curr = head;
26-
while (curr != null) {
27-
curr = curr.next;
28-
l++;
29-
}
30-
31-
int[] arr = new int[l];
32-
curr = head;
33-
34-
for (int i=0;i<l;i++) {
35-
arr[i] = curr.val;
36-
curr = curr.next;
37-
}
38-
39-
k = k%l;
40-
41-
while (k > 0) {
42-
rotateArr(arr);
43-
k--;
44-
}
45-
46-
curr = head;
47-
int j = 0;
48-
while (curr != null) {
49-
curr.val = arr[j];
50-
j++;
51-
curr = curr.next;
52-
}
53-
54-
return head;
16+
ListNode curr = head;
17+
// Get length of list & update the number of rotations
18+
int count = 0;
19+
while (curr != null) {
20+
curr = curr.next;
21+
count++;
5522
}
23+
k %= count;
24+
if (k == 0) {
25+
return head;
26+
}
27+
// Move the pointer just before the rotation index
28+
int stop = count - k;
29+
int currCount = 1;
30+
curr = head;
31+
while (currCount < stop) {
32+
currCount++;
33+
curr = curr.next;
34+
}
35+
ListNode nextNode = curr.next;
36+
// Detach rotation part of list and append it in the beginning of list
37+
curr.next = null;
38+
ListNode newHead = nextNode;
39+
while (nextNode.next != null) {
40+
nextNode = nextNode.next;
41+
}
42+
nextNode.next = head;
43+
return newHead;
44+
}
5645
}

0 commit comments

Comments
 (0)