saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 온라인저지
  • BFS
  • LeetCode
  • 취업준비
  • 리트코드
  • 딥러닝
  • 문제해결능력
  • 백준
  • 개발자
  • 취준
  • 딕셔너리
  • two pointer
  • Python
  • 파이썬
  • DFS
  • c++
  • 개발자 취업준비
  • 알고리즘
  • 알고리즘문제해결
  • 릿코드

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[LeetCode] 985. Sum of Even Numbers After Queries

2022. 9. 27. 02:51

985. Sum of Even Numbers After Queries

Medium

You are given an integer array nums and an array queries where queries[i] = [vali, indexi].

For each query i, first, apply nums[indexi] = nums[indexi] + vali, then print the sum of the even values of nums.

Return an integer array answer where answer[i] is the answer to the ith query.

 

Example 1:

Input: nums = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
Output: [8,6,2,4]
Explanation: At the beginning, the array is [1,2,3,4].
After adding 1 to nums[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.
After adding -3 to nums[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.
After adding -4 to nums[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.
After adding 2 to nums[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.

Example 2:

Input: nums = [1], queries = [[4,0]]
Output: [0]

 

Constraints:

  • 1 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • 1 <= queries.length <= 104
  • -104 <= vali <= 104
  • 0 <= indexi < nums.length

문제 풀이

  1. 더해야 하는 값, 더해야 할 자리 인덱스가 쿼리로 주어진다.
  2. 최악의경우 쿼리의 갯수는 10^4, 숫자의 리스트는 10^4 개가 있을 수 있다. 
  3. 제한 사항이 N^2 알고리즘까지 사용할 수 있을 줄 알았으나, 시간초과가 난다.
  4. 숫자 리스트와 쿼리리스트를 매번 돌리면서 계산하지않고, 미리 숫자들의 합을 계산한다.
  5. 그리고 쿼리를 사용할때, 그 숫자 위치의 숫자가 짝수라면 빼주고, 쿼리의 숫자를 더해준다.
  6. 그 자리에 더하기 연산을 한 후에도 짝수라면 토탈 값에 더해준다.
  7. 그리고 결과 리스트에 삽입한다.

소스 코드

class Solution:
    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:
        total = sum(x for x in nums if x % 2 == 0)
        ans = []
        for num, idx in queries:
            if nums[idx] % 2 == 0:
                total -= nums[idx]
            nums[idx] += num
            if nums[idx] % 2 == 0:
                total += nums[idx]
            ans.append(total)
        return ans
저작자표시 (새창열림)

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode] 113. Path Sum II  (0) 2022.09.28
[LeetCode] 838. Push Dominoes  (0) 2022.09.28
[LeetCode] 298. Binary Tree Longest Consecutive Sequence  (0) 2022.09.27
[LeetCode] 990. Satisfiability of Equality Equations  (0) 2022.09.26
[LeetCode] 159. Longest Substring with At Most Two Distinct Characters  (1) 2022.09.24
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 113. Path Sum II
    • [LeetCode] 838. Push Dominoes
    • [LeetCode] 298. Binary Tree Longest Consecutive Sequence
    • [LeetCode] 990. Satisfiability of Equality Equations
    saurus2
    saurus2
    Simple is Best

    티스토리툴바