您的位置:首页 > 职场人生

面试笔试杂项积累-leetcode 121-125

2016-02-05 23:50 507 查看

121.121-Best Time to Buy and Sell Stock-Difficulty: Medium

Say you have an array for which the ith element is the price of a given stock on day
i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

思路

股票相关的,大概意思是这一个数组是每一天的行情,要做到低如高抛才能赚,求最大的盈利。

依旧动态规划

开始没看懂题意,后来上网查了一下懂了= =,,,接下来两道题都是相同背景

参考:
http://www.2cto.com/kf/201504/389181.html
public class Solution {
public int MaxProfit(int[] prices) {
if (prices.Length == 0)
return 0;
int minPrice = prices[0];
int max = 0;
for (int i = 1; i < prices.Length; i++)
{
minPrice = minPrice > prices[i] ? prices[i] : minPrice;
if (max < prices[i] - minPrice)
{
max = prices[i] - minPrice;
}
}
return max;
}
}

122.122-Best Time to Buy and Sell Stock II-Difficulty: Medium

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).

思路

此题是上题的变形,买入卖出的次数没有限制,但是第二次买入必须在第一次卖出的时间节点之后,,此时存在一个局部最优,即 2 4 5 3 6 8,此时8-2一定小于5-2+8-3,,因此就有取数组每次递增的收益即为局部最优,然后所有的局部最优加起来就是全局最优

参考:
http://www.2cto.com/kf/201504/389181.html
public class Solution {
public int MaxProfit(int[] prices) {
if (prices.Length == 0)
return 0;
int minPrice = prices[0];
int sum = 0;
for (int i = 1; i < prices.Length; i++)
{
if (prices[i] < prices[i - 1])
{
sum += prices[i-1] - minPrice;
minPrice = prices[i];
}
}

return sum + prices[prices.Length - 1] - minPrice;
}
}

123.123-Best Time to Buy and Sell Stock III-Difficulty: Hard

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 at most
two transactions.

Note:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

思路

分析:此题亦即变形,也就是求交易次数最多为两次。当然有两种方法,第一种暴力,对于每个i,我们求[0,i]与[i,n-1]两次收益,然后求和,遍历i,可以取其中的最大值,需要O(N^2)的时间。第二种方法是动态规划,用两个数组,第一个数组f1[i]用来表示在[0,i]内进行买入卖出的最大收益,用f2[i]表示在[i,n-1]内进行买入卖出的最大收益。然后最大收益即为max(f1[i]+f2[i]),如何求f1[i]和f2[i],,第一题的方法已经给出了。

参考:
http://www.2cto.com/kf/201504/389181.html public class Solution {
public int MaxProfit(int[] prices) {
if (prices.Length == 0)
return 0;
int priceTemp = prices[0];
int temp = 0;
int[] sum1 = new int[prices.Length];
int[] sum2 = new int[prices.Length];

for (int i = 1; i < prices.Length; i++)
{
priceTemp = priceTemp > prices[i] ? prices[i] : priceTemp;
temp = temp > prices[i] - priceTemp ? temp : prices[i] - priceTemp;
sum1[i] = temp;
}
priceTemp = prices[prices.Length - 1];
temp = 0;
for (int i = prices.Length - 2; i > 0; i--)
{
priceTemp = priceTemp < prices[i] ? prices[i] : priceTemp;
temp = temp >priceTemp- prices[i] ? temp :priceTemp- prices[i] ;
sum2[i] = temp;
}
temp = 0;
for (int i = 0; i < prices.Length; i++)
temp = temp > sum1[i] + sum2[i] ? temp : sum1[i] + sum2[i];
return temp;
}
}

124.124-Binary Tree Maximum Path Sum-Difficulty: Hard

Given a binary tree, find the maximum path sum.

For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root.

For example:

Given the below binary tree,

1
/ \
2   3

Return
6
.

思路

求最长路径,起点为叶节点

博主依旧照getdeep的思路改了,也就是深度遍历,

要构成一个路径,就是左侧路径和右侧路径还有当前节点组成。

新建一个list,储存当前的路径也就是左侧路径+右侧路径+当前节点,然后最后在这个list选出最大值即可

另外注意,当左侧和右侧都<=0时这条路径就不要了,因为有它肯定不是最大路径///如果root加左儿子单路或者右儿子单路最后的值都小于0,则返回0,意味着不要root开始的这个单路了

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
IList<int> list = new List<int>();
public int MaxPathSum(TreeNode root) {
getDeep(root);
int max = list[0] ;
for (int i = 1; i < list.Count; i++)
max = max > list[i] ? max : list[i];
return max;
}
public int getDeep(TreeNode temp)
{
int deep = 0;
if (temp != null)
{
int lDeep = getDeep(temp.left);
int rDeep = getDeep(temp.right);
list.Add(temp.val);
if (lDeep <= 0 && rDeep <= 0)
{
deep = temp.val;
return deep;
}
list.Add(lDeep + rDeep + temp.val);
deep = (lDeep > rDeep ? lDeep : rDeep) + temp.val;
list.Add(deep);
}
return deep;
}
}

125.125-Valid Palindrome-Difficulty: Easy

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama"
is a palindrome.
"race a car"
is not a palindrome.

Note:

Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

思路

判断字符串是否是回文

注意:需要过滤空格和字符,会问包括,字母(不分大小写)和数字

步骤:首先遍历一遍存储有效字母和数字,并把小写字母转为大写,然后在遍历判断即可,全程ascii码

public class Solution {
public bool IsPalindrome(string s) {
IList<int> list = new List<int>();
for (int i = 0; i < s.Length; i++)
{
if ((s[i] >= 65 && s[i] <= 90) || (s[i] >= 48 && s[i] <= 57))
list.Add(s[i]);
else if (s[i] >= 97 && s[i] <= 122)
list.Add(s[i] - 32);
}
for (int i = 0; i < list.Count / 2; i++)
{
if (list[i] != list[list.Count - 1-i])
return false;
}
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: