Maximize Sum Of Array After K Negations

1         heapq.heapify(A)
2         for i in range(K):
3             heapq.heapreplace(A, -A[0])
4
5         return sum(A)

最近在看python,该题现在最快的算法

 1 A.sort()
 2         bZero = False
 3         ret = 0;
 4         minNum = sys.maxsize
 5         for a in A:
 6             if a < 0:
 7                 if K > 0:
 8                     a = -a
 9                     K -= 1
10                 ret += a
11             elif a == 0:
12                 bZero = True
13             else:
14                 ret+=a
15
16             minNum = min(minNum, a)
17
18         if bZero:
19             return ret
20         else:
21             if K%2 == 1:
22                 ret -= minNum*2
23
24         return ret

自己写的,记录一下

原文地址:https://www.cnblogs.com/houtianzym/p/10556113.html

时间: 2024-10-31 15:35:22

Maximize Sum Of Array After K Negations的相关文章

[Swift Weekly Contest 127]LeetCode1005. K 次取反后最大化的数组和 | Maximize Sum Of Array After K Negations

Given an array A of integers, we must modify the array in the following way: we choose an i and replace A[i] with -A[i], and we repeat this process K times in total.  (We may choose the same index i multiple times.) Return the largest possible sum of

1005. Maximize Sum Of Array After K Negations

题目 Given an array A of integers, we must modify the array in the following way: we choose an i and replace A[i] with -A[i], and we repeat this process K times in total. (We may choose the same index i multiple times.) Return the largest possible sum

leetcode 1005 Maximize Sum Of Array After K Negations &amp; leetcode 1006 Clumsy Factorial

leetcode 1005 Sort the array first. The negation rules are quite simple: execute negation for K times,so use a for loop after negation, if the next number (if has) is smaller, the next number is next to negation (if still in for loop). Here we use a

LeetCode 1005. Maximize Sum Of Array After K Negations (K 次取反后最大化的数组和)

题目标签:Greedy 每一次都找最小的值 进行更改. 可以利用 priority queue 来实现操作. 具体看code. Java Solution: Runtime:  5 ms, faster than 33.84% Memory Usage: 38.6 MB, less than 11.76 % 完成日期:02/26/2020 关键点:priority queue class Solution { public int largestSumAfterKNegations(int[]

LeetCode OJ_题解(python):001-Two Sum 【Array】【Easy】

题目: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. 输入一个数组和target,要在一个数组中找到两个数字,其和为t

215. Kth Largest Element in an Array 第K大的数

class Solution { public: int quicksort(vector<int>& nums, int start, int end, int k){ int i = start; int j = end; int x = nums[i]; while (i<j){ //快排核心- while (nums[j]<x && i<j) j--; if (i<j) nums[i++] = nums[j]; while (nums[i

软件工程课堂练习——N层电梯只停一层求乘客爬楼层数最少(基本方法+优化方法)

题目: •石家庄铁道大学基础大楼一共有四部电梯,每层都有人上下,电梯在每层都停.信1201-1班的张一东觉得在每层都停觉得不耐烦. •由于楼层不太高,在上下课高峰期时时,电梯从一层上行,但只允许停在某一楼层.在一楼时,每个乘客选择自己的目的层,电梯则自动计算出应停的楼层. •问电梯停在那一楼层,能够保证这次乘坐电梯的所有乘客爬楼梯的层数之和最少. 一.设计思想 1.解法一 可以从第一层开始枚举一直到第N层,然后计算如果电梯在第x层停的话所有乘客总共要爬多少层楼.这是最为直接的一个解法.程序代码就

Leetcode: Max Sum of Rectangle No Larger Than K

Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k. Example: Given matrix = [ [1, 0, 1], [0, -2, 3] ] k = 2 The answer is 2. Because the sum of rectangle [[0, 1], [

[LeetCode] K sum(2Sum、3Sum、4Sum)

1 2Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please not