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

leetcode 1005

Sort the array first.

The negation rules are quite simple:

  1. execute negation for K times,so use a for loop
  2. 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 greedy strategy. If the next number is non-negative and smaller than the current one, negation it will result in less sum loss(for example current is 4, and next is 1), if the next number is negative and smaller than the current one, negation it will result in more sum (for example current is 4 and next is -3).

After that , compute the sum.

class Solution {
    public int largestSumAfterKNegations(int[] A, int K) {
        Arrays.sort(A);
        int idx = 0;
        for (int i = 0; i < K; ++i) {
            A[idx] = -A[idx];
            if (idx + 1 < A.length) {
                if (A[idx + 1] < A[idx]) idx += 1;
            }
        }
        int sum = 0;
        for (int i = 0; i < A.length; ++i) sum += A[i];
        return sum;
    }
}


leetcode 1006

Use brute-force way. Code seems quite ugly but works fine. The complexity is O(n)

    public int clumsy(int N) {
        int ret = 0;
        int flag = 1;
        for (;N > 0;) {
            if (N == 1) {
                ret += (flag * N);
                break;
            }
            else if (N == 2) {
                ret += (flag * N * (N - 1));
                break;
            }
            else if (N == 3) {
                ret += flag * ((N * (N - 1)) / (N - 2));
                break;
            }
            else {
                ret = ret + flag * ((N * (N - 1)) / (N - 2)) + N - 3;
                flag = -1;
                N -= 4;
            }
        }
        return ret;
    }

Also I saw an O(n) solution with the fact that for n >= 5, (n + 2) > (n * (n - 1)) / (n - 2) > (n + 1).

so we get (n * (n - 1)) / (n -2) = n + 1 here.

therefore for n = 1, result = 1

for n = 2, result = 2

for n = 3, result = 6

for n = 4, result = 7,

for n >= 5, for (n - 1) % 4 == 0, result is 5 * 4 / 3 + 2 - 1 or n * (n - 1)/ (n - 2) + … + (8 * 7 / 6) - 5 * 4/ 3 + 2 - 1 = (n + 1) + 2 - 1 = n + 2

if (n - 1) % 4 == 1, result is 6 * 5 / 4 + 3 - 2 * 1 = (n + 1) + 1 = n + 2

if (n - 1) % 4 == 2 , result = 7 * 6 / 5 + 4 - (3 * 2) = n + 1 - 2 = n -1;

if (n - 1) % 4 == 3, result = 8 * 7 / 6 + 5 - 4 * 3 / 2 + 1 = n + 1

the code is as below, although I may not spend much time figuring out the formula

class Solution {
    public int clumsy(int N) {
        int[] ret = {1, 2, 6, 7};
        if (N < 5) return ret[N - 1];
        if ((N - 1) % 4 == 0) return N + 2;
        else if ((N - 1) % 4 == 1) return N + 2;
        else if ((N - 1) % 4 == 2) return N - 1;
        else return N + 1;
    }
}

原文地址:https://www.cnblogs.com/exhausttolive/p/10527347.html

时间: 2024-08-30 16:36:55

leetcode 1005 Maximize Sum Of Array After K Negations & leetcode 1006 Clumsy Factorial的相关文章

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[]

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

[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

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 =

[array] leetcode - 39. Combination Sum - Medium

leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from

[array] leetcode - 40. Combination Sum II - Medium

leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combinat

[LeetCode] 689. Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和

In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be of size k, and we want to maximize the sum of all 3*k entries. Return the result as a list of indices representing the starting p

LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays

原题链接在这里:https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/ 题目: In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be of size k, and we want to maximize the sum

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1