博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[CareerCup][Google Interview] Find kth number in a BST
阅读量:4312 次
发布时间:2019-06-06

本文共 952 字,大约阅读时间需要 3 分钟。

Given n elements, sort the elements. Here, only one operation is permitted decreaseValue..

Note that you cannot swap the values.. output should be a sorted list..
if input is 4 5 2 1 3
output is 3 3 3.. There can be many answers.. Give the optimum solution with minimum cost. where as cost is the sum of decreased amounts..
for this example, 4,5 are decreased by 1.. 2 is decreased by 2.. 1 is decreased by 1..

 

一开始也想到用DP,但感觉DP需要一个子问题独立,但我似乎怎么看都找不到子问题独立。

最后一位高人给出了神解答

min_cost[a_i+1] = if a_i+1 >= a_i then min_cost[a_i]

else min( min_cost[a_i] + a_i+1, min_cost[a_i] + Summation[(j <- 1..i) (a_j - a_i)])

where a_i, a_i+1 are the i_th and i+1_th element in the considered array.

Can u write a recursive program for this and check? It's 5 in the morning and am not sure of the approach 

关键就在于这个summation,点睛之笔。处理了不想删掉当前数,怎么样去前向处理前面的数。当前面的数在上一个子问题中被删去的时候,这种处理可以完美的解决掉。

所以也就形成了子问题独立,因此可以用DP

转载于:https://www.cnblogs.com/chkkch/archive/2012/11/09/2763317.html

你可能感兴趣的文章
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>
C++——string类和标准模板库
查看>>
zt C++ list 类学习笔记
查看>>
git常用命令
查看>>
探讨和比较Java和_NET的序列化_Serialization_框架
查看>>
1、jQuery概述
查看>>
数组比较大小的几种方法及math是方法
查看>>
FTP站点建立 普通电脑版&&服务器版
查看>>
js 给一段代码,给出运行后的最终结果的一些综合情况、
查看>>
webservice 详解
查看>>
js自动补全实例
查看>>
VS无法启动调试:“生成下面的模块时,启用了优化或没有调试信息“
查看>>
npm 安装 sass=-=-=
查看>>
WINFORM中加入WPF控件并绑定数据源实现跨线程自动更新
查看>>
C#类对象的事件定义
查看>>
各类程序员学习路线图
查看>>
HDU 5510 Bazinga KMP
查看>>
关于select @@IDENTITY的初识
查看>>
ASP.NET MVC ajax提交 防止CSRF攻击
查看>>