Skip to content

Commit be2ba51

Browse files
add 1335
1 parent 2dec60b commit be2ba51

File tree

7 files changed

+131
-5
lines changed

7 files changed

+131
-5
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -627,4 +627,5 @@ LeetCode
627627
|1331|[Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/)|c|[c++](./src/1331-Rank-Transform-of-an-Array/1331.cpp)|[python](./src/1331-Rank-Transform-of-an-Array/1331.py)|[go](./src/1331-Rank-Transform-of-an-Array/1331.go)|[js](./src/1331-Rank-Transform-of-an-Array/1331.js)|[java](./src/1331-Rank-Transform-of-an-Array/1331.java)|Easy|
628628
|1332|[Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/)|c|[c++](./src/1332-Remove-Palindromic-Subsequences/1332.cpp)|[python](./src/1332-Remove-Palindromic-Subsequences/1332.py)|[go](./src/1332-Remove-Palindromic-Subsequences/1332.go)|[js](./src/1332-Remove-Palindromic-Subsequences/1332.js)|[java](./src/1332-Remove-Palindromic-Subsequences/1332.java)|Easy|
629629
|1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/)|c|[c++](./src/1333-Filter-Restaurants-by-Vegan-Friendly,-Price-and-Distance/1333.cpp)|[python](./src/1333-Filter-Restaurants-by-Vegan-Friendly,-Price-and-Distance/1333.py)|[go](./src/1333-Filter-Restaurants-by-Vegan-Friendly,-Price-and-Distance/1333.go)|[js](./src/1333-Filter-Restaurants-by-Vegan-Friendly,-Price-and-Distance/1333.js)|[java](./src/1333-Filter-Restaurants-by-Vegan-Friendly,-Price-and-Distance/1333.java)|Medium|
630-
|1334|[Find the City With the Smallest Number of Neighbors at a Threshold Distance](https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/)|c|[c++](./src/1334-Find-the-City-With-the-Smallest-Number-of-Neighbors-at-a-Threshold-Distance/1334.cpp)|[python](./src/1334-Find-the-City-With-the-Smallest-Number-of-Neighbors-at-a-Threshold-Distance/1334.py)|[go](./src/1334-Find-the-City-With-the-Smallest-Number-of-Neighbors-at-a-Threshold-Distance/1334.go)|[js](./src/1334-Find-the-City-With-the-Smallest-Number-of-Neighbors-at-a-Threshold-Distance/1334.js)|[java](./src/1334-Find-the-City-With-the-Smallest-Number-of-Neighbors-at-a-Threshold-Distance/1334.java)|Medium|
630+
|1334|[Find the City With the Smallest Number of Neighbors at a Threshold Distance](https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/)|c|[c++](./src/1334-Find-the-City-With-the-Smallest-Number-of-Neighbors-at-a-Threshold-Distance/1334.cpp)|[python](./src/1334-Find-the-City-With-the-Smallest-Number-of-Neighbors-at-a-Threshold-Distance/1334.py)|[go](./src/1334-Find-the-City-With-the-Smallest-Number-of-Neighbors-at-a-Threshold-Distance/1334.go)|[js](./src/1334-Find-the-City-With-the-Smallest-Number-of-Neighbors-at-a-Threshold-Distance/1334.js)|[java](./src/1334-Find-the-City-With-the-Smallest-Number-of-Neighbors-at-a-Threshold-Distance/1334.java)|Medium|
631+
|1335|[Minimum Difficulty of a Job Schedule](https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/)|c|[c++](./src/1335-Minimum-Difficulty-of-a-Job-Schedule/1335.cpp)|[python](./src/1335-Minimum-Difficulty-of-a-Job-Schedule/1335.py)|[go](./src/1335-Minimum-Difficulty-of-a-Job-Schedule/1335.go)|[js](./src/1335-Minimum-Difficulty-of-a-Job-Schedule/1335.js)|[java](./src/1335-Minimum-Difficulty-of-a-Job-Schedule/1335.java)|Hard|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution
2+
{
3+
public:
4+
int minDifficulty(vector<int>& jobDifficulty, int d)
5+
{
6+
int n = jobDifficulty.size();
7+
if (n < d) return -1;
8+
9+
int dp[n + 1] = {0};
10+
for (int i = n - 1; i >= 0; i--) dp[i] = max(dp[i + 1], jobDifficulty[i]);
11+
12+
for (int t = 1; t <= d; t++)
13+
{
14+
for (int i = 0; i <= n - t; i++)
15+
{
16+
int m = 0; dp[i] = 10010;
17+
for (int j = i; j <= n - t; j++)
18+
{
19+
m = max(m, jobDifficulty[j]);
20+
dp[i] = min(dp[i], m + dp[j + 1]);
21+
}
22+
}
23+
}
24+
return dp[0];
25+
}
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
func minDifficulty(jobDifficulty []int, d int) int {
2+
n := len(jobDifficulty)
3+
if n < d {
4+
return -1
5+
}
6+
7+
dp := make([]int, n + 1)
8+
for i := n - 1; i >= 0; i-- {
9+
dp[i] = max(dp[i + 1], jobDifficulty[i])
10+
}
11+
12+
for t := 1; t <= d; t++ {
13+
for i := 0; i <= n - t; i++ {
14+
m := 0
15+
dp[i] = 10010
16+
for j := i; j <= n - t; j++ {
17+
m = max(m, jobDifficulty[j])
18+
dp[i] = min(dp[i], m + dp[j + 1])
19+
}
20+
}
21+
}
22+
return dp[0]
23+
}
24+
25+
func max(a, b int) int {
26+
if a > b {
27+
return a
28+
}
29+
return b
30+
}
31+
32+
func min(a, b int) int {
33+
if a < b {
34+
return a
35+
}
36+
return b
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int minDifficulty(int[] jobDifficulty, int d) {
3+
int n = jobDifficulty.length;
4+
if (n < d) return -1;
5+
6+
int[] dp = new int[n + 1];
7+
for (int i = n - 1; i >= 0; i--) {
8+
dp[i] = Math.max(dp[i + 1], jobDifficulty[i]);
9+
}
10+
11+
for (int t = 1; t <= d; t++) {
12+
for (int i = 0; i <= n - t; i++) {
13+
int m = 0; dp[i] = 10010;
14+
for (int j = i; j <= n - t; j++) {
15+
m = Math.max(m, jobDifficulty[j]);
16+
dp[i] = Math.min(dp[i], m + dp[j + 1]);
17+
}
18+
}
19+
}
20+
return dp[0];
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var minDifficulty = function(jobDifficulty, d) {
2+
let n = jobDifficulty.length;
3+
if (n < d) return -1;
4+
5+
let dp = new Array(n + 1);
6+
dp.fill(0);
7+
for (let i = n - 1; i >= 0; i--) {
8+
dp[i] = Math.max(dp[i + 1], jobDifficulty[i]);
9+
}
10+
11+
for (let t = 1; t <= d; t++) {
12+
for (let i = 0; i <= n - t; i++) {
13+
let m = 0; dp[i] = 10010;
14+
for (let j = i; j <= n - t; j++) {
15+
m = Math.max(m, jobDifficulty[j]);
16+
dp[i] = Math.min(dp[i], m + dp[j + 1]);
17+
}
18+
}
19+
}
20+
return dp[0];
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from functools import lru_cache
2+
class Solution:
3+
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
4+
n = len(jobDifficulty)
5+
if n < d:
6+
return -1
7+
8+
@lru_cache(None)
9+
def dfs(u, d):
10+
if d == 1:
11+
return max(jobDifficulty[u:])
12+
13+
res, m = float("inf"), 0
14+
for i in range(u, n - d + 1):
15+
m = max(m, jobDifficulty[i])
16+
res = min(res, dfs(i + 1, d - 1) + m)
17+
return res
18+
19+
return dfs(0, d)

src/addProb.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import os, bisect
33

44
# 题目名称
5-
name = "Find the City With the Smallest Number of Neighbors at a Threshold Distance"
6-
ID = 1334
7-
url = "https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/"
8-
difficult = "Medium"
5+
name = "Minimum Difficulty of a Job Schedule"
6+
ID = 1335
7+
url = "https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/"
8+
difficult = "Hard"
99
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']
1010

1111

0 commit comments

Comments
 (0)