-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,210 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_822.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_823.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_824.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_825.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_826.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.