Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

1167. Minimum Cost to Connect Sticks

Posted on 2020-07-08 | Edited on 2021-01-22

LeetCode

Greedy, Min heap, PriorityQueue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int connectSticks(int[] sticks) {
PriorityQueue<Integer> q=new PriorityQueue<>();
for(int i:sticks) q.offer(i);
int res=0;
while(q.size()>1){
int a=q.poll();
int b=q.poll();
int sum=a+b;
res+=sum;
q.offer(sum);
}
return res;
}
}

<1…747576…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%