当前位置:网站首页>365 day challenge leetcode 1000 questions - day 040 design jump table + avoid flooding + find the latest grouping with size M + color ball with reduced sales value
365 day challenge leetcode 1000 questions - day 040 design jump table + avoid flooding + find the latest grouping with size M + color ball with reduced sales value
2022-07-29 05:25:00 【ShowM3TheCode】
List of articles
1206. Design jump table

Code implementation ( Look at the explanation )
constexpr int MAX_LEVEL = 32;
constexpr double P_FACTOR = 0.25;
struct SkiplistNode {
int val;
vector<SkiplistNode *> forward;
SkiplistNode(int _val, int _maxLevel = MAX_LEVEL) : val(_val), forward(_maxLevel, nullptr) {
}
};
class Skiplist {
private:
SkiplistNode * head;
int level;
mt19937 gen{
random_device{
}()};
uniform_real_distribution<double> dis;
public:
Skiplist(): head(new SkiplistNode(-1)), level(0), dis(0, 1) {
}
bool search(int target) {
SkiplistNode *curr = this->head;
for (int i = level - 1; i >= 0; i--) {
/* Find No i The layer is smaller than and closest to target The elements of */
while (curr->forward[i] && curr->forward[i]->val < target) {
curr = curr->forward[i];
}
}
curr = curr->forward[0];
/* Detect whether the value of the current element is equal to target */
if (curr && curr->val == target) {
return true;
}
return false;
}
void add(int num) {
vector<SkiplistNode *> update(MAX_LEVEL, head);
SkiplistNode *curr = this->head;
for (int i = level - 1; i >= 0; i--) {
/* Find No i The layer is smaller than and closest to num The elements of */
while (curr->forward[i] && curr->forward[i]->val < num) {
curr = curr->forward[i];
}
update[i] = curr;
}
int lv = randomLevel();
level = max(level, lv);
SkiplistNode *newNode = new SkiplistNode(num, lv);
for (int i = 0; i < lv; i++) {
/* Right. i Update the status of the layer , Change the current element's forward Point to new node */
newNode->forward[i] = update[i]->forward[i];
update[i]->forward[i] = newNode;
}
}
bool erase(int num) {
vector<SkiplistNode *> update(MAX_LEVEL, nullptr);
SkiplistNode *curr = this->head;
for (int i = level - 1; i >= 0; i--) {
/* Find No i The layer is smaller than and closest to num The elements of */
while (curr->forward[i] && curr->forward[i]->val < num) {
curr = curr->forward[i];
}
update[i] = curr;
}
curr = curr->forward[0];
/* If the value does not exist, it returns false */
if (!curr || curr->val != num) {
return false;
}
for (int i = 0; i < level; i++) {
if (update[i]->forward[i] != curr) {
break;
}
/* Right. i Update the status of the layer , take forward Point to the next hop of the deleted node */
update[i]->forward[i] = curr->forward[i];
}
delete curr;
/* Update the current level */
while (level > 1 && head->forward[level - 1] == nullptr) {
level--;
}
return true;
}
int randomLevel() {
int lv = 1;
/* Random generation lv */
while (dis(gen) < P_FACTOR && lv < MAX_LEVEL) {
lv++;
}
return lv;
}
};
1488. Avoid flooding

Code implementation ( Part depends on the solution )
class Solution {
public:
vector<int> avoidFlood(vector<int>& rains) {
const int n = rains.size();
vector<int> ans(n, 1);
set<int> sunny;
map<int, int> full;
for (int i = 0; i < n; i++) {
if (rains[i] == 0) {
sunny.emplace(i);
continue;
}
if (full.count(rains[i])) {
auto it = sunny.lower_bound(full[rains[i]]);
if (it == sunny.end()) return {
};
ans[*it] = rains[i];
sunny.erase(it);
}
full[rains[i]] = i;
ans[i] = -1;
}
return ans;
}
};
1562. Find size is M The latest group of

Code implementation ( Look at the explanation )
int link[100001] = {
0};
class Solution {
public:
int findLatestStep(vector<int>& arr, int m) {
int cnt = 0;
memset(link, -1, sizeof(link));
int anw = -1;
for(int i = 0; i < arr.size(); i++) {
int pos = arr[i] - 1;
link[pos] = pos;
int L = pos, R = pos;
if(0 < pos && link[pos-1] != -1) {
if(pos-1 - link[pos-1] + 1 == m) {
cnt--;
}
L = link[pos-1];
}
if(pos+1 < arr.size() && link[pos+1] != -1) {
if(link[pos+1] - (pos+1) + 1 == m) {
cnt--;
}
R = link[pos+1];
}
link[L] = R;
link[R] = L;
if(R-L+1 == m) {
cnt++;
}
if(cnt > 0) {
anw = i+1;
}
}
return anw;
}
};
1648. Color balls with reduced sales value

Code implementation ( Self solution )
class Solution {
private:
const int V = 1000000007;
public:
int maxProfit(vector<int>& inventory, int orders) {
if (inventory.size() == 1) {
return (long long)
(2 * inventory[0] - orders + 1) % V * orders % V / 2;
}
sort(inventory.begin(), inventory.end(), greater<int>());
inventory.push_back(0);
const int n = inventory.size();
long long sum = 0, nextRound = 0, ans = 0;
for (int i = 0; i < n - 1; i++) {
nextRound = inventory[i] - inventory[i + 1];
if (sum + nextRound * (i + 1) >= orders) {
int j = i + 1;
long long r1 = (orders - sum) / j;
ans = ans + (i + 1)
* (2 * inventory[i] - r1 + 1) * r1 / 2;
long long r2 = (orders - sum) % j;
ans = ans + (long long) r2 * (inventory[i] - r1);
// cout << "r1 = " << r1 << " r2 = " << r2 << endl;
//cout << "ans = " << ans << endl;
return ans % V;
}
ans = ans + (long long) (i + 1) *
(inventory[i] + inventory[i + 1] + 1) * nextRound / 2;
sum += (i + 1) * nextRound;
//cout << "ans = " << ans << " sum = " << sum << endl;
}
return -1;
}
};
边栏推荐
- 365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions
- Xiaobai high salary shortcut Qt development game Snake
- How mongodb inserts, deletes and updates documents
- 最新坦克大战2022-全程开发笔记-3
- C how to realize simple factory mode
- R & D efficiency | analysis of kubernetes' core technology and Devops' landing experience
- vs2019编译cryengine失败问题处理
- Ros1 dead chicken data is stored in txt and SQLite
- C语言数组典型应用代码详细讲解—高手误入(逐步代码详解)
- QT learning: qdropevent drag event
猜你喜欢

Getting started with solidity

osg3.6.5编译freetype失败

C语言连连看秒杀辅助

510000 prize pool invites you to fight! The second Alibaba cloud ECS cloudbuild developer competition is coming

数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录

Live broadcast preview | how to save 30% labor cost and shorten 80% trademark processing cycle?

平行云CEO 李岩:CloudXR ,开启通往元宇宙的通道

321, Jingdong Yanxi × Nlpcc 2022 challenge starts!

Arfoundation starts from zero 9-ar anchor

JD cloud golden autumn cloud special offer is in progress! Code scanning participation activities
随机推荐
Qml控件:ComboBox
Adb常用命令列表
Helm chart for Kubernetes
Helm chart for Kubernetes
CSDN的md编辑器如何输入上下标?公式和非公式的输入方式不一样
2022年泰迪杯数据挖掘挑战赛C题方案及赛后总结
321,京东言犀×NLPCC 2022挑战赛开赛!
During the appointment, the 2022 JD cloud industrial integration new product launch was launched online
源码编译pytorch坑
QML定制TabBar
C语言宏#define命令练习
数据泄漏、删除事件频发,企业应如何构建安全防线?
7.2-function-overloading
Is Huatai Securities an AA level securities company? How about this company? Is it safe to open an account?
ARFoundation入门教程7-url动态加载图像跟踪库
C语言连连看秒杀辅助
研发效能生态完整图谱&DevOps工具选型必看
直播预告:京东云DevOps与JFrog制品库的融合
Qt版的贪食蛇游戏项目
Custom QML control: imagebutton