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 1027, 1028, 1029, 1030
  • Loading branch information
ThanhNIT committed Feb 26, 2022
commit 4eb852e7054ef785f5f37de2b4d75fb284df9e1f
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package g1001_1100.s1027_longest_arithmetic_subsequence;

// #Medium #Array #Hash_Table #Dynamic_Programming #Binary_Search
// #2022_02_26_Time_47_ms_(98.28%)_Space_50.8_MB_(93.45%)

import java.util.Arrays;

public class Solution {
public int longestArithSeqLength(int[] nums) {
int max = maxElement(nums);
int min = minElement(nums);
int diff = max - min;
int n = nums.length;
int[][] dp = new int[n][2 * diff + 2];
for (int[] d : dp) {
Arrays.fill(d, 1);
}

int ans = 0;
for (int i = 0; i < n; i++)
for (int j = i - 1; j >= 0; j--) {
int difference = nums[i] - nums[j] + diff;
int temp = dp[j][difference];
dp[i][difference] = Math.max(dp[i][difference], temp + 1);
if (ans < dp[i][difference]) {
ans = dp[i][difference];
}
}

return ans;
}

public int maxElement(int[] arr) {
int max = Integer.MIN_VALUE;
for (Integer e : arr) {
if (max < e) {
max = e;
}
}

return max;
}

public int minElement(int[] arr) {
int min = Integer.MAX_VALUE;
for (Integer e : arr) {
if (min > e) {
min = e;
}
}

return min;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
1027\. Longest Arithmetic Subsequence

Medium

Given an array `nums` of integers, return the **length** of the longest arithmetic subsequence in `nums`.

Recall that a _subsequence_ of an array `nums` is a list <code>nums[i<sub>1</sub>], nums[i<sub>2</sub>], ..., nums[i<sub>k</sub>]</code> with <code>0 <= i<sub>1</sub> < i<sub>2</sub> < ... < i<sub>k</sub> <= nums.length - 1</code>, and that a sequence `seq` is _arithmetic_ if `seq[i+1] - seq[i]` are all the same value (for `0 <= i < seq.length - 1`).

**Example 1:**

**Input:** nums = [3,6,9,12]

**Output:** 4

**Explanation:** The whole array is an arithmetic sequence with steps of length = 3.

**Example 2:**

**Input:** nums = [9,4,7,2,10]

**Output:** 3

**Explanation:** The longest arithmetic subsequence is [4,7,10].

**Example 3:**

**Input:** nums = [20,1,15,3,10,5,8]

**Output:** 4

**Explanation:** The longest arithmetic subsequence is [20,15,10,5].

**Constraints:**

* `2 <= nums.length <= 1000`
* `0 <= nums[i] <= 500`
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package g1001_1100.s1028_recover_a_tree_from_preorder_traversal;

// #Hard #String #Depth_First_Search #Tree #Binary_Tree
// #2022_02_26_Time_5_ms_(77.57%)_Space_46.5_MB_(30.81%)

import com_github_leetcode.TreeNode;

public class Solution {
private int ptr = 0;

public TreeNode recoverFromPreorder(String traversal) {
return find(traversal, 0);
}

private TreeNode find(String traversal, int level) {
if (ptr == traversal.length()) {
return null;
}
int i = ptr;
int count = 0;
while (traversal.charAt(i) == '-') {
count++;
i++;
}
if (count == level) {
int start = i;
while (i < traversal.length() && traversal.charAt(i) != '-') {
i++;
}
int val = Integer.parseInt(traversal.substring(start, i));
ptr = i;
TreeNode root = new TreeNode(val);
root.left = find(traversal, level + 1);
root.right = find(traversal, level + 1);
return root;
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
1028\. Recover a Tree From Preorder Traversal

Hard

We run a preorder depth-first search (DFS) on the `root` of a binary tree.

At each node in this traversal, we output `D` dashes (where `D` is the depth of this node), then we output the value of this node. If the depth of a node is `D`, the depth of its immediate child is `D + 1`. The depth of the `root` node is `0`.

If a node has only one child, that child is guaranteed to be **the left child**.

Given the output `traversal` of this traversal, recover the tree and return _its_ `root`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2019/04/08/recover-a-tree-from-preorder-traversal.png)

**Input:** traversal = "1-2--3--4-5--6--7"

**Output:** [1,2,5,3,4,6,7]

**Example 2:**

![](https://assets.leetcode.com/uploads/2019/04/11/screen-shot-2019-04-10-at-114101-pm.png)

**Input:** traversal = "1-2--3---4-5--6---7"

**Output:** [1,2,5,3,null,6,null,4,null,7]

**Example 3:**

![](https://assets.leetcode.com/uploads/2019/04/11/screen-shot-2019-04-10-at-114955-pm.png)

**Input:** traversal = "1-401--349---90--88"

**Output:** [1,401,null,349,88,90]

**Constraints:**

* The number of nodes in the original tree is in the range `[1, 1000]`.
* <code>1 <= Node.val <= 10<sup>9</sup></code>
20 changes: 20 additions & 0 deletions src/main/java/g1001_1100/s1029_two_city_scheduling/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g1001_1100.s1029_two_city_scheduling;

// #Medium #Array #Sorting #Greedy

import java.util.Arrays;

public class Solution {
public int twoCitySchedCost(int[][] costs) {
Arrays.sort(costs, (a, b) -> (a[0] - a[1] - (b[0] - b[1])));
int cost = 0;
for (int i = 0; i < costs.length; i++) {
if (i < costs.length / 2) {
cost += costs[i][0];
} else {
cost += costs[i][1];
}
}
return cost;
}
}
44 changes: 44 additions & 0 deletions src/main/java/g1001_1100/s1029_two_city_scheduling/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
1029\. Two City Scheduling

Medium

A company is planning to interview `2n` people. Given the array `costs` where <code>costs[i] = [aCost<sub>i</sub>, bCost<sub>i</sub>]</code>, the cost of flying the <code>i<sup>th</sup></code> person to city `a` is <code>aCost<sub>i</sub></code>, and the cost of flying the <code>i<sup>th</sup></code> person to city `b` is <code>bCost<sub>i</sub></code>.

Return _the minimum cost to fly every person to a city_ such that exactly `n` people arrive in each city.

**Example 1:**

**Input:** costs = [[10,20],[30,200],[400,50],[30,20]]

**Output:** 110

**Explanation:**

The first person goes to city A for a cost of 10.

The second person goes to city A for a cost of 30.

The third person goes to city B for a cost of 50.

The fourth person goes to city B for a cost of 20.

The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.

**Example 2:**

**Input:** costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]

**Output:** 1859

**Example 3:**

**Input:** costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]

**Output:** 3086

**Constraints:**

* `2 * n == costs.length`
* `2 <= costs.length <= 100`
* `costs.length` is even.
* <code>1 <= aCost<sub>i</sub>, bCost<sub>i</sub> <= 1000</code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g1001_1100.s1030_matrix_cells_in_distance_order;

// #Easy #Array #Math #Sorting #Matrix #Geometry
// #2022_02_27_Time_15_ms_(69.74%)_Space_72.2_MB_(52.05%)

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Solution {
public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
Map<Integer, List<int[]>> map = new TreeMap<>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
map.computeIfAbsent(
Math.abs(i - rCenter) + Math.abs(j - cCenter),
k -> new ArrayList<>())
.add(new int[] {i, j});
}
}

int[][] res = new int[rows * cols][];
int i = 0;
for (List<int[]> list : map.values()) {
for (int[] each : list) {
res[i++] = each;
}
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
1029\. Two City Scheduling

Medium

A company is planning to interview `2n` people. Given the array `costs` where <code>costs[i] = [aCost<sub>i</sub>, bCost<sub>i</sub>]</code>, the cost of flying the <code>i<sup>th</sup></code> person to city `a` is <code>aCost<sub>i</sub></code>, and the cost of flying the <code>i<sup>th</sup></code> person to city `b` is <code>bCost<sub>i</sub></code>.

Return _the minimum cost to fly every person to a city_ such that exactly `n` people arrive in each city.

**Example 1:**

**Input:** costs = [[10,20],[30,200],[400,50],[30,20]]

**Output:** 110

**Explanation:**

The first person goes to city A for a cost of 10.

The second person goes to city A for a cost of 30.

The third person goes to city B for a cost of 50.

The fourth person goes to city B for a cost of 20.

The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.

**Example 2:**

**Input:** costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]

**Output:** 1859

**Example 3:**

**Input:** costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]

**Output:** 3086

**Constraints:**

* `2 * n == costs.length`
* `2 <= costs.length <= 100`
* `costs.length` is even.
* <code>1 <= aCost<sub>i</sub>, bCost<sub>i</sub> <= 1000</code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package g1001_1100.s1027_longest_arithmetic_subsequence;

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

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void longestArithSeqLength() {
assertThat(new Solution().longestArithSeqLength(new int[] {3, 6, 9, 12}), equalTo(4));
}

@Test
void longestArithSeqLength2() {
assertThat(new Solution().longestArithSeqLength(new int[] {9, 4, 7, 2, 10}), equalTo(3));
}

@Test
void longestArithSeqLength3() {
assertThat(
new Solution().longestArithSeqLength(new int[] {20, 1, 15, 3, 10, 5, 8}),
equalTo(4));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package g1001_1100.s1028_recover_a_tree_from_preorder_traversal;

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 recoverFromPreorder() {
TreeNode expected = TreeNode.create(Arrays.asList(1, 2, 5, 3, 4, 6, 7));
assertThat(
new Solution().recoverFromPreorder("1-2--3--4-5--6--7").toString(),
equalTo(expected.toString()));
}

@Test
void recoverFromPreorder2() {
TreeNode expected = TreeNode.create(Arrays.asList(1, 2, 5, 3, null, 6, null, 4, null, 7));
assertThat(
new Solution().recoverFromPreorder("1-2--3---4-5--6---7").toString(),
equalTo(expected.toString()));
}

@Test
void recoverFromPreorder3() {
TreeNode expected = TreeNode.create(Arrays.asList(1, 401, null, 349, 88, 90));
assertThat(
new Solution().recoverFromPreorder("1-401--349---90--88").toString(),
equalTo(expected.toString()));
}
}
Loading