为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode985. 查询后的偶数和 | Sum of Even Numbers After Queries

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10351683.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

We have an array A of integers, and an array queries of queries.

For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index].  Then, the answer to the i-th query is the sum of the even values of A.

(Here, the given index = queries[i][1] is a 0-based index, and each query permanently modifies the array A.)

Return the answer to all queries.  Your answer array should have answer[i] as the answer to the i-th query.

Example 1:

Input: A = [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 A[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.
After adding -3 to A[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.
After adding -4 to A[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.
After adding 2 to A[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.

Note:

  1. 1 <= A.length <= 10000
  2. -10000 <= A[i] <= 10000
  3. 1 <= queries.length <= 10000
  4. -10000 <= queries[i][0] <= 10000
  5. 0 <= queries[i][1] < A.length

给出一个整数数组 A 和一个查询数组 queries

对于第 i 次查询,有 val = queries[i][0], index = queries[i][1],我们会把 val 加到 A[index] 上。然后,第 i 次查询的答案是 A 中偶数值的和。

(此处给定的 index = queries[i][1] 是从 0 开始的索引,每次查询都会永久修改数组 A。)

返回所有查询的答案。你的答案应当以数组 answer 给出,answer[i] 为第 i 次查询的答案。

示例:

输入:A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
输出:[8,6,2,4]
解释:
开始时,数组为 [1,2,3,4]。
将 1 加到 A[0] 上之后,数组为 [2,2,3,4],偶数值之和为 2 + 2 + 4 = 8。
将 -3 加到 A[1] 上之后,数组为 [2,-1,3,4],偶数值之和为 2 + 4 = 6。
将 -4 加到 A[0] 上之后,数组为 [-2,-1,3,4],偶数值之和为 -2 + 4 = 2。
将 2 加到 A[3] 上之后,数组为 [-2,-1,3,6],偶数值之和为 -2 + 6 = 4。

提示:

  1. 1 <= A.length <= 10000
  2. -10000 <= A[i] <= 10000
  3. 1 <= queries.length <= 10000
  4. -10000 <= queries[i][0] <= 10000
  5. 0 <= queries[i][1] < A.length

 740ms

 1 class Solution {
 2     func sumEvenAfterQueries(_ A: [Int], _ queries: [[Int]]) -> [Int] {
 3         var result = [Int]()
 4         var a = A
 5         var sum = sumOfEven(a)
 6         
 7         for i in 0..<queries.count {
 8             let queryIndex = queries[i][1]
 9             let queryValue = queries[i][0]
10             
11             let aPrev = a[queryIndex]
12             a[queryIndex] += queryValue
13 
14             if isEven(aPrev) {
15                 sum -= aPrev
16             }
17             if isEven(a[queryIndex]) {
18                 sum += a[queryIndex]
19             }
20             result.append(sum)
21         }
22         
23         return result
24     }
25     
26     func sumOfEven(_ nums: [Int]) -> Int {
27         var result = 0
28         for n in nums {
29             if isEven(n) {
30                 result += n
31             }
32         }
33         return result
34     }
35     
36     func isEven(_ n: Int) -> Bool {
37         return n % 2 == 0
38     }
39 }

748ms

 1 final class Solution {
 2     func sumEvenAfterQueries(_ A: [Int], _ queries: [[Int]]) -> [Int] {
 3         var state = A
 4         var sum = state.reduce(0) {
 5             if $1.isEvent {
 6                 return $0 + $1
 7             } else {
 8                 return $0
 9             }
10         }
11         var result = [Int]()
12         
13         for q in queries {
14             let prevVal = state[q[1]]
15             state[q[1]] += q[0]
16             if prevVal.isEvent {
17                 sum -= prevVal
18             }
19             if state[q[1]].isEvent {
20                 sum += state[q[1]]
21             }
22             result.append(sum)
23         }
24         
25         return result
26     }
27 }
28 
29 extension Int {
30     var isEvent: Bool {
31         return self % 2 == 0
32     }
33 }

752 ms

 1 class Solution {
 2     func sumEvenAfterQueries(_ A: [Int], _ queries: [[Int]]) -> [Int] {
 3         var A = A
 4         var s:Int = 0
 5         for v in A
 6         {
 7             if v % 2 == 0
 8             {
 9                 s += v           
10             }
11         }
12         var q:Int = queries.count
13         var ret:[Int] = [Int](repeating:0,count:q)
14         for i in 0..<q
15         {
16             var d:Int = queries[i][0]
17             var pos:Int = queries[i][1]
18             if A[pos] % 2 == 0
19             {
20                 s -= A[pos]
21             }
22             A[pos] += d
23             if A[pos] % 2 == 0
24             {
25                 s += A[pos]
26             }
27             ret[i] = s
28         }
29         return ret
30     }
31 }

756ms

 1 class Solution {
 2     func sumEvenAfterQueries(_ A: [Int], _ queries: [[Int]]) -> [Int] {
 3     guard A.count > 0 else {
 4         return []
 5     }
 6     var A = A
 7     // find out the original sum
 8     var sum: Int = 0
 9     for num in A where num % 2 == 0 {
10         sum += num
11     }
12     var result: [Int] = []
13     
14     for query in queries {
15         let queryIndex = query[1]
16         let queryNum = query[0]
17         
18         let currentNum = A[queryIndex]
19         let resultNum = currentNum + queryNum
20         if currentNum % 2 == 0 {
21             if resultNum % 2 == 0 {
22                 sum += queryNum
23             } else {
24                 sum -= currentNum
25             }
26         } else {
27             if resultNum % 2 == 0 {
28                 sum += resultNum
29             } else {
30                 // we dont do anything
31             }
32         }
33         
34         A[queryIndex] = resultNum
35         result.append(sum)
36     }
37     return result
38  }
39 }

768ms

 1 class Solution {
 2     func sumEvenAfterQueries(_ A: [Int], _ queries: [[Int]]) -> [Int] {
 3         var sum = 0
 4         for a in A where a % 2 == 0 {
 5             sum += a
 6         }
 7         var A = A
 8         var result = [Int]()
 9         for query in queries {
10             if A[query[1]] % 2 == 0 && query[0] % 2 == 0 {
11                 sum += query[0]
12             } else if A[query[1]] % 2 != 0 && query[0] % 2 != 0 {
13                 sum += A[query[1]] + query[0]
14             } else if A[query[1]] % 2 == 0 && query[0] % 2 != 0 {
15                 sum -= A[query[1]]
16             }
17             result.append(sum)
18             A[query[1]] += query[0]
19         }
20         return result
21     }
22 }

772ms

 1 class Solution {
 2     func sumEvenAfterQueries(_ A: [Int], _ queries: [[Int]]) -> [Int] {
 3         var arr = A
 4         var evemSum = A.reduce(0, { (res: Int, a: Int) -> Int in
 5             res + (a & 1 == 0 ? a : 0)
 6         })
 7         
 8         var resArr = [Int](repeating: 0, count: queries.count)
 9         for (i, q) in queries.enumerated() {
10             if arr[q[1]] & 1 == 0 {
11                 evemSum -= arr[q[1]]
12             }
13             arr[q[1]] += q[0]
14             if arr[q[1]] & 1 == 0 {
15                 evemSum += arr[q[1]]
16             }
17             resArr[i] = evemSum
18         }
19         
20         return resArr
21     }
22 }

776ms

 1 class Solution {
 2     func sumEvenAfterQueries(_ A: [Int], _ queries: [[Int]]) -> [Int] {
 3         var result = [Int]()
 4         var A = A
 5         
 6         var sum = A.reduce(0, { x, y in 
 7             if y % 2 == 0 {
 8                 return x + y
 9             }
10             return x
11         })
12         
13         queries.forEach { query in 
14             let val = query[0]
15             let index = query[1]
16             let prev = A[index]
17             
18             A[index] += val
19                          
20             if prev % 2 == 0 && A[index] % 2 == 0 {
21                 sum += val
22             } else if A[index] % 2 == 0 {
23                 sum += A[index]
24             } else if prev % 2 == 0 {
25                 sum -= prev
26             }
27             result.append(sum)
28         }
29         
30         return result
31     }
32 }

808ms

 1 class Solution {
 2     func sumEvenAfterQueries(_ A: [Int], _ queries: [[Int]]) -> [Int] {
 3     var sum: [Int] = []
 4     var a = A
 5     a[queries[0][1]] += queries[0][0]
 6     sum.append(a.reduce(0) { result, value in
 7         return value%2 == 0 ? (result + value) : result
 8     })
 9     
10     for i in 1..<queries.count {
11         let lastNumber = a[queries[i][1]]
12         var partialSum = sum[i-1]
13         let lastNumberWasEven = a[queries[i][1]] % 2 == 0
14 
15         a[queries[i][1]] += queries[i][0]
16 
17         let currentNumberIsEven = a[queries[i][1]] % 2 == 0
18 
19         if !lastNumberWasEven && currentNumberIsEven {
20             partialSum = sum[i-1] + a[queries[i][1]]
21         } else if lastNumberWasEven && !currentNumberIsEven {
22             partialSum = sum[i-1] - lastNumber
23         } else if lastNumberWasEven && currentNumberIsEven {
24             partialSum = sum[i-1] + queries[i][0]
25         }
26 
27         sum.append(partialSum)
28     }
29         
30     return sum
31   }
32 }

860ms

 1 class Solution {
 2     func sumEvenAfterQueries(_ A: [Int], _ queries: [[Int]]) -> [Int] {
 3         var result = [Int]()
 4         var sum = 0
 5         for (i, n) in A.enumerated(){
 6             if n % 2 == 0{
 7                 sum += n
 8             }
 9         }
10         
11         var aCopy = A
12         for (i,q) in queries.enumerated(){
13             let index = q[1]
14             let value = q[0]
15             let valueInA = aCopy[index]
16             let newValue = value + valueInA
17             if newValue % 2 == 0{
18                 sum += value
19                 if abs(valueInA % 2) == 1{
20                     sum += valueInA
21                 }
22             }else{
23                 if valueInA % 2 == 0{
24                     sum -= valueInA
25                 }
26             }
27             result.append(sum)
28             aCopy[index] = newValue
29         }
30         return result
31     }
32 }

 

posted @ 2019-02-04 13:19  为敢技术  阅读(291)  评论(0编辑  收藏  举报