1191. K-Concatenation Maximum Sum

Given an integer array arr and an integer k, modify the array by repeating it k times.

For example, if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2].

Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be 0 and its sum in that case is 0.

As the answer can be very large, return the answer modulo 10^9 + 7.

 

Example 1:

Input: arr = [1,2], k = 3
Output: 9

Example 2:

Input: arr = [1,-2,1], k = 5
Output: 2

Example 3:

Input: arr = [-1,-2], k = 7
Output: 0

 

Constraints:

  • 1 <= arr.length <= 10^5
  • 1 <= k <= 10^5
  • -10^4 <= arr[i] <= 10^4

思路:结果来源无非3种

1. subarray来源于arr_part

2. subarray来源于arr_part + arr_part

3. subarray来源于arr_part + arr * (k-2) + arr_part

class Solution(object):
    def kConcatenationMaxSum(self, arr, k):
        """
        :type arr: List[int]
        :type k: int
        :rtype: int
        """
        mod = int(1e9+7)
        if max(arr)<=0: return 0
        
        def get_max_sub(a):
            mi,su = 0,0
            ma = 0
            for i in a: 
                su+=i
                ma = max(ma, su-mi)
                mi = min(mi, su)
            return ma
        
        ma1 = get_max_sub(arr)
        ma2 = get_max_sub(arr+arr)
        su = sum(arr)
        
        if k==1: return ma1%mod
        return max([ma1, ma2, ma2+(k-2)*su])%mod
    
s=Solution()
print(s.kConcatenationMaxSum(arr = [1,2], k = 3))
print(s.kConcatenationMaxSum(arr = [1,-2,1], k = 5))
print(s.kConcatenationMaxSum(arr = [-1,-2], k = 7))

 

### TensorFlow Layers Concatenation Usage and Documentation In TensorFlow, the `tf.keras.layers.Concatenate` layer or function serves as an essential tool for combining multiple tensors into one by joining them along a specified axis. This operation is particularly useful when building complex neural network architectures where outputs from different layers need integration. The primary parameters include: - **axis**: Specifies which axis to concatenate over; default value `-1`, meaning the last dimension. - ****kwargs**: Additional keyword arguments passed through to parent constructors. A simple example demonstrating how to apply concatenation within a model structure follows below: ```python import tensorflow as tf from tensorflow import keras input1 = keras.Input(shape=(16,)) x1 = keras.layers.Dense(8)(input1) input2 = keras.Input(shape=(32,)) x2 = keras.layers.Dense(8)(input2) concatenated = keras.layers.Concatenate()([x1, x2]) # Combining two dense layers' output output = keras.layers.Dense(1)(concatenated) model = keras.Model(inputs=[input1, input2], outputs=output) ``` This code snippet creates a new tensor named `concatenated` that merges the results produced by `x1` and `x2`. By specifying no explicit argument for `axis`, this defaults to merging on the final dimensions of both inputs[^1]. For more detailed information regarding available options and advanced configurations associated with concatenating operations in TensorFlow's high-level API (`tf.keras`), refer directly to official documentation resources provided by TensorFlow team members.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值