Skip to content

Commit 6690064

Browse files
Solutions
1 parent 0cc7843 commit 6690064

File tree

115 files changed

+3441
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+3441
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* // Definition for a _Node.
3+
* function _Node(val, neighbors) {
4+
* this.val = val === undefined ? 0 : val;
5+
* this.neighbors = neighbors === undefined ? [] : neighbors;
6+
* };
7+
*/
8+
9+
/**
10+
* @param {_Node} node
11+
* @return {_Node}
12+
*/
13+
var cloneGraph = function (node) {
14+
const g = new Map();
15+
const dfs = node => {
16+
if (!node) {
17+
return null;
18+
}
19+
if (g.has(node)) {
20+
return g.get(node);
21+
}
22+
const cloned = new _Node(node.val);
23+
g.set(node, cloned);
24+
for (const nxt of node.neighbors) {
25+
cloned.neighbors.push(dfs(nxt));
26+
}
27+
return cloned;
28+
};
29+
return dfs(node);
30+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode ReverseList(ListNode head) {
14+
if (head == null || head.next == null) {
15+
return head;
16+
}
17+
ListNode ans = ReverseList(head.next);
18+
head.next.next = head;
19+
head.next = null;
20+
return ans;
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
11+
public class Solution {
12+
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
13+
while (true) {
14+
if (root.val < Math.Min(p.val, q.val)) {
15+
root = root.right;
16+
} else if (root.val > Math.Max(p.val, q.val)) {
17+
root = root.left;
18+
} else {
19+
return root;
20+
}
21+
}
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
11+
public class Solution {
12+
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
13+
if (root.val < Math.Min(p.val, q.val)) {
14+
return LowestCommonAncestor(root.right, p, q);
15+
}
16+
if (root.val > Math.Max(p.val, q.val)) {
17+
return LowestCommonAncestor(root.left, p, q);
18+
}
19+
return root;
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function minMeetingRooms(intervals: number[][]): number {
2+
const m = Math.max(...intervals.map(([_, r]) => r));
3+
const d: number[] = Array(m + 1).fill(0);
4+
for (const [l, r] of intervals) {
5+
d[l]++;
6+
d[r]--;
7+
}
8+
let [ans, s] = [0, 0];
9+
for (const v of d) {
10+
s += v;
11+
ans = Math.max(ans, s);
12+
}
13+
return ans;
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int minMeetingRooms(vector<vector<int>>& intervals) {
4+
map<int, int> d;
5+
for (const auto& e : intervals) {
6+
d[e[0]]++;
7+
d[e[1]]--;
8+
}
9+
int ans = 0, s = 0;
10+
for (auto& [_, v] : d) {
11+
s += v;
12+
ans = max(ans, s);
13+
}
14+
return ans;
15+
}
16+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func minMeetingRooms(intervals [][]int) (ans int) {
2+
d := make(map[int]int)
3+
for _, e := range intervals {
4+
d[e[0]]++
5+
d[e[1]]--
6+
}
7+
8+
keys := make([]int, 0, len(d))
9+
for k := range d {
10+
keys = append(keys, k)
11+
}
12+
sort.Ints(keys)
13+
14+
s := 0
15+
for _, k := range keys {
16+
s += d[k]
17+
ans = max(ans, s)
18+
}
19+
return
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int minMeetingRooms(int[][] intervals) {
3+
Map<Integer, Integer> d = new TreeMap<>();
4+
for (var e : intervals) {
5+
d.merge(e[0], 1, Integer::sum);
6+
d.merge(e[1], -1, Integer::sum);
7+
}
8+
int ans = 0, s = 0;
9+
for (var e : d.values()) {
10+
s += e;
11+
ans = Math.max(ans, s);
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
3+
d = defaultdict(int)
4+
for l, r in intervals:
5+
d[l] += 1
6+
d[r] -= 1
7+
ans = s = 0
8+
for _, v in sorted(d.items()):
9+
s += v
10+
ans = max(ans, s)
11+
return ans
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn min_meeting_rooms(intervals: Vec<Vec<i32>>) -> i32 {
5+
let mut d: HashMap<i32, i32> = HashMap::new();
6+
for interval in intervals {
7+
let (l, r) = (interval[0], interval[1]);
8+
*d.entry(l).or_insert(0) += 1;
9+
*d.entry(r).or_insert(0) -= 1;
10+
}
11+
12+
let mut times: Vec<i32> = d.keys().cloned().collect();
13+
times.sort();
14+
15+
let mut ans = 0;
16+
let mut s = 0;
17+
for time in times {
18+
s += d[&time];
19+
ans = ans.max(s);
20+
}
21+
22+
ans
23+
}
24+
}

0 commit comments

Comments
 (0)