Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d4447d

Browse files
add java solutions of 822 ~ 840
1 parent 3c090a8 commit 0d4447d

File tree

20 files changed

+1210
-10
lines changed

20 files changed

+1210
-10
lines changed

‎README.md‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -713,16 +713,16 @@ My LeetCode Solutions!
713713
|828|[Unique Letter String](https://leetcode.com/problems/unique-letter-string/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_828.java) & Python |![hard](https://github.com/guobinhit/myleetcode/blob/master/images/hard.png)| Others
714714
|829|[Consecutive Numbers Sum](https://leetcode.com/problems/consecutive-numbers-sum/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_829.java) & Python |![hard](https://github.com/guobinhit/myleetcode/blob/master/images/hard.png)| Others
715715
|830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_830.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
716-
|831|[Masking Personal Information]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_831.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
717-
|832|[Flipping an Image]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_832.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
718-
|833|[Find And Replace in String]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_833.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
719-
|834|[Sum of Distances in Tree]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_834.java) & Python |![hard](https://github.com/guobinhit/myleetcode/blob/master/images/hard.png)| Others
720-
|835|[Image Overlap]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_835.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
721-
|836|[Rectangle Overlap]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_836.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
722-
|837|[New 21 Game]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_837.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
723-
|838|[Push Dominoes]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_838.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
724-
|839|[Similar String Groups]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_839.java) & Python |![hard](https://github.com/guobinhit/myleetcode/blob/master/images/hard.png)| Others
725-
|840|[Magic Squares In Grid]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_840.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
716+
|831|[Masking Personal Information](https://leetcode.com/problems/masking-personal-information/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_831.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
717+
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_832.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
718+
|833|[Find And Replace in String](https://leetcode.com/problems/find-and-replace-in-string/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_833.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
719+
|834|[Sum of Distances in Tree](https://leetcode.com/problems/sum-of-distances-in-tree/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_834.java) & Python |![hard](https://github.com/guobinhit/myleetcode/blob/master/images/hard.png)| Others
720+
|835|[Image Overlap](https://leetcode.com/problems/image-overlap/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_835.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
721+
|836|[Rectangle Overlap](https://leetcode.com/problems/rectangle-overlap/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_836.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
722+
|837|[New 21 Game](https://leetcode.com/problems/new-21-game/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_837.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
723+
|838|[Push Dominoes](https://leetcode.com/problems/push-dominoes/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_838.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
724+
|839|[Similar String Groups](https://leetcode.com/problems/similar-string-groups/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_839.java) & Python |![hard](https://github.com/guobinhit/myleetcode/blob/master/images/hard.png)| Others
725+
|840|[Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_840.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
726726
|842|[Split Array into Fibonacci Sequence]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_842.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
727727
|843|[Guess the Word]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_843.java) & Python |![hard](https://github.com/guobinhit/myleetcode/blob/master/images/hard.png)| Others
728728
|844|[Backspace String Compare]()|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_844.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.hit.basmath.learn.others;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* 822. Card Flipping Game
8+
* <p>
9+
* On a table are N cards, with a positive integer printed on the front and back of each card (possibly different).
10+
* <p>
11+
* We flip any number of cards, and after we choose one card.
12+
* <p>
13+
* If the number X on the back of the chosen card is not on the front of any card, then this number X is good.
14+
* <p>
15+
* What is the smallest number that is good? If no number is good, output 0.
16+
* <p>
17+
* Here, fronts[i] and backs[i] represent the number on the front and back of card i.
18+
* <p>
19+
* A flip swaps the front and back numbers, so the value on the front is now on the back and vice versa.
20+
* <p>
21+
* Example:
22+
* <p>
23+
* Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
24+
* Output: 2
25+
* Explanation: If we flip the second card, the fronts are [1,3,4,4,7] and the backs are [1,2,4,1,3].
26+
* We choose the second card, which has number 2 on the back, and it isn't on the front of any card, so 2 is good.
27+
* <p>
28+
* Note:
29+
* <p>
30+
* 1 <= fronts.length == backs.length <= 1000.
31+
* 1 <= fronts[i] <= 2000.
32+
* 1 <= backs[i] <= 2000.
33+
*/
34+
public class _822 {
35+
public int flipgame(int[] fronts, int[] backs) {
36+
Set<Integer> set = new HashSet<>();
37+
int n = fronts.length;
38+
for (int i = 0; i < n; i++) {
39+
if (fronts[i] == backs[i]) {
40+
set.add(fronts[i]);
41+
}
42+
}
43+
int min = Integer.MAX_VALUE;
44+
for (int i = 0; i < n; i++) {
45+
if (!set.contains(backs[i])) {
46+
min = Math.min(min, backs[i]);
47+
}
48+
if (!set.contains(fronts[i])) {
49+
min = Math.min(min, fronts[i]);
50+
}
51+
}
52+
return min == Integer.MAX_VALUE ? 0 : min;
53+
}
54+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.hit.basmath.learn.others;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
6+
/**
7+
* 823. Binary Trees With Factors
8+
* <p>
9+
* Given an array of unique integers, each integer is strictly greater than 1.
10+
* <p>
11+
* We make a binary tree using these integers and each number may be used for any number of times.
12+
* <p>
13+
* Each non-leaf node's value should be equal to the product of the values of it's children.
14+
* <p>
15+
* How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
16+
* <p>
17+
* Example 1:
18+
* <p>
19+
* Input: A = [2, 4]
20+
* Output: 3
21+
* Explanation: We can make these trees: [2], [4], [4, 2, 2]
22+
* <p>
23+
* Example 2:
24+
* <p>
25+
* Input: A = [2, 4, 5, 10]
26+
* Output: 7
27+
* Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
28+
* <p>
29+
* Note:
30+
* <p>
31+
* 1 <= A.length <= 1000.
32+
* 2 <= A[i] <= 10 ^ 9.
33+
*/
34+
public class _823 {
35+
public int numFactoredBinaryTrees(int[] A) {
36+
long res = 0L, mod = (long) 1e9 + 7;
37+
Arrays.sort(A);
38+
HashMap<Integer, Long> dp = new HashMap<>();
39+
for (int i = 0; i < A.length; ++i) {
40+
dp.put(A[i], 1L);
41+
for (int j = 0; j < i; ++j)
42+
if (A[i] % A[j] == 0)
43+
dp.put(A[i], (dp.get(A[i]) + dp.get(A[j]) * dp.getOrDefault(A[i] / A[j], 0L)) % mod);
44+
res = (res + dp.get(A[i])) % mod;
45+
}
46+
return (int) res;
47+
}
48+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.hit.basmath.learn.others;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* 824. Goat Latin
8+
* <p>
9+
* A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.
10+
* <p>
11+
* We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.)
12+
* <p>
13+
* The rules of Goat Latin are as follows:
14+
* <p>
15+
* If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word.
16+
* For example, the word 'apple' becomes 'applema'.
17+
* <p>
18+
* If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add "ma".
19+
* For example, the word "goat" becomes "oatgma".
20+
* <p>
21+
* Add one letter 'a' to the end of each word per its word index in the sentence, starting with 1.
22+
* For example, the first word gets "a" added to the end, the second word gets "aa" added to the end and so on.
23+
* Return the final sentence representing the conversion from S to Goat Latin.
24+
* <p>
25+
* Example 1:
26+
* <p>
27+
* Input: "I speak Goat Latin"
28+
* Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
29+
* <p>
30+
* Example 2:
31+
* <p>
32+
* Input: "The quick brown fox jumped over the lazy dog"
33+
* Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
34+
* <p>
35+
* Notes:
36+
* <p>
37+
* S contains only uppercase, lowercase and spaces. Exactly one space between each word.
38+
* 1 <= S.length <= 150.
39+
*/
40+
public class _824 {
41+
public String toGoatLatin(String S) {
42+
Set<Character> vowel = new HashSet<Character>();
43+
for (char c : "aeiouAEIOU".toCharArray()) vowel.add(c);
44+
String res = "";
45+
int i = 0, j = 0;
46+
for (String w : S.split("\\s")) {
47+
res += ' ' + (vowel.contains(w.charAt(0)) ? w : w.substring(1) + w.charAt(0)) + "ma";
48+
for (j = 0, ++i; j < i; ++j) res += "a";
49+
}
50+
;
51+
return res.substring(1);
52+
}
53+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.hit.basmath.learn.others;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 825. Friends Of Appropriate Ages
8+
* <p>
9+
* Some people will make friend requests. The list of their ages is given and ages[i] is the age of the ith person.
10+
* <p>
11+
* Person A will NOT friend request person B (B != A) if any of the following conditions are true:
12+
* <p>
13+
* age[B] <= 0.5 * age[A] + 7
14+
* age[B] > age[A]
15+
* age[B] > 100 && age[A] < 100
16+
* Otherwise, A will friend request B.
17+
* <p>
18+
* Note that if A requests B, B does not necessarily request A. Also, people will not friend request themselves.
19+
* <p>
20+
* How many total friend requests are made?
21+
* <p>
22+
* Example 1:
23+
* <p>
24+
* Input: [16,16]
25+
* Output: 2
26+
* Explanation: 2 people friend request each other.
27+
* <p>
28+
* Example 2:
29+
* <p>
30+
* Input: [16,17,18]
31+
* Output: 2
32+
* Explanation: Friend requests are made 17 -> 16, 18 -> 17.
33+
* <p>
34+
* Example 3:
35+
* <p>
36+
* Input: [20,30,100,110,120]
37+
* Output:
38+
* Explanation: Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.
39+
* <p>
40+
* Notes:
41+
* <p>
42+
* 1 <= ages.length <= 20000.
43+
* 1 <= ages[i] <= 120.
44+
*/
45+
public class _825 {
46+
public int numFriendRequests(int[] ages) {
47+
Map<Integer, Integer> count = new HashMap<>();
48+
for (int age : ages) count.put(age, count.getOrDefault(age, 0) + 1);
49+
int res = 0;
50+
for (Integer a : count.keySet())
51+
for (Integer b : count.keySet())
52+
if (request(a, b)) res += count.get(a) * (count.get(b) - (a == b ? 1 : 0));
53+
return res;
54+
}
55+
56+
private boolean request(int a, int b) {
57+
return !(b <= 0.5 * a + 7 || b > a || (b > 100 && a < 100));
58+
}
59+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.hit.basmath.learn.others;
2+
3+
import java.util.Map;
4+
import java.util.TreeMap;
5+
6+
/**
7+
* 826. Most Profit Assigning Work
8+
* <p>
9+
* We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.
10+
* <p>
11+
* Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].
12+
* <p>
13+
* Every worker can be assigned at most one job, but one job can be completed multiple times.
14+
* <p>
15+
* For example, if 3 people attempt the same job that pays 1,ドル then the total profit will be 3ドル. If a worker cannot complete any job, his profit is 0ドル.
16+
* <p>
17+
* What is the most profit we can make?
18+
* <p>
19+
* Example 1:
20+
* <p>
21+
* Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
22+
* Output: 100
23+
* Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately.
24+
* <p>
25+
* Notes:
26+
* <p>
27+
* 1 <= difficulty.length = profit.length <= 10000
28+
* 1 <= worker.length <= 10000
29+
* difficulty[i], profit[i], worker[i] are in range [1, 10^5]
30+
*/
31+
public class _826 {
32+
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
33+
34+
TreeMap<Integer, Integer> tmap = new TreeMap<>();
35+
// in case two jobs have same difficulty but different profit, we want to count
36+
// the higher profit
37+
for (int i = 0; i < difficulty.length; i++) {
38+
tmap.put(difficulty[i], Math.max(profit[i], tmap.getOrDefault(difficulty[i], 0)));
39+
}
40+
41+
int max = 0, res = 0;
42+
// maximum profit at this difficulty or below in case
43+
// lower difficulty job offers higher profit
44+
for (Integer key : tmap.keySet()) {
45+
max = Math.max(tmap.get(key), max);
46+
tmap.put(key, max);
47+
}
48+
49+
Map.Entry<Integer, Integer> entry = null;
50+
for (int i = 0; i < worker.length; i++) {
51+
if (tmap.containsKey(worker[i])) {
52+
res += tmap.get(worker[i]);
53+
} else {
54+
entry = tmap.floorEntry(worker[i]);
55+
if (entry != null) {
56+
res += entry.getValue();
57+
}
58+
}
59+
}
60+
return res;
61+
}
62+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /