Skip to content

Commit ffe284e

Browse files
committed
Added 4 solutions & modified 1 solution
1 parent 8839319 commit ffe284e

5 files changed

+132
-18
lines changed

Easy/Defanging an IP Address.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public String defangIPaddr(String address) {
3+
StringBuilder sb = new StringBuilder();
4+
for (char c : address.toCharArray()) {
5+
if (c == '.') {
6+
sb.append("[.]");
7+
}
8+
else {
9+
sb.append(c);
10+
}
11+
}
12+
13+
return sb.toString();
14+
}
15+
}

Medium/Corporate Flight Bookings.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int[] corpFlightBookings(int[][] bookings, int n) {
3+
int[] ans = new int[n];
4+
5+
for (int[] booking : bookings) {
6+
int tickets = booking[2];
7+
int from = booking[0];
8+
int to = booking[1];
9+
10+
ans[from - 1] += tickets;
11+
if (to < n) {
12+
ans[to] -= tickets;
13+
}
14+
}
15+
16+
int currSum = 0;
17+
for (int i = 0; i < n; i++) {
18+
currSum += ans[i];
19+
ans[i] = currSum;
20+
}
21+
22+
return ans;
23+
}
24+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
List<TreeNode> forest;
12+
public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
13+
forest = new ArrayList<>();
14+
Set<Integer> toDelete = new HashSet<>();
15+
for (int val : to_delete) {
16+
toDelete.add(val);
17+
}
18+
19+
helper(root, toDelete);
20+
21+
if (!toDelete.contains(root.val)) {
22+
forest.add(root);
23+
}
24+
25+
return forest;
26+
}
27+
28+
private TreeNode helper(TreeNode root, Set<Integer> toDelete) {
29+
if (root == null) {
30+
return null;
31+
}
32+
33+
root.left = helper(root.left, toDelete);
34+
root.right = helper(root.right, toDelete);
35+
36+
if (toDelete.contains(root.val)) {
37+
if (root.left != null) {
38+
forest.add(root.left);
39+
}
40+
41+
if (root.right != null) {
42+
forest.add(root.right);
43+
}
44+
45+
return null;
46+
}
47+
48+
return root;
49+
}
50+
}

Medium/Random Pick Index.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
Map<Integer, List<Integer>> map = new HashMap();
3+
Random rand;
4+
public Solution(int[] nums) {
5+
rand = new Random();
6+
for (int i = 0; i < nums.length; i++) {
7+
map.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i);
8+
}
9+
}
10+
11+
public int pick(int target) {
12+
List<Integer> indexes = map.get(target);
13+
return indexes.get(rand.nextInt(indexes.size()));
14+
}
15+
}
16+
17+
/**
18+
* Your Solution object will be instantiated and called as such:
19+
* Solution obj = new Solution(nums);
20+
* int param_1 = obj.pick(target);
21+
*/

Medium/Random Pick With Weight.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
class Solution {
22

3-
List<Integer> prefixSum;
4-
int total;
5-
Random rand;
3+
int[] accumulatedSum;
4+
Random random;
65
public Solution(int[] w) {
7-
prefixSum = new ArrayList<>();
8-
total = 0;
9-
for (int i=0; i<w.length; i++) {
10-
total += w[i];
11-
prefixSum.add(total);
6+
random = new Random();
7+
accumulatedSum = new int[w.length];
8+
int currSum = 0;
9+
for (int i = 0; i < w.length; i++) {
10+
currSum += w[i];
11+
accumulatedSum[i] = currSum;
1212
}
13-
rand = new Random();
1413
}
1514

1615
public int pickIndex() {
17-
int target = rand.nextInt(total);
18-
int i = 0;
19-
int j = prefixSum.size() - 1;
20-
while (i != j) {
21-
int mid = (i+j)/2;
22-
if (target >= prefixSum.get(mid)) {
23-
i = mid + 1;
16+
int target = random.nextInt(accumulatedSum[accumulatedSum.length - 1]) + 1;
17+
return binarySearch(accumulatedSum, 0, accumulatedSum.length - 1, target);
18+
}
19+
20+
private int binarySearch(int[] arr, int left, int right, int target) {
21+
while (left <= right) {
22+
int mid = (left + right) / 2;
23+
if (arr[mid] == target) {
24+
return mid;
25+
}
26+
else if (arr[mid] > target) {
27+
right = mid - 1;
2428
}
2529
else {
26-
j = mid;
30+
left = mid + 1;
2731
}
2832
}
2933

30-
return j;
34+
return left;
3135
}
3236
}
3337

0 commit comments

Comments
 (0)