[E模拟] lc1716. 计算力扣银行的钱(模拟+找规律+双周赛43_1)

1. 题目来源

链接:1716. 计算力扣银行的钱

2. 题目解析

简单的模拟问题,找规律或者直接模拟都是可以的,n 很小。
在这里插入图片描述
思路:

  • 找规律定位到当前天数应当存的钱数,计算行、列,并找到行列与钱数相关的规律即可。
  • 或者直接硬模拟,每隔 7 天在周一的时候重置一下本周的起始值为上周一加 1,然后周内每天递增 1 即可。这就是纯暴力模拟了,也是我在竞赛过程中采用的无脑写法。

  • 时间复杂度 O ( n ) O(n) O(n)
  • 空间复杂度 O ( 1 ) O(1) O(1)

代码:

class Solution {
    
    
public:
    int totalMoney(int n) {
    
    
        int res = 0;
        for (int i = 1; i <= n; i ++ ) {
    
    
            int r = (i - 1) / 7, c = (i - 1) % 7;
            res += r + c + 1;
        }
        return res;
    }
};

考试时的暴力写法:

class Solution {
    
    
public:
    int totalMoney(int n) {
    
    
        int res = 0;
        int t = 1, a = 1;
        for (int i = 1; i <= n; i ++ ) {
    
    
            res += t ++ ;
            if (i % 7 == 0) t = ++ a ;
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/yl_puyu/article/details/112681162