add: 230410-CN

This commit is contained in:
Eatswap 2023-04-10 02:47:54 +08:00
parent 6b86835c06
commit 1c281801e8
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 63 additions and 1 deletions

62
cpp/2304/230410-CN.cpp Normal file
View File

@ -0,0 +1,62 @@
#include <vector>
#include <queue>
#include <ostream>
#include <iostream>
struct ListNode {
int val;
ListNode* next;
explicit ListNode(int x = 0, ListNode* next = nullptr) : val(x), next(next) {}
};
ListNode* construct(int x) {
return new ListNode(x);
}
template<typename... Ts>
ListNode* construct(int x, Ts... xs) {
return new ListNode(x, construct(xs...));
}
/**
* 1019. Next Greater Node In Linked List
*
* You are given the head of a linked list with n nodes.
* For each node in the list, find the value of the next greater node. That is, for each node, find the value of the first node that is next to it and has a strictly larger value than it.
* Return an integer array answer where answer[i] is the value of the next greater node of the ith node (1-indexed). If the ith node does not have a next greater node, set answer[i] = 0.
*/
class Solution {
public:
template<typename T>
using V = std::vector<T>;
using VI = V<int>;
using PII = std::pair<int, int>;
static VI nextLargerNodes(const ListNode*);
};
Solution::VI Solution::nextLargerNodes(const ListNode* head) {
VI ret;
std::priority_queue<PII, V<PII>, std::greater<>> q;
for (const ListNode* ptr = head; ptr; ptr = ptr->next) {
q.emplace(ptr->val, ret.size());
ret.push_back(0);
for (; q.top().first < ptr->val; q.pop())
ret[q.top().second] = ptr->val;
}
return ret;
}
template<typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
for (auto&& x : vec)
os << x << std::endl;
return os;
}
int main() {
std::cout << Solution::nextLargerNodes(construct(2,7,4,3,5));
return 0;
}

View File

@ -4,4 +4,4 @@ PROJECT(2304)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
SET(CMAKE_EXPORT_COMPILE_COMMANDS true) SET(CMAKE_EXPORT_COMPILE_COMMANDS true)
ADD_EXECUTABLE(2304 230409.cpp) ADD_EXECUTABLE(2304 230410-CN.cpp)