LeetCode 121th Weekly Contest 總結


984. String Without AAA or BBB

題意

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();
    }
}

981. Time Based Key-Value Store

題意

Create a timebased key-value store class TimeMap, that supports two operations.app

    1. set(string key, string value, int timestamp)

Stores the key and value, along with the given timestamp.ui

    1. 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

思路

  • 直接想到了用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.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

思路

  • 分析:感受應該是用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]; 
    }
}

總結:

本身思路也不清晰,成績很差

相關文章
相關標籤/搜索