T1、T2、T3 过了,T4 有点思路,没抓住重点,加油!!!
力扣
没啥好说的,模拟即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution {public : int prefixCount (vector<string>& words, string pref) { int ans = 0 , m = pref.size (); bool flag = false ; for (string word: words){ if (word.size () >= m){ flag = true ; for (int i =0 ; i < m; i++){ if (pref[i] != word[i]) flag = false ; } if (flag) ans ++; } } return ans; } };
力扣
主要是理解,找到两个字符串不同字符的个数即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution {public : int minSteps (string s, string t) { vector<int > count1 (26 ,0 ) ; for (char c: s){ count1[c - 'a' ] ++; } for (char c: t){ count1[c - 'a' ] --; } int ans = 0 ; for (int i =0 ; i < 26 ; i++){ ans += abs (count1[i]); } return ans; } };
力扣
二分法,找到最小和最大时间,在判断是否可以完成旅途,算是一个二分+判断的典型题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 class Solution {public : long long minimumTime (vector<int >& time, int totalTrips) { int n = time.size (); long long minTime = 10000001 ; for (long long t: time){ minTime = min (t, minTime); } long long r = totalTrips * minTime; long long l = 1 ; long long sz = 0 ; long long ans = 0 ; while (l <= r){ long long m = l + ( (r - l) >> 1 ); sz = 0 ; for (int t: time){ sz += m/t; } if (sz >= totalTrips){ ans = m; r = m - 1 ; }else { l = m + 1 ; } } return ans; } };
力扣
没做出来
力扣
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Solution {public : int minimumFinishTime (vector<vector<int >>& tires, int changeTime, int numLaps) { vector<int > mintimes (20 , 1e9 ) ; for (auto & v : tires) { long long f = v[0 ], r = v[1 ], fir = f + changeTime ; for (int i = 1 , sum = fir; f <= fir; i++, f = f * r, sum += f) { mintimes[i] = min (mintimes[i], sum); } } vector<int > dp (numLaps + 1 , 1e9 ) ; dp[0 ] = 0 ; for (int i = 1 ; i <= numLaps; ++i) { for (int j = 1 ; j <= min (19 , i); ++j) { dp[i] = min (dp[i], dp[i-j] + mintimes[j]); } } return dp[numLaps] - changeTime; } };