LeetCode 1335 solution

LeetCode 1335 solution

1335. Minimum Difficulty of a Job Schedule

#Hard

Problem URL
You want to schedule a list of jobs in d days. Jobs are dependent (i.e To work on the ith job, you have to finish all the jobs j where 0 <= j < i).

You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the d days. The difficulty of a day is the maximum difficulty of a job done on that day.

You are given an integer array jobDifficulty and an integer d. The difficulty of the ith job is jobDifficulty[i].

Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return -1.

Example 1:

  • Input: jobDifficulty = [6,5,4,3,2,1], d = 2
  • Output: 7
  • Explanation: First day you can finish the first 5 jobs, total difficulty = 6.
    Second day you can finish the last job, total difficulty = 1.
    The difficulty of the schedule = 6 + 1 = 7

python

def minDifficulty(jobDifficulty: List[int], d: int) -> int:
    n = len(jobDifficulty)
    if n < d: 
        return -1

    dp = [[float('inf')] * (n + 1) for _ in range(d + 1)]
    dp[0][n] = 0

    for day in range(1, d + 1):
        for i in range(n - day, -1, -1):
            max_diff = 0
            for j in range(i, n - day + 1):
                max_diff = max(max_diff, jobDifficulty[j])
                dp[day][i] = min(dp[day][i], max_diff + dp[day - 1][j + 1])

    return dp[d][0]

This code is designed to solve the "Minimum Difficulty of a Job Schedule" problem, which is a dynamic programming challenge.

Here's a breakdown of the code:

  1. Function Definition:
    • def minDifficulty(jobDifficulty: List[int], d: int) -> int
      • This defines a function named minDifficulty that takes two parameters: jobDifficulty, which is a list of integers representing the difficulty of each job, and d, an integer representing the number of days to complete all jobs. The function returns an integer.
  2. Initial Checks:
    • n = len(jobDifficulty)
      • This line calculates the length of the jobDifficulty list and stores it in n.
    • if n < d: return -1
      • This checks if the number of jobs is less than the number of days. If it is, the function returns -1, indicating that it's impossible to schedule the jobs within the given days.
  3. Dynamic Programming Table Initialization:
    • dp = [[float('inf')] * (n + 1) for _ in range(d + 1)]
      • This line initializes a 2D list (matrix) called dp with dimensions (d + 1) x (n + 1). Each element is set to float('inf'), which represents a very high value (effectively infinity).
    • dp[0][n] = 0
      • This sets the base condition for the DP table. It means that there is no difficulty on the 0th day after completing all jobs.
  4. Dynamic Programming Computation:
    • for day in range(1, d + 1):
      • This loop iterates through each day.
    • for i in range(n - day, -1, -1):
      • This nested loop iterates backwards through the jobs. The range (n - day, -1, -1) ensures that there are enough jobs left to fill the remaining days.
    • Inside the nested loops:
      • max_diff = 0
        • Initializes the maximum difficulty encountered on the current day to 0.
      • for j in range(i, n - day + 1):
        • Another nested loop to consider each job starting from i up to n - day.
      • max_diff = max(max_diff, jobDifficulty[j])
        • Updates max_diff to be the maximum of the current max_diff and the difficulty of the job at index j.
      • dp[day][i] = min(dp[day][i], max_diff + dp[day - 1][j + 1])
        • Updates the DP table. It calculates the minimum difficulty for completing i jobs in day days, considering the current job difficulty and the difficulty calculated for previous days.
  5. Return Statement:
    • return dp[d][0]
      • Finally, the function returns the minimum difficulty for completing all jobs in d days, which is stored in dp[d][0].

In summary, the code uses dynamic programming to calculate the minimum difficulty required to schedule a given number of jobs over a specified number of days, where each job has its own difficulty level. The approach involves filling up a DP table with the minimum difficulties calculated based on the maximum difficulty of jobs done on each day.

Read more

airflow 구성하고 vscode로 코딩하기

맥에서 했으면 훨씬 구성이 쉬웠겠지만, 그리고 poetry로 했으면 훨씬 쉬웠겠지만 워낙 규모가 있는 라이브러리이다 보니 과정이 어려워 다른 참조들을 보면서 따라했다. 기본적으로 poetry랑 쓰기 어려운 이유는 airflow 내부의 라이브러리에 따라 poetry가 버전을 참조하지 못해서 에러가 나는 경우가 존재한다고 한다. 또한 하나의 문제는 mac에서는 그냥 리눅스가 존재하지만 윈도우에서 하려면 윈도우용 linux인

[Json] dump vs dumps

json은 javascript object notation의 줄임말로 웹 어플리케이션에서 구조화된 데이터를 표현하기 위한 string 기반의 포맷이다. 서버에서 클라인트로 데이터를 전송하여 표현하거나, 그 반대로 클라이언트에서 서버로 보내는 경우들에 사용된다. javascript 객체 문법과 굉장히 유사하지만 워낙에 범용성이 넓게 설계되어 있어서 다른 언어들에도 많이 사용된다. 기본적으로 python 에는 json 이 내장 모듈이다. 바로 import json해주면