Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
format code
  • Loading branch information
ThanhNIT committed Feb 2, 2022
commit 2e1587c2e196457fc2691fe773e5b9e5d72c3b1f
27 changes: 13 additions & 14 deletions src/main/java/g0801_0900/s0822_card_flipping_game/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,31 @@ public class Solution {
public int flipgame(int[] fronts, int[] backs) {
int max = findMax(fronts, backs);
int value = 10000;
int[] twin_card_hash = new int[max + 1];
int[] existing_numbers_hash = new int[max + 1];
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]) {
twin_card_hash[fronts[i]]++;
twinCardHash[fronts[i]]++;
}
existing_numbers_hash[fronts[i]]++;
existing_numbers_hash[backs[i]]++;
existingNumbersHash[fronts[i]]++;
existingNumbersHash[backs[i]]++;
}

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

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

private static int findMax(int[] fronts, int[] backs) {
int max = 0;
for (int front : fronts) {
Expand All @@ -46,7 +45,7 @@ private static int findMax(int[] fronts, int[] backs) {
max = back;
}
}

return max;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
public class Solution {
private Map<Integer, Long> dp = new HashMap<>();
private Map<Integer, Integer> nums = new HashMap<>();
private final int mod = (int) 1e9+7;
private final int mod = (int) 1e9 + 7;

public int numFactoredBinaryTrees(int[] arr) {
Arrays.sort(arr);
for (int i = 0; i<arr.length; i++) {
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;
for (int i = arr.length - 1; i >= 0; i--) {
ans = (ans % mod + recursion(arr, arr[i], i) % mod) % mod;
}
return (int) ans;
}
Expand All @@ -28,13 +28,18 @@ private long recursion(int[] arr, int v, int idx) {
return dp.get(v);
}
long ret = 1;
for (int i = 0; i<idx; i++) {
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;
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;
}
}
}
13 changes: 6 additions & 7 deletions src/main/java/g0801_0900/s0824_goat_latin/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class Solution {
public String toGoatLatin(String sentence) {
String [] splits = sentence.split(" ");
String[] splits = sentence.split(" ");
StringBuilder sb = new StringBuilder();
StringBuilder a = new StringBuilder();
for (String word : splits) {
Expand All @@ -18,13 +18,12 @@ public String toGoatLatin(String sentence) {
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';
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E'
|| c == 'I' || c == 'O' || c == 'U';
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
// #Medium #Array #Sorting #Binary_Search #Two_Pointers

public class Solution {
public int numFriendRequests(int[] ages) {
public int numFriendRequests(int[] ages) {
int[] counter = new int[121];
for (int k: ages) {
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
Expand Up @@ -4,21 +4,21 @@

public class Solution {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
int N = 100000;
int[] max_profit = new int[N];
for (int i=0; i<difficulty.length; i++) {
max_profit[difficulty[i]] = Math.max(max_profit[difficulty[i]], profit[i]);
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++) {
max_profit[i] = Math.max(max_profit[i], max_profit[i-1]);
for (int i = 1; i < n; i++) {
maxProfit[i] = Math.max(maxProfit[i], maxProfit[i - 1]);
}
int sum=0;
for (int efficiency:worker) {
sum += max_profit[efficiency];

int sum = 0;
for (int efficiency : worker) {
sum += maxProfit[efficiency];
}
return sum;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package g0801_0900.s0822_card_flipping_game;

import org.junit.jupiter.api.Test;

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));
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));
assertThat(new Solution().flipgame(new int[] {1}, new int[] {1}), equalTo(0));
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package g0801_0900.s0823_binary_trees_with_factors;

import org.junit.jupiter.api.Test;

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

import org.junit.jupiter.api.Test;

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

@Test
void numFactoredBinaryTrees2() {
assertThat(new Solution().numFactoredBinaryTrees(new int[]{2,4,5,10}), equalTo(7));
assertThat(new Solution().numFactoredBinaryTrees(new int[] {2, 4, 5, 10}), equalTo(7));
}
}
13 changes: 9 additions & 4 deletions src/test/java/g0801_0900/s0824_goat_latin/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package g0801_0900.s0824_goat_latin;

import org.junit.jupiter.api.Test;

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

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void toGoatLatin() {
assertThat(new Solution().toGoatLatin("I speak Goat Latin"), equalTo("Imaa peaksmaaa oatGmaaaa atinLmaaaaa"));
assertThat(
new Solution().toGoatLatin("I speak Goat Latin"),
equalTo("Imaa peaksmaaa oatGmaaaa atinLmaaaaa"));
}

@Test
void toGoatLatin2() {
assertThat(new Solution().toGoatLatin("The quick brown fox jumped over the lazy dog"), equalTo("heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"));
assertThat(
new Solution().toGoatLatin("The quick brown fox jumped over the lazy dog"),
equalTo(
"heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"));
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package g0801_0900.s0825_friends_of_appropriate_ages;

import org.junit.jupiter.api.Test;

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

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void numFriendRequests() {
assertThat(new Solution().numFriendRequests(new int[]{16,16}), equalTo(2));
assertThat(new Solution().numFriendRequests(new int[] {16, 16}), equalTo(2));
}

@Test
void numFriendRequests2() {
assertThat(new Solution().numFriendRequests(new int[]{16,17,18}), equalTo(2));
assertThat(new Solution().numFriendRequests(new int[] {16, 17, 18}), equalTo(2));
}

@Test
void numFriendRequests3() {
assertThat(new Solution().numFriendRequests(new int[]{20,30,100,110,120}), equalTo(3));
assertThat(new Solution().numFriendRequests(new int[] {20, 30, 100, 110, 120}), equalTo(3));
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package g0801_0900.s0826_most_profit_assigning_work;

import org.junit.jupiter.api.Test;

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

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void maxProfitAssignment() {
assertThat(new Solution().maxProfitAssignment(new int[]{2,4,6,8,10}, new int[]{10,20,30,40,50}, new int[]{4,5,6,7}), equalTo(100));
assertThat(
new Solution()
.maxProfitAssignment(
new int[] {2, 4, 6, 8, 10},
new int[] {10, 20, 30, 40, 50},
new int[] {4, 5, 6, 7}),
equalTo(100));
}

@Test
void maxProfitAssignment2() {
assertThat(new Solution().maxProfitAssignment(new int[]{85,47,57}, new int[]{24,66,99}, new int[]{40,25,25}), equalTo(0));
assertThat(
new Solution()
.maxProfitAssignment(
new int[] {85, 47, 57},
new int[] {24, 66, 99},
new int[] {40, 25, 25}),
equalTo(0));
}
}