SeangG
def _(): _()
SeangG
전체 방문자
오늘
어제
  • 분류 전체보기 (37)
    • Programming Language (0)
      • Python (0)
      • Web (0)
    • Algorithm (34)
    • Art (0)
      • 3D Modeling (0)
      • Pixel (0)
      • Picture (0)
    • Game (0)
    • Project (3)
      • Problems (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Daily Question
  • BFS
  • 문자열
  • github oauth
  • Tree
  • react.js
  • Fast Refresh
  • Queue
  • graph
  • 매핑 테이블
  • dfs
  • next.js
  • Python
  • leetcode
  • 로마 숫자
  • Leecode
  • string
  • spring boot
  • LeedCode
  • WSL

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
SeangG

def _(): _()

Algorithm

[LeetCode] 950. Reveal Cards In Increasing Order (Daily Question)

2024. 4. 10. 10:20

문제



You are given an integer array deck. There is a deck of cards where every card has a unique integer. The integer on the ith card is deck[i].

You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.

You will do the following steps repeatedly until all cards are revealed:
    - Take the top card of the deck, reveal it, and take it out of the deck.
    - If there are still cards in the deck then put the next top card of the deck at the bottom of the deck.
    - If there are still unrevealed cards, go back to step 1. Otherwise, stop.

Return an ordering of the deck that would reveal the cards in increasing order.

Note that the first entry in the answer is considered to be the top of the deck.

카드 덱이 주어질 때, 다음 조건을 만족하는 순서의 덱을 반환해야 한다.

 

1. 맨 위의 카드를 제거하고 다음 카드를 맨 아래로 보낸다.

2. 카드가 남아있다면 계속 반복한다.

3. 오름차순으로 제거돼야 한다.

 


Example 1:
Input: deck = [17,13,11,2,3,5,7]
Output: [2,13,3,11,5,17,7]
Explanation: We get the deck in the order [17,13,11,2,3,5,7] (this order does not matter), and reorder it.
After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].
We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].
We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].
We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].
We reveal 11, and move 17 to the bottom. The deck is now [13,17].
We reveal 13, and move 17 to the bottom. The deck is now [17].
We reveal 17.
Since all the cards revealed are in increasing order, the answer is correct.

Example 2:
Input: deck = [1,1000]
Output: [1,1000]

 

풀이


아이디어

조건의 단계를 거꾸로 수행하면 조건에 만족하는 덱을 만들 수 있을 것이다.

 

1. 비어있는 덱(덱1) 생성

2. 주어진 덱(덱2)을 정렬

3. 덱1 맨 밑의 카드를 맨 앞으로 올림

4. 덱2 가장 높은 카드를 덱1 맨 앞에 놓음

5. 덱2가 비어있을 때까지 3 - 4 반복

 

예를 들면 [1, 2, 3]이 주어졌을 때 다음의 과정을 거친다.

 

1. 덱1 : []
2. 덱2 : [3, 2, 1] or [1, 2, 3]

3.1. 덱1 : []
4.1. 덱1 : [3], 덱2 : [2, 1]


3.1. 덱1 : [3]
4.1. 덱1 : [2, 3], 덱2 : [1]


3.1. 덱1 : [3, 2]
4.1. 덱1 : [1, 3, 2], 덱2 : []

덱1 : [1, 3, 2] 반환

 

코드

class Solution:
    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
        deck.sort()
        answer = [deck.pop()]
        while deck:
            answer.insert(0, answer.pop())
            answer.insert(0, deck.pop())
        return answer

 

후기


난이도

문제이해를 잘하고 아이디어를 잘 떠올린다면 쉽게 풀 수 있는 문제이다.

예외사항이 없어서 까다롭지는 않다.

 

다른 풀이

deque를 이용해서 answer.insert(0, value) 대신에 answer.appendleft()를 사용할 수 있다.

'Algorithm' 카테고리의 다른 글

[LeetCode] 42. Trapping Rain Water (Daily Question)  (0) 2024.04.12
[LeetCode] 402. Remove K Digits (Daily Question)  (0) 2024.04.11
[LeetCode] 2073. Time Needed to Buy Tickets (Daily Question)  (1) 2024.04.08
[LeetCode] 1700. Number of Students Unable to Eat Lunch (Daily Question)  (0) 2024.04.08
[LeetCode] 678. Valid Parenthesis String (Daily Question)  (0) 2024.04.07
    'Algorithm' 카테고리의 다른 글
    • [LeetCode] 42. Trapping Rain Water (Daily Question)
    • [LeetCode] 402. Remove K Digits (Daily Question)
    • [LeetCode] 2073. Time Needed to Buy Tickets (Daily Question)
    • [LeetCode] 1700. Number of Students Unable to Eat Lunch (Daily Question)
    SeangG
    SeangG

    티스토리툴바