Skip to content

Commit 74a2f87

Browse files
committed
1090 added.
1 parent 563f1e1 commit 74a2f87

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(B)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(B main.cpp)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/// Source : https://leetcode.com/problems/largest-values-from-labels/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-06-15
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <unordered_map>
8+
9+
using namespace std;
10+
11+
12+
/// Sorting and Greedy
13+
/// Time Complexity: O(nlogn)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
public:
17+
int largestValsFromLabels(vector<int>& values, vector<int>& labels, int num_wanted, int use_limit) {
18+
19+
vector<pair<int, int>> vec;
20+
for(int i = 0; i < values.size(); i ++)
21+
vec.push_back(make_pair(values[i], labels[i]));
22+
sort(vec.begin(), vec.end(), greater<pair<int, int>>());
23+
24+
unordered_map<int, int> freq;
25+
int sum = 0, num = 0;
26+
for(const pair<int, int>& p: vec)
27+
if(freq[p.second] < use_limit){
28+
29+
sum += p.first;
30+
num ++;
31+
freq[p.second] ++;
32+
if(num == num_wanted)
33+
break;
34+
}
35+
return sum;
36+
}
37+
};
38+
39+
40+
int main() {
41+
42+
vector<int> values = {5,4,3,2,1};
43+
vector<int> labels = {1,1,2,2,3};
44+
int num_wanted = 3, use_limit = 1;
45+
cout << Solution().largestValsFromLabels(values, labels, num_wanted, use_limit) << endl;
46+
47+
return 0;
48+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,4 +740,5 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
740740
| 1080 | [Insufficient Nodes in Root to Leaf Paths](https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/) | [] | [C++](1080-Insufficient-Nodes-in-Root-to-Leaf-Paths/cpp-1080/) | | |
741741
| | | | | | |
742742
| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [] | [C++](1089-Duplicate-Zeros/cpp-1089/) | | |
743+
| 1090 | [Largest Values From Labels](https://leetcode.com/problems/largest-values-from-labels/) | [] | [C++](1090-Largest-Values-From-Labels/cpp-1090/) | | |
743744
| | | | | | |

0 commit comments

Comments
 (0)