반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 전략패턴
- cypressBDD
- 시스템설계면접
- s3이미지다운로드됨
- 디자인패턴
- 리팩토링2판4장
- react-ga
- 가상면접3장
- cypress React
- 시스템설계방법
- file not found Error
- Git commit 합치기
- gitsquash
- 리액트구글애널리틱스
- FirebaseAnalytics
- 가상면접2장
- formik react-query submitting not working
- git commit 협업
- 가상면접으로대규모시스템
- 시스템설계
- 헤드퍼스트전략패턴
- 시스템설계면접팁
- react
- 테스트코드책
- formik submitting not working
- 시스템설계면접예시
- awss3
- git commit merge
- 리팩터링2판테스트
- git squash
Archives
- Today
- Total
mingg IT
[LeetCode] 1005. Maximize Sum Of Array After K Negations 본문
https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/
Maximize Sum Of Array After K Negations - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
priorityQueue 를 사용하면 쉽게 풀 수 있다.
처음엔 Array를 계속 정렬했는데 그럴 필요가 없음.
class Solution {
public int largestSumAfterKNegations(int[] nums, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int i = 0; i < nums.length; i++) {
pq.offer(nums[i]);
}
while(k > 0) {
pq.offer(pq.poll() * -1);
k--;
}
int sum = 0;
while(!pq.isEmpty()) {
sum += pq.poll();
}
return sum;
}
}
1트 : Array를 정렬하면서 풀었을 경우
2트 : PriorityQueue를 사용하여 풀었을 경우
앞으로 문제를 풀 때 시간복잡도를 한번 더 생각하고 풀어야겠음.
'코딩테스트' 카테고리의 다른 글
[Codility] PassingCars (Java) (0) | 2022.01.15 |
---|---|
[Java] 프로그래머스 구명보트 (0) | 2021.11.29 |
[Java] compare 메서드 Override, 특정 규칙에 따라 정렬하기 (0) | 2021.07.25 |
[Java] n 진수로 바꾸는 방법 (0) | 2021.07.19 |
[JAVA] set -> int [] 로 변환하는 방법 (0) | 2021.07.19 |
Comments