有一排商品,每一个商品都有自己的价值,现在需要花一定金额购买这些商品。规则是:如果一个商品的价值比它旁边的一个商品要高,那么这个商品就必须比它旁边的商品花费更多金额。所有的商品至少要进行一次金额购买。假设一次购买花费金额单位为1,最少需要多少金额可以购买所有商品?
现给定一个数组,数组元素表示每个商品的价值。请编写代码输出最少需要花费的金额。
有一排商品,每一个商品都有自己的价值,现在需要花一定金额购买这些商品。规则是:如果一个商品的价值比它旁边的一个商品要高,那么这个商品就必须比它旁边的商品花费更多金额。所有的商品至少要进行一次金额购买。假设一次购买花费金额单位为1,最少需要多少金额可以购买所有商品?
现给定一个数组,数组元素表示每个商品的价值。请编写代码输出最少需要花费的金额。
[1,0,2]
5
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型一维数组 * @return int整型 */ public int cost (int[] array) { // write code here int n = array.length; int[] cost = new int[n]; Arrays.fill(cost, 1); for(int i = 1; i < n; i++){ if(array[i] > array[i - 1] && cost[i] <= cost[i - 1]) cost[i] = cost[i - 1] + 1; } for(int i = n - 2; i >= 0; i--){ if(array[i] > array[i + 1] && cost[i] <= cost[i + 1]) cost[i] = cost[i + 1] + 1; } int res = 0; for(int i = 0; i < n; i++) res += cost[i]; return res; } }
int cost(vector<int>& array) { int len = array.size(); vector<int> l(len, 1); vector<int> r(len, 1); for(int i = 1; i < len; i ++) if(array[i] > array[i - 1]) l[i] = l[i - 1] + 1; for(int i = len - 2; i >= 0; i --) if(array[i] > array[i + 1]) r[i] = r[i + 1] + 1; int res = 0; for(int i = 0; i < len; i ++) res += max(l[i], r[i]); return res; }
class Solution: def cost(self, array): # write code here length = len(array) cost_list = [1] * length for i in range(1, length): if array[i] > array[i - 1] and cost_list[i] <= cost_list[i - 1]: cost_list[i] = cost_list[i - 1] + 1 for i in range(length - 2, -1, -1): if array[i] > array[i + 1] and cost_list[i] <= cost_list[i + 1]: cost_list[i] = cost_list[i + 1] + 1 # print(array) # print(cost_list) cost = sum(cost_list) return cost
class Solution: def cost(self , array ): cost = [1] * len(array) for i in range(1, len(array) ): if array[i]>array[i-1]: cost[i]=cost[i-1]+1 if array[0]>array[1]: cost[0]=cost[1]+1 return sum(cost)
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型一维数组 * @return int整型 */ public int cost (int[] array) { // write code here int count=0; int meansure=0; int min=0x3f3f3f3f; int len=array.length; for(int i=1;i<len;i++) { if(array[i]>array[i-1]) meansure++; else if(array[i]<array[i-1]) meansure--; count+=meansure; min=Math.min(min,meansure); } if(min<0) count+=Math.abs(min)*len; count+=len; return count; } }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param array int整型一维数组 # @return int整型 # class Solution: def cost(self , array ): # write code here dp = [1]*len(array) for i in range(1,len(array)): if array[i]>array[i-1]: dp[i] = dp[i-1]+1 elif array[i]==array[i-1]: dp[i] = dp[i-1] else: dp[i] = dp[i-1]-1 min_v = min(dp) add_v = 1-min_v res = sum(dp)+add_v*len(dp) return res
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型vector * @return int整型 */ int cost(vector<int>& array) { // write code here int n = array.size(); vector<int> l2r(n), r2l(n); l2r[0] = 1, r2l[n - 1] = 1; int count = 1; for (int i = 1; i < n; ++i) { if (array[i] > array[i - 1]) count++; else count = 1; l2r[i] = count; } count = 1; for (int i = n - 2; i >= 0; --i) { if (array[i] > array[i + 1]) count++; else count = 1; r2l[i] = count; } int ans = 0; for (int i = 0; i < n; ++i) { ans += max(l2r[i], r2l[i]); } return ans; } };
public int cost (List<int> array) { int[] cost = new int[array.Count]; int offset = 0; cost[0] = 1; for (int i = 1; i < array.Count; i++) { if (array[i] > array[i - 1])//右边大 { cost[i] = cost[i - 1] + 1; } else if(array[i] == array[i - 1]) { cost[i] = cost[i - 1]; } else//右边小 { cost[i] = cost[i - 1] - 1; if ((cost[i] + offset) == 0) { offset++; } } } int sum = 0; for (int i = 0; i < cost.Length; i++) { sum += cost[i]; } sum += cost.Length * offset; return sum; }