牛牛切木棒
class Solution {
public:
/**
*
* @param a long长整型 木棒的长度
* @return int整型
*/
int stick(long long a) {
ull ans = 2;
ull now = 1, sum = 2;
ull x = 1, y = 1;
if(a == 1) {puts("1"); return 0;}
while(1)
{
now = x + y;
if(now > a - sum ) break;
now = x + y;
x = y;
y = now;
ans ++;
sum = sum + now;
}
return ans;
// write code here
}
};
Tree II
class Solution
{
public:
typedef long long ll;
ll n, k, d[100010];
ll ans = 0;
inline void dfs(ll x)
{
for (ll i = -k + 2; i <= 1; ++i)
{
if (x * k + i <= n)
{
dfs(x * k + i);
ans += (d[x] ^ d[x * k + i]);
}
else
return;
}
}
long long tree2(int K, vector<int> &a)
{
n = a.size();
k = K;
for (int i = 0; i < n; ++i)
{
d[i + 1] = a[i];
}
dfs(1);
return ans;
}
};
数据分析
class Solution {
public:
/**
* 找到所有长度子数组中最大值的最小值
* @param numbers int整型vector
* @return int整型vector
*/
vector<int> getMinimums(vector<int>& numbers) {
int n = numbers.size();
vector<int> left(n, -1), right(n, n);
vector<int> ans(n, numeric_limits<int>::max());
stack<int> s;
for (int i = 0; i < n; i++) {
while (!s.empty()) {
auto t = s.top();
if (numbers[t] <= numbers[i]) {
right[t] = i;
s.pop();
} else {
break;
}
}
if (!s.empty()) {
left[i] = s.top();
}
s.push(i);
}
for (int i = 0; i < n; i++) {
int sz = right[i] - left[i] - 1;
ans[sz - 1] = min(ans[sz - 1], numbers[i]);
}
for (int i = n - 2; i >= 0; i--) {
ans[i] = min(ans[i], ans[i + 1]);
}
return ans;
}
};
全部评论
(0) 回帖