Given two ijava
- 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(); } }
Create a timebased key-value store class TimeMap, that supports two operations.app
- set(string key, string value, int timestamp)
Stores the key and value, along with the given timestamp.ui
- 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 ("").code
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]; } }
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.ip
Train tickets are sold in 3 different ways:get
- 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.string
Return the minimum number of dollars you need to travel every day in the given list of days.it
有三種旅行費用方案:1天7天和30天,而且每種旅行方案的費用不同,假設給出了一個旅行計劃數組days,求最少的總花費io
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]; } }
本身思路也不清晰,成績很差