Skip to content

Commit 7327bbd

Browse files
authored
Added tasks 810, 811, 812, 813
1 parent 7019087 commit 7327bbd

File tree

13 files changed

+392
-0
lines changed

13 files changed

+392
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
17111711
| 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
17121712
| 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
17131713
| 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
1714+
| 0813 |[Largest Sum of Averages](src/main/kotlin/g0801_0900/s0813_largest_sum_of_averages/Solution.kt)| Medium | Array, Dynamic_Programming | 160 | 100.00
1715+
| 0812 |[Largest Triangle Area](src/main/kotlin/g0801_0900/s0812_largest_triangle_area/Solution.kt)| Easy | Array, Math, Geometry | 156 | 71.43
1716+
| 0811 |[Subdomain Visit Count](src/main/kotlin/g0801_0900/s0811_subdomain_visit_count/Solution.kt)| Medium | Array, String, Hash_Table, Counting | 220 | 100.00
1717+
| 0810 |[Chalkboard XOR Game](src/main/kotlin/g0801_0900/s0810_chalkboard_xor_game/Solution.kt)| Hard | Array, Math, Bit_Manipulation, Game_Theory, Brainteaser | 172 | 100.00
17141718
| 0809 |[Expressive Words](src/main/kotlin/g0801_0900/s0809_expressive_words/Solution.kt)| Medium | Array, String, Two_Pointers | 158 | 100.00
17151719
| 0808 |[Soup Servings](src/main/kotlin/g0801_0900/s0808_soup_servings/Solution.kt)| Medium | Dynamic_Programming, Math, Probability_and_Statistics | 112 | 100.00
17161720
| 0807 |[Max Increase to Keep City Skyline](src/main/kotlin/g0801_0900/s0807_max_increase_to_keep_city_skyline/Solution.kt)| Medium | Array, Greedy, Matrix | 158 | 100.00
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package g0801_0900.s0810_chalkboard_xor_game
2+
3+
// #Hard #Array #Math #Bit_Manipulation #Game_Theory #Brainteaser
4+
// #2023_03_17_Time_172_ms_(100.00%)_Space_37_MB_(33.33%)
5+
6+
class Solution {
7+
fun xorGame(nums: IntArray): Boolean {
8+
var xor = 0
9+
for (i in nums) {
10+
xor = xor xor i
11+
}
12+
return xor == 0 || nums.size and 1 == 0
13+
}
14+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
810\. Chalkboard XOR Game
2+
3+
Hard
4+
5+
You are given an array of integers `nums` represents the numbers written on a chalkboard.
6+
7+
Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become `0`, then that player loses. The bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is `0`.
8+
9+
Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to `0`, then that player wins.
10+
11+
Return `true` _if and only if Alice wins the game, assuming both players play optimally_.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,1,2]
16+
17+
**Output:** false
18+
19+
**Explanation:**
20+
21+
Alice has two choices: erase 1 or erase 2.
22+
23+
If she erases 1, the nums array becomes [1, 2].
24+
25+
The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3.
26+
27+
Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose.
28+
29+
If Alice erases 2 first, now nums become [1, 1].
30+
31+
The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0.
32+
33+
Alice will lose.
34+
35+
**Example 2:**
36+
37+
**Input:** nums = [0,1]
38+
39+
**Output:** true
40+
41+
**Example 3:**
42+
43+
**Input:** nums = [1,2,3]
44+
45+
**Output:** true
46+
47+
**Constraints:**
48+
49+
* `1 <= nums.length <= 1000`
50+
* <code>0 <= nums[i] < 2<sup>16</sup></code>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0801_0900.s0811_subdomain_visit_count
2+
3+
// #Medium #Array #String #Hash_Table #Counting
4+
// #2023_03_17_Time_220_ms_(100.00%)_Space_36.6_MB_(100.00%)
5+
6+
class Solution {
7+
fun subdomainVisits(d: Array<String>): List<String> {
8+
val fmap: MutableMap<String, Int> = HashMap()
9+
for (s in d) {
10+
var rep = 0
11+
var i: Int = 0
12+
while (i < s.length) {
13+
val c = s[i]
14+
rep = if (c in '0'..'9') {
15+
rep * 10 + (c.code - '0'.code)
16+
} else {
17+
break
18+
}
19+
i++
20+
}
21+
val str = s.substring(i + 1)
22+
seperate(rep, str, fmap)
23+
}
24+
val res: MutableList<String> = ArrayList()
25+
for (entry in fmap.entries.iterator()) {
26+
var comp = ""
27+
comp += entry.value
28+
comp += " "
29+
comp += entry.key
30+
res.add(comp)
31+
}
32+
return res
33+
}
34+
35+
private fun seperate(rep: Int, s: String, fmap: MutableMap<String, Int>) {
36+
var i = s.length - 1
37+
while (i >= 0) {
38+
while (i >= 0 && s[i] != '.') {
39+
i--
40+
}
41+
val toHash: String = s.substring(i + 1)
42+
fmap[toHash] = fmap.getOrDefault(toHash, 0) + rep
43+
i--
44+
}
45+
}
46+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
811\. Subdomain Visit Count
2+
3+
Medium
4+
5+
A website domain `"discuss.leetcode.com"` consists of various subdomains. At the top level, we have `"com"`, at the next level, we have `"leetcode.com"` and at the lowest level, `"discuss.leetcode.com"`. When we visit a domain like `"discuss.leetcode.com"`, we will also visit the parent domains `"leetcode.com"` and `"com"` implicitly.
6+
7+
A **count-paired domain** is a domain that has one of the two formats `"rep d1.d2.d3"` or `"rep d1.d2"` where `rep` is the number of visits to the domain and `d1.d2.d3` is the domain itself.
8+
9+
* For example, `"9001 discuss.leetcode.com"` is a **count-paired domain** that indicates that `discuss.leetcode.com` was visited `9001` times.
10+
11+
Given an array of **count-paired domains** `cpdomains`, return _an array of the **count-paired domains** of each subdomain in the input_. You may return the answer in **any order**.
12+
13+
**Example 1:**
14+
15+
**Input:** cpdomains = ["9001 discuss.leetcode.com"]
16+
17+
**Output:** ["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"]
18+
19+
**Explanation:** We only have one website domain: "discuss.leetcode.com". As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times.
20+
21+
**Example 2:**
22+
23+
**Input:** cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
24+
25+
**Output:** ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
26+
27+
**Explanation:** We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times. For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.
28+
29+
**Constraints:**
30+
31+
* `1 <= cpdomain.length <= 100`
32+
* `1 <= cpdomain[i].length <= 100`
33+
* `cpdomain[i]` follows either the <code>"rep<sub>i</sub> d1<sub>i</sub>.d2<sub>i</sub>.d3<sub>i</sub>"</code> format or the <code>"rep<sub>i</sub> d1<sub>i</sub>.d2<sub>i</sub>"</code> format.
34+
* <code>rep<sub>i</sub></code> is an integer in the range <code>[1, 10<sup>4</sup>]</code>.
35+
* <code>d1<sub>i</sub></code>, <code>d2<sub>i</sub></code>, and <code>d3<sub>i</sub></code> consist of lowercase English letters.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0801_0900.s0812_largest_triangle_area
2+
3+
// #Easy #Array #Math #Geometry #2023_03_17_Time_156_ms_(71.43%)_Space_34.3_MB_(57.14%)
4+
5+
import kotlin.math.abs
6+
7+
class Solution {
8+
fun largestTriangleArea(points: Array<IntArray>): Double {
9+
val n = points.size
10+
var max = 0.0
11+
for (i in 0 until n) {
12+
for (j in i + 1 until n) {
13+
for (k in j + 1 until n) {
14+
var area: Double
15+
val a = points[i]
16+
val b = points[j]
17+
val c = points[k]
18+
area = abs(area(a, b) + area(b, c) + area(c, a))
19+
if (area > max) {
20+
max = area
21+
}
22+
}
23+
}
24+
}
25+
return max
26+
}
27+
28+
private fun area(a: IntArray, b: IntArray): Double {
29+
val l = b[0] - a[0]
30+
val h = (a[1] + b[1] + 200) / 2.0
31+
return l * h
32+
}
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
812\. Largest Triangle Area
2+
3+
Easy
4+
5+
Given an array of points on the **X-Y** plane `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>, return _the area of the largest triangle that can be formed by any three different points_. Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.
6+
7+
**Example 1:**
8+
9+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/04/1027.png)
10+
11+
**Input:** points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
12+
13+
**Output:** 2.00000
14+
15+
**Explanation:** The five points are shown in the above figure. The red triangle is the largest.
16+
17+
**Example 2:**
18+
19+
**Input:** points = [[1,0],[0,0],[0,1]]
20+
21+
**Output:** 0.50000
22+
23+
**Constraints:**
24+
25+
* `3 <= points.length <= 50`
26+
* <code>-50 <= x<sub>i</sub>, y<sub>i</sub> <= 50</code>
27+
* All the given points are **unique**.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0801_0900.s0813_largest_sum_of_averages
2+
3+
// #Medium #Array #Dynamic_Programming #2023_03_17_Time_160_ms_(100.00%)_Space_35.3_MB_(100.00%)
4+
5+
class Solution {
6+
fun largestSumOfAverages(nums: IntArray, k: Int): Double {
7+
return helper(nums, k, 0, Array(k + 1) { arrayOfNulls(nums.size) })
8+
}
9+
10+
private fun helper(nums: IntArray, k: Int, idx: Int, memo: Array<Array<Double?>>): Double {
11+
if (memo[k][idx] != null) {
12+
return memo[k][idx]!!
13+
}
14+
var maxAvg = 0.0
15+
var sum = 0.0
16+
for (i in idx..nums.size - k) {
17+
sum += nums[i].toDouble()
18+
if (k - 1 > 0) {
19+
maxAvg = maxAvg.coerceAtLeast(sum / (i - idx + 1) + helper(nums, k - 1, i + 1, memo))
20+
} else if (i == nums.size - 1) {
21+
maxAvg = maxAvg.coerceAtLeast(sum / (i - idx + 1))
22+
}
23+
}
24+
memo[k][idx] = maxAvg
25+
return maxAvg
26+
}
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
813\. Largest Sum of Averages
2+
3+
Medium
4+
5+
You are given an integer array `nums` and an integer `k`. You can partition the array into **at most** `k` non-empty adjacent subarrays. The **score** of a partition is the sum of the averages of each subarray.
6+
7+
Note that the partition must use every integer in `nums`, and that the score is not necessarily an integer.
8+
9+
Return _the maximum **score** you can achieve of all the possible partitions_. Answers within <code>10<sup>-6</sup></code> of the actual answer will be accepted.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [9,1,2,3,9], k = 3
14+
15+
**Output:** 20.00000
16+
17+
**Explanation:**
18+
19+
The best choice is to partition nums into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
20+
21+
We could have also partitioned nums into [9, 1], [2], [3, 9], for example.
22+
23+
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [1,2,3,4,5,6,7], k = 4
28+
29+
**Output:** 20.50000
30+
31+
**Constraints:**
32+
33+
* `1 <= nums.length <= 100`
34+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
35+
* `1 <= k <= nums.length`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0801_0900.s0810_chalkboard_xor_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 xorGame() {
10+
assertThat(Solution().xorGame(intArrayOf(1, 1, 2)), equalTo(false))
11+
}
12+
13+
@Test
14+
fun xorGame2() {
15+
assertThat(Solution().xorGame(intArrayOf(0, 1)), equalTo(true))
16+
}
17+
18+
@Test
19+
fun xorGame3() {
20+
assertThat(Solution().xorGame(intArrayOf(1, 2, 3)), equalTo(true))
21+
}
22+
}

0 commit comments

Comments
 (0)