-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
768 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Best Time to Buy and Sell Stock | ||
|
||
LeetCode #: [121](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | ||
|
||
Difficulty: Easy | ||
|
||
Topics: Array, dynamic programming. | ||
|
||
## Problem | ||
|
||
Say you have an array for which the `i`th element is the price of a given stock on day `i`. | ||
|
||
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. | ||
|
||
Note that you cannot sell a stock before you buy one. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: [7,1,5,3,6,4] | ||
Output: 5 | ||
Explanation: | ||
Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. | ||
Not 7-1 = 6, as selling price needs to be larger than buying price. | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: [7,6,4,3,1] | ||
Output: 0 | ||
Explanation: In this case, no transaction is done, i.e. max profit = 0. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @param {number[]} prices | ||
* @return {number} | ||
*/ | ||
const maxProfit = (prices) => { | ||
let curProfit = 0 | ||
let posBuyDay = 0 | ||
|
||
for (let i = 1; i < prices.length; i++) { | ||
const curPrice = prices[i] | ||
|
||
if (curPrice < prices[posBuyDay]) { | ||
posBuyDay = i | ||
} | ||
|
||
if (curPrice - prices[posBuyDay] > curProfit) { | ||
curProfit = curPrice - prices[posBuyDay] | ||
} | ||
} | ||
|
||
return curProfit | ||
} | ||
|
||
module.exports = maxProfit |
17 changes: 17 additions & 0 deletions
17
problems/best-time-to-buy-and-sell-stock/maxProfit.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const maxProfit = require('./maxProfit') | ||
|
||
test('Example 1', () => { | ||
const prices = [7, 1, 5, 3, 6, 4] | ||
|
||
const result = maxProfit(prices) | ||
|
||
expect(result).toBe(5) | ||
}) | ||
|
||
test('Example 2', () => { | ||
const prices = [7, 6, 4, 3, 1] | ||
|
||
const result = maxProfit(prices) | ||
|
||
expect(result).toBe(0) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Climbing Stairs | ||
|
||
LeetCode #: [70](https://leetcode.com/problems/climbing-stairs/) | ||
|
||
Difficulty: Easy | ||
|
||
Topics: Dynamic Programming. | ||
|
||
## Problem | ||
|
||
You are climbing a stair case. It takes `n` steps to reach to the top. | ||
|
||
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? | ||
|
||
Note: Given `n` will be a positive integer. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: 2 | ||
Output: 2 | ||
Explanation: There are two ways to climb to the top. | ||
1. 1 step + 1 step | ||
2. 2 steps | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: 3 | ||
Output: 3 | ||
Explanation: There are three ways to climb to the top. | ||
1. 1 step + 1 step + 1 step | ||
2. 1 step + 2 steps | ||
3. 2 steps + 1 step | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
const climbStairs = (n) => { | ||
if (n === 1) return 1 | ||
if (n === 2) return 2 | ||
|
||
let dist2 = 1 | ||
let dist1 = 2 | ||
let cur = 0 | ||
for (let i = 3; i <= n; i++) { | ||
cur = dist1 + dist2 | ||
|
||
dist2 = dist1 | ||
dist1 = cur | ||
} | ||
|
||
return cur | ||
} | ||
|
||
module.exports = climbStairs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const climbStairs = require('./climbStairs') | ||
|
||
test('Example 1', () => { | ||
const n = 2 | ||
|
||
const result = climbStairs(n) | ||
|
||
expect(result).toBe(2) | ||
}) | ||
|
||
test('Example 2', () => { | ||
const n = 3 | ||
|
||
const result = climbStairs(n) | ||
|
||
expect(result).toBe(3) | ||
}) | ||
|
||
test('n=1 should return 1', () => { | ||
const n = 1 | ||
|
||
const result = climbStairs(n) | ||
|
||
expect(result).toBe(1) | ||
}) | ||
|
||
test('n=4 should return 5', () => { | ||
const n = 4 | ||
|
||
const result = climbStairs(n) | ||
|
||
expect(result).toBe(5) | ||
}) | ||
|
||
test('n=5 should return 8', () => { | ||
const n = 5 | ||
|
||
const result = climbStairs(n) | ||
|
||
expect(result).toBe(8) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Divisor Game | ||
|
||
LeetCode #: [1025](https://leetcode.com/problems/divisor-game/) | ||
|
||
Difficulty: Easy | ||
|
||
Topic: Math, Dynamic Programming. | ||
|
||
## Problem | ||
|
||
Alice and Bob take turns playing a game, with Alice starting first. | ||
|
||
Initially, there is a number `N` on the chalkboard. On each player's turn, that player makes a move consisting of: | ||
|
||
- Choosing any `x` with `0 < x < N` and `N % x == 0`. | ||
- Replacing the number `N` on the chalkboard with `N - x`. | ||
|
||
Also, if a player cannot make a move, they lose the game. | ||
|
||
Return `True` if and only if Alice wins the game, assuming both players play optimally. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: 2 | ||
Output: true | ||
Explanation: Alice chooses 1, and Bob has no more moves. | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: 3 | ||
Output: false | ||
Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves. | ||
``` | ||
|
||
Note: | ||
|
||
- `1 <= N <= 1000` | ||
|
||
## Solution Reference | ||
|
||
- [Solution by bupt_wc](https://leetcode.com/problems/divisor-game/discuss/274566/just-return-N-2-0-(proof)) | ||
- [Solution by lee215](https://leetcode.com/problems/divisor-game/discuss/274606/JavaC%2B%2BPython-return-N-2-0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @param {number} N | ||
* @return {boolean} | ||
*/ | ||
const divisorGame = (N) => { | ||
return N % 2 === 0 | ||
} | ||
|
||
module.exports = divisorGame |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const divisorGame = require('./divisorGame') | ||
|
||
test('Example 1', () => { | ||
const N = 2 | ||
|
||
const result = divisorGame(N) | ||
|
||
expect(result).toBe(true) | ||
}) | ||
|
||
test('Example 2', () => { | ||
const N = 3 | ||
|
||
const result = divisorGame(N) | ||
|
||
expect(result).toBe(false) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# House Robber | ||
|
||
LeetCode #: [198](https://leetcode.com/problems/house-robber/) | ||
|
||
Difficulty: Easy | ||
|
||
Topics: Dynamic Programming. | ||
|
||
## Problem | ||
|
||
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. | ||
|
||
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: [1,2,3,1] | ||
Output: 4 | ||
Explanation: | ||
Rob house 1 (money = 1) and then rob house 3 (money = 3). | ||
Total amount you can rob = 1 + 3 = 4. | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: [2,7,9,3,1] | ||
Output: 12 | ||
Explanation: | ||
Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). | ||
Total amount you can rob = 2 + 9 + 1 = 12. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const rob = (nums) => { | ||
if (nums.length === 0) return 0 | ||
if (nums.length === 1) return nums[0] | ||
|
||
let amountP2 = nums[0] | ||
let amountP1 = Math.max(nums[0], nums[1]) | ||
|
||
for (let i = 2; i < nums.length; i++) { | ||
const cur = Math.max( | ||
amountP2 + nums[i], | ||
amountP1 | ||
) | ||
|
||
amountP2 = amountP1 | ||
amountP1 = cur | ||
} | ||
|
||
return Math.max(amountP2, amountP1) | ||
} | ||
|
||
module.exports = rob |
Oops, something went wrong.