由买买提看人间百态

topics

全部话题 - 话题: maxdiff
(共0页)
r**h
发帖数: 1288
1
来自主题: JobHunting版 - 面试问题求教
lst = [('a',2), ('b',3), ('a',34), ('a',3), ('b',8), ('c',3), ('a',123), ('b
',6)]
def getMaxDiff(lst):
prev, maxDiff = {}, {}
for t in lst:
if t[0] not in prev.keys():
prev[t[0]] = t[1]
maxDiff[t[0]] = 0
else:
curDiff = t[1] - prev[t[0]]
if curDiff > maxDiff[t[0]]:
maxDiff[t[0]] = curDiff
prev[t[0]] = t[1]
return maxDiff

md = getMaxDiff(lst)
print(lst)
print(md)
输入:
[('a', 2), ('b', 3), (... 阅读全帖
s**********4
发帖数: 3
2
来自主题: JobHunting版 - 问一个java的函数调用问题
会写c++,只是拿leetcode一个例子来问一下java的函数调用问题:
比如说我现在创建了两个文件:Solution.java和SolutionTest.java:
Solution.java:
public class Solution {
public int maxProfit(int[] prices) {
// Start typing your Java solution below
// DO NOT write main() function
if (prices.length < 2)
return 0;
int maxDiff = 0;
int minValue = prices[0];
for (int i = 1; i < prices.length; ++i) {
if (prices[i] < minValue)
minValue = prices[i];
... 阅读全帖
g*****g
发帖数: 212
3
来自主题: JobHunting版 - 问个 matrix 的问题 (CS)
here is the code
int maxInMatrix(vector> &matrix)
{
int n = matrix.size();
int m = matrix[0].size();
vector premin(m+1, INT_MAX);
int maxdiff = 0;
for(int i=0; i {
vector curmin(m+1, INT_MAX);
for(int j=0; j {
curmin[j+1] = min(premin[j+1], curmin[j], matrix[i][j]);
int diff = matrix[i][j] - curmin[j+1];
if (diff > maxdiff)
... 阅读全帖
j*****u
发帖数: 1133
4
1. 我一开始想成根据history的stock price来做predict了,想了半天。。。
其实是这个题的变种
int array A里找两个position i和j,要求A[j]和A[i]的差maximum,同时i static void BestDay(int[] stockPrices, out int buyDate, out int sellDate)
{
buyDate = sellDate = 0;
int maxDiff = 0, minPrice = stockPrices[0], minPriceDate = 0;
for (int i = 1; i < stockPrices.Length; ++i)
{
int price = stockPrices[i];
int diff = price - minPrice;
if (diff > maxDiff)
{
buyDate = minPriceDate;
s... 阅读全帖
d*******8
发帖数: 23
5
来自主题: JobHunting版 - 请教一道算法题
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
(共0页)