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
Next Next commit
Added tasks 811, 812, 813, 814
  • Loading branch information
ThanhNIT committed Feb 1, 2022
commit 602796ce56a00cb7b670b685d7375e23395262df
47 changes: 47 additions & 0 deletions src/main/java/g0801_0900/s0811_subdomain_visit_count/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package g0801_0900.s0811_subdomain_visit_count;

// #Medium #Array #String #Hash_Table #Counting

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Solution {
public List<String> subdomainVisits(String[] d) {
HashMap<String, Integer> fmap = new HashMap<>();
for (String s : d) {
int rep = 0;
int i = 0;
for (i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= '0' && c <= '9') {
rep = rep * 10 + (c - '0');
} else {
break;
}
}
String str = s.substring(i + 1);
Seperate(rep, str, fmap);
}
List<String> res = new ArrayList<>();
for (String key : fmap.keySet()) {
String comp = "";
comp += fmap.get(key);
comp += " ";
comp += key;
res.add(comp);
}
return res;
}

private void Seperate(int rep, String s, HashMap<String, Integer> fmap) {
for (int i = s.length() - 1; i >= 0; i--) {
String to_Hash = "";
while (i >= 0 && s.charAt(i) != '.') {
i--;
}
to_Hash = s.substring(i + 1);
fmap.put(to_Hash, fmap.getOrDefault(to_Hash, 0) + rep);
}
}
}
35 changes: 35 additions & 0 deletions src/main/java/g0801_0900/s0811_subdomain_visit_count/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
811\. Subdomain Visit Count

Medium

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.

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.

* For example, `"9001 discuss.leetcode.com"` is a **count-paired domain** that indicates that `discuss.leetcode.com` was visited `9001` times.

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**.

**Example 1:**

**Input:** cpdomains = ["9001 discuss.leetcode.com"]

**Output:** ["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"]

**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.

**Example 2:**

**Input:** cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]

**Output:** ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]

**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.

**Constraints:**

* `1 <= cpdomain.length <= 100`
* `1 <= cpdomain[i].length <= 100`
* `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.
* <code>rep<sub>i</sub></code> is an integer in the range <code>[1, 10<sup>4</sup>]</code>.
* <code>d1<sub>i</sub></code>, <code>d2<sub>i</sub></code>, and <code>d3<sub>i</sub></code> consist of lowercase English letters.
32 changes: 32 additions & 0 deletions src/main/java/g0801_0900/s0812_largest_triangle_area/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g0801_0900.s0812_largest_triangle_area;

// #Easy #Array #Math #Geometry

public class Solution {
public double largestTriangleArea(int[][] points) {
int n = points.length;
double max = 0;

for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
double area = 0;
int[] a = points[i];
int[] b = points[j];
int[] c = points[k];
area = Math.abs(area(a, b) + area(b, c) + area(c, a));
if (area > max) {
max = area;
}
}
}
}
return max;
}

private double area(int[] a, int[] b) {
int l = b[0] - a[0];
double h = (a[1] + b[1] + 200) / 2.0;
return l * h;
}
}
27 changes: 27 additions & 0 deletions src/main/java/g0801_0900/s0812_largest_triangle_area/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
812\. Largest Triangle Area

Easy

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.

**Example 1:**

![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/04/1027.png)

**Input:** points = [[0,0],[0,1],[1,0],[0,2],[2,0]]

**Output:** 2.00000

**Explanation:** The five points are shown in the above figure. The red triangle is the largest.

**Example 2:**

**Input:** points = [[1,0],[0,0],[0,1]]

**Output:** 0.50000

**Constraints:**

* `3 <= points.length <= 50`
* <code>-50 <= x<sub>i</sub>, y<sub>i</sub> <= 50</code>
* All the given points are **unique**.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g0801_0900.s0813_largest_sum_of_averages;

// #Medium #Array #Dynamic_Programming

public class Solution {
public double largestSumOfAverages(int[] nums, int k) {
return helper(nums, k, 0, new Double[k + 1][nums.length]);
}

private double helper(int[] nums, int k, int idx, Double[][] memo) {
if (memo[k][idx] != null) {
return memo[k][idx];
}
double maxAvg = 0;
double sum = 0;
for (int i = idx; i <= nums.length - k; i++) {
sum += nums[i];
if (k - 1 > 0) {
maxAvg = Math.max(maxAvg, (sum / (i - idx + 1)) + helper(nums, k - 1, i + 1, memo));
} else if (i == nums.length - 1) {
maxAvg = Math.max(maxAvg, sum / (i - idx + 1));
}
}
return memo[k][idx] = maxAvg;
}
}
35 changes: 35 additions & 0 deletions src/main/java/g0801_0900/s0813_largest_sum_of_averages/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
813\. Largest Sum of Averages

Medium

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.

Note that the partition must use every integer in `nums`, and that the score is not necessarily an integer.

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.

**Example 1:**

**Input:** nums = [9,1,2,3,9], k = 3

**Output:** 20.00000

**Explanation:**

The best choice is to partition nums into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.

We could have also partitioned nums into [9, 1], [2], [3, 9], for example.

That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.

**Example 2:**

**Input:** nums = [1,2,3,4,5,6,7], k = 4

**Output:** 20.50000

**Constraints:**

* `1 <= nums.length <= 100`
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
* `1 <= k <= nums.length`
19 changes: 19 additions & 0 deletions src/main/java/g0801_0900/s0814_binary_tree_pruning/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g0801_0900.s0814_binary_tree_pruning;

// #Medium #Depth_First_Search #Tree #Binary_Tree

import com_github_leetcode.TreeNode;

public class Solution {
public TreeNode pruneTree(TreeNode root) {
if (root == null) {
return root;
}
root.left = pruneTree(root.left);
root.right = pruneTree(root.right);
if (root.left == null && root.right == null && root.val == 0) {
return null;
}
return root;
}
}
38 changes: 38 additions & 0 deletions src/main/java/g0801_0900/s0814_binary_tree_pruning/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
814\. Binary Tree Pruning

Medium

Given the `root` of a binary tree, return _the same tree where every subtree (of the given tree) not containing a_ `1` _has been removed_.

A subtree of a node `node` is `node` plus every node that is a descendant of `node`.

**Example 1:**

![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_2.png)

**Input:** root = [1,null,0,0,1]

**Output:** [1,null,0,null,1]

**Explanation:** Only the red nodes satisfy the property "every subtree not containing a 1". The diagram on the right represents the answer.

**Example 2:**

![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_1.png)

**Input:** root = [1,0,1,0,0,0,1]

**Output:** [1,null,1,null,1]

**Example 3:**

![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/05/1028.png)

**Input:** root = [1,1,0,1,1,0,1,0]

**Output:** [1,1,0,1,1,null,1]

**Constraints:**

* The number of nodes in the tree is in the range `[1, 200]`.
* `Node.val` is either `0` or `1`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package g0801_0900.s0811_subdomain_visit_count;

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

import java.util.Arrays;
import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void subdomainVisits() {
assertThat(
new Solution().subdomainVisits(new String[] {"9001 discuss.leetcode.com"}),
equalTo(
Arrays.asList(
"9001 com", "9001 leetcode.com", "9001 discuss.leetcode.com")));
}

@Test
void subdomainVisits2() {
assertThat(
new Solution()
.subdomainVisits(
new String[] {
"900 google.mail.com",
"50 yahoo.com",
"1 intel.mail.com",
"5 wiki.org"
}),
equalTo(
Arrays.asList(
"951 com",
"900 google.mail.com",
"1 intel.mail.com",
"5 org",
"5 wiki.org",
"901 mail.com",
"50 yahoo.com")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g0801_0900.s0812_largest_triangle_area;

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

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void largestTriangleArea() {
assertThat(
new Solution()
.largestTriangleArea(new int[][] {{0, 0}, {0, 1}, {1, 0}, {0, 2}, {2, 0}}),
equalTo(2.0));
}

@Test
void largestTriangleArea2() {
assertThat(
new Solution().largestTriangleArea(new int[][] {{1, 0}, {0, 0}, {0, 1}}),
equalTo(0.5));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g0801_0900.s0813_largest_sum_of_averages;

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

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void largestSumOfAverages() {
assertThat(
new Solution().largestSumOfAverages(new int[] {9, 1, 2, 3, 9}, 3), equalTo(20.0));
}

@Test
void largestSumOfAverages2() {
assertThat(
new Solution().largestSumOfAverages(new int[] {1, 2, 3, 4, 5, 6, 7}, 4),
equalTo(20.5));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g0801_0900.s0814_binary_tree_pruning;

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

import com_github_leetcode.TreeNode;
import java.util.Arrays;
import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void pruneTree() {
TreeNode treeNode = TreeNode.create(Arrays.asList(1, null, 0, 0, 1));
TreeNode expected = TreeNode.create(Arrays.asList(1, null, 0, null, 1));
assertThat(new Solution().pruneTree(treeNode).toString(), equalTo(expected.toString()));
}

@Test
void pruneTree2() {
TreeNode treeNode = TreeNode.create(Arrays.asList(1, 0, 1, 0, 0, 0, 1));
TreeNode expected = TreeNode.create(Arrays.asList(1, null, 1, null, 1));
assertThat(new Solution().pruneTree(treeNode).toString(), equalTo(expected.toString()));
}

@Test
void pruneTree3() {
TreeNode treeNode = TreeNode.create(Arrays.asList(1, 1, 0, 1, 1, 0, 1, 0));
TreeNode expected = TreeNode.create(Arrays.asList(1, 1, 0, 1, 1, null, 1));
assertThat(new Solution().pruneTree(treeNode).toString(), equalTo(expected.toString()));
}
}