File tree Expand file tree Collapse file tree 5 files changed +84
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +84
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+ class Solution {
4
+ public:
5
+ bool containsDuplicate (vector<int >& nums) {
6
+ unordered_map<int , int > mp;
7
+ for (int a : nums){
8
+ if (++mp[a] >= 2 ){
9
+ return true ;
10
+ }
11
+ }
12
+ return false ;
13
+ }
14
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int rob (vector<int >& nums) {
4
+ int n = nums.size ();
5
+ if (n == 0 )return 0 ;
6
+ if (n == 1 )return nums[0 ];
7
+ if (n == 2 )return max (nums[0 ], nums[1 ]);
8
+
9
+ vector<int > dp (n);
10
+ dp[0 ] = nums[0 ];
11
+ dp[1 ] = max (nums[0 ], nums[1 ]);
12
+ for (int i = 2 ; i < n; i++){
13
+ dp[i] = max (dp[i - 1 ], dp[i - 2 ] + nums[i]);
14
+ }
15
+ return dp[n - 1 ];
16
+ }
17
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int longestConsecutive (vector<int >& nums) {
4
+ const int INF = 987654321 ;
5
+ int temp = INF, ret = 0 , cur = 0 ;
6
+
7
+ sort (nums.begin (), nums.end ());
8
+ for (int a : nums){
9
+ if (a == temp)continue ;
10
+ if (temp == INF || temp + 1 == a){
11
+ cur++; temp = a;
12
+ } else {
13
+ ret = max (ret, cur);
14
+ cur = 1 ;
15
+ temp = a;
16
+ }
17
+ }
18
+ ret = max (ret, cur);
19
+ return ret;
20
+ }
21
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int > topKFrequent (vector<int >& nums, int k) {
4
+ map<int , int > mp;
5
+ priority_queue<pair<int , int >> pq;
6
+ vector<int > ans;
7
+
8
+ for (auto b : nums) mp[b]++;
9
+
10
+ for (auto p : mp) pq.push ({p.second , p.first });
11
+
12
+ while (k--)ans.push_back (pq.top ().second ), pq.pop ();
13
+
14
+ return ans;
15
+ }
16
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool isPalindrome (string s) {
4
+ string clean = " " ;
5
+ for (char c : s) {
6
+ if (isalnum (c)) {
7
+ clean += tolower (c);
8
+ }
9
+ }
10
+
11
+ string reversed = clean;
12
+ reverse (reversed.begin (), reversed.end ());
13
+
14
+ return clean == reversed;
15
+ }
16
+ };
You can’t perform that action at this time.
0 commit comments