Skip to content

Commit d611149

Browse files
authored
Added tasks 2412, 2413, 2414, 2415, 2416, 2418, 2419, 2420, 2421, 2423, 2424, 2425, 2426.
1 parent 4325521 commit d611149

File tree

39 files changed

+1295
-0
lines changed

39 files changed

+1295
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g2401_2500.s2412_minimum_money_required_before_transactions;
2+
3+
// #Hard #Array #Sorting #Greedy #2022_11_15_Time_4_ms_(97.57%)_Space_98_MB_(87.50%)
4+
5+
public class Solution {
6+
public long minimumMoney(int[][] transactions) {
7+
int max = 0;
8+
long sum = 0;
9+
for (int[] transaction : transactions) {
10+
int diff = transaction[1] - transaction[0];
11+
if (diff < 0) {
12+
sum -= diff;
13+
transaction[0] += diff;
14+
}
15+
max = Math.max(max, transaction[0]);
16+
}
17+
return max + sum;
18+
}
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2412\. Minimum Money Required Before Transactions
2+
3+
Hard
4+
5+
You are given a **0-indexed** 2D integer array `transactions`, where <code>transactions[i] = [cost<sub>i</sub>, cashback<sub>i</sub>]</code>.
6+
7+
The array describes transactions, where each transaction must be completed exactly once in **some order**. At any given moment, you have a certain amount of `money`. In order to complete transaction `i`, <code>money >= cost<sub>i</sub></code> must hold true. After performing a transaction, `money` becomes <code>money - cost<sub>i</sub> + cashback<sub>i</sub></code>.
8+
9+
Return _the minimum amount of_ `money` _required before any transaction so that all of the transactions can be completed **regardless of the order** of the transactions._
10+
11+
**Example 1:**
12+
13+
**Input:** transactions = [[2,1],[5,0],[4,2]]
14+
15+
**Output:** 10
16+
17+
**Explanation:**
18+
19+
Starting with money = 10, the transactions can be performed in any order.
20+
21+
It can be shown that starting with money < 10 will fail to complete all transactions in some order.
22+
23+
**Example 2:**
24+
25+
**Input:** transactions = [[3,0],[0,3]]
26+
27+
**Output:** 3
28+
29+
**Explanation:**
30+
31+
- If transactions are in the order [[3,0],[0,3]], the minimum money required to complete the transactions is 3.
32+
33+
- If transactions are in the order [[0,3],[3,0]], the minimum money required to complete the transactions is 0.
34+
35+
Thus, starting with money = 3, the transactions can be performed in any order.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= transactions.length <= 10<sup>5</sup></code>
40+
* `transactions[i].length == 2`
41+
* <code>0 <= cost<sub>i</sub>, cashback<sub>i</sub> <= 10<sup>9</sup></code>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package g2401_2500.s2413_smallest_even_multiple;
2+
3+
// #Easy #Math #Number_Theory #2022_11_15_Time_0_ms_(100.00%)_Space_40.9_MB_(48.99%)
4+
5+
public class Solution {
6+
public int smallestEvenMultiple(int n) {
7+
return n % 2 == 0 ? n : n * 2;
8+
}
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2413\. Smallest Even Multiple
2+
3+
Easy
4+
5+
Given a **positive** integer `n`, return _the smallest positive integer that is a multiple of **both**_ `2` _and_ `n`.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 5
10+
11+
**Output:** 10
12+
13+
**Explanation:** The smallest multiple of both 5 and 2 is 10.
14+
15+
**Example 2:**
16+
17+
**Input:** n = 6
18+
19+
**Output:** 6
20+
21+
**Explanation:** The smallest multiple of both 6 and 2 is 6. Note that a number is a multiple of itself.
22+
23+
**Constraints:**
24+
25+
* `1 <= n <= 150`
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2401_2500.s2414_length_of_the_longest_alphabetical_continuous_substring;
2+
3+
// #Medium #String #2022_11_15_Time_28_ms_(36.44%)_Space_53.9_MB_(81.43%)
4+
5+
public class Solution {
6+
public int longestContinuousSubstring(String s) {
7+
int ans = 0;
8+
int cnt = 1;
9+
int j = 1;
10+
while (j < s.length()) {
11+
if (s.charAt(j) == s.charAt(j - 1) + 1) {
12+
cnt++;
13+
} else {
14+
ans = Math.max(ans, cnt);
15+
cnt = 1;
16+
}
17+
j++;
18+
}
19+
return Math.max(ans, cnt);
20+
}
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2414\. Length of the Longest Alphabetical Continuous Substring
2+
3+
Medium
4+
5+
An **alphabetical continuous string** is a string consisting of consecutive letters in the alphabet. In other words, it is any substring of the string `"abcdefghijklmnopqrstuvwxyz"`.
6+
7+
* For example, `"abc"` is an alphabetical continuous string, while `"acb"` and `"za"` are not.
8+
9+
Given a string `s` consisting of lowercase letters only, return the _length of the **longest** alphabetical continuous substring._
10+
11+
**Example 1:**
12+
13+
**Input:** s = "abacaba"
14+
15+
**Output:** 2
16+
17+
**Explanation:** There are 4 distinct continuous substrings: "a", "b", "c" and "ab".
18+
19+
"ab" is the longest continuous substring.
20+
21+
**Example 2:**
22+
23+
**Input:** s = "abcde"
24+
25+
**Output:** 5
26+
27+
**Explanation:** "abcde" is the longest continuous substring.
28+
29+
**Constraints:**
30+
31+
* <code>1 <= s.length <= 10<sup>5</sup></code>
32+
* `s` consists of only English lowercase letters.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package g2401_2500.s2415_reverse_odd_levels_of_binary_tree;
2+
3+
// #Medium #Tree #Binary_Tree #Depth_First_Search #Breadth_First_Search
4+
// #2022_11_15_Time_12_ms_(64.14%)_Space_49.7_MB_(87.20%)
5+
6+
import com_github_leetcode.TreeNode;
7+
import java.util.ArrayList;
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
import java.util.Queue;
11+
12+
/*
13+
* Definition for a binary tree node.
14+
* public class TreeNode {
15+
* int val;
16+
* TreeNode left;
17+
* TreeNode right;
18+
* TreeNode() {}
19+
* TreeNode(int val) { this.val = val; }
20+
* TreeNode(int val, TreeNode left, TreeNode right) {
21+
* this.val = val;
22+
* this.left = left;
23+
* this.right = right;
24+
* }
25+
* }
26+
*/
27+
public class Solution {
28+
private List<Integer> list = new ArrayList<>();
29+
30+
public TreeNode reverseOddLevels(TreeNode root) {
31+
solve(root);
32+
return enrich(list, 0);
33+
}
34+
35+
private TreeNode enrich(List<Integer> list, int i) {
36+
TreeNode root = null;
37+
if (i < list.size()) {
38+
root = new TreeNode(list.get(i));
39+
root.left = enrich(list, 2 * i + 1);
40+
root.right = enrich(list, 2 * i + 2);
41+
}
42+
return root;
43+
}
44+
45+
private void solve(TreeNode root) {
46+
Queue<TreeNode> q = new LinkedList<>();
47+
q.add(root);
48+
int level = 0;
49+
while (!q.isEmpty()) {
50+
int size = q.size();
51+
List<Integer> res = new ArrayList<>();
52+
for (int i = 0; i < size; i++) {
53+
TreeNode cur = q.remove();
54+
res.add(cur.val);
55+
if (cur.left != null) {
56+
q.add(cur.left);
57+
}
58+
if (cur.right != null) {
59+
q.add(cur.right);
60+
}
61+
}
62+
if (level % 2 != 0) {
63+
for (int i = res.size() - 1; i >= 0; i--) {
64+
list.add(res.get(i));
65+
}
66+
} else {
67+
list.addAll(res);
68+
}
69+
level++;
70+
}
71+
}
72+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2415\. Reverse Odd Levels of Binary Tree
2+
3+
Medium
4+
5+
Given the `root` of a **perfect** binary tree, reverse the node values at each **odd** level of the tree.
6+
7+
* For example, suppose the node values at level 3 are `[2,1,3,4,7,11,29,18]`, then it should become `[18,29,11,7,4,3,1,2]`.
8+
9+
Return _the root of the reversed tree_.
10+
11+
A binary tree is **perfect** if all parent nodes have two children and all leaves are on the same level.
12+
13+
The **level** of a node is the number of edges along the path between it and the root node.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2022/07/28/first_case1.png)
18+
19+
**Input:** root = [2,3,5,8,13,21,34]
20+
21+
**Output:** [2,5,3,8,13,21,34]
22+
23+
**Explanation:**
24+
25+
The tree has only one odd level.
26+
27+
The nodes at level 1 are 3, 5 respectively, which are reversed and become 5, 3.
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2022/07/28/second_case3.png)
32+
33+
**Input:** root = [7,13,11]
34+
35+
**Output:** [7,11,13]
36+
37+
**Explanation:**
38+
39+
The nodes at level 1 are 13, 11, which are reversed and become 11, 13.
40+
41+
**Example 3:**
42+
43+
**Input:** root = [0,1,2,0,0,0,0,1,1,1,1,2,2,2,2]
44+
45+
**Output:** [0,2,1,0,0,0,0,2,2,2,2,1,1,1,1]
46+
47+
**Explanation:**
48+
49+
The odd levels have non-zero values.
50+
51+
The nodes at level 1 were 1, 2, and are 2, 1 after the reversal.
52+
53+
The nodes at level 3 were 1, 1, 1, 1, 2, 2, 2, 2, and are 2, 2, 2, 2, 1, 1, 1, 1 after the reversal.
54+
55+
**Constraints:**
56+
57+
* The number of nodes in the tree is in the range <code>[1, 2<sup>14</sup>]</code>.
58+
* <code>0 <= Node.val <= 10<sup>5</sup></code>
59+
* `root` is a **perfect** binary tree.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g2401_2500.s2416_sum_of_prefix_scores_of_strings;
2+
3+
// #Hard #Array #String #Counting #Trie #2022_11_15_Time_179_ms_(94.98%)_Space_374.1_MB_(10.98%)
4+
5+
public class Solution {
6+
private Solution[] child;
7+
private int ct;
8+
9+
public Solution() {
10+
child = new Solution[26];
11+
ct = 0;
12+
}
13+
14+
public int[] sumPrefixScores(String[] words) {
15+
for (String s : words) {
16+
insert(s);
17+
}
18+
int[] res = new int[words.length];
19+
for (int i = 0; i < words.length; i++) {
20+
String word = words[i];
21+
res[i] = countPre(word);
22+
}
23+
return res;
24+
}
25+
26+
private void insert(String word) {
27+
Solution cur = this;
28+
for (int i = 0; i < word.length(); i++) {
29+
int id = word.charAt(i) - 'a';
30+
if (cur.child[id] == null) {
31+
cur.child[id] = new Solution();
32+
}
33+
cur.child[id].ct++;
34+
cur = cur.child[id];
35+
}
36+
}
37+
38+
private int countPre(String word) {
39+
Solution cur = this;
40+
int localCt = 0;
41+
for (int i = 0; i < word.length(); i++) {
42+
int id = word.charAt(i) - 'a';
43+
if (cur.child[id] == null) {
44+
return localCt;
45+
}
46+
localCt += cur.ct;
47+
cur = cur.child[id];
48+
}
49+
localCt += cur.ct;
50+
return localCt;
51+
}
52+
}

0 commit comments

Comments
 (0)