Skip to content

Commit ba90006

Browse files
authored
Added tasks 822, 823, 824, 825
1 parent 50857c7 commit ba90006

File tree

13 files changed

+389
-0
lines changed

13 files changed

+389
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
17121712
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
17131713
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
17141714
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys/Solution.kt)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
1715+
| 0825 |[Friends Of Appropriate Ages](src/main/kotlin/g0801_0900/s0825_friends_of_appropriate_ages/Solution.kt)| Medium | Array, Sorting, Binary_Search, Two_Pointers | 278 | 100.00
1716+
| 0824 |[Goat Latin](src/main/kotlin/g0801_0900/s0824_goat_latin/Solution.kt)| Easy | String | 146 | 100.00
1717+
| 0823 |[Binary Trees With Factors](src/main/kotlin/g0801_0900/s0823_binary_trees_with_factors/Solution.kt)| Medium | Array, Hash_Table, Dynamic_Programming | 298 | 100.00
1718+
| 0822 |[Card Flipping Game](src/main/kotlin/g0801_0900/s0822_card_flipping_game/Solution.kt)| Medium | Array, Hash_Table | 186 | 100.00
17151719
| 0821 |[Shortest Distance to a Character](src/main/kotlin/g0801_0900/s0821_shortest_distance_to_a_character/Solution.kt)| Easy | Array, String, Two_Pointers | 168 | 88.00
17161720
| 0820 |[Short Encoding of Words](src/main/kotlin/g0801_0900/s0820_short_encoding_of_words/Solution.kt)| Medium | Array, String, Hash_Table, Trie | 231 | 100.00
17171721
| 0819 |[Most Common Word](src/main/kotlin/g0801_0900/s0819_most_common_word/Solution.kt)| Easy | String, Hash_Table, Counting | 211 | 83.33
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g0801_0900.s0822_card_flipping_game
2+
3+
// #Medium #Array #Hash_Table #2023_03_25_Time_186_ms_(100.00%)_Space_41.2_MB_(33.33%)
4+
5+
class Solution {
6+
fun flipgame(fronts: IntArray, backs: IntArray): Int {
7+
val max = findMax(fronts, backs)
8+
var value = 10000
9+
val twinCardHash = IntArray(max + 1)
10+
val existingNumbersHash = IntArray(max + 1)
11+
for (i in fronts.indices) {
12+
if (fronts[i] == backs[i]) {
13+
twinCardHash[fronts[i]]++
14+
}
15+
existingNumbersHash[fronts[i]]++
16+
existingNumbersHash[backs[i]]++
17+
}
18+
for (i in 1..max) {
19+
if (twinCardHash[i] == 0 && i < value && existingNumbersHash[i] != 0) {
20+
value = i
21+
break
22+
}
23+
}
24+
return if (value == 10000) {
25+
0
26+
} else {
27+
value
28+
}
29+
}
30+
31+
companion object {
32+
private fun findMax(fronts: IntArray, backs: IntArray): Int {
33+
var max = 0
34+
for (front in fronts) {
35+
if (max < front) {
36+
max = front
37+
}
38+
}
39+
for (back in backs) {
40+
if (max < back) {
41+
max = back
42+
}
43+
}
44+
return max
45+
}
46+
}
47+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
822\. Card Flipping Game
2+
3+
Medium
4+
5+
You are given two **0-indexed** integer arrays `fronts` and `backs` of length `n`, where the <code>i<sup>th</sup></code> card has the positive integer `fronts[i]` printed on the front and `backs[i]` printed on the back. Initially, each card is placed on a table such that the front number is facing up and the other is facing down. You may flip over any number of cards (possibly zero).
6+
7+
After flipping the cards, an integer is considered **good** if it is facing down on some card and **not** facing up on any card.
8+
9+
Return _the minimum possible good integer after flipping the cards_. If there are no good integers, return `0`.
10+
11+
**Example 1:**
12+
13+
**Input:** fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
If we flip the second card, the face up numbers are [1,3,4,4,7] and the face down are [1,2,4,1,3].
20+
21+
2 is the minimum good integer as it appears facing down but not facing up.
22+
23+
It can be shown that 2 is the minimum possible good integer obtainable after flipping some cards.
24+
25+
**Example 2:**
26+
27+
**Input:** fronts = [1], backs = [1]
28+
29+
**Output:** 0
30+
31+
**Explanation:** There are no good integers no matter how we flip the cards, so we return 0.
32+
33+
**Constraints:**
34+
35+
* `n == fronts.length == backs.length`
36+
* `1 <= n <= 1000`
37+
* `1 <= fronts[i], backs[i] <= 2000`
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g0801_0900.s0823_binary_trees_with_factors
2+
3+
// #Medium #Array #Hash_Table #Dynamic_Programming
4+
// #2023_03_25_Time_298_ms_(100.00%)_Space_45.1_MB_(100.00%)dsecx
5+
6+
import java.util.Arrays
7+
8+
class Solution {
9+
private val dp: MutableMap<Int, Long> = HashMap()
10+
private val nums: MutableMap<Int, Int> = HashMap()
11+
fun numFactoredBinaryTrees(arr: IntArray): Int {
12+
Arrays.sort(arr)
13+
for (i in arr.indices) {
14+
nums[arr[i]] = i
15+
}
16+
var ans: Long = 0
17+
for (i in arr.indices.reversed()) {
18+
ans = (ans % MOD + recursion(arr, arr[i], i) % MOD) % MOD
19+
}
20+
return ans.toInt()
21+
}
22+
23+
private fun recursion(arr: IntArray, v: Int, idx: Int): Long {
24+
if (dp.containsKey(v)) {
25+
return dp[v]!!
26+
}
27+
var ret: Long = 1
28+
for (i in 0 until idx) {
29+
val child = arr[i]
30+
if (v % child == 0 && nums.containsKey(v / child)) {
31+
ret += (
32+
(
33+
recursion(arr, child, nums[arr[i]]!!) %
34+
MOD
35+
* recursion(arr, v / child, nums[v / child]!!) %
36+
MOD
37+
) %
38+
MOD
39+
)
40+
}
41+
}
42+
dp[v] = ret
43+
return ret
44+
}
45+
46+
companion object {
47+
private const val MOD = 1e9.toInt() + 7
48+
}
49+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
823\. Binary Trees With Factors
2+
3+
Medium
4+
5+
Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.
6+
7+
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.
8+
9+
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>.
10+
11+
**Example 1:**
12+
13+
**Input:** arr = [2,4]
14+
15+
**Output:** 3
16+
17+
**Explanation:** We can make these trees: `[2], [4], [4, 2, 2]`
18+
19+
**Example 2:**
20+
21+
**Input:** arr = [2,4,5,10]
22+
23+
**Output:** 7
24+
25+
**Explanation:** We can make these trees: `[2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2]`.
26+
27+
**Constraints:**
28+
29+
* `1 <= arr.length <= 1000`
30+
* <code>2 <= arr[i] <= 10<sup>9</sup></code>
31+
* All the values of `arr` are **unique**.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0801_0900.s0824_goat_latin
2+
3+
// #Easy #String #2023_03_25_Time_146_ms_(100.00%)_Space_35.8_MB_(90.00%)
4+
5+
class Solution {
6+
fun toGoatLatin(sentence: String): String {
7+
val splits = sentence.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
8+
val sb = StringBuilder()
9+
val a = StringBuilder()
10+
for (word in splits) {
11+
if (isVowel(word[0])) {
12+
sb.append(word).append("ma")
13+
} else {
14+
val firstChar = word[0]
15+
sb.append(word.substring(1)).append(firstChar).append("ma")
16+
}
17+
a.append("a")
18+
sb.append(a)
19+
sb.append(" ")
20+
}
21+
return sb.toString().trim { it <= ' ' }
22+
}
23+
24+
private fun isVowel(c: Char): Boolean {
25+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
26+
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'
27+
}
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
824\. Goat Latin
2+
3+
Easy
4+
5+
You are given a string `sentence` that consist of words separated by spaces. Each word consists of lowercase and uppercase letters only.
6+
7+
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:
8+
9+
* If a word begins with a vowel (`'a'`, `'e'`, `'i'`, `'o'`, or `'u'`), append `"ma"` to the end of the word.
10+
* For example, the word `"apple"` becomes `"applema"`.
11+
* 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"`.
12+
* For example, the word `"goat"` becomes `"oatgma"`.
13+
* Add one letter `'a'` to the end of each word per its word index in the sentence, starting with `1`.
14+
* For example, the first word gets `"a"` added to the end, the second word gets `"aa"` added to the end, and so on.
15+
16+
Return _the final sentence representing the conversion from sentence to Goat Latin_.
17+
18+
**Example 1:**
19+
20+
**Input:** sentence = "I speak Goat Latin"
21+
22+
**Output:** "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
23+
24+
**Example 2:**
25+
26+
**Input:** sentence = "The quick brown fox jumped over the lazy dog"
27+
28+
**Output:** "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
29+
30+
**Constraints:**
31+
32+
* `1 <= sentence.length <= 150`
33+
* `sentence` consists of English letters and spaces.
34+
* `sentence` has no leading or trailing spaces.
35+
* All the words in `sentence` are separated by a single space.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0801_0900.s0825_friends_of_appropriate_ages
2+
3+
// #Medium #Array #Sorting #Binary_Search #Two_Pointers
4+
// #2023_03_25_Time_278_ms_(100.00%)_Space_39.8_MB_(100.00%)
5+
6+
class Solution {
7+
fun numFriendRequests(ages: IntArray): Int {
8+
val counter = IntArray(121)
9+
for (k in ages) {
10+
counter[k]++
11+
}
12+
var result = 0
13+
for (k in 15 until counter.size) {
14+
if (counter[k] == 0) {
15+
continue
16+
}
17+
result += counter[k] * (counter[k] - 1)
18+
var y = k - 1
19+
while (y > k / 2.0 + 7) {
20+
result += counter[k] * counter[y]
21+
y--
22+
}
23+
}
24+
return result
25+
}
26+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
825\. Friends Of Appropriate Ages
2+
3+
Medium
4+
5+
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.
6+
7+
A Person `x` will not send a friend request to a person `y` (`x != y`) if any of the following conditions is true:
8+
9+
* `age[y] <= 0.5 * age[x] + 7`
10+
* `age[y] > age[x]`
11+
* `age[y] > 100 && age[x] < 100`
12+
13+
Otherwise, `x` will send a friend request to `y`.
14+
15+
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.
16+
17+
Return _the total number of friend requests made_.
18+
19+
**Example 1:**
20+
21+
**Input:** ages = [16,16]
22+
23+
**Output:** 2
24+
25+
**Explanation:** 2 people friend request each other.
26+
27+
**Example 2:**
28+
29+
**Input:** ages = [16,17,18]
30+
31+
**Output:** 2
32+
33+
**Explanation:** Friend requests are made 17 -> 16, 18 -> 17.
34+
35+
**Example 3:**
36+
37+
**Input:** ages = [20,30,100,110,120]
38+
39+
**Output:** 3
40+
41+
**Explanation:** Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.
42+
43+
**Constraints:**
44+
45+
* `n == ages.length`
46+
* <code>1 <= n <= 2 * 10<sup>4</sup></code>
47+
* `1 <= ages[i] <= 120`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0801_0900.s0822_card_flipping_game
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun flipame() {
10+
assertThat(
11+
Solution().flipgame(intArrayOf(1, 2, 4, 4, 7), intArrayOf(1, 3, 4, 1, 3)),
12+
equalTo(2)
13+
)
14+
}
15+
16+
@Test
17+
fun flipame2() {
18+
assertThat(Solution().flipgame(intArrayOf(1), intArrayOf(1)), equalTo(0))
19+
}
20+
}

0 commit comments

Comments
 (0)