-
99클럽 코테 스터디 7일차 TIL: 배열, 1282. Group the People Given the Group Size They Belong ToToday_I_Learned/Algorithm 2024. 6. 17. 00:06
문제
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/description/
통과 여부
Pass
나의 접근법
- 2차원 List 인 tmp 를 생성한다.
- groupSizes 를 순회하며 'groupSizes 의 값 -> tmp 의 Index 가 되는 요소(List)에 groupSizes 의 인덱스 번호를 추가' 를 반복한다.
- tmp 를 순회하며 각 요소 배열을 인덱스 번호의 길이로 나눈다.
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: answer = [] tmp = [[] for _ in range(len(groupSizes)+1)] for i, size in enumerate(groupSizes): tmp[size].append(i) # groupSizes 의 값 -> tmp 의 Index, groupSizes 의 인덱스 번호 -> tmp 요소 배열에 추가 for j, group in enumerate(tmp): if j == 0 or len(group) == 0: continue if len(group) > j: k = j while k <= len(group): answer.append(group[k-j:k]) # tmp 를 순회하며 각 요소 배열을 인덱스 번호의 길이로 나눈다. k += j else: answer.append(group) return answer
'Today_I_Learned > Algorithm' 카테고리의 다른 글
99클럽 코테 스터디 9일차 TIL: 배열, 1529. Minimum Suffix Flips (0) 2024.06.19 99클럽 코테 스터디 8일차 TIL: 배열, 1286. Iterator for Combination (0) 2024.06.18 99클럽 코테 스터디 6일차 TIL: 배열, LeetCode 2433. Find The Original Array of Prefix Xor (0) 2024.06.16 99클럽 코테 스터디 5일차 TIL: 배열, Leetcode 1476. Subrectangle Queries (0) 2024.06.15 99클럽 코테 스터디 4일차 TIL : Graph, 프로그래머스, 순위 (0) 2024.06.13