由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教一道算法题
相关主题
问一道面试题目问道题,谁给个效率高点的解法
Google电话面试题目菜鸟问个two sum的变型题
求教一道DP的面试题,minimum adjustment cost请教个面试题
[合集] Google电话面试题目Minimum number of moves to make an integer array balance
一道面试算法题问个最近面试里的题目
算法作业求教问个 matrix 的问题 (CS)
这题怎么做?一道老题
Given an array of N integers from range [0, N] and one is missing. Find the missing number.careercup上这道题我竟然没看懂
相关话题的讨论汇总
话题: int话题: dp话题: array话题: target话题: adjustment
进入JobHunting版参与讨论
1 (共1页)
m******a
发帖数: 84
1
Give an integer array, adjust each integers so that the difference of every
adjacent integers are not greater than a given number target. If the array
before adjustment is A, the array after adjustment is B, you should minimize
the sum of abs(A[i] - B[i])
You can assume each number in the array is a positive integer and not
greater than 100
Example:
Given [1,4,2,3] and target=1, one of the solutions is [2,3,2,3], the
adjustment cost is 2 and it's minimal. Return 2.
题目如上,请问大牛们有没有好的想法?谢谢!
x********k
发帖数: 256
2
感觉像leetcode的candy问题?
t*******y
发帖数: 3
3
动态规划
m*****k
发帖数: 731
4
>>Given [1,4,2,3] and target=1, one of the solutions is >>[2,3,2,3], the
>>adjustment cost is 2 and it's minimal. Return 2.
结果为啥不是[1,2,2,3] , adjust cost = 1 ?
b******g
发帖数: 3616
5
4>2所以adjust以后也得是这个关系?
mark,回头想想

【在 m*****k 的大作中提到】
: >>Given [1,4,2,3] and target=1, one of the solutions is >>[2,3,2,3], the
: >>adjustment cost is 2 and it's minimal. Return 2.
: 结果为啥不是[1,2,2,3] , adjust cost = 1 ?

m******a
发帖数: 84
6
这个cost是2, 第二个元素由4变成了2了

【在 m*****k 的大作中提到】
: >>Given [1,4,2,3] and target=1, one of the solutions is >>[2,3,2,3], the
: >>adjustment cost is 2 and it's minimal. Return 2.
: 结果为啥不是[1,2,2,3] , adjust cost = 1 ?

m******a
发帖数: 84
7
能详细点么?

【在 t*******y 的大作中提到】
: 动态规划
m******a
发帖数: 84
8
这个没有规定adjust后还是原来的大小关系吧

【在 b******g 的大作中提到】
: 4>2所以adjust以后也得是这个关系?
: mark,回头想想

r****7
发帖数: 2282
9
复杂度太高了
应该要用到正数和小于100的特点,不过目前还没想到啥好办法

【在 t*******y 的大作中提到】
: 动态规划
i*****o
发帖数: 105
10
不是排版的变种?
相关主题
算法作业求教问道题,谁给个效率高点的解法
这题怎么做?菜鸟问个two sum的变型题
Given an array of N integers from range [0, N] and one is missing. Find the missing number.请教个面试题
进入JobHunting版参与讨论
g***s
发帖数: 3811
11
复杂度也就100N,
100的特点就是target就算是无穷大也和100一样。

★ 发自iPhone App: ChineseWeb 8.6

【在 r****7 的大作中提到】
: 复杂度太高了
: 应该要用到正数和小于100的特点,不过目前还没想到啥好办法

r****7
发帖数: 2282
12
恩,后来睡觉的时候想了想,100就是为了限制一个constant time的,否则是个NP
problem

【在 g***s 的大作中提到】
: 复杂度也就100N,
: 100的特点就是target就算是无穷大也和100一样。
:
: ★ 发自iPhone App: ChineseWeb 8.6

m******a
发帖数: 84
13
动态规划是怎么做得呢?能详细一点么?

【在 r****7 的大作中提到】
: 恩,后来睡觉的时候想了想,100就是为了限制一个constant time的,否则是个NP
: problem

s***c
发帖数: 639
14
dfs+pruning,你看成不成。数大小都在100以内,复杂度就能估计,可还是太狠

every
minimize

【在 m******a 的大作中提到】
: Give an integer array, adjust each integers so that the difference of every
: adjacent integers are not greater than a given number target. If the array
: before adjustment is A, the array after adjustment is B, you should minimize
: the sum of abs(A[i] - B[i])
: You can assume each number in the array is a positive integer and not
: greater than 100
: Example:
: Given [1,4,2,3] and target=1, one of the solutions is [2,3,2,3], the
: adjustment cost is 2 and it's minimal. Return 2.
: 题目如上,请问大牛们有没有好的想法?谢谢!

x*******9
发帖数: 138
15
略暴力,Lintcode上的题。(没看错,是Lintcode,不是Leetcode)
class Solution {
public:
/**
* @param A: An integer array.
* @param target: An integer.
*/
int MinAdjustmentCost(vector A, int target) {
if (A.empty()) {
return 0;
}
int n = A.size();
int ptr = 0;
memset(dp, 0, sizeof(dp));

for (int i = 0; i < n; i++) {
int cur = A[i];
int next = ptr ^ 1;
memset(dp[next], INF, sizeof(dp[next]));
for (int j = 1; j <= 100; j++) {
int diff = abs(cur - j);
int range_l = max(1, j - target);
int range_r = min(100, j + target);
for (int k = range_l; k <= range_r; k++) {
dp[next][j] = min(dp[next][j], dp[ptr][k] + diff);
}
}
ptr ^= 1;
}
int ans = INF;
for (int i = 1; i <= SIZE; i++) {
ans = min(ans, dp[ptr][i]);
}
return ans;
}
private:
static const int SIZE = 100;
static const int INF = 0x3f3f3f3f;
int dp[2][SIZE + 5];
};
b********r
发帖数: 620
16
my 2 cents for DP, define min as cost of between i and j, inclusive.
min(i,j)=min(i,k)+min(k,j), i i am guessing probably that's the reason why the array element has a range
limit.
by the way the solution is not unique, e.g, both 2,3,2,3 and 1,2,2,3 work
d*******8
发帖数: 23
17
Using Dynamic Programming.
The time complexity is O(maxDiff * N), where N is array size and maxDiff is
the maximum diff allowed between adjacent elements
My codes here:
http://ideone.com/btd3WU
1 (共1页)
进入JobHunting版参与讨论
相关主题
careercup上这道题我竟然没看懂一道面试算法题
算法题:两列找共同元素有O(n)的算法吗?算法作业求教
a[i] + b[j] = c[k] 的题有靠谱的答案不?这题怎么做?
amazon 电面问题 求解答, 在线等Given an array of N integers from range [0, N] and one is missing. Find the missing number.
问一道面试题目问道题,谁给个效率高点的解法
Google电话面试题目菜鸟问个two sum的变型题
求教一道DP的面试题,minimum adjustment cost请教个面试题
[合集] Google电话面试题目Minimum number of moves to make an integer array balance
相关话题的讨论汇总
话题: int话题: dp话题: array话题: target话题: adjustment