Given an array of integers coins, where each el
Constraints
N/A
Example 1
Input:
coins = [3, 4, 6]
Output:
[2, 2, 3]
Explanation: No explanation for now..
解法
经典 LC 441 变体:对每个 n = coins[i],找最大 k 使 k*(k+1)/2 ≤ n。可直接二分或求根公式 k = floor((sqrt(8n+1)-1)/2)。时间复杂度 O(len(coins)),空间复杂度 O(len(coins)) 用于结果数组。
from typing import List
import math
def arrangeCoins(coins: List[int]) -> List[int]:
res = []
for n in coins:
k = int((math.isqrt(8 * n + 1) - 1) // 2)
res.append(k)
return resclass Solution {
int[] arrangeCoins(int[] coins) {
int[] res = new int[coins.length];
for (int i = 0; i < coins.length; i++) {
long n = coins[i];
long k = (long) ((Math.sqrt(8.0 * n + 1) - 1) / 2);
while ((k + 1) * (k + 2) / 2 <= n) k++;
while (k * (k + 1) / 2 > n) k--;
res[i] = (int) k;
}
return res;
}
}class Solution {
public:
vector<int> arrangeCoins(vector<int>& coins) {
vector<int> res;
for (int n : coins) {
long long k = (long long) ((sqrt(8.0 * n + 1) - 1) / 2);
while ((k + 1) * (k + 2) / 2 <= n) k++;
while (k * (k + 1) / 2 > n) k--;
res.push_back((int) k);
}
return res;
}
};At Alarm.com we need a new piece of functionality that will tell us how many days it's been since a user last logged into our site. Our product manager has given us the following requirements that they would like implemented so that we can start tracking inactive users. Minimally Viable Product Input should be a string in the format "MM/dd/yyyy" The input should be converted to a date and subtracted from today's date Return the absolute whole number of days as a string Things to consider If the input does not contain any characters or is only whitespace then return error code "E8430" If the input is in the wrong format return error code "E9021" If the input is in the correct format but not a valid date then return error code "E1756"
Constraints
lastLoginDateis a string up to 10 characters- Format must be
MM/dd/yyyy(or returns error codes)
Example 1
Input:
lastLoginDate = "07/06/2022"
Output:
"1"
Explanation: Input = "07/06/2022" Today = 07/07/2022 Output = "1"
Example 2
Input:
lastLoginDate = "02/30/2020"
Output:
"E1756"
Explanation: The input "02/30/2020" is in the correct format but not a valid date because February does not have 30 days. Therefore, the function returns the error code "E1756". (By Tomtato. If you find it wrong, pls lmk! THX !)
解锁全部 1 道题的解法
题面你已经看到了 — 解法 + 三语代码 + 复杂度推导 + 边界讨论, Pro 解锁.
- 📚1000+ 道真实北美 OA, Python / Java / C++ 三语题解
- 📊个人 dashboard + 进度可视化 + 14 天活跃图
- 📝题目笔记跨设备同步 + 个人复盘库
- 🔓随时取消下次续费, Stripe Customer Portal 自助管理