|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author Openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/reverse-substrings-between-each-pair-of-parentheses "Reverse Substrings Between Each Pair of Parentheses") |
| 9 | + |
| 10 | +[Next >](https://github.com/openset/leetcode/tree/master/problems/critical-connections-in-a-network "Critical Connections in a Network") |
| 11 | + |
| 12 | +## 1191. K-Concatenation Maximum Sum (Medium) |
| 13 | + |
| 14 | +<p>Given an integer array <code>arr</code> and an integer <code>k</code>, modify the array by repeating it <code>k</code> times.</p> |
| 15 | + |
| 16 | +<p>For example, if <code>arr = [1, 2]</code> and <code>k = 3 </code>then the modified array will be <code>[1, 2, 1, 2, 1, 2]</code>.</p> |
| 17 | + |
| 18 | +<p>Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be <code>0</code> and its sum in that case is <code>0</code>.</p> |
| 19 | + |
| 20 | +<p>As the answer can be very large, return the answer <strong>modulo</strong> <code>10^9 + 7</code>.</p> |
| 21 | + |
| 22 | +<p> </p> |
| 23 | +<p><strong>Example 1:</strong></p> |
| 24 | + |
| 25 | +<pre> |
| 26 | +<strong>Input:</strong> arr = [1,2], k = 3 |
| 27 | +<strong>Output:</strong> 9 |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p><strong>Example 2:</strong></p> |
| 31 | + |
| 32 | +<pre> |
| 33 | +<strong>Input:</strong> arr = [1,-2,1], k = 5 |
| 34 | +<strong>Output:</strong> 2 |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p><strong>Example 3:</strong></p> |
| 38 | + |
| 39 | +<pre> |
| 40 | +<strong>Input:</strong> arr = [-1,-2], k = 7 |
| 41 | +<strong>Output:</strong> 0 |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | +<p><strong>Constraints:</strong></p> |
| 46 | + |
| 47 | +<ul> |
| 48 | + <li><code>1 <= arr.length <= 10^5</code></li> |
| 49 | + <li><code>1 <= k <= 10^5</code></li> |
| 50 | + <li><code>-10^4 <= arr[i] <= 10^4</code></li> |
| 51 | +</ul> |
| 52 | + |
| 53 | +### Related Topics |
| 54 | + [[Dynamic Programming](https://github.com/openset/leetcode/tree/master/tag/dynamic-programming/README.md)] |
| 55 | + |
| 56 | +### Hints |
| 57 | +<details> |
| 58 | +<summary>Hint 1</summary> |
| 59 | +How to solve the problem for k=1 ? |
| 60 | +</details> |
| 61 | + |
| 62 | +<details> |
| 63 | +<summary>Hint 2</summary> |
| 64 | +Use Kadane's algorithm for k=1. |
| 65 | +</details> |
| 66 | + |
| 67 | +<details> |
| 68 | +<summary>Hint 3</summary> |
| 69 | +What are the possible cases for the answer ? |
| 70 | +</details> |
| 71 | + |
| 72 | +<details> |
| 73 | +<summary>Hint 4</summary> |
| 74 | +The answer is the maximum between, the answer for k=1, the sum of the whole array multiplied by k, or the maximum suffix sum plus the maximum prefix sum plus (k-2) multiplied by the whole array sum for k > 1. |
| 75 | +</details> |
0 commit comments