Skip to content

Commit 21d71df

Browse files
committed
240423
1 parent f11c7bd commit 21d71df

9 files changed

+625
-2
lines changed

src/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
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.Q673.NumberOfLongestIncreasingSubsequence;
1617
import leetcode.editor.en.Q71.SimplifyPath;
18+
import leetcode.editor.en.Q727.MinimumWindowSubsequence;
1719
import leetcode.editor.en.Q740.DeleteAndEarn;
1820
import leetcode.editor.en.Q879.ProfitableSchemes;
1921
import leetcode.editor.en.Q946.ValidateStackSequences;
@@ -31,7 +33,7 @@
3133
public class Main {
3234
public static void main(String[] args) throws IOException {
3335

34-
System.out.println(new RestoreTheArray().numberOfArrays("1317", 2000));
36+
System.out.println(new NumberOfLongestIncreasingSubsequence().findNumberOfLIS(toIntArrayFromFile("testcase.txt")));
3537

3638
}
3739

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package leetcode.editor.en.Q1046;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//You are given an array of integers stones where stones[i] is the weight of
8+
//the iᵗʰ stone.
9+
//
10+
// We are playing a game with the stones. On each turn, we choose the heaviest
11+
//two stones and smash them together. Suppose the heaviest two stones have weights
12+
//x and y with x <= y. The result of this smash is:
13+
//
14+
//
15+
// If x == y, both stones are destroyed, and
16+
// If x != y, the stone of weight x is destroyed, and the stone of weight y has
17+
//new weight y - x.
18+
//
19+
//
20+
// At the end of the game, there is at most one stone left.
21+
//
22+
// Return the weight of the last remaining stone. If there are no stones left,
23+
//return 0.
24+
//
25+
//
26+
// Example 1:
27+
//
28+
//
29+
//Input: stones = [2,7,4,1,8,1]
30+
//Output: 1
31+
//Explanation:
32+
//We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
33+
//we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
34+
//we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
35+
//we combine 1 and 1 to get 0 so the array converts to [1] then that's the
36+
//value of the last stone.
37+
//
38+
//
39+
// Example 2:
40+
//
41+
//
42+
//Input: stones = [1]
43+
//Output: 1
44+
//
45+
//
46+
//
47+
// Constraints:
48+
//
49+
//
50+
// 1 <= stones.length <= 30
51+
// 1 <= stones[i] <= 1000
52+
//
53+
//
54+
// 👍 4737 👎 83
55+
56+
57+
//leetcode submit region begin(Prohibit modification and deletion)
58+
class Solution {
59+
public int lastStoneWeight(int[] stones) {
60+
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
61+
for (int stone : stones) {
62+
pq.add(stone);
63+
}
64+
65+
while (pq.size() > 1) {
66+
int stoneOne = pq.poll();
67+
int stoneTwo = 0;
68+
if (!pq.isEmpty()) {
69+
stoneTwo = pq.poll();
70+
}
71+
if (stoneOne == stoneTwo) continue;
72+
pq.add(stoneOne - stoneTwo);
73+
}
74+
75+
return pq.isEmpty() ? 0 : pq.poll();
76+
77+
}
78+
}
79+
//leetcode submit region end(Prohibit modification and deletion)
80+
81+
82+
public class LastStoneWeight extends Solution {
83+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode.editor.en.Q2651;
2+
import java.util.*;
3+
import javafx.util.Pair;
4+
5+
//You are given a positive integer arrivalTime denoting the arrival time of a
6+
//train in hours, and another positive integer delayedTime denoting the amount of
7+
//delay in hours.
8+
//
9+
// Return the time when the train will arrive at the station.
10+
//
11+
// Note that the time in this problem is in 24-hours format.
12+
//
13+
//
14+
// Example 1:
15+
//
16+
//
17+
//Input: arrivalTime = 15, delayedTime = 5
18+
//Output: 20
19+
//Explanation: Arrival time of the train was 15:00 hours. It is delayed by 5
20+
//hours. Now it will reach at 15+5 = 20 (20:00 hours).
21+
//
22+
//
23+
// Example 2:
24+
//
25+
//
26+
//Input: arrivalTime = 13, delayedTime = 11
27+
//Output: 0
28+
//Explanation: Arrival time of the train was 13:00 hours. It is delayed by 11
29+
//hours. Now it will reach at 13+11=24 (Which is denoted by 00:00 in 24 hours
30+
//format so return 0).
31+
//
32+
//
33+
//
34+
// Constraints:
35+
//
36+
//
37+
// 1 <= arrivaltime < 24
38+
// 1 <= delayedTime <= 24
39+
//
40+
//
41+
// 👍 62 👎 12
42+
43+
44+
//leetcode submit region begin(Prohibit modification and deletion)
45+
class Solution {
46+
public int findDelayedArrivalTime(int arrivalTime, int delayedTime) {
47+
return (arrivalTime + delayedTime) % 24;
48+
}
49+
}
50+
//leetcode submit region end(Prohibit modification and deletion)
51+
52+
53+
54+
public class CalculateDelayedArrivalTime extends Solution {}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package leetcode.editor.en.Q2652;
2+
import java.util.*;
3+
import javafx.util.Pair;
4+
5+
//Given a positive integer n, find the sum of all integers in the range [1, n]
6+
//inclusive that are divisible by 3, 5, or 7.
7+
//
8+
// Return an integer denoting the sum of all numbers in the given range
9+
//satisfying the constraint.
10+
//
11+
//
12+
// Example 1:
13+
//
14+
//
15+
//Input: n = 7
16+
//Output: 21
17+
//Explanation: Numbers in the range [1, 7] that are divisible by 3, 5, or 7 are
18+
//3, 5, 6, 7. The sum of these numbers is 21.
19+
//
20+
//
21+
// Example 2:
22+
//
23+
//
24+
//Input: n = 10
25+
//Output: 40
26+
//Explanation: Numbers in the range [1, 10] that are divisible by 3, 5, or 7
27+
//are 3, 5, 6, 7, 9, 10. The sum of these numbers is 40.
28+
//
29+
//
30+
// Example 3:
31+
//
32+
//
33+
//Input: n = 9
34+
//Output: 30
35+
//Explanation: Numbers in the range [1, 9] that are divisible by 3, 5, or 7 are
36+
//3, 5, 6, 7, 9. The sum of these numbers is 30.
37+
//
38+
//
39+
//
40+
// Constraints:
41+
//
42+
//
43+
// 1 <= n <= 10³
44+
//
45+
//
46+
// 👍 60 👎 5
47+
48+
49+
//leetcode submit region begin(Prohibit modification and deletion)
50+
class Solution {
51+
public int sumOfMultiples(int n) {
52+
int ans = 0;
53+
54+
for (int i = 1; i <= n; i++) {
55+
if (i % 3 == 0 || i % 5 == 0 || i % 7 == 0) {
56+
// System.out.println(i);
57+
ans += i;
58+
}
59+
60+
}
61+
return ans;
62+
63+
}
64+
}
65+
//leetcode submit region end(Prohibit modification and deletion)
66+
67+
68+
69+
public class SumMultiples extends Solution {}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package leetcode.editor.en.Q2653;
2+
import java.util.*;
3+
import javafx.util.Pair;
4+
5+
//Given an integer array nums containing n integers, find the beauty of each
6+
//subarray of size k.
7+
//
8+
// The beauty of a subarray is the xᵗʰ smallest integer in the subarray if it
9+
//is negative, or 0 if there are fewer than x negative integers.
10+
//
11+
// Return an integer array containing n - k + 1 integers, which denote the
12+
//beauty of the subarrays in order from the first index in the array.
13+
//
14+
//
15+
// A subarray is a contiguous non-empty sequence of elements within an array.
16+
//
17+
//
18+
//
19+
// Example 1:
20+
//
21+
//
22+
//Input: nums = [1,-1,-3,-2,3], k = 3, x = 2
23+
//Output: [-1,-2,-2]
24+
//Explanation: There are 3 subarrays with size k = 3.
25+
//The first subarray is [1, -1, -3] and the 2ⁿᵈ smallest negative integer is -1.
26+
// 
27+
//The second subarray is [-1, -3, -2] and the 2ⁿᵈ smallest negative integer is -
28+
//2. 
29+
//The third subarray is [-3, -2, 3] and the 2ⁿᵈ smallest negative integer is -2.
30+
//
31+
//
32+
// Example 2:
33+
//
34+
//
35+
//Input: nums = [-1,-2,-3,-4,-5], k = 2, x = 2
36+
//Output: [-1,-2,-3,-4]
37+
//Explanation: There are 4 subarrays with size k = 2.
38+
//For [-1, -2], the 2ⁿᵈ smallest negative integer is -1.
39+
//For [-2, -3], the 2ⁿᵈ smallest negative integer is -2.
40+
//For [-3, -4], the 2ⁿᵈ smallest negative integer is -3.
41+
//For [-4, -5], the 2ⁿᵈ smallest negative integer is -4. 
42+
//
43+
// Example 3:
44+
//
45+
//
46+
//Input: nums = [-3,1,2,-3,0,-3], k = 2, x = 1
47+
//Output: [-3,0,-3,-3,-3]
48+
//Explanation: There are 5 subarrays with size k = 2.
49+
//For [-3, 1], the 1ˢᵗ smallest negative integer is -3.
50+
//For [1, 2], there is no negative integer so the beauty is 0.
51+
//For [2, -3], the 1ˢᵗ smallest negative integer is -3.
52+
//For [-3, 0], the 1ˢᵗ smallest negative integer is -3.
53+
//For [0, -3], the 1ˢᵗ smallest negative integer is -3.
54+
//
55+
//
56+
// Constraints:
57+
//
58+
//
59+
// n == nums.length
60+
// 1 <= n <= 10⁵
61+
// 1 <= k <= n
62+
// 1 <= x <= k
63+
// -50 <= nums[i] <= 50
64+
//
65+
//
66+
// 👍 249 👎 88
67+
68+
69+
//leetcode submit region begin(Prohibit modification and deletion)
70+
class Solution {
71+
public int[] getSubarrayBeauty(int[] nums, int k, int x) {
72+
TreeMap<Integer, Integer> numbers = new TreeMap<>();
73+
LinkedList<Integer> sliding = new LinkedList<>();
74+
int n = nums.length;
75+
int[] ans = new int[n - k + 1];
76+
int ansIdx = 0;
77+
for (int num : nums) {
78+
sliding.add(num);
79+
numbers.put(num, numbers.getOrDefault(num, 0) + 1);
80+
if (sliding.size() == k) {
81+
int toRemove = sliding.pollFirst();
82+
int index = 0;
83+
for (Map.Entry<Integer, Integer> kv : numbers.entrySet()) {
84+
index += kv.getValue();
85+
if (index >= x) {
86+
int value = kv.getKey();
87+
if (value < 0) ans[ansIdx++] = value;
88+
else ansIdx++;
89+
break;
90+
}
91+
}
92+
int total = numbers.get(toRemove);
93+
total--;
94+
if (total == 0) numbers.remove(toRemove);
95+
else numbers.put(toRemove, total);
96+
}
97+
}
98+
return ans;
99+
}
100+
101+
102+
}
103+
//leetcode submit region end(Prohibit modification and deletion)
104+
105+
106+
107+
public class SlidingSubarrayBeauty extends Solution {}

0 commit comments

Comments
 (0)