Skip to content

Commit 19ba49d

Browse files
committed
Added 2 solutions
1 parent 2486a23 commit 19ba49d

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Easy/Hexspeak.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
Integer[] keys = {0, 1, 10, 11, 12, 13, 14, 15};
3+
String[] values = {"O", "I", "A", "B", "C", "D", "E", "F"};
4+
Map<Long, String> map;
5+
public String toHexspeak(String num) {
6+
map = new HashMap<>();
7+
for (int i = 0; i < keys.length; i++) {
8+
map.put((long) keys[i], values[i]);
9+
}
10+
StringBuilder sb = new StringBuilder();
11+
Long n = Long.parseLong(num);
12+
while (n > 0) {
13+
Long quot = n % 16;
14+
n /= 16;
15+
if (map.containsKey(quot)) {
16+
sb.append(map.get(quot));
17+
}
18+
else {
19+
return "ERROR";
20+
}
21+
}
22+
return sb.reverse().toString();
23+
}
24+
}

Medium/Remove Interval.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
public List<List<Integer>> removeInterval(int[][] intervals, int[] toBeRemoved) {
3+
List<List<Integer>> list = new ArrayList<>();
4+
int removeStart = toBeRemoved[0];
5+
int removeEnd = toBeRemoved[1];
6+
for (int[] interval : intervals) {
7+
int start = interval[0];
8+
int end = interval[1];
9+
// Case 1: Overlap the removal interval. Skip this interval
10+
if (start >= removeStart && end <= removeEnd) {
11+
continue;
12+
}
13+
// Case 2 and 3: Completely miss out the removal interval either on left side or right side of number line.
14+
// Add the complete interval
15+
if ((start <= removeStart && end <= removeStart) ||
16+
(start >= removeEnd && end >= removeEnd)) {
17+
list.add(Arrays.asList(start, end));
18+
continue;
19+
}
20+
// Case 4: Interval partially overlaps on left side of removal interval on number line.
21+
// Add the modified interval
22+
if (start <= removeStart) {
23+
int tempStart = Math.min(start, removeStart);
24+
int tempEnd = Math.max(start, removeStart);
25+
if (tempStart != tempEnd) {
26+
list.add(Arrays.asList(tempStart, tempEnd));
27+
}
28+
}
29+
// Case 5: Interval partially overlaps on right side of removal interval on number line.
30+
// Add the modified interval
31+
if (end >= removeEnd) {
32+
int tempStart = Math.min(end, removeEnd);
33+
int tempEnd = Math.max(end, removeEnd);
34+
if (tempStart != tempEnd) {
35+
list.add(Arrays.asList(tempStart, tempEnd));
36+
}
37+
}
38+
}
39+
return list;
40+
}
41+
}

0 commit comments

Comments
 (0)