984. String Without AAA or BBB
题意
Given two i
- S has length A + B and contains exactly A 'a' letters, and exactly B 'b' letters;
- The substring 'aaa' does not occur in S;
- The substring 'bbb' does not occur in S.
思路
周赛时被这道题坑了好久哦;String类型题目有时候真烦人
代码
class Solution {
public String strWithout3a3b(int A, int B) {
StringBuilder sb = new StringBuilder();
if (A > B) {
while (A-- > 0) sb.append('a');
for (int i = 2; i < sb.length() && B > 0; i += 3) {
sb.insert(i, 'b');
B--;
}
if (B >= 2) {
for (int i = 1; i < sb.length() && B > 0; i++) {
if (sb.charAt(i) == 'a' && sb.charAt(i) == sb.charAt(i - 1)) {
sb.insert(i, 'b');
B--;
}
}
}
while (B-- > 0) sb.append('b');
} else {
while (B-- > 0) sb.append('b');
for (int i = 2; i < sb.length() && A > 0; i += 3) {
sb.insert(i, 'a');
A--;
}
if (A >= 2) {
for (int i = 1; i < sb.length() && A > 0; i++) {
if (sb.charAt(i) == 'b' && sb.charAt(i) == sb.charAt(i - 1)) {
sb.insert(i, 'a');
A--;
}
}
}
while (A-- > 0) sb.append('a');
}
return sb.toString();
}
}
981. Time Based Key-Value Store
题意
Create a timebased key-value store class TimeMap, that supports two operations.
- set(string key, string value, int timestamp)
Stores the key and value, along with the given timestamp.
- get(string key, int timestamp)
Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp.
If there are multiple such values, it returns the one with the largest timestamp_prev.
If there are no values, it returns the empty string ("").
思路
- 直接想到了用TreeMap,并且注意到timestamp是递增的,也就是不会重复,那么就不用考虑考虑一个timestamp会有多个key-value了,直接解就好
代码
class TimeMap {
Map<Integer, String> kv;
TreeMap<Integer, String> map;
/** Initialize your data structure here. */
public TimeMap() {
kv = new HashMap<>();
map = new TreeMap<>();
}
public void set(String key, String value, int timestamp) {
String tmp = key + "_" + value;
kv.put(timestamp, tmp);
map.put(timestamp, key);
}
public String get(String key, int timestamp) {
Integer lastTime = map.floorKey(timestamp);
if (lastTime == null) return "";
while (!map.get(lastTime).equals(key)) {
lastTime = map.floorKey(lastTime - 1);
}
String tmp = kv.get(lastTime);
return tmp.split("_")[1];
}
}
983. Minimum Cost For Tickets
题意
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
- a 1-day pass is sold for costs[0] dollars;
- a 7-day pass is sold for costs[1] dollars;
- a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
有三种旅行费用方案:1天7天和30天,并且每种旅行方案的费用不一样,假设给出了一个旅行计划数组days,求最少的总花费
思路
- 分析:感觉应该是用dp,但是想不太到怎么弄
- 思路:dp[i]表示到第i天为止最少的旅行计划
遍历所有是天知道最后一天,然后第i天属于旅行计划的一天,则面临三种选择,解雇哦是取三种选择中最小的一个;
如果不是旅行计划的一天,则花费不变dp[i] = dp[i - 1]
代码
class Solution {
public int mincostTickets(int[] days, int[] costs) {
int maxDay = 1;
List<Integer> totalDays = new ArrayList<>();
for (int day : days) {
totalDays.add(day);
maxDay = Math.max(maxDay, day);
}
int[] dp = new int[maxDay + 1];
dp[0] = 0;
for (int i = 1; i <= maxDay; i++) {
if (!totalDays.contains(i)) {
dp[i] = dp[i - 1];
continue;
}
int minCost = Integer.MAX_VALUE;
minCost = Math.min(minCost, dp[Math.max(0, i - 1)] + costs[0]);
minCost = Math.min(minCost, dp[Math.max(0, i - 7)] + costs[1]);
minCost = Math.min(minCost, dp[Math.max(0, i - 30)] + costs[2]);
dp[i] = minCost;
}
return dp[maxDay];
}
}
总结:
自己思路也不清晰,成绩不好