Dennis Leung

前端小天才智多星班进修中

  • 主页
  • 干货储物柜
所有文章 关于我

Dennis Leung

前端小天才智多星班进修中

  • 主页
  • 干货储物柜

Best Time to Buy and Sell Stock

2020-05-22

121. Best Time to Buy and Sell Stock

原题:121. Best Time to Buy and Sell Stock

source code

1
2
3
4
5
6
7
8
9
10
11
12
// Runtime: 80 ms, faster than 33.60% of JavaScript online submissions for Best Time to Buy and Sell Stock.
// Memory Usage: 38.6 MB, less than 7.41% of JavaScript online submissions for Best Time to Buy and Sell Stock.
var maxProfit = function (prices) {
let minBuy = prices.shift();
let maxProfit = 0;
for (let price of prices) {
maxProfit = Math.max(maxProfit, price - minBuy);
minBuy = Math.min(price, minBuy);
}

return maxProfit;
};

test cases:

1
2
3
4
5
6
7
8
9
10
11
test("test1", () => {
expect(maxProfit([1, 2, 3, 4, 5])).toEqual(4);
});

test("test2", () => {
expect(maxProfit([7, 6, 4, 3, 1])).toEqual(0);
});

test("test3", () => {
expect(maxProfit([7, 1, 5, 3, 6, 4])).toEqual(5);
});

122. Best Time to Buy and Sell Stock II

原题:122. Best Time to Buy and Sell Stock II

这道题的思路是:因为能买卖无数次,只要能赚钱(价格比前一天高),就尽快卖出。能同一天进行买卖。

source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Runtime: 84 ms, faster than 27.73% of JavaScript online submissions for Best Time to Buy and Sell Stock II.
// Memory Usage: 37 MB, less than 23.81% of JavaScript online submissions for Best Time to Buy and Sell Stock II.
var maxProfit = function (prices) {
let sum = 0;
for (let i = 0, j = 1; j < prices.length; ) {
if (prices[j] > prices[i]) {
sum += prices[j] - prices[i];
}
i = j;
j++;
}

return sum;
};

test cases

1
2
3
4
5
6
7
8
9
10
11
test("test1", () => {
expect(maxProfit([7, 1, 5, 3, 6, 4])).toEqual(7);
});

test("test2", () => {
expect(maxProfit([7, 6, 4, 3, 1])).toEqual(0);
});

test("test3", () => {
expect(maxProfit([1, 2, 3, 4, 5])).toEqual(4);
});

123. Best Time to Buy and Sell Stock III

原题:123. Best Time to Buy and Sell Stock III

source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Runtime: 2064 ms, faster than 5.19% of JavaScript online submissions for Best Time to Buy and Sell Stock III.
// Memory Usage: 47.7 MB, less than 50.00% of JavaScript online submissions for Best Time to Buy and Sell Stock III.
var maxProfit = function (prices) {
const helper = function (prices) {//helper实际上是121的代码
let minBuy = prices.shift();
let maxProfit = 0;
for (let price of prices) {
maxProfit = Math.max(maxProfit, price - minBuy);
minBuy = Math.min(price, minBuy);
}

return maxProfit;
};

let maxProfit = 0;
for (let i = 1; i <= prices.length; i++) {
maxProfit = Math.max(
maxProfit,
helper(prices.slice(0, i)) + helper(prices.slice(i))
);
}

return maxProfit;
};

test code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
test("test1", () => {
expect(maxProfit([3, 3, 5, 0, 0, 3, 1, 4])).toEqual(6);
});

test("test2", () => {
expect(maxProfit([7, 6, 4, 3, 1])).toEqual(0);
});

test("test3", () => {
expect(maxProfit([1, 2, 3, 4, 5])).toEqual(4);
});

test("test4", () => {
expect(maxProfit([2, 1, 2, 0, 1])).toEqual(2);
});
  • leetcode
  • 算法
102. Binary Tree Level Order Traversal
模拟es6 Class中的private/public
© 2023 Dennis Leung
Hexo Theme Yilia by Litten

粤公网安备 44030502007083号

粤ICP备2021020087号

  • 所有文章
  • 关于我

tag:

  • leetcode
  • 算法
  • 并查集
  • JavaScript
  • 干货储物柜
  • Yii
  • php
  • Python
  • http
  • html
  • HTML
  • CSS
  • hexo
  • javascript
  • hybrid
  • 网络安全
  • Apple Script
  • 爬虫
  • 微信公众号
  • css
  • 数据结构
  • 编译原理
  • 位运算
  • 干活储物柜
  • 移动开发

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • 利用Apple Script脚本爬取微信公众号文章

    2020-07-07

    #Apple Script#爬虫#微信公众号

  • 实现每隔i秒循环打印i

    2020-06-14

    #javascript

  • 多次调用bind时this的指向问题

    2020-06-14

    #javascript

  • 箭头函数this的指向问题

    2020-06-14

    #javascript

  • javascript中的那些类型隐式转换

    2020-06-14

    #javascript

  • 严格模式下的this指向

    2020-06-14

    #javascript

  • 287. Find the Duplicate Number

    2020-06-13

    #leetcode#算法

  • 295. Find Median from Data Stream

    2020-06-13

    #leetcode#算法

  • js与native交互方式总结

    2020-06-12

    #javascript#hybrid

  • 315. Count of Smaller Numbers After Self

    2020-06-11

    #leetcode#算法

  • 124. Binary Tree Maximum Path Sum

    2020-06-11

    #leetcode#算法

  • 227. Basic Calculator II

    2020-06-10

    #leetcode#算法

  • 454. 4Sum II

    2020-06-09

    #leetcode#算法

  • 斐波那契求解效率对比

    2020-06-03

    #算法

  • 并查集的实现

    2020-06-03

    #算法#数据结构

  • 200. Number of Islands

    2020-06-03

    #leetcode#算法#并查集

  • 常用位运算

    2020-06-03

    #javascript#位运算#干活储物柜

  • 72. Edit Distance

    2020-06-02

    #leetcode#算法

  • 形如if(0<100<0)怎么执行

    2020-06-01

    #javascript#编译原理

  • 322. Coin Change

    2020-06-01

    #leetcode#算法

  • 300. Longest Increasing Subsequence

    2020-05-31

    #leetcode#算法

  • 188. Best Time to Buy and Sell Stock IV

    2020-05-30

    #leetcode#算法

  • 买卖股票问题通用解法

    2020-05-30

    #leetcode#算法

  • 用generator+promise实现async/await

    2020-05-29

    #javascript

  • 152. Maximum Product Subarray

    2020-05-26

    #leetcode#算法

  • 实现 simplifyUrl

    2020-05-26

    #javascript

  • microTask/macroTask执行顺序

    2020-05-26

    #javascript

  • 120. Triangle

    2020-05-26

    #leetcode#算法

  • 70. Climbing Stairs

    2020-05-26

    #leetcode#算法

  • 52. N-Queens II

    2020-05-25

    #leetcode#算法

  • 338. Counting Bits

    2020-05-25

    #leetcode#算法

  • 231. Power of Two

    2020-05-24

    #leetcode#算法

  • 191. Number of 1 Bits

    2020-05-24

    #leetcode#算法

  • 212. Word Search II

    2020-05-24

    #leetcode#算法

  • 79. Word Search

    2020-05-24

    #leetcode#算法

  • 208. Implement Trie (Prefix Tree)

    2020-05-24

    #leetcode#算法

  • 69. Sqrt(x)

    2020-05-24

    #leetcode#算法

  • 37. Sudoku Solver

    2020-05-24

    #leetcode#算法

  • 36. Valid Sudoku

    2020-05-24

    #leetcode#算法

  • 51. N-Queens

    2020-05-23

    #leetcode#算法

  • 22. Generate Parentheses

    2020-05-23

    #leetcode#算法

  • 111. Minimum Depth of Binary Tree

    2020-05-23

    #leetcode#算法

  • 104. Maximum Depth of Binary Tree

    2020-05-23

    #leetcode#算法

  • 102. Binary Tree Level Order Traversal

    2020-05-23

    #leetcode#算法

  • Best Time to Buy and Sell Stock

    2020-05-22

    #leetcode#算法

  • 模拟es6 Class中的private/public

    2020-05-22

    #javascript

  • 19. Remove Nth Node From End of List

    2020-05-21

    #leetcode#算法

  • 为contenteditable增加placeholder

    2020-05-21

    #html#css

  • 用纯css实现一个iOS的switch开关

    2020-05-21

    #css

  • 169. Majority Element

    2020-05-21

    #leetcode#算法

  • 50. Pow(x, n)

    2020-05-21

    #leetcode#算法

  • 235. Lowest Common Ancestor of a Binary Search Tree

    2020-05-20

    #leetcode#html

  • 98. Validate Binary Search Tree

    2020-05-20

    #leetcode#算法

  • 1. Two Sum

    2020-05-19

    #leetcode#算法

  • 239. Sliding Window Maximum

    2020-05-19

    #leetcode#算法

  • 703. Kth Largest Element in a Stream

    2020-05-19

    #leetcode#算法

  • 232. Implement Queue using Stacks

    2020-05-18

    #leetcode#算法

  • 20. Valid Parentheses

    2020-05-18

    #leetcode#算法

  • Reverse Nodes in k-Group

    2020-05-18

    #leetcode#算法

  • 142. Linked List Cycle II

    2020-05-17

    #leetcode#算法

  • 找出旧数组变成新数组的操作

    2020-05-16

    #算法

  • 浏览器为什么要限制同源策略?为什么script标签不受同源策略限制?

    2020-05-16

    #html#网络安全

  • 移动端触摸/点击事件发生顺序

    2020-05-15

    #html#移动开发

  • this指向的改变

    2020-05-14

    #javascript

  • hexo-lazyload-image不生效

    2020-05-12

    #hexo

  • promise.prototype.finally的shim不生效解决办法

    2020-05-12

    #javascript

  • 让fetch支持timeout

    2020-05-12

    #javascript

  • "为什么我插入body的script标签不执行?"

    2020-05-12

    #javascript

  • flex布局学习笔记

    2020-05-11

    #CSS

  • xss防范

    2020-05-11

    #网络安全

  • BFC

    2020-05-11

    #HTML#CSS

  • String.raw处理模板字符串

    2020-05-11

    #javascript

  • 图片加载失败自动使用默认占位图的方法

    2020-05-06

    #干货储物柜#html#css

  • hexo插入本地图片失败问题

    2020-05-06

    #hexo

  • 利用python在本地快速搭建一个http server

    2020-05-05

    #干货储物柜#Python#http

  • 相同父域ajax请求cookie无法携带/写入

    2017-08-19

    #JavaScript#干货储物柜

  • iOS上input清空数据光标出现空格

    2017-08-11

    #JavaScript#干货储物柜

  • 阻止浏览器自动填充

    2017-08-11

    #JavaScript#干货储物柜

  • iOS微信二维码长按无法识别的问题

    2017-06-25

    #JavaScript#干货储物柜

  • Yii中事务不起作用的问题

    2017-06-04

    #干货储物柜#Yii#php

  • Yii中try/catch不起作用的问题

    2017-06-04

    #干货储物柜#Yii#php

  • JavaScript 中的“+”运算

    2016-11-06

    #JavaScript#干货储物柜

不思进取的前端开发攻城狮。
梦想是躺着也能拯救世界。