알고리즘/codefights

19>areEquallyStrong

Diademata 2017. 7. 2. 22:40
반응형

Call two arms equally strong if the heaviest weights they each are able to lift are equal.


Call two people equally strong if their strongest arms are equally strong (the strongest arm can be both the right and the left), and so are their weakest arms.


Given your and your friend's arms' lifting capabilities find out if you two are equally strong.


Example


For yourLeft = 10, yourRight = 15, friendsLeft = 15 and friendsRight = 10, the output should be

areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight) = true;

For yourLeft = 15, yourRight = 10, friendsLeft = 15 and friendsRight = 10, the output should be

areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight) = true;

For yourLeft = 15, yourRight = 10, friendsLeft = 15 and friendsRight = 9, the output should be

areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight) = false


code >>


bool areEquallyStrong(int yourLeft, int yourRight, int friendsLeft, int friendsRight) {

    return yourLeft + yourRight == friendsLeft + friendsRight && std::max(yourLeft, yourRight) == std::max(friendsLeft, friendsRight);

}



minmax 함수를 사용하면 최소값과 최대값을 검색해서 알려준다.



bool areEquallyStrong(int yourLeft, int yourRight, int friendsLeft, int friendsRight) {

    return minmax(yourLeft, yourRight) == minmax(friendsLeft, friendsRight);}


문제를 풀면서 이런 것도 알아가고 좋은 것 같다.

반응형

'알고리즘 > codefights' 카테고리의 다른 글

21>isIPv4Address  (0) 2017.07.04
20>arrayMaximalAdjacentDifference  (0) 2017.07.04
18>palindromeRearranging  (0) 2017.07.02
17>arrayChange  (0) 2017.07.01
16>areSimilar  (0) 2017.06.28