위지원의 데이터 일기 🐈
Home
  • 분류 전체보기 (564) N
    • ✎ 2025년 (5) N
    • 2024년 (16)
    • 2023년 (6)
    • 2022년 (35)
      • Developement (22)
      • Error (9)
    • 2021년 (68)
      • ERROR (9)
      • 알고리즘 (11)
      • 개발공부 (21)
      • Data (15)
      • 21.下 (12)
    • 2020년 (164)
      • 코테 (84)
      • Development (29)
      • 정처기 (41)
    • 2019년 (27)
    • 2018년 (89)
      • English Speaking (8)
      • Error (12)
      • C, Java, FileSystem (13)
      • DataBase (15)
      • Java (2)
      • 지식 (16)
      • Go (3)
      • spark (9)
      • 영어 (5)
      • 알고리즘 (6)
    • 2017년 (143)
      • Error (17)
      • machine learning (16)
      • Spark (20)
      • Database (19)
      • Python (17)
      • Spring (9)
      • etc. (10)
      • 백준 (5)
      • Google Platform (12)
      • web Development (7)
      • Docker (3)
      • Linux (8)
Home
  • 분류 전체보기 (564) N
    • ✎ 2025년 (5) N
    • 2024년 (16)
    • 2023년 (6)
    • 2022년 (35)
      • Developement (22)
      • Error (9)
    • 2021년 (68)
      • ERROR (9)
      • 알고리즘 (11)
      • 개발공부 (21)
      • Data (15)
      • 21.下 (12)
    • 2020년 (164)
      • 코테 (84)
      • Development (29)
      • 정처기 (41)
    • 2019년 (27)
    • 2018년 (89)
      • English Speaking (8)
      • Error (12)
      • C, Java, FileSystem (13)
      • DataBase (15)
      • Java (2)
      • 지식 (16)
      • Go (3)
      • spark (9)
      • 영어 (5)
      • 알고리즘 (6)
    • 2017년 (143)
      • Error (17)
      • machine learning (16)
      • Spark (20)
      • Database (19)
      • Python (17)
      • Spring (9)
      • etc. (10)
      • 백준 (5)
      • Google Platform (12)
      • web Development (7)
      • Docker (3)
      • Linux (8)
블로그 내 검색
포트폴리오

위지원의 데이터 일기 🐈

데이터를 사랑하고 궁금해하는 기록쟁이입니다! 😉 Super Data Girl이 되는 그날까지🏃‍♀️ 화이팅!

  • 🖥 깃블로그
  • 🌍 위키원
  • 📑 내맘대로 스크랩
  • 💌 메일
  • 2020년/코테

    [코테 연습] Fair Candy Swap

    2020. 9. 24. 22:34

    by. 위지원

    leetcode.com/problems/fair-candy-swap/

     

    Fair Candy Swap - 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

    Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has, and B[j] is the size of the j-th bar of candy that Bob has.

    Since they are friends, they would like to exchange one candy bar each so that after the exchange, they both have the same total amount of candy.  (The total amount of candy a person has is the sum of the sizes of candy bars they have.)

    Return an integer array ans where ans[0] is the size of the candy bar that Alice must exchange, and ans[1] is the size of the candy bar that Bob must exchange.

    If there are multiple answers, you may return any one of them.  It is guaranteed an answer exists.


    class Solution:
        def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]:
            diff = (sum(B) - sum(A))/2
            for candy in A:
                if candy + diff in B:
                    return [candy, candy + diff] 
           
    #set을 이용해 체크할 숫자의 개수를 줄임
    
    class Solution:
        def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]:
            diff = (sum(B) - sum(A))/2
            B = set(B)
            for candy in A:
                if candy + diff in B:
                    return [candy, candy + diff] 
    저작자표시 (새창열림)

    '2020년 > 코테' 카테고리의 다른 글

    [코테 연습] find-the-difference  (0) 2020.09.24
    [코테 연습] Product of Array Except Self  (0) 2020.09.24
    [코테 연습] Valid Parentheses  (0) 2020.09.24
    [코테 연습] All Elements in Two Binary Search Trees  (0) 2020.09.16
    [코테 연습] Sort the Matrix Diagonally  (0) 2020.09.16

    잠깐만요~! 읽으신김에 이런 글들은 어떠세요? 👀

    • [코테 연습] find-the-difference 2020.09.24
    • [코테 연습] Product of Array Except Self 2020.09.24
    • [코테 연습] Valid Parentheses 2020.09.24
    • [코테 연습] All Elements in Two Binary Search Trees 2020.09.16
    맨 위로
전체 글 보기
Tistory 로그인
Tistory 로그아웃
로그아웃 글쓰기 관리

열정! 열정! 열정! 🔥

Designed by Nana
블로그 이미지
위지원
데이터와 관련된 일을 모두 좋아합니다

티스토리툴바

티스토리툴바