Skip to content

Commit 1330b34

Browse files
committed
250423
1 parent 21d71df commit 1330b34

File tree

5 files changed

+380
-1
lines changed

5 files changed

+380
-1
lines changed

src/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import leetcode.editor.en.Q459.RepeatedSubstringPattern;
1414
import leetcode.editor.en.Q461.HammingDistance;
1515
import leetcode.editor.en.Q516.LongestPalindromicSubsequence;
16+
import leetcode.editor.en.Q63.UniquePathsIi;
17+
import leetcode.editor.en.Q646.MaximumLengthOfPairChain;
1618
import leetcode.editor.en.Q673.NumberOfLongestIncreasingSubsequence;
1719
import leetcode.editor.en.Q71.SimplifyPath;
1820
import leetcode.editor.en.Q727.MinimumWindowSubsequence;
@@ -33,7 +35,7 @@
3335
public class Main {
3436
public static void main(String[] args) throws IOException {
3537

36-
System.out.println(new NumberOfLongestIncreasingSubsequence().findNumberOfLIS(toIntArrayFromFile("testcase.txt")));
38+
System.out.println(new MaximumLengthOfPairChain().findLongestChain(toIntMatrix("[[-6,9],[1,6],[8,10],[-1,4],[-6,-2],[-9,8],[-5,3],[0,3]]")));
3739

3840
}
3941

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package leetcode.editor.en.Q2336;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//You have a set which contains all positive integers [1, 2, 3, 4, 5, ...].
8+
//
9+
// Implement the SmallestInfiniteSet class:
10+
//
11+
//
12+
// SmallestInfiniteSet() Initializes the SmallestInfiniteSet object to contain
13+
//all positive integers.
14+
// int popSmallest() Removes and returns the smallest integer contained in the
15+
//infinite set.
16+
// void addBack(int num) Adds a positive integer num back into the infinite set,
17+
// if it is not already in the infinite set.
18+
//
19+
//
20+
//
21+
// Example 1:
22+
//
23+
//
24+
//Input
25+
//["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest",
26+
//"popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]
27+
//[[], [2], [], [], [], [1], [], [], []]
28+
//Output
29+
//[null, null, 1, 2, 3, null, 1, 4, 5]
30+
//
31+
//Explanation
32+
//SmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet();
33+
//smallestInfiniteSet.addBack(2); // 2 is already in the set, so no change
34+
//is made.
35+
//smallestInfiniteSet.popSmallest(); // return 1, since 1 is the smallest
36+
//number, and remove it from the set.
37+
//smallestInfiniteSet.popSmallest(); // return 2, and remove it from the set.
38+
//smallestInfiniteSet.popSmallest(); // return 3, and remove it from the set.
39+
//smallestInfiniteSet.addBack(1); // 1 is added back to the set.
40+
//smallestInfiniteSet.popSmallest(); // return 1, since 1 was added back to the
41+
//set and
42+
// // is the smallest number, and remove it
43+
//from the set.
44+
//smallestInfiniteSet.popSmallest(); // return 4, and remove it from the set.
45+
//smallestInfiniteSet.popSmallest(); // return 5, and remove it from the set.
46+
//
47+
//
48+
//
49+
// Constraints:
50+
//
51+
//
52+
// 1 <= num <= 1000
53+
// At most 1000 calls will be made in total to popSmallest and addBack.
54+
//
55+
//
56+
// 👍 759 👎 76
57+
58+
59+
//leetcode submit region begin(Prohibit modification and deletion)
60+
class SmallestInfiniteSet {
61+
TreeSet<Integer> numbers;
62+
HashSet<Integer> removed;
63+
int pos;
64+
65+
public SmallestInfiniteSet() {
66+
this.numbers = new TreeSet<>();
67+
this.removed = new HashSet<>();
68+
this.pos = 1;
69+
}
70+
71+
public int popSmallest() {
72+
int ans = pos;
73+
if (!numbers.isEmpty() && numbers.first() <= pos) {
74+
ans = numbers.first();
75+
numbers.remove(ans);
76+
}
77+
if (ans == pos) {
78+
pos++;
79+
}
80+
removed.add(ans);
81+
return ans;
82+
83+
}
84+
85+
public void addBack(int num) {
86+
if (removed.contains(num)) {
87+
numbers.add(num);
88+
}
89+
}
90+
}
91+
92+
/**
93+
* Your SmallestInfiniteSet object will be instantiated and called as such:
94+
* SmallestInfiniteSet obj = new SmallestInfiniteSet();
95+
* int param_1 = obj.popSmallest();
96+
* obj.addBack(num);
97+
*/
98+
//leetcode submit region end(Prohibit modification and deletion)
99+
100+
101+
public class SmallestNumberInInfiniteSet extends SmallestInfiniteSet {
102+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package leetcode.editor.en.Q63;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//You are given an m x n integer array grid. There is a robot initially located
8+
//at the top-left corner (i.e., grid[0][0]). The robot tries to move to the
9+
//bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down
10+
//or right at any point in time.
11+
//
12+
// An obstacle and space are marked as 1 or 0 respectively in grid. A path that
13+
//the robot takes cannot include any square that is an obstacle.
14+
//
15+
// Return the number of possible unique paths that the robot can take to reach
16+
//the bottom-right corner.
17+
//
18+
// The testcases are generated so that the answer will be less than or equal to
19+
//2 * 10⁹.
20+
//
21+
//
22+
// Example 1:
23+
//
24+
//
25+
//Input: obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
26+
//Output: 2
27+
//Explanation: There is one obstacle in the middle of the 3x3 grid above.
28+
//There are two ways to reach the bottom-right corner:
29+
//1. Right -> Right -> Down -> Down
30+
//2. Down -> Down -> Right -> Right
31+
//
32+
//
33+
// Example 2:
34+
//
35+
//
36+
//Input: obstacleGrid = [[0,1],[0,0]]
37+
//Output: 1
38+
//
39+
//
40+
//
41+
// Constraints:
42+
//
43+
//
44+
// m == obstacleGrid.length
45+
// n == obstacleGrid[i].length
46+
// 1 <= m, n <= 100
47+
// obstacleGrid[i][j] is 0 or 1.
48+
//
49+
//
50+
// 👍 6881 👎 434
51+
52+
53+
//leetcode submit region begin(Prohibit modification and deletion)
54+
class Solution {
55+
Integer[][] visited;
56+
int VISITING = -1;
57+
int STONE = 1;
58+
59+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
60+
visited = new Integer[obstacleGrid.length][obstacleGrid[0].length];
61+
return uniquePathsWithObstacles(0, 0, obstacleGrid);
62+
}
63+
64+
private int uniquePathsWithObstacles(int row, int col, int[][] obstacleGrid) {
65+
if (!isValidIdx(row, col, obstacleGrid)) return 0;
66+
if (row == obstacleGrid.length - 1 && col == obstacleGrid[row].length - 1) {
67+
return 1;
68+
}
69+
if (visited[row][col] != null && visited[row][col] >= 0) {
70+
return visited[row][col];
71+
}
72+
visited[row][col] = VISITING;
73+
int ans = 0;
74+
if (isValidIdx(row + 1, col, obstacleGrid) && (visited[row + 1][col] == null || visited[row + 1][col] != VISITING)) {
75+
ans += uniquePathsWithObstacles(row + 1, col, obstacleGrid);
76+
}
77+
if (isValidIdx(row, col + 1, obstacleGrid) && (visited[row][col + 1] == null || visited[row][col + 1] != VISITING)) {
78+
ans += uniquePathsWithObstacles(row, col + 1, obstacleGrid);
79+
}
80+
81+
visited[row][col] = ans;
82+
return ans;
83+
84+
}
85+
86+
private boolean isValidIdx(int row, int col, int[][] grid) {
87+
return row >= 0 && row < grid.length && col >= 0 && col < grid[row].length && grid[row][col] != STONE;
88+
}
89+
90+
91+
}
92+
//leetcode submit region end(Prohibit modification and deletion)
93+
94+
95+
public class UniquePathsIi extends Solution {
96+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package leetcode.editor.en.Q646;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and
8+
//lefti < righti.
9+
//
10+
// A pair p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can
11+
//be formed in this fashion.
12+
//
13+
// Return the length longest chain which can be formed.
14+
//
15+
// You do not need to use up all the given intervals. You can select pairs in
16+
//any order.
17+
//
18+
//
19+
// Example 1:
20+
//
21+
//
22+
//Input: pairs = [[1,2],[2,3],[3,4]]
23+
//Output: 2
24+
//Explanation: The longest chain is [1,2] -> [3,4].
25+
//
26+
//
27+
// Example 2:
28+
//
29+
//
30+
//Input: pairs = [[1,2],[7,8],[4,5]]
31+
//Output: 3
32+
//Explanation: The longest chain is [1,2] -> [4,5] -> [7,8].
33+
//
34+
//
35+
//
36+
// Constraints:
37+
//
38+
//
39+
// n == pairs.length
40+
// 1 <= n <= 1000
41+
// -1000 <= lefti < righti <= 1000
42+
//
43+
//
44+
// 👍 2943 👎 113
45+
46+
47+
//leetcode submit region begin(Prohibit modification and deletion)
48+
class Solution {
49+
Integer cache[];
50+
public int findLongestChain(int[][] pairs) {
51+
Arrays.sort(pairs, Arrays::compare);
52+
cache = new Integer[pairs.length];
53+
int ans = 0;
54+
for (int i = 0; i < pairs.length; i++) {
55+
ans = Math.max(ans, findLongestChain(i, pairs));
56+
}
57+
return ans;
58+
}
59+
60+
public int findLongestChain(int i, int[][] pairs) {
61+
if (i == pairs.length) {
62+
return 0;
63+
}
64+
if(cache[i] != null){
65+
return cache[i];
66+
}
67+
int bound = pairs[i][1];
68+
int ans = 1;
69+
for (int j = i + 1; j < pairs.length; j++) {
70+
if (pairs[j][0] > bound) {
71+
ans = Math.max(ans, 1 + findLongestChain(j, pairs));
72+
}
73+
}
74+
cache[i] = ans;
75+
return ans;
76+
77+
}
78+
}
79+
//leetcode submit region end(Prohibit modification and deletion)
80+
81+
82+
public class MaximumLengthOfPairChain extends Solution {
83+
}

0 commit comments

Comments
 (0)