-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.13
More file actions
60 lines (48 loc) · 1.75 KB
/
8.13
File metadata and controls
60 lines (48 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
8.13
https://leetcode.com/problems/last-stone-weight/solutions/7027678/priority-queue-heap-solution-beat-100-of-other-submissions-runtime/?envType=problem-list-v2&envId=heap-priority-queue
https://leetcode.com/problems/two-sum/description/?envType=problem-list-v2&envId=hash-table
class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
priority_queue<int> pq;
for(int i : stones){
pq.push(i);
}
while(pq.size() > 1){
int a = pq.top(); pq.pop();
int b = pq.top(); pq.pop();
if(a != b){
pq.push(abs(a-b));
}
}
return pq.empty() ? 0 : pq.top();
}
};
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, vector<int>> hashTable;
vector<int> result;
for (int i = 0; i < nums.size(); i++) {
hashTable[nums[i]].push_back(i);
}
for(int j=0; j<nums.size(); j++){
int findNumberAddMillion = target - nums[j] + 100000000;
int findNumber = target - nums[j];
int findNumberIndexSecond;
if (hashTable.find(findNumber) != hashTable.end() && !hashTable[findNumber].empty()){
if (hashTable[findNumber][0] == j && hashTable[findNumber].size() > 1) {
findNumberIndexSecond = hashTable[findNumber][1];
}else if (hashTable[findNumber][0] != j) {
findNumberIndexSecond = hashTable[findNumber][0];
}else{
continue;
}
result.push_back(j);
result.push_back(findNumberIndexSecond);
break;
}
}
return result;
}
};