由买买提看人间百态

topics

全部话题 - 话题: nmax
(共0页)
w****x
发帖数: 2483
1
/*
Say you have an array for which the ith element is the price of a given
stock on day i.
Design an algorithm to find the maximum profit. You may complete as many
transactions as you like (ie, buy one and sell one share of the stock
multiple times). However, you may not engage in multiple transactions at the
same time (ie, you must sell the stock before you buy again).
*/
class Solution {
public:
int maxProfit(vector &a) {

int n = a.size();
int* rec = new int[n]... 阅读全帖
s***e
发帖数: 911
2
来自主题: Science版 - 4-阶龙格库塔子程序
SUBROUTINE rkdumb(vstart,nvar,x1,x2,nstep,derivs)
INTEGER nstep,nvar,NMAX,NSTPMX
PARAMETER (NMAX=50,NSTPMX=200)
REAL x1,x2,vstart(nvar),xx(NSTPMX),y(NMAX,NSTPMX)
EXTERNAL derivs
COMMON /path/ xx,y
CU USES rk4
INTEGER i,k
REAL h,x,dv(NMAX),v(NMAX)
do 11 i=1,nvar
v(i)=vstart(i)
y(i,1)=v(i)
11 continue
xx(1)=x1
x=x1
h=(x2-x1)/nstep
do 13 k=1,nstep
call derivs(x,v,dv)
call rk4(v,dv,nva
w****x
发帖数: 2483
3
来自主题: JobHunting版 - longest valid Parentheses有O(n)算法么
int longestValidParentheses(const char* str) {
assert(str);
int nMax = 0;
const char* p = str;
stack stk;
while (*p != 0)
{
if (*p == '(')
stk.push(p);
else if (*p == ')')
{
if (!stk.empty() && *stk.top() == '(')
{
stk.pop();
nMax = max(p - (stk.empty() ? str-1 : stk.top()), nMax);
}
else stk.push(p);
}
p++;
}
return nMax... 阅读全帖
w****x
发帖数: 2483
4
来自主题: JobHunting版 - longest valid Parentheses有O(n)算法么
int longestValidParentheses(const char* str) {
assert(str);
int nMax = 0;
const char* p = str;
stack stk;
while (*p != 0)
{
if (*p == '(')
stk.push(p);
else if (*p == ')')
{
if (!stk.empty() && *stk.top() == '(')
{
stk.pop();
nMax = max(p - (stk.empty() ? str-1 : stk.top()), nMax);
}
else stk.push(p);
}
p++;
}
return nMax... 阅读全帖
w****x
发帖数: 2483
5
partition解法, 一点都没觉得简单:
int _inner_min_set(int a[], int n, int k)
{
if (n <= 0) return 0;
if (n == 1) return a[0] >= k ? 1 : 0;
swap(a[0], a[n/2]);
int i = 1;
int j = n-1;
while (i <= j)
{
if (a[i] < a[0])
i++;
else if (a[j] >= a[0])
j--;
else swap(a[i], a[j]);
}
swap(a[0], a[j]);
int nSum = 0;
int nLft = 0;
if (j == n-1)
{
nSum = a[j];
nLft = n-1;
}
else
{
for (... 阅读全帖
w****x
发帖数: 2483
6
来自主题: JobHunting版 - 贴一个OJ 的 longest valid parenthesis
int longestValidParentheses(string s) {
const char* str = s.c_str();

int nMax = 0;

const char* p = str;
stack stk;
while (*p != 0)
{
if (*p == '(')
stk.push(p);
else if (*p == ')')
{
if (!stk.empty() && *stk.top() == '(')
{
stk.pop();
nMax = max(p - (stk.empty() ? str-1 : stk.top()), nMax);
... 阅读全帖
w****x
发帖数: 2483
7
来自主题: JobHunting版 - 请教两个题
max rectangle做了个O(n^3)的解法, histogram那个我是不指望了
const int M = 5;
int GetMaxRect(bool A[M][M])
{
int rec[M][M];
for (int j = 0; j < M; j++)
{
for (int i = 0; i < M; i++)
{
if (A[i][j])
rec[i][j] = 0;
else
rec[i][j] = i == 0 ? 1 : 1 + rec[i-1][j];
}
}
int nMax = 0;
for (int i = M-1; i >= 0; i--)
{
for (int j = i; j >= 0; j--)
{
int nLen = 0;
int nBeg = -... 阅读全帖
x*********w
发帖数: 533
8
假设都是正数:
int GetMinSet(int a[], int n, int k)
{
if (k < 0)
{
int nMax = a[0];
for (int i = 1; i < n; i++)
nMax = max(a[i], nMax);
return nMax >= k ? 1 : 0;
}
//Get the positive part
int i = 0;
int j = n-1;
int nSumPos = 0;
while (i <= j)
{
if (a[i] < 0)
i++;
else if (a[j] >= 0)
{
nSumPos += a[j];
j--;
}
else swap(a[i], a[j]);
}
if (i >= n || n... 阅读全帖
d**f
发帖数: 264
9
来自主题: JobHunting版 - 讨论几个general question

1.This is answer to how to avoid mem leaking.
If there is mem leaking already in other peoples code, how to check?
2. if you want assign a+b to c, you need check if b < (NMAX-a).
Another thing you need pay attention to, 0 mid = i+(j-i)/2;
3.
debug tools. gdb,windbg
memory debug tools
crash dump analysis
static code analysis
Q: There is a program. Sometime it works, sometime it crash.How to find this
bug?
A: Mostly it is caused by uninitialized vars or overflow or p... 阅读全帖
b*******h
发帖数: 53
10
这个dp算法有一个地方不懂:rec[j]是在第jth day卖出股票,这时的最大收益值。
这一块: int nBase = j-1 < 0 ? 0 : rec[j-1];
if (nBase + a[i] - a[j] > nMax)
nMax = nBase + a[i] - a[j];
为什么j-1th day卖出,一定要在jth day买入呢?有可能从j th day,股票一路下跌
,然后再长回来。

the
r*c
发帖数: 167
11
来自主题: JobHunting版 - G家电面
第一题要用binary search?
试写了一下,借用二爷的原代码
#include "stdafx.h" //cause I am using VC++
#include
#include
#include
using namespace std;
class Solution{
public:
Solution(unordered_mapmap){
_map = map;
_size = _map.size();
unordered_map::iterator it = _map.begin();
while(it != _map.end())
_vec.push_back(it++->first);
}

bool Prob() //borrowed from LeetCode
{
return r... 阅读全帖
g**e
发帖数: 6127
12
来自主题: JobHunting版 - 又想起一道google题目
/**
Maintain two pointers, head and tail,
1. start sweeping the array from head if a[head] otherwise
2. for each a[i], if current value is less or equal to a[head] (or a[tail]),
skip
3. else compute area with a[i] & a[tail] (or a[head]), set start=i (or stop=
i), update max volume if necessary
4. until head meets tail, return max volume
All items are scanned at most once, total time O(n).
*/
public static int maxVolume(int[] a) {
if (a.length<=1)
return ... 阅读全帖
l******6
发帖数: 340
13
来自主题: JobHunting版 - 问个题,请大家指教
F(n , h) = abs(h(n) - h) if n == nmax
F(n , h) = abs(h - h(n)) + min (F(n + 1 , h + x)) where x = -X to X , limit
to h + x is within hmin and hmax
hmin = min(all h) hmax = max (all h)
ret = min F(0 , h)
D*********2
发帖数: 535
14
来自主题: Statistics版 - 求助:Import .sas7bdat to R
Thanks a lot!!!!!
Yes, I do have missing values in the three numeric variables, denoted as "."
, Should I correct it in SAS before importing them into R, like recoded it
is "999"? Is it the common way to do this? Because I was thinking we may
need to avoid hard coding in SAS.
Besides, I doubt it is caused by missing value, here is the error message I
got after read.table
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,
scan() expected 'a real', got '"117.200"'
Thanks
a****m
发帖数: 693
15
来自主题: Statistics版 - R 里面怎么读入.dat 文件?

这个就是有3行数字,好像每次对人都会出错:
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,
line 3 did not have 10 elements
假如是这样的:
1 2 3 4 5 65 66 66 5 2
1 3 3 3 3 4 4 4 4 4
2 3
thanks
(共0页)