2024/10/01 今日与昨日每日一题
This commit is contained in:
parent
07fc2578e1
commit
ea77084c07
|
@ -0,0 +1,39 @@
|
|||
package com.cool.one_question_per_day;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @Author: Cool
|
||||
* @Date: 2024/10/01/13:30
|
||||
* DayNumber 1
|
||||
* Hard 2
|
||||
* Level 7
|
||||
* Score 1786
|
||||
* @Description 983. 最低票价
|
||||
*/
|
||||
public class LeetCode20241001 {
|
||||
public int mincostTickets(int[] days, int[] costs) {
|
||||
int lastDay = days[days.length - 1];
|
||||
boolean[] isTravel = new boolean[lastDay + 1];
|
||||
for (int d : days) {
|
||||
isTravel[d] = true;
|
||||
}
|
||||
int[] memo = new int[lastDay + 1];
|
||||
return dfs(lastDay, isTravel, costs, memo);
|
||||
}
|
||||
|
||||
private int dfs(int i, boolean[] isTravel, int[] costs, int[] memo) {
|
||||
if (i <= 0) {
|
||||
return 0;
|
||||
}
|
||||
if (memo[i] > 0) { // 之前计算过
|
||||
return memo[i];
|
||||
}
|
||||
if (!isTravel[i]) {
|
||||
return memo[i] = dfs(i - 1, isTravel, costs, memo);
|
||||
}
|
||||
return memo[i] = Math.min(dfs(i - 1, isTravel, costs, memo) + costs[0],
|
||||
Math.min(dfs(i - 7, isTravel, costs, memo) + costs[1],
|
||||
dfs(i - 30, isTravel, costs, memo) + costs[2]));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.cool.one_question_per_day;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @Author: Cool
|
||||
* @Date: 2024/09/30/20:24
|
||||
* DayNumber 3
|
||||
* Level 3
|
||||
* Score 1429
|
||||
* Hard 2
|
||||
* @link https://leetcode.cn/problems/seat-reservation-manager/description/?envType=daily-question&envId=2024-09-30
|
||||
* @Description 1845. 座位预约管理系统
|
||||
*/
|
||||
public class SeatManager {
|
||||
private final PriorityQueue<Integer> available = new PriorityQueue<>();
|
||||
private int seats;
|
||||
|
||||
public SeatManager(int n) {
|
||||
}
|
||||
|
||||
public int reserve() {
|
||||
if (!available.isEmpty()) { // 有空出来的椅子
|
||||
return available.poll(); // 坐编号最小的
|
||||
}
|
||||
return ++seats; // 添加一把新的椅子
|
||||
}
|
||||
|
||||
public void unreserve(int seatNumber) {
|
||||
available.add(seatNumber); // 有人离开了椅子
|
||||
}
|
||||
|
||||
|
||||
// boolean[] manager;
|
||||
// int minIndex = 0;
|
||||
//
|
||||
// public SeatManager(int n) {
|
||||
// manager = new boolean[n];
|
||||
// Arrays.fill(manager, true);
|
||||
// }
|
||||
//
|
||||
// public int reserve() {
|
||||
// if (minIndex < manager.length) {
|
||||
// while (!manager[minIndex]) {
|
||||
// Q
|
||||
// minIndex++;
|
||||
// }
|
||||
// manager[minIndex++] = false;
|
||||
// }
|
||||
// return minIndex;
|
||||
// }
|
||||
//
|
||||
// public void unreserve(int seatNumber) {
|
||||
// manager[seatNumber - 1] = true;
|
||||
// minIndex = Math.min(minIndex, seatNumber - 1);
|
||||
// }
|
||||
|
||||
}
|
Loading…
Reference in New Issue