Contents

ARST打卡第148周[148/521]

Algorithm

lc2100_适合打劫银行的日子

思路: 顺序倒序都要非递增time个,只要有一个递增的,那就得从递增的那个重新计算起来,也就是说可以重新把递增的那个作为数组起点

所以做好递归退出条件

后续: 做了40分钟,wa了2次…还是要多练啊 看题解后,发现题解用的是左右天数做状态,和自己的第一直觉想法是一样的,但是自己没有继续往这个思路想…

我的解法-递归

 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Solution {
    vector<int> ans;
public:
    void dfsGoodDaysToRobBank(vector<int>& security, int time, int index) {
        if (security.size() - index < (time << 1) + 1) {
            return ;
        }

        int i = index;
        int sz = time;
        for (; sz--; i++) {
            if (security[i] < security[i + 1]) {
                return dfsGoodDaysToRobBank(security, time, i + 1);
            }
        }

        int may_ans_index = i;
        sz = time;
        for (; sz--; i++) {
            if (security[i] > security[i + 1]) {
                // [1,2,5,4,1,0,2,4,5,3,1,2,4,3,2,4,8] 2
                // 这个不会输出结果 5 , 只输出 [10, 14], 而不是[5,10,14]
                // return dfsGoodDaysToRobBank(security, time, i);
                return dfsGoodDaysToRobBank(security, time, i + 1 - time);
            }
        }

        ans.push_back(may_ans_index);
        for (int j = may_ans_index; j <= security.size() - time; j++) {
            // 前面可知security[j] <= security[j + 1]
            if (security[j] == security[j + 1]) {
                if (security[j + time] <= security[j + time + 1]) {
                    ans.push_back(j + 1);
                } else {
                    return dfsGoodDaysToRobBank(security, time, j + time);
                }
            }
            if (security[j] < security[j + 1]) {
                return dfsGoodDaysToRobBank(security, time, j + 1);
            }
        }
    }

    vector<int> goodDaysToRobBank(vector<int>& security, int time) {
        ans.clear();
        if (time == 0) {
            for (int i = 0; i < security.size(); i++) {
                ans.push_back(i);
            }
            return ans;
        }

        if (security.size() < (time << 1) + 1) {
            return ans;
        }

        int index = 0;
        dfsGoodDaysToRobBank(security, time, index);
        return ans;
    }
};

题解-动规

作者:LeetCode-Solution 链接:https://leetcode-cn.com/problems/find-good-days-to-rob-the-bank/solution/gua-he-da-jie-yin-xing-de-ri-zi-by-leetc-z6r1/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
    vector<int> goodDaysToRobBank(vector<int>& security, int time) {
        int n = security.size();
        vector<int> left(n);
        vector<int> right(n);
        for (int i = 1; i < n; i++) {
            if (security[i] <= security[i - 1]) {
                left[i] = left[i - 1] + 1;
            }
            if (security[n - i - 1] <= security[n - i]) {
                right[n - i - 1] = right[n - i] + 1;
            }
        }

        vector<int> ans;
        for (int i = time; i < n - time; i++) {
            if (left[i] >= time && right[i] >= time) {
                ans.emplace_back(i);
            }
        }
        return ans;
    }
};

Review

【TED演讲】人工智能如何再次引发工业革命

AI将会成为基础设施,帮助人类进行工作

Tips

Vdbench测试步骤

Share-#ifdef和#define为0不是配套的

测试脚本发现:

  • ifdef和define是配套的,但是和define为0并不配套,因为define为0也是define了
  • define为0和if才是配套使用的
 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
36
37
38
#include <stdio.h>

#define zero_test 0
#define null_test

int main() {
#ifdef zero_test
        puts("define test 0 also like define\n");
#endif

#ifdef null_test
        puts("define test nothing also like define\n");
#endif

#ifdef nothing
        puts("nothing define also like define\n")
#endif

#if zero_test
        puts("if test 0 also like define\n");
#endif

#if 0
#if null_test
        puts("if test nothing also like define\n");
#endif

define.c: In function ‘main’:
define.c:20:14: error: #if with no expression
 #if null_test              ^
              ^

#endif

        puts("finished:!\n");

        return 0;
}