Skip to content

Commit 1ff38ef

Browse files
committed
Added 7 solutions
1 parent 8ce0747 commit 1ff38ef

7 files changed

+252
-0
lines changed

Easy/Flip Game.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public static List<String> generatePossibleNextMoves(String s) {
3+
List<String> ans = new ArrayList<>();
4+
5+
if (s.length() < 2) {
6+
return ans;
7+
}
8+
9+
if (s.equals("--")) {
10+
return ans;
11+
}
12+
13+
int i = 0;
14+
while (i < s.length()-1) {
15+
if (s.charAt(i) == s.charAt(i+1) && s.charAt(i) == '+') {
16+
StringBuilder sb = new StringBuilder();
17+
sb.append(s.substring(0, i));
18+
sb.append('-');
19+
sb.append('-');
20+
sb.append(s.substring(i+2));
21+
22+
ans.add(sb.toString());
23+
}
24+
25+
i++;
26+
}
27+
28+
return ans;
29+
}
30+
}

Easy/Logger Rate Limiter.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Logger {
2+
3+
/** Initialize your data structure here. */
4+
Map<String, Integer> map;
5+
public Logger() {
6+
map = new HashMap<>();
7+
}
8+
9+
/** Returns true if the message should be printed in the given timestamp, otherwise returns false.
10+
If this method returns false, the message will not be printed.
11+
The timestamp is in seconds granularity. */
12+
public boolean shouldPrintMessage(int timestamp, String message) {
13+
14+
if (map.containsKey(message) && timestamp - map.get(message) < 10) {
15+
return false;
16+
}
17+
18+
map.put(message, timestamp);
19+
20+
return true;
21+
}
22+
}
23+
24+
/**
25+
* Your Logger object will be instantiated and called as such:
26+
* Logger obj = new Logger();
27+
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
28+
*/

Easy/Meeting Rooms.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for an interval.
3+
* public class Interval {
4+
* int start;
5+
* int end;
6+
* Interval() { start = 0; end = 0; }
7+
* Interval(int s, int e) { start = s; end = e; }
8+
* }
9+
*/
10+
class Solution {
11+
public static boolean canAttendMeetings(Interval[] intervals) {
12+
Arrays.sort(intervals, new Comparator<Interval>() {
13+
@Override
14+
public int compare(Interval o1, Interval o2) {
15+
return o1.start - o2.start;
16+
}
17+
});
18+
19+
int i=0;
20+
while (i < intervals.length - 1) {
21+
if (intervals[i].end > intervals[i+1].start) {
22+
return false;
23+
}
24+
25+
i++;
26+
}
27+
28+
return true;
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class MovingAverage {
2+
3+
/** Initialize your data structure here. */
4+
Queue<Integer> queue;
5+
int capacity;
6+
int sum = 0;
7+
public MovingAverage(int size) {
8+
capacity = size;
9+
queue = new LinkedList<>();
10+
}
11+
12+
public double next(int val) {
13+
if (queue.size() == capacity) {
14+
sum -= queue.remove();
15+
}
16+
17+
queue.add(val);
18+
sum += val;
19+
20+
double avg = (double) sum / queue.size();
21+
22+
return avg;
23+
}
24+
}
25+
/**
26+
* Your MovingAverage object will be instantiated and called as such:
27+
* MovingAverage obj = new MovingAverage(size);
28+
* double param_1 = obj.next(val);
29+
*/

Easy/Palindrome Permutation.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public static boolean canPermutePalindrome(String s) {
3+
Map<Character, Integer> map = new HashMap<>();
4+
char[] chars = s.toCharArray();
5+
6+
for (char c : chars) {
7+
map.put(c, map.getOrDefault(c, 0) + 1);
8+
}
9+
10+
int oddVal = 0;
11+
12+
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
13+
if (entry.getValue()%2 != 0) {
14+
oddVal++;
15+
}
16+
17+
if (oddVal == 2) {
18+
return false;
19+
}
20+
}
21+
22+
return true;
23+
}
24+
}

Medium/Design Hit Counter.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class HitCounter {
2+
3+
/** Initialize your data structure here. */
4+
Queue<Integer> queue;
5+
public HitCounter() {
6+
queue = new LinkedList<>();
7+
}
8+
9+
/** Record a hit.
10+
@param timestamp - The current timestamp (in seconds granularity). */
11+
public void hit(int timestamp) {
12+
queue.add(timestamp);
13+
}
14+
15+
/** Return the number of hits in the past 5 minutes.
16+
@param timestamp - The current timestamp (in seconds granularity). */
17+
public int getHits(int timestamp) {
18+
while (!queue.isEmpty() && timestamp - queue.peek() >= 300) {
19+
queue.remove();
20+
}
21+
22+
return queue.size();
23+
}
24+
}
25+
26+
/**
27+
* Your HitCounter object will be instantiated and called as such:
28+
* HitCounter obj = new HitCounter();
29+
* obj.hit(timestamp);
30+
* int param_2 = obj.getHits(timestamp);
31+
*/

Medium/Meeting Rooms II.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Definition for an interval.
3+
* public class Interval {
4+
* int start;
5+
* int end;
6+
* Interval() { start = 0; end = 0; }
7+
* Interval(int s, int e) { start = s; end = e; }
8+
* }
9+
*/
10+
class Solution {
11+
public static int minMeetingRooms(Interval[] intervals) {
12+
13+
if (intervals.length == 0) {
14+
return 0;
15+
}
16+
17+
int[] start = new int[intervals.length];
18+
int[] end = new int[intervals.length];
19+
20+
int i = 0;
21+
22+
for (Interval interval : intervals) {
23+
start[i] = interval.start;
24+
end[i] = interval.end;
25+
26+
i++;
27+
}
28+
29+
Arrays.sort(start);
30+
Arrays.sort(end);
31+
32+
int slow = 0;
33+
int fast = 0;
34+
int count = 0;
35+
36+
while (slow < intervals.length) {
37+
if (start[slow] >= end[fast]) {
38+
count--;
39+
fast++;
40+
}
41+
42+
count++;
43+
slow++;
44+
}
45+
46+
return count;
47+
}
48+
49+
public static int minMeetingRoomsQueue(Interval[] intervals) {
50+
if (intervals.length == 0) {
51+
return 0;
52+
}
53+
54+
PriorityQueue<Integer> endTimes = new PriorityQueue<>(intervals.length, new Comparator<Integer>() {
55+
@Override
56+
public int compare(Integer o1, Integer o2) {
57+
return o1 - o2;
58+
}
59+
});
60+
61+
Arrays.sort(intervals, new Comparator<Interval>() {
62+
@Override
63+
public int compare(Interval o1, Interval o2) {
64+
return o1.start - o2.start;
65+
}
66+
});
67+
68+
endTimes.add(intervals[0].end);
69+
70+
for (int i=1; i<intervals.length; i++) {
71+
if (intervals[i].start >= endTimes.peek()) {
72+
endTimes.poll();
73+
}
74+
75+
endTimes.add(intervals[i].end);
76+
}
77+
78+
return endTimes.size();
79+
}
80+
}

0 commit comments

Comments
 (0)