[Leetcode] 643 Maximum Average Subarray I (Python)
leetcode2025. 9. 10. 18:16[Leetcode] 643 Maximum Average Subarray I (Python)

문제Maximum Average Subarray I - LeetCode설명You are given an integer array nums consisting of n elements, and an integer k. Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10-5 will be accepted.풀이 계획배열을 n-k만큼 돌면서 모든 경우의 최대합 값을 비교하여 구한다. 마지막까지 돌고 반복문을 나와서 최대합 값을 k로 나눈 값을 리턴하면 될 것으로 생각했다. 배열의..

[Leetcode] 283 Move Zeros (Python)
leetcode2025. 9. 9. 14:28[Leetcode] 283 Move Zeros (Python)

문제https://leetcode.com/problems/move-zeroes 설명Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.Note that you must do this in-place without making a copy of the array.풀이 계획원본 배열을 수정하는 방식으로 풀어야 한다는 것이 포인트인 것 같다. nums 리스트를 돌면서 i번째 리스트에 0이 있다면 해당 인덱스부터 n-1번째 요소 까지 우측과 바꿔가면서 0이 점점 우측으로 몰리게끔 풀어보았다.초기 풀이class Solution: def moveZ..

[Leetcode] 2348 Number of Zero-Filled Subarrays (Python)
leetcode2025. 8. 20. 00:35[Leetcode] 2348 Number of Zero-Filled Subarrays (Python)

Number of Zero-Filled Subarrays - LeetCode생각리스트를 한번 돌면서 0이 이어진다면 몇번까지 이어지는지 확인하고 이어지는 그룹이 몇개인지도 파악한다.몇번까지 이어지는지만 알면 subarrays 개수는 구할 수 있다.그때그때 0이 이어지는 상태인지 확인이 필요하다.삼각수 배열을 미리 뽑아놓고 갖다 쓰면 좋지 않을까?초기코드class Solution: def zeroFilledSubarray(self, nums: List[int]) -> int: limit = 10**5 triangular_numbers = [n * (n + 1) // 2 for n in range(1, limit + 1)] arr = [] streakF..

728x90
반응형
image