Skip to content

Commit b58bd6b

Browse files
committed
Update
1 parent 266fed8 commit b58bd6b

19 files changed

+702
-12
lines changed

src/array/MeetingRooms.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/meeting-rooms
8+
* Difficulty: Easy
9+
* Time complexity: O(nlog n)
10+
* Space complexity: O(1)
11+
*/
12+
public class MeetingRooms {
13+
14+
public boolean canAttendMeetings(Interval[] intervals) {
15+
Arrays.sort(intervals, Comparator.comparingInt(i -> i.start));
16+
17+
for (int i = 1; i < intervals.length; i++) {
18+
if (intervals[i].start < intervals[i - 1].end) {
19+
return false;
20+
}
21+
}
22+
23+
return true;
24+
}
25+
26+
private static class Interval {
27+
int start;
28+
int end;
29+
}
30+
}

src/binary/AddBinary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Description: https://leetcode.com/problems/add-binary
55
* Difficulty: Easy
66
* Time complexity: O(n)
7-
* Space complexity: O(1)
7+
* Space complexity: O(n)
88
*/
99
public class AddBinary {
1010

src/heap/KClosestPointsToOrigin.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ public int[][] kClosestViaSorting(int[][] points, int k) {
2323
* Time complexity: O(nlog k)
2424
* Space complexity: O(k)
2525
*/
26-
public int[][] kClosestViaMaxHeap(int[][] points, int k) {
27-
Queue<int[]> maxHeap = new PriorityQueue<>((p1, p2) -> Integer.compare(distance(p2), distance(p1)));
26+
public int[][] kClosestViaMinHeap(int[][] points, int k) {
27+
Queue<int[]> minHeap = new PriorityQueue<>((p1, p2) -> Integer.compare(distance(p2), distance(p1)));
2828

2929
for (int[] point : points) {
30-
maxHeap.offer(point);
31-
if (maxHeap.size() > k) {
32-
maxHeap.poll();
30+
minHeap.offer(point);
31+
if (minHeap.size() > k) {
32+
minHeap.poll();
3333
}
3434
}
3535

3636
int[][] result = new int[k][2];
3737
for (int i = 0; i < k; i++) {
38-
result[i] = maxHeap.poll();
38+
result[i] = minHeap.poll();
3939
}
4040

4141
return result;

src/linked_list/AddTwoNumbers.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package linked_list;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/add-two-numbers
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(n)
8+
*/
9+
public class AddTwoNumbers {
10+
11+
public ListNode addTwoNumbers(ListNode first, ListNode second) {
12+
int carry = 0;
13+
ListNode head = new ListNode();
14+
ListNode current = head;
15+
16+
while (first != null || second != null) {
17+
int sum = carry;
18+
if (first != null) {
19+
sum += first.val;
20+
first = first.next;
21+
}
22+
23+
if (second != null) {
24+
sum += second.val;
25+
second = second.next;
26+
}
27+
28+
current.next = new ListNode(sum % 10);
29+
current = current.next;
30+
carry = sum / 10;
31+
}
32+
33+
if (carry != 0) {
34+
current.next = new ListNode(carry);
35+
}
36+
37+
return head.next;
38+
}
39+
40+
private static class ListNode {
41+
ListNode next;
42+
int val;
43+
44+
public ListNode() {
45+
}
46+
47+
public ListNode(int val) {
48+
this.val = val;
49+
}
50+
}
51+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package linked_list;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/remove-nth-node-from-end-of-list
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class RemoveNthNodeFromEndOfList {
10+
11+
public ListNode removeNthFromEnd(ListNode head, int n) {
12+
ListNode slow = head;
13+
ListNode fast = head;
14+
15+
for (int i = 0; i < n; i++) {
16+
fast = fast.next;
17+
}
18+
19+
if (fast == null) { // n == list.size() -> head deletion
20+
return head.next;
21+
}
22+
23+
while (fast.next != null) {
24+
slow = slow.next;
25+
fast = fast.next;
26+
}
27+
28+
slow.next = slow.next.next;
29+
30+
return head;
31+
}
32+
33+
private static class ListNode {
34+
ListNode next;
35+
}
36+
}

src/matrix/SpiralMatrix.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package matrix;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/spiral-matrix
8+
* Difficulty: Medium
9+
* Time complexity: O(n * m)
10+
* Space complexity: O(n * m)
11+
*/
12+
public class SpiralMatrix {
13+
14+
private static final int RIGHT = 0;
15+
private static final int BOTTOM = 1;
16+
private static final int LEFT = 2;
17+
private static final int TOP = 3;
18+
19+
public List<Integer> spiralOrder(int[][] matrix) {
20+
List<Integer> result = new ArrayList<>();
21+
22+
int rowStart = 0;
23+
int rowEnd = matrix[0].length - 1;
24+
int colStart = 0;
25+
int colEnd = matrix.length - 1;
26+
27+
int direction = RIGHT;
28+
29+
while (rowStart <= rowEnd && colStart <= colEnd) {
30+
switch (direction) {
31+
case RIGHT:
32+
for (int i = rowStart; i <= rowEnd; i++) {
33+
result.add(matrix[colStart][i]);
34+
}
35+
colStart++;
36+
break;
37+
case BOTTOM:
38+
for (int i = colStart; i <= colEnd; i++) {
39+
result.add(matrix[i][rowEnd]);
40+
}
41+
rowEnd--;
42+
break;
43+
case LEFT:
44+
for (int i = rowEnd; i >= rowStart; i--) {
45+
result.add(matrix[colEnd][i]);
46+
}
47+
colEnd--;
48+
break;
49+
case TOP:
50+
for (int i = colEnd; i >= colStart; i--) {
51+
result.add(matrix[i][rowStart]);
52+
}
53+
rowStart++;
54+
break;
55+
}
56+
57+
direction = (direction + 1) % 4;
58+
}
59+
60+
return result;
61+
}
62+
}

src/matrix/ValidSudoku.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package matrix;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/valid-sudoku
8+
* Difficulty: Medium
9+
* Time complexity: O(n * m)
10+
* Space complexity: O(n * m)
11+
*/
12+
public class ValidSudoku {
13+
14+
public boolean isValidSudoku(char[][] board) {
15+
Set<String> seen = new HashSet<>();
16+
17+
for (int i = 0; i < board.length; i++) {
18+
for (int j = 0; j < board[0].length; j++) {
19+
char cell = board[i][j];
20+
if (cell == '.') continue;
21+
22+
if (!seen.add(cell + " in row " + i)
23+
|| !seen.add(cell + " in col " + j)
24+
|| !seen.add(cell + " in square " + i/3 + ":" + j/3)) {
25+
return false;
26+
}
27+
}
28+
}
29+
30+
return true;
31+
}
32+
}

src/recursion/CombinationSum.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
/**
77
* Description: https://leetcode.com/problems/combination-sum
88
* Difficulty: Medium
9-
* Time complexity: O(n * n!)
10-
* Space complexity: O(n)
9+
* Time complexity: O(2^n)
10+
* Space complexity: O(2^n)
1111
*/
1212
public class CombinationSum {
1313

src/recursion/CombinationSum2.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package recursion;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* Description: https://leetcode.com/problems/combination-sum-ii
9+
* Difficulty: Medium
10+
* Time complexity: O(2^n)
11+
* Space complexity: O(2^n)
12+
*/
13+
public class CombinationSum2 {
14+
15+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
16+
Arrays.sort(candidates);
17+
18+
List<List<Integer>> combinations = new ArrayList<>();
19+
backtrack(candidates, 0, new ArrayList<>(), target, combinations);
20+
21+
return combinations;
22+
}
23+
24+
private void backtrack(
25+
int[] candidates,
26+
int start,
27+
List<Integer> combination,
28+
int remains,
29+
List<List<Integer>> result) {
30+
if (remains < 0) return;
31+
32+
if (remains == 0) {
33+
result.add(new ArrayList<>(combination));
34+
return;
35+
}
36+
37+
for (int i = start; i < candidates.length; i++) {
38+
if (i > start && candidates[i] == candidates[i - 1]) continue;
39+
40+
combination.add(candidates[i]);
41+
backtrack(candidates, i + 1, combination, remains - candidates[i], result);
42+
combination.remove(combination.size() - 1);
43+
}
44+
}
45+
}

src/recursion/GenerateParentheses.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Description: https://leetcode.com/problems/generate-parentheses
88
* Difficulty: Medium
99
* Time complexity: O(2^2n)
10-
* Space complexity: O(n)
10+
* Space complexity: O(2^2n)
1111
*/
1212
public class GenerateParentheses {
1313

0 commit comments

Comments
 (0)