Skip to content

Commit d0313f6

Browse files
committed
2386
1 parent e66c023 commit d0313f6

File tree

15 files changed

+499
-0
lines changed

15 files changed

+499
-0
lines changed

leetcode/2386/1.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[2,4,-2]
2+
5

leetcode/2386/12.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[-487322177,-656480132,850198596,-291605661,-272668395,110865952,-162529283,-145886963,202657909,125667049,-282090943,120877054,-85849348,-482630078,-802177895,-574862206,374637017,804297842]
2+
1707

leetcode/2386/15.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[380342990,-246234524,-778364240,224959608,569711933,568348883,734397453,-610236078,662947362,29215261,-791207453,-75950938,46847233,971804154,-512427950,920138728]
2+
942

leetcode/2386/2.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[1,-2,3,4,-10,12]
2+
16

leetcode/2386/32.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[508960725,-591576738,-842983833,841747633,350699661]
2+
11

leetcode/2386/35.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[153123449,-974739108,-408679566,-996444415,-978921261,805907128,-102259288,-397930077,51033052,-193994032,158654659,-486195972,-294264190,-65262667,375941242,-890038230,315970860,403847239,-32469129,-350561293,192113942,794248972,-632675681,434029943,746632801,500370163,164413366,346449701,473890512]
2+
1906

leetcode/2386/36.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[-91282107,-541711002,-276822556,90917395,834041510,-840892824,-508608840,-147048391,557843113,272412766,-918980912,820869808,385517519,-341785025,486628402,993154325,-629135417,677219246,62835597,64218970,928242835,896481126,-738154826,478700010,-893696727,-119562113,-43819130,-987768829,157402884,-386180872,237072809,-919540108,267447436,201913793,-281453262,168068441,690937145,894616338,428922554,355032016,-282793837,-445107592,-559015912,266029154,-245027665,324705364,-727272924,186301685,-905146561,-209906218,111448330,7925324,-103030197,62731785,652384508,242259114,56539784,69378804,190147339,-844137898,-595488526,-731717935,-480766744,767916483,119646172,16969979,906477612,387179801,-845067849,-703297363,816507867,436741820,285885943,-305638994,374442522,-657030826,-770219295,-543436120,612879155,646087640,-403176698,393578994,-155171985,-408422544,-487489088]
2+
1477

leetcode/2386/58.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[26929228,570672773,944323923,-270730815,930794290,-67116629,-721033930,627057977,926459554,-191968216,-398561424,-515687801,204037911,951799536,112713617,-226334699,-397918319,-410676131,533102608,577982132,-511728515,619395005,144696870,131772970,383285071,650142277,475663783,-865748557,-864544818,-301195584,-943119002,23511617,-491988973,-390592083,-660186889,-813962712,698408444,58251670,684475819,891816718,359849434,403687766,-187872803,-294533016,753455126,-304740665,-253456889,111122495,933806534,876313714,-238027634,550781753,492246509]
2+
1867

leetcode/2386/main.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
3+
*/
4+
5+
#include <bits/stdc++.h>
6+
#include "../../lib/LeetCodeInputTemplate.hpp"
7+
#include "solution-2.1.cpp"
8+
using namespace std;
9+
10+
typedef vector<int> vi;
11+
typedef vector<vector<int>> vvi;
12+
13+
int main()
14+
{
15+
LeetCodeInput li("36.in");
16+
17+
auto l0 = li.get<vi>(0);
18+
auto l1 = li.get<int>(1);
19+
20+
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
21+
Solution solution;
22+
auto output = solution.kSum(l0, l1);
23+
chrono::steady_clock::time_point end = chrono::steady_clock::now();
24+
cout << "Time difference = " << chrono::duration_cast<chrono::microseconds> (end - begin).count() << "µs" << endl;
25+
26+
cout << output << endl;
27+
28+
// for (int i: output){
29+
// cout << i << ' ';
30+
// }
31+
32+
// for (auto i: output){
33+
// for (int j: i){
34+
// cout << j << ' ';
35+
// }
36+
// cout << '\n';
37+
// }
38+
39+
return 0;
40+
}

leetcode/2386/solution-1.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
3+
time: O()
4+
space: O()
5+
6+
Runtime: tle
7+
Memory Usage:
8+
9+
bfs + pq
10+
*/
11+
12+
#include <bits/stdc++.h>
13+
14+
using namespace std;
15+
16+
#define all(x) begin(x), end(x)
17+
typedef vector<int> vi;
18+
typedef vector<vector<int>> vvi;
19+
20+
class Solution {
21+
public:
22+
long long kSum(vector<int>& nums, int k) {
23+
sort(all(nums), [&nums](int & i, int & j){return abs(i)<abs(j);});
24+
long long sum = 0;
25+
long long smallest = 0;
26+
27+
int n = size(nums);
28+
for (int i=0; i<n; ++i){
29+
sum += nums[i];
30+
if (nums[i]<0) {
31+
smallest += nums[i];
32+
}
33+
}
34+
35+
priority_queue<long long, vector<long long>, greater<long long>> pq;
36+
37+
vector<long long> subsets;
38+
subsets.push_back(smallest);
39+
pq.push(smallest);
40+
long long last_top = pq.top();
41+
42+
int count = 0;
43+
int pos = 0;
44+
45+
int maximum = 0;
46+
47+
while (count != k){
48+
if (pq.empty() || (pos<n && pq.top()>smallest+abs(nums[pos]))){
49+
int m = size(subsets);
50+
subsets.resize(m<<1);
51+
for (int i = 0; i<m; ++i){
52+
long long new_num = subsets[i]+abs(nums[pos]);
53+
subsets[m+i]=new_num;
54+
pq.push(new_num);
55+
}
56+
++pos;
57+
}
58+
last_top = pq.top();
59+
pq.pop();
60+
++count;
61+
}
62+
return sum-last_top;
63+
}
64+
};
65+
66+
const static auto initialize = [] {
67+
std::ios::sync_with_stdio(false);
68+
std::cin.tie(nullptr);
69+
std::cout.tie(nullptr);
70+
return nullptr;
71+
}();

0 commit comments

Comments
 (0)