Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Updates and CPP codes#96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
neetcode-gh merged 8 commits intoneetcode-gh:mainfromUnresolvedCold:main
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletionscpp/1046-Last-Stone-Weight.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
priority_queue<int, vector<int>> pq(stones.begin(), stones.end());
pq.push(0);

while (pq.size()>=1) {
int t1 = pq.top();
pq.pop();
if(pq.empty()) break;
int t2 = pq.top();
pq.pop();
pq.push(abs(t1-t2));
}

return pq.top();
}
};
8 changes: 8 additions & 0 deletionscpp/215-Kth-Largest-Element-in-an-Array.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int, vector<int>, greater<int>> pq(nums.begin(), nums.end());
while (pq.size() > k)pq.pop();
return pq.top();
}
};
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletionscpp/543-Diameter-of-Binary-Tree.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:

int MaxHeight(TreeNode* root, int& res) {
if (root == NULL) return 0;
int l = MaxHeight(root->left, res);
int r = MaxHeight(root->right, res);
res = max(res, max(max(l, r), l + r));
return 1 + max(l, r);
}

int diameterOfBinaryTree(TreeNode* root) {
int breadth = 0;
MaxHeight(root, breadth);
return breadth;
}
};
File renamed without changes.
26 changes: 26 additions & 0 deletionscpp/703-Kth-Largest-Element-in-a-Stream.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
class KthLargest {
public:
priority_queue<int, vector<int>, greater<int>> pq;
int k;
KthLargest(int k, vector<int>& nums) {
this->k = k;
for (auto a: nums) pq.push(a);
while(pq.size()>k){
pq.pop();
}
}

int add(int val) {
pq.push(val);
while(pq.size()>k){
pq.pop();
}
return pq.top();
}
};

/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest* obj = new KthLargest(k, nums);
* int param_1 = obj->add(val);
*/
File renamed without changes.
39 changes: 39 additions & 0 deletionscpp/973-K-Closest-Points-to-Origin.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
class Solution {
public:
struct cords {
int x, y;
cords(vector<int> a) {
this -> x = a[0];
this -> y = a[1];
}
cords(int x, int y){
this -> x = x;
this -> y = y;
}
int dist(){
return x*x+y*y;
}
};

struct compare {
bool operator()(cords a, cords b) {
return a.dist() < b.dist();
}
};

vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
priority_queue<cords, vector<cords>, compare> pq;
for (auto a: points) {
pq.push(cords(a));
}

while (pq.size() > k) pq.pop();

vector<vector<int>> res;
while (!pq.empty()) {
res.push_back({pq.top().x, pq.top().y});
pq.pop();
}
return res;
}
};
File renamed without changes.
File renamed without changes.

[8]ページ先頭

©2009-2025 Movatter.jp