일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- coding
- lv4
- mysql
- 코테
- itertools
- 데이터분석
- 파이썬
- 코딩
- 시간복잡도
- join
- 프로그래머스
- CodingTest
- coding test
- Stack
- BFS
- level4
- 조합
- Level2
- collections
- Queue
- import re
- sql
- counter
- 코딩테스트
- 연습문제
- python
- time complexity
- programmers
- lambda
- 완전탐색
- Today
- Total
ror_coding
[Programmers] 체육복 - 42862 본문
728x90
같은 의미로 보이지만 코드 위치에 따라 결과가 달라지는 경우이다.
Question
전체 학생의 수 n, 체육복을 도난당한 학생들의 번호가 담긴 배열 lost, 여벌의 체육복을 가져온 학생들의 번호가 담긴 배열 reserve가 매개변수로 주어질 때, 체육수업을 들을 수 있는 학생의 최댓값을 return 하도록 solution 함수를 작성해주세요.
Code
아래 주석처럼 체육복을 도난 당한 자기 자신을 여분(reserve)을 이용해 1로 되돌리는 코드를 따로 for문을 이용해 만들게 되면 테스트 케이스 몇 군데 에서 틀린 답이 나오게 된다.
따라서 체육복 도난과 동시에 여분이 있는 것은 0으로 만들지 않고 상쇄시키는 코드를 짜야한다.
def solution(n, lost, reserve):
students = [1]*(n+2)
for i in lost :
if i in reserve : reserve.remove(i);continue
students[i] = 0
# for i in reserve :
# if students[i] == 0 : students[i] = 1 ;reserve.remove(i)
for i in sorted(reserve) :
if students[i-1] == 0 : students[i-1] = 1
elif students[i+1] == 0 : students[i+1] = 1
return sum(students)-2
![](https://t1.daumcdn.net/keditor/emoticon/face/large/012.png)
now me
On my github
728x90
'Algorithm > Python' 카테고리의 다른 글
[Programmers] 공원 산책 - 172928 (3) | 2024.10.23 |
---|---|
[Programmers] 숫자 짝꿍 - 131128 (0) | 2024.10.22 |
[Programmers] 둘만의 암호 - 155652 (0) | 2024.10.14 |
[Programmers] 대충 만든 자판 - 160586 (0) | 2024.10.14 |
[Programmers] [1차] 다트 게임 - 17682 (1) | 2024.10.13 |