LeetCode #1046 LastStoneWeight. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Last Stone Weight.
Memory Usage: 8.3 MB, less than 100.00% of C++ online submissions for Last Stone Weight.
LeetCode #1046
Q.
We have a collection of rocks, each rock has a positive integer weight.
Each turn, we choose the two heaviest rocks and smash them together. Suppose the stones have weights x and ywith x <= y. The result of this smash is:
- If x == y, both stones are totally destroyed;
- If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x.
At the end, there is at most 1 stone left. Return the weight of this stone (or 0 if there are no stones left.)
바위를 모아둔 것이 있는데, 각 바위들은 양의정수로 무게를 가지고 있다.
각 턴마다, 우리는 두개의 가장 무거운 바위들을 부딪힌다. 무게가 x, y 인 돌들을 가지고 있다고 가정하고, x <= y 라고 생각해보자. 부딪힌 결과는 다음과 같다 :
- 만약 x == y 라면, 돌 둘다 완전 깨진다.
- 만약 x != y 라면, x 는 완전히 깨지고, y 는 y-x 값만큼 남아있는다.
마지막에는, 1개의 돌이 남는다. 이 돌의 무게를 반환해라. (아니면 돌이 없으면 0 을 반환해라.)
Example 1:
Input: [2,7,4,1,8,1]
Output: 1
Explanation:
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1]
then, we combine 2 and 4 to get 2 so the array converts to [2,1,1,1]
then, we combine 2 and 1 to get 1 so the array converts to [1,1,1]
then, we combine 1 and 1 to get 0 so the array converts to [1]
then that's the value of last stone.
Note:
- 1 <= stones.length <= 30
- 1 <= stones[i] <= 1000
Process
// Process
//1. Input stones
//2. Iterate till stones length becomes 0
// 2.1. Sort stones (decending)
// 2.2. Check if [0] and [1] are the same or not
// 2.2.1. If so -> remove both
// 2.2.2. If not -> subtract bigger one to smaller one, and remove smaller one
//3. Return resultStone weight
// 처리과정
//1. 바위 배열을 입력받는다.
//2. 바위가 1개보다 적을 때까지 반복한다.
// 2.1. 내림차순 정렬한다.
// 2.2. [0] 번째와, [1] 번째 값이 같은지 다른지 비교해서
// 2.2.1. 같으면 -> 둘다 지운다.
// 2.2.2. 다르면 -> 큰값은 작은값 빼고, 작은값은 지운다.
//3. 돌 값 반환한다.
Code.. lemme see example code!!!
코드.. 예제코드를 보자!!!
class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
if (stones.size() > 0) {
while (stones.size() > 1) {
sort(stones.begin(), stones.end(), greater<int>());
if (stones[0] == stones[1]) {
stones.erase(stones.begin());
stones.erase(stones.begin());
}
else {
stones[0] = stones[0] - stones[1];
stones.erase(stones.begin() + 1);
}
}
}
if (stones.size() > 0)
return stones[0];
else
return 0;
}
};
Something else you might like...?
2019/08/14 - [Life/Item review] - Mi Band 4 review, 미밴드4 후기, 장점, 단점, 리뷰, 한글, global review, 미밴드4 글로벌 후기, 리뷰, 구매, 사용방법, setting, 세팅, ProsNCons
2019/08/27 - [Programming] - 개발자 선배들에게서 배운 것들. Things I Learnt from a Senior Software Engineer. 코딩 잘하는 방법, how to code well, 소프트웨어,프로그래머,programmer
2019/08/28 - [Algorithm/Leet Code] - LeetCode #821 ShortestDistanceToACharacter. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접..
2019/08/25 - [Algorithm/Leet Code] - LeetCode #191 NumberOf1Bits. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트
2019/08/19 - [Algorithm/Leet Code] - LeetCode #942 DI StringMatch. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트
2019/07/21 - [Algorithm/Leet Code] - LeetCode #590 N-aryTreePostorderTraversal. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,..
2019/07/20 - [Algorithm/Leet Code] - LeetCode #589 N-aryTreePreorderTraversal. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,..
2019/07/17 - [Algorithm/Leet Code] - LeetCode #905 SortArrayByParity. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리..
2019/07/13 - [Algorithm/Leet Code] - LeetCode #994 RottingOranges. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트
2019/06/25 - [Algorithm/Leet Code] - LeetCode #766 ToeplitzMatrix. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트
2019/06/19 - [Algorithm/Leet Code] - LeetCode #771 JewelsAndStones. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스..
2019/06/18 - [Algorithm/Leet Code] - LeetCode #739 DailyTempertures. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리..
2019/06/15 - [Algorithm/Leet Code] - LeetCode #724 FindPivotIndex. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트
2019/06/12 - [Algorithm/Leet Code] - LeetCode #705 RelativeRanks. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트
2019/04/14 - [Programming/C++] - C++ Math - sqrt (square root, 제곱근, 루트). stl, math.h, 씨쁠쁠, example code, 예제코드
2018/10/19 - [Programming/Design Pattern ] - Design pattern - Prototype (디자인패턴 - 프로토타입) / Java C++ C#
2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #60 sudoku. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive
2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #59 spiralNumbers. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive
2019/01/08 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #58 messageFromBinaryCode. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive
2019/01/07 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #57 fileNaming. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive
2019/01/04 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #56 digitsProduct. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar
2019/01/04 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #55 differentSquares. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar
2018/12/28 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 - (#9, Tell, Don't Ask)
2018/12/26 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 (1)
2019/01/14 - [Programming/Java] - 자바 메모리 누수 체크/확인/고치는 방법, Memory leak check/fix in Java application, cleanCode/좋은코드/oop/객체지향
2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법
2019/02/16 - [Life/Health care] - Finasteride 피나스테라이드,탈모약 usage/side effects/dosage 효능/부작용/효과/sexual effect/두타스테라이드/프로페시아/propecia/finpecia/카피약/copy drug/hair loss
2019/02/25 - [Life/Health care] - Folic Acid 엽산 vitaminB9,비타민M usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법
2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법
2019/03/03 - [Life/Health care] - Vitamin B1, Thiamine, 비타민 B1, 티아민 usage/side effects/dosage 효능/부작용/성인,소아 용법