Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to maximize the sum of arrays after K-times inversion by greedy algorithm

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to maximize the array sum after K times inversion of greedy algorithm". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to maximize the array sum after K-times inversion of the greedy algorithm.

Many recording friends have feedback on yesterday's topic: greedy algorithm: jumping game II is very difficult, so I am relieved, , because when I just explained greed, some recording friends will advise me: greed is not necessary to speak alone, just talk about moving rules. Many students should feel greedy. What's so hard about it? Now we can find that although the truth of greed is simple, it is very clever to solve problems, and there is no difference in difficulty.

Today is a simple problem, the key is to cultivate greedy problem-solving ideas!

Given an integer array A, we can only modify the array by selecting an index I and replacing A [I] with-A [I], and then repeat the process K times in total. (we can select the same index I multiple times.)

After modifying the array in this way, the largest possible sum of the array is returned.

Example 1: input: a = [4jin2jin3], K = 1

Output: 5

Explanation: select the index (1,), and then A becomes [4mai Yu 2jue 3].

Example 2:

Input: a = [3mai Mei 1JI 0JI 2], K = 3

Output: 6

Explanation: select the index (1, 2, 2), and then A becomes [3meme1p0Pol 2].

Example 3:

Input: a = [2, 5, 5, 4], K = 2

Output: 13

Explanation: select the index (1, 4), and then A becomes [2, 3, 4, 1, 5, 5, 4].

Tip:

one

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report