Skip to content

Commit da5774a

Browse files
committed
Updated 3 solutions
1 parent 32caf5f commit da5774a

File tree

3 files changed

+60
-67
lines changed

3 files changed

+60
-67
lines changed

Easy/Strobogrammatic Number.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
class Solution {
22
public boolean isStrobogrammatic(String num) {
3-
Set<String> set = new HashSet<>();
4-
set.add("0");
5-
set.add("1");
6-
set.add("8");
7-
set.add("00");
8-
set.add("11");
9-
set.add("88");
10-
set.add("69");
11-
set.add("96");
12-
13-
int start = 0;
14-
int end = num.length()-1;
15-
16-
while (start <= end) {
17-
if (!set.contains(num.charAt(start) + "" + num.charAt(end))) {
3+
StringBuilder sb = new StringBuilder();
4+
for (char c : num.toCharArray()) {
5+
if (c == '0' || c == '1' || c == '8') {
6+
sb.append(c);
7+
}
8+
else if (c == '6' || c == '9') {
9+
sb.append(c == '6' ? '9' : '6');
10+
}
11+
else {
1812
return false;
1913
}
20-
start++;
21-
end--;
2214
}
2315

24-
return true;
16+
return sb.reverse().toString().equals(num);
2517
}
2618
}

Hard/Insert Delete GetRandom O(1) - Duplicates Allowed.java

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,44 @@
11
class RandomizedCollection {
22

33
/** Initialize your data structure here. */
4-
List<Integer> list;
54
Map<Integer, Set<Integer>> map;
5+
List<Integer> list;
66

77
public RandomizedCollection() {
8-
list = new ArrayList<>();
98
map = new HashMap<>();
9+
list = new ArrayList<>();
1010
}
1111

1212
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
1313
public boolean insert(int val) {
14-
boolean returnVal = map.containsKey(val);
15-
16-
Set<Integer> indexes = returnVal ? map.get(val) : new HashSet<>();
17-
indexes.add(list.size());
14+
map.computeIfAbsent(val, k -> new LinkedHashSet<>()).add(list.size());
1815
list.add(val);
19-
map.put(val, indexes);
20-
21-
return !returnVal;
16+
return map.get(val).size() == 1;
2217
}
2318

2419
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
2520
public boolean remove(int val) {
26-
if (!map.containsKey(val)) {
21+
if (!map.containsKey(val) || map.get(val).size() == 0) {
2722
return false;
2823
}
2924

30-
Set<Integer> indexes = map.get(val);
31-
int index = indexes.iterator().next();
32-
indexes.remove(index);
25+
int idxToRemove = map.get(val).iterator().next();
26+
map.get(val).remove(idxToRemove);
27+
int lastVal = list.get(list.size() - 1);
28+
list.set(idxToRemove, lastVal);
29+
map.get(lastVal).add(idxToRemove);
30+
map.get(lastVal).remove(list.size() - 1);
31+
list.remove(list.size() - 1);
3332

34-
if (indexes.size() == 0) {
35-
map.remove(val);
36-
}
37-
38-
if (index != list.size()-1) {
39-
int lastVal = list.get(list.size()-1);
40-
Set<Integer> lastValIndexes = map.get(lastVal);
41-
list.set(index, lastVal);
42-
lastValIndexes.remove(list.size()-1);
43-
lastValIndexes.add(index);
44-
}
45-
46-
list.remove(list.size()-1);
47-
4833
return true;
4934
}
5035

5136
/** Get a random element from the collection. */
5237
public int getRandom() {
53-
return list.get((int)(Math.random() * list.size()));
38+
return list.get(new java.util.Random().nextInt(list.size()));
5439
}
5540
}
5641

57-
5842
/**
5943
* Your RandomizedCollection object will be instantiated and called as such:
6044
* RandomizedCollection obj = new RandomizedCollection();

Medium/Task Scheduler.java

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
class Solution {
22
public int leastInterval(char[] tasks, int n) {
3-
int[] map = new int[26];
4-
int time = 0;
5-
6-
for (char c : tasks) {
7-
map[c - 'A']++;
3+
Map<Character, Integer> map = new HashMap<>();
4+
for (char task : tasks) {
5+
map.put(task, map.getOrDefault(task, 0) + 1);
86
}
9-
10-
Arrays.sort(map);
11-
12-
while(map[25] > 0) {
13-
int i = 0;
14-
while (i <= n) {
15-
if (map[25] == 0) break;
16-
if (i < 26 && map[25-i] > 0) {
17-
map[25 - i]--;
7+
8+
PriorityQueue<Character> pq = new PriorityQueue<>((o1, o2) -> {
9+
int c = map.get(o2).compareTo(map.get(o1));
10+
if (c != 0) {
11+
return c;
12+
}
13+
14+
return o1 - o2;
15+
});
16+
pq.addAll(map.keySet());
17+
18+
int count = 0;
19+
while (!pq.isEmpty()) {
20+
int jump = n + 1;
21+
List<Character> temp = new ArrayList<>();
22+
while (jump > 0 && !pq.isEmpty()) {
23+
Character polled = pq.poll();
24+
map.put(polled, map.get(polled) - 1);
25+
temp.add(polled);
26+
jump--;
27+
count++;
28+
}
29+
30+
for (Character executedTask : temp) {
31+
if (map.get(executedTask) > 0) {
32+
pq.add(executedTask);
1833
}
19-
i++;
20-
time++;
2134
}
22-
23-
Arrays.sort(map);
35+
36+
if (pq.isEmpty()) {
37+
break;
38+
}
39+
40+
count += jump;
2441
}
25-
26-
return time;
42+
43+
return count;
2744
}
2845
}

0 commit comments

Comments
 (0)