Skip to content

Commit

Permalink
add java solutions of 822 ~ 840
Browse files Browse the repository at this point in the history
  • Loading branch information
guobinhit committed Sep 29, 2019
1 parent 3c090a8 commit 0d4447d
Show file tree
Hide file tree
Showing 20 changed files with 1,210 additions and 10 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -713,16 +713,16 @@ My LeetCode Solutions!
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
|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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.hit.basmath.learn.others;

import java.util.HashSet;
import java.util.Set;

/**
* 822. Card Flipping Game
* <p>
* On a table are N cards, with a positive integer printed on the front and back of each card (possibly different).
* <p>
* We flip any number of cards, and after we choose one card.
* <p>
* 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.
* <p>
* What is the smallest number that is good? If no number is good, output 0.
* <p>
* Here, fronts[i] and backs[i] represent the number on the front and back of card i.
* <p>
* A flip swaps the front and back numbers, so the value on the front is now on the back and vice versa.
* <p>
* Example:
* <p>
* Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
* Output: 2
* Explanation: If we flip the second card, the fronts are [1,3,4,4,7] and the backs are [1,2,4,1,3].
* 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.
* <p>
* Note:
* <p>
* 1 <= fronts.length == backs.length <= 1000.
* 1 <= fronts[i] <= 2000.
* 1 <= backs[i] <= 2000.
*/
public class _822 {
public int flipgame(int[] fronts, int[] backs) {
Set<Integer> set = new HashSet<>();
int n = fronts.length;
for (int i = 0; i < n; i++) {
if (fronts[i] == backs[i]) {
set.add(fronts[i]);
}
}
int min = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (!set.contains(backs[i])) {
min = Math.min(min, backs[i]);
}
if (!set.contains(fronts[i])) {
min = Math.min(min, fronts[i]);
}
}
return min == Integer.MAX_VALUE ? 0 : min;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.hit.basmath.learn.others;

import java.util.Arrays;
import java.util.HashMap;

/**
* 823. Binary Trees With Factors
* <p>
* Given an array of unique integers, each integer is strictly greater than 1.
* <p>
* We make a binary tree using these integers and each number may be used for any number of times.
* <p>
* Each non-leaf node's value should be equal to the product of the values of it's children.
* <p>
* How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
* <p>
* Example 1:
* <p>
* Input: A = [2, 4]
* Output: 3
* Explanation: We can make these trees: [2], [4], [4, 2, 2]
* <p>
* Example 2:
* <p>
* Input: A = [2, 4, 5, 10]
* Output: 7
* Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
* <p>
* Note:
* <p>
* 1 <= A.length <= 1000.
* 2 <= A[i] <= 10 ^ 9.
*/
public class _823 {
public int numFactoredBinaryTrees(int[] A) {
long res = 0L, mod = (long) 1e9 + 7;
Arrays.sort(A);
HashMap<Integer, Long> dp = new HashMap<>();
for (int i = 0; i < A.length; ++i) {
dp.put(A[i], 1L);
for (int j = 0; j < i; ++j)
if (A[i] % A[j] == 0)
dp.put(A[i], (dp.get(A[i]) + dp.get(A[j]) * dp.getOrDefault(A[i] / A[j], 0L)) % mod);
res = (res + dp.get(A[i])) % mod;
}
return (int) res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.hit.basmath.learn.others;

import java.util.HashSet;
import java.util.Set;

/**
* 824. Goat Latin
* <p>
* A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.
* <p>
* We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.)
* <p>
* The rules of Goat Latin are as follows:
* <p>
* If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word.
* For example, the word 'apple' becomes 'applema'.
* <p>
* 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".
* For example, the word "goat" becomes "oatgma".
* <p>
* Add one letter 'a' to the end of each word per its word index in the sentence, starting with 1.
* For example, the first word gets "a" added to the end, the second word gets "aa" added to the end and so on.
* Return the final sentence representing the conversion from S to Goat Latin.
* <p>
* Example 1:
* <p>
* Input: "I speak Goat Latin"
* Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
* <p>
* Example 2:
* <p>
* Input: "The quick brown fox jumped over the lazy dog"
* Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
* <p>
* Notes:
* <p>
* S contains only uppercase, lowercase and spaces. Exactly one space between each word.
* 1 <= S.length <= 150.
*/
public class _824 {
public String toGoatLatin(String S) {
Set<Character> vowel = new HashSet<Character>();
for (char c : "aeiouAEIOU".toCharArray()) vowel.add(c);
String res = "";
int i = 0, j = 0;
for (String w : S.split("\\s")) {
res += ' ' + (vowel.contains(w.charAt(0)) ? w : w.substring(1) + w.charAt(0)) + "ma";
for (j = 0, ++i; j < i; ++j) res += "a";
}
;
return res.substring(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.hit.basmath.learn.others;

import java.util.HashMap;
import java.util.Map;

/**
* 825. Friends Of Appropriate Ages
* <p>
* Some people will make friend requests. The list of their ages is given and ages[i] is the age of the ith person.
* <p>
* Person A will NOT friend request person B (B != A) if any of the following conditions are true:
* <p>
* age[B] <= 0.5 * age[A] + 7
* age[B] > age[A]
* age[B] > 100 && age[A] < 100
* Otherwise, A will friend request B.
* <p>
* Note that if A requests B, B does not necessarily request A. Also, people will not friend request themselves.
* <p>
* How many total friend requests are made?
* <p>
* Example 1:
* <p>
* Input: [16,16]
* Output: 2
* Explanation: 2 people friend request each other.
* <p>
* Example 2:
* <p>
* Input: [16,17,18]
* Output: 2
* Explanation: Friend requests are made 17 -> 16, 18 -> 17.
* <p>
* Example 3:
* <p>
* Input: [20,30,100,110,120]
* Output:
* Explanation: Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.
* <p>
* Notes:
* <p>
* 1 <= ages.length <= 20000.
* 1 <= ages[i] <= 120.
*/
public class _825 {
public int numFriendRequests(int[] ages) {
Map<Integer, Integer> count = new HashMap<>();
for (int age : ages) count.put(age, count.getOrDefault(age, 0) + 1);
int res = 0;
for (Integer a : count.keySet())
for (Integer b : count.keySet())
if (request(a, b)) res += count.get(a) * (count.get(b) - (a == b ? 1 : 0));
return res;
}

private boolean request(int a, int b) {
return !(b <= 0.5 * a + 7 || b > a || (b > 100 && a < 100));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.hit.basmath.learn.others;

import java.util.Map;
import java.util.TreeMap;

/**
* 826. Most Profit Assigning Work
* <p>
* We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.
* <p>
* 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].
* <p>
* Every worker can be assigned at most one job, but one job can be completed multiple times.
* <p>
* 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.
* <p>
* What is the most profit we can make?
* <p>
* Example 1:
* <p>
* Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
* Output: 100
* Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately.
* <p>
* Notes:
* <p>
* 1 <= difficulty.length = profit.length <= 10000
* 1 <= worker.length <= 10000
* difficulty[i], profit[i], worker[i] are in range [1, 10^5]
*/
public class _826 {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {

TreeMap<Integer, Integer> tmap = new TreeMap<>();
// in case two jobs have same difficulty but different profit, we want to count
// the higher profit
for (int i = 0; i < difficulty.length; i++) {
tmap.put(difficulty[i], Math.max(profit[i], tmap.getOrDefault(difficulty[i], 0)));
}

int max = 0, res = 0;
// maximum profit at this difficulty or below in case
// lower difficulty job offers higher profit
for (Integer key : tmap.keySet()) {
max = Math.max(tmap.get(key), max);
tmap.put(key, max);
}

Map.Entry<Integer, Integer> entry = null;
for (int i = 0; i < worker.length; i++) {
if (tmap.containsKey(worker[i])) {
res += tmap.get(worker[i]);
} else {
entry = tmap.floorEntry(worker[i]);
if (entry != null) {
res += entry.getValue();
}
}
}
return res;
}
}
Loading

0 comments on commit 0d4447d

Please sign in to comment.