LeetCode 1029. Two City Scheduling

原题链接在这里:https://leetcode.com/problems/two-city-scheduling/

题目:

There are 2N people a company is planning to interview. The cost of flying the i-th person to city A is costs[i][0], and the cost of flying the i-th person to city B is costs[i][1].

Return the minimum cost to fly every person to a city such that exactly N people arrive in each city.

Example 1:

Input: [[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.

Note:

  1. 1 <= costs.length <= 100
  2. It is guaranteed that costs.length is even.
  3. 1 <= costs[i][0], costs[i][1] <= 1000

题解:

Let dp[i][j] denote for the first i + j people, minimum cost to fly i people to A and j people to B.

Then dp[i][j] = min of dp[i - 1][j] + costs[i + j - 1][0], fly i + j - 1 person to A and previous cost to fly i - 1 people to A and j people to B.

and dp[i][j - 1] + costs[i + j - 1][1], fly i + j - 1 person to B and previous cost to fly i people to A and j - 1 people to B.

Time Complexity: O(n ^ 2).

Space: O(n ^ 2).

AC Java:

 1 class Solution {
 2     public int twoCitySchedCost(int[][] costs) {
 3         int n = costs.length / 2;
 4         int [][] dp = new int[n + 1][n + 1];
 5
 6         for(int i = 1; i <= n; i++){
 7             dp[i][0] = dp[i - 1][0] + costs[i- 1][0];
 8         }
 9
10         for(int j = 1; j <= n; j++){
11             dp[0][j] = dp[0][j - 1] + costs[j - 1][1];
12         }
13
14         for(int i = 1; i <= n; i++){
15             for(int j = 1; j <= n; j++){
16                 dp[i][j] = Math.min(dp[i - 1][j] + costs[i + j - 1][0], dp[i][j - 1] + costs[i + j - 1][1]);
17             }
18         }
19
20         return dp[n][n];
21     }
22 }

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12321484.html

时间: 2024-10-25 05:53:20

LeetCode 1029. Two City Scheduling的相关文章

【leetcode】1029. Two City Scheduling

题目如下: There are 2N people a company is planning to interview. The cost of flying the i-th person to city A is costs[i][0], and the cost of flying the i-th person to city B is costs[i][1]. Return the minimum cost to fly every person to a city such tha

Leetcode 1029. 可被 5 整除的二进制前缀

1029. 可被 5 整除的二进制前缀 显示英文描述 我的提交返回竞赛 用户通过次数467 用户尝试次数662 通过次数477 提交次数1964 题目难度Easy 给定由若干 0 和 1 组成的数组 A.我们定义 N_i:从 A[0] 到 A[i] 的第 i 个子数组被解释为一个二进制数(从最高有效位到最低有效位). 返回布尔值列表 answer,只有当 N_i 可以被 5 整除时,答案 answer[i] 为 true,否则为 false. 示例 1: 输入:[0,1,1] 输出:[true,

[LeetCode] 807. Max Increase to Keep City Skyline

807.Max Increase to Keep City Skyline In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts can be differen

Leetcode题解 - 贪心算法部分简单题目代码+思路(860、944、1005、1029、1046、1217、1221)

leetcode真的是一个学习阅读理解的好地方 860. 柠檬水找零 """ 因为用户支付的只会有5.10.20 对于10元的用户必须找一个5 对于20元的用户可以找(三个5)或者(一个10一个5),每次都从大的开始找起来 """ class Solution: def lemonadeChange(self, bills) -> bool: five = 0 ten = 0 for i in bills: if i == 5: five

(Medium) Max Increase to Keep City SkyLine LeetCode

Description: In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts can be different for different buildings

[LeetCode] Maximum Vacation Days 最大化休假日

LeetCode wants to give one of its best employees the option to travel among N cities to collect algorithm problems. But all work and no play makes Jack a dull boy, you could take vacations in some particular cities and weeks. Your job is to schedule

leetcode 181. Employees Earning More Than Their Managers

题目链接: https://leetcode.com/problems/employees-earning-more-than-their-managers/ The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id. +----+-------+--------+---------

LeetCode:Combine Two Tables - 跨表查询

1.题目名称 Combine Two Tables(跨表查询) 2.题目地址 https://leetcode.com/problems/combine-two-tables/ 3.题目内容 现在有两张表Person和Address,它们的表结构如下: 表Person: +-------------+---------+ | Column Name | Type    | +-------------+---------+ | PersonId    | int     | | FirstNam

【leetcode】Combine Two Tables

突然发现leetcode出sql题了,找了道最简单的,找找自信.. 题目如下: Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ PersonId is the primary key c