一.算法类
1622题,困难,奇妙序列
class Fancy {
public:
static const int MOD = 1e9 + 7;
long long M_total; // cumulative multiplicative factor
long long A_total; // cumulative additive factor
vector<long long> val; // original values
vector<long long> M_i; // multiplicative factor at time of append
vector<long long> A_i; // additive factor at time of append
Fancy() {
M_total = 1;
A_total = 0;
}
// Function to compute x^y % MOD
long long modPow(long long x, long long y) {
long long result = 1;
x %= MOD;
while (y > 0) {
if (y % 2 == 1)
result = result * x % MOD;
x = x * x % MOD;
y /= 2;
}
return result;
}
// Function to compute modular inverse of a modulo MOD
long long modInverse(long long a) {
return modPow(a, MOD - 2);
}
void append(int val_) {
val.push_back(val_);
M_i.push_back(M_total);
A_i.push_back(A_total);
}
void addAll(int inc) {
A_total = (A_total + inc) % MOD;
}
void multAll(int m) {
M_total = (M_total * m) % MOD;
A_total = (A_total * m) % MOD;
}
int getIndex(int idx) {
if (idx >= val.size())
return -1;
long long v_i = val[idx];
long long M_i_inv = modInverse(M_i[idx]);
long long M = M_total * M_i_inv % MOD;
long long A_i_mul_M = A_i[idx] * M % MOD;
long long A = (A_total - A_i_mul_M + MOD) % MOD;
long long result = (v_i * M % MOD + A) % MOD;
return (int)result;
}
};
通关截图
-------------------------------------------------------------------------------------------------------------------------------
2043题,中等,简易银行系统
class Bank {
public:
vector<long long> balance; // 存储每个账户的余额
Bank(vector<long long>& balance_) {
balance = balance_; // 初始化账户余额
}
bool transfer(int account1, int account2, long long money) {
// 检查账户是否有效
if (account1 < 1 || account1 > balance.size() || account2 < 1 || account2 > balance.size())
return false;
// 检查账户1的余额是否足够
if (balance[account1 - 1] < money)
return false;
// 执行转账操作
balance[account1 - 1] -= money;
balance[account2 - 1] += money;
return true;
}
bool deposit(int account, long long money) {
// 检查账户是否有效
if (account < 1 || account > balance.size())
return false;
// 执行存款操作
balance[account - 1] += money;
return true;
}
bool withdraw(int account, long long money) {
// 检查账户是否有效
if (account < 1 || account > balance.size())
return false;
// 检查账户余额是否足够
if (balance[account - 1] < money)
return false;
// 执行取款操作
balance[account - 1] -= money;
return true;
}
};
通关截图
-------------------------------------------------------------------------------------------------------------------------------
二.数据库
262题,困难,行程和用户
SELECT
t.request_at AS Day,
ROUND(
AVG(t.status IN ('cancelled_by_driver', 'cancelled_by_client')), 2
) AS 'Cancellation Rate'
FROM
Trips t
JOIN Users cu ON t.client_id = cu.users_id AND cu.banned = 'No'
JOIN Users du ON t.driver_id = du.users_id AND du.banned = 'No'
WHERE
t.request_at BETWEEN '2013-10-01' AND '2013-10-03'
GROUP BY
t.request_at;
通关截图