Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/main/java/g0801_0900/s0822_card_flipping_game/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package g0801_0900.s0822_card_flipping_game;

// #Medium #Array #Hash_Table

public class Solution {

public int flipgame(int[] fronts, int[] backs) {
int max = findMax(fronts, backs);
int value = 10000;
int[] twinCardHash = new int[max + 1];
int[] existingNumbersHash = new int[max + 1];

for (int i = 0; i < fronts.length; i++) {
if (fronts[i] == backs[i]) {
twinCardHash[fronts[i]]++;
}
existingNumbersHash[fronts[i]]++;
existingNumbersHash[backs[i]]++;
}

for (int i = 1; i <= max; i++) {
if ((twinCardHash[i] == 0) && (i < value) && (existingNumbersHash[i] != 0)) {
value = i;
break;
}
}

if (value == 10000) {
return 0;
} else {
return value;
}
}

private static int findMax(int[] fronts, int[] backs) {
int max = 0;
for (int front : fronts) {
if (max < front) {
max = front;
}
}

for (int back : backs) {
if (max < back) {
max = back;
}
}

return max;
}
}
38 changes: 38 additions & 0 deletions src/main/java/g0801_0900/s0822_card_flipping_game/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
822\. Card Flipping Game

Medium

You are given `n` cards, with a positive integer printed on the front and back of each card (possibly different). You can flip any number of cards (possibly zero).

After choosing the front and the back of each card, you will pick each card, and if the integer printed on the back of this card is not printed on the front of any other card, then this integer is **good**.

You are given two integer array `fronts` and `backs` where `fronts[i]` and `backs[i]` are the integers printer on the front and the back of the <code>i<sup>th</sup></code> card respectively.

Return _the smallest good and integer after flipping the cards_. If there are no good integers, return `0`.

**Note** that a **flip** swaps the front and back numbers, so the value on the front is now on the back and vice versa.

**Example 1:**

**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 the number 2 on the back, and it is not on the front of any card, so 2 is good.

**Example 2:**

**Input:** fronts = [1], backs = [1]

**Output:** 0

**Constraints:**

* `n == fronts.length`
* `n == backs.length`
* `1 <= n <= 1000`
* `1 <= fronts[i], backs[i] <= 2000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package g0801_0900.s0823_binary_trees_with_factors;

// #Medium #Array #Hash_Table #Dynamic_Programming

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

public class Solution {
private Map<Integer, Long> dp = new HashMap<>();
private Map<Integer, Integer> nums = new HashMap<>();
private static final int MOD = (int) 1e9 + 7;

public int numFactoredBinaryTrees(int[] arr) {
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
nums.put(arr[i], i);
}
long ans = 0;
for (int i = arr.length - 1; i >= 0; i--) {
ans = (ans % MOD + recursion(arr, arr[i], i) % MOD) % MOD;
}
return (int) ans;
}

private long recursion(int[] arr, int v, int idx) {
if (dp.containsKey(v)) {
return dp.get(v);
}
long ret = 1;
for (int i = 0; i < idx; i++) {
int child = arr[i];
if (v % child == 0 && nums.containsKey(v / child)) {
ret +=
(recursion(arr, child, nums.get(arr[i]))
% MOD
* recursion(arr, v / child, nums.get(v / child))
% MOD)
% MOD;
}
}
dp.put(v, ret);
return ret;
}
}
31 changes: 31 additions & 0 deletions src/main/java/g0801_0900/s0823_binary_trees_with_factors/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
823\. Binary Trees With Factors

Medium

Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.

We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.

Return _the number of binary trees we can make_. The answer may be too large so return the answer **modulo** <code>10<sup>9</sup> + 7</code>.

**Example 1:**

**Input:** arr = [2,4]

**Output:** 3

**Explanation:** We can make these trees: `[2], [4], [4, 2, 2]`

**Example 2:**

**Input:** arr = [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]`.

**Constraints:**

* `1 <= arr.length <= 1000`
* <code>2 <= arr[i] <= 10<sup>9</sup></code>
* All the values of `arr` are **unique**.
29 changes: 29 additions & 0 deletions src/main/java/g0801_0900/s0824_goat_latin/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g0801_0900.s0824_goat_latin;

// #Easy #String

public class Solution {
public String toGoatLatin(String sentence) {
String[] splits = sentence.split(" ");
StringBuilder sb = new StringBuilder();
StringBuilder a = new StringBuilder();
for (String word : splits) {
if (isVowel(word.charAt(0))) {
sb.append(word).append("ma");
} else {
char firstChar = word.charAt(0);
sb.append(word.substring(1, word.length())).append(firstChar).append("ma");
}
a.append("a");
sb.append(a);
sb.append(" ");
}

return sb.toString().trim();
}

private boolean isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E'
|| c == 'I' || c == 'O' || c == 'U';
}
}
35 changes: 35 additions & 0 deletions src/main/java/g0801_0900/s0824_goat_latin/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
824\. Goat Latin

Easy

You are given a string `sentence` that consist of words separated by spaces. Each word consists of lowercase and uppercase letters only.

We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows:

* 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"`.
* 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"`.
* 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 sentence to Goat Latin_.

**Example 1:**

**Input:** sentence = "I speak Goat Latin"

**Output:** "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

**Example 2:**

**Input:** sentence = "The quick brown fox jumped over the lazy dog"

**Output:** "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

**Constraints:**

* `1 <= sentence.length <= 150`
* `sentence` consists of English letters and spaces.
* `sentence` has no leading or trailing spaces.
* All the words in `sentence` are separated by a single space.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g0801_0900.s0825_friends_of_appropriate_ages;

// #Medium #Array #Sorting #Binary_Search #Two_Pointers

public class Solution {
public int numFriendRequests(int[] ages) {
int[] counter = new int[121];
for (int k : ages) {
counter[k]++;
}

int result = 0;
for (int k = 15; k < counter.length; k++) {
if (counter[k] == 0) {
continue;
}
result += counter[k] * (counter[k] - 1);

for (int y = k - 1; y > k / 2.0 + 7; y--) {
result += counter[k] * counter[y];
}
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
825\. Friends Of Appropriate Ages

Medium

There are `n` persons on a social media website. You are given an integer array `ages` where `ages[i]` is the age of the <code>i<sup>th</sup></code> person.

A Person `x` will not send a friend request to a person `y` (`x != y`) if any of the following conditions is true:

* `age[y] <= 0.5 * age[x] + 7`
* `age[y] > age[x]`
* `age[y] > 100 && age[x] < 100`

Otherwise, `x` will send a friend request to `y`.

Note that if `x` sends a request to `y`, `y` will not necessarily send a request to `x`. Also, a person will not send a friend request to themself.

Return _the total number of friend requests made_.

**Example 1:**

**Input:** ages = [16,16]

**Output:** 2

**Explanation:** 2 people friend request each other.

**Example 2:**

**Input:** ages = [16,17,18]

**Output:** 2

**Explanation:** Friend requests are made 17 -> 16, 18 -> 17.

**Example 3:**

**Input:** ages = [20,30,100,110,120]

**Output:** 3

**Explanation:** Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.

**Constraints:**

* `n == ages.length`
* <code>1 <= n <= 2 * 10<sup>4</sup></code>
* `1 <= ages[i] <= 120`
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package g0801_0900.s0826_most_profit_assigning_work;

// #Medium #Array #Sorting #Greedy #Binary_Search #Two_Pointers

public class Solution {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
int n = 100000;
int[] maxProfit = new int[n];

for (int i = 0; i < difficulty.length; i++) {
maxProfit[difficulty[i]] = Math.max(maxProfit[difficulty[i]], profit[i]);
}

for (int i = 1; i < n; i++) {
maxProfit[i] = Math.max(maxProfit[i], maxProfit[i - 1]);
}

int sum = 0;
for (int efficiency : worker) {
sum += maxProfit[efficiency];
}
return sum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
826\. Most Profit Assigning Work

Medium

You have `n` jobs and `m` workers. You are given three arrays: `difficulty`, `profit`, and `worker` where:

* `difficulty[i]` and `profit[i]` are the difficulty and the profit of the <code>i<sup>th</sup></code> job, and
* `worker[j]` is the ability of <code>j<sup>th</sup></code> worker (i.e., the <code>j<sup>th</sup></code> worker can only complete a job with difficulty at most `worker[j]`).

Every worker can be assigned **at most one job**, but one job can be **completed multiple times**.

* For example, if three workers attempt the same job that pays `$1`, then the total profit will be `$3`. If a worker cannot complete any job, their profit is `$0`.

Return the maximum profit we can achieve after assigning the workers to the jobs.

**Example 1:**

**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 a profit of [20,20,30,30] separately.

**Example 2:**

**Input:** difficulty = [85,47,57], profit = [24,66,99], worker = [40,25,25]

**Output:** 0

**Constraints:**

* `n == difficulty.length`
* `n == profit.length`
* `m == worker.length`
* <code>1 <= n, m <= 10<sup>4</sup></code>
* <code>1 <= difficulty[i], profit[i], worker[i] <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g0801_0900.s0822_card_flipping_game;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void flipame() {
assertThat(
new Solution().flipgame(new int[] {1, 2, 4, 4, 7}, new int[] {1, 3, 4, 1, 3}),
equalTo(2));
}

@Test
void flipame2() {
assertThat(new Solution().flipgame(new int[] {1}, new int[] {1}), equalTo(0));
}
}
Loading