- Notifications
You must be signed in to change notification settings - Fork2.4k
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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
ba781a4 [Update] Renamed Certain Files in cpp directory
UnresolvedColdb7c4810 [CREATE] [CPP] 543. Diameter of Binary Tree
UnresolvedCold840b495 [Update] Renammed 543 cpp
UnresolvedCold60fe6e7 [Update] Renamed files under all the directory to follw same nomencla…
UnresolvedCold7062852 [CREATE] [CPP] 703. Kth Largest Element in a Stream
UnresolvedCold4884c73 [CREATE] [CPP] 215. Kth Largest Element in an Array
UnresolvedColdf40fa85 [CREATE] [CPP] 1046. Last Stone Weight
UnresolvedColdd7d8386 [CREATE] [CPP] 973. K Closest Points to Origin
UnresolvedColdFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
18 changes: 18 additions & 0 deletionscpp/1046-Last-Stone-Weight.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff 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(); | ||
| } | ||
| }; |
File renamed without changes.
8 changes: 8 additions & 0 deletionscpp/215-Kth-Largest-Element-in-an-Array.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff 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.
File renamed without changes.
28 changes: 28 additions & 0 deletionscpp/543-Diameter-of-Binary-Tree.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff 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.
File renamed without changes.
39 changes: 39 additions & 0 deletionscpp/973-K-Closest-Points-to-Origin.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff 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.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.