Skip to content

Commit

Permalink
Merge branch 'release/1.22.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ecgan committed Apr 17, 2020
2 parents f49a377 + 167ad9c commit 2556c09
Show file tree
Hide file tree
Showing 30 changed files with 768 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ The listing below is sorted based on LeetCode #. If you are interested to see my
|-----------:|:------|:-----------|:-------|
| 1 | [Two Sum](/problems/two-sum) | Easy | Array, hash table |
| 35 | [Search Insert Position](/problems/search-insert-position) | Easy | Array, binary search |
| 53 | [Maximum Subarray](/problems/maximum-subarray) | Easy | Array, Divide and Conquer, Dynamic Programming |
| 70 | [Climbing Stairs](/problems/climbing-stairs) | Easy | Dynamic Programming |
| 121 | [Best Time to Buy and Sell Stock](/problems/best-time-to-buy-and-sell-stock) | Easy | Array, Dynamic Programming |
| 136 | [Single Number](/problems/single-number) | Easy | Hash table, bit manipulation |
| 189 | [Rotate Array](/problems/rotate-array) | Easy | Array |
| 198 | [House Robber](/problems/house-robber) | Easy | Dynamic Programming |
| 303 | [Range Sum Query - Immutable](/problems/range-sum-query-immutable) | Easy | Dynamic Programming |
| 349 | [Intersection of Two Arrays](/problems/intersection-of-two-arrays) | Easy | Hash table, two pointers, binary search, sort, set |
| 350 | [Intersection of Two Arrays II](/problems/intersection-of-two-arrays-ii) | Easy | Hash Table, two pointers, binary search, sort |
| 392 | [Is Subsequence](/problems/is-subsequence) | Easy | Binary Search, Dynamic Programming, Greedy |
| 746 | [Min Cost Climbing Stairs](/problems/min-cost-climbing-stairs) | Easy | Array, Dynamic Programming |
| 1025 | [Divisor Game](/problems/divisor-game) | Easy | Math, Dynamic Programming |
| 1144 | [Decrease Elements To Make Array Zigzag](/problems/decrease-elements-to-make-array-zigzag) | Medium | Array |
| 1145 | [Binary Tree Coloring Game](/problems/binary-tree-coloring-game/) | Medium | Tree, depth-first search |
| 1150 | [Check If a Number Is Majority Element in a Sorted Array](/problems/is-a-a-majority-element) | Easy | Array, binary search |
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leetcode",
"version": "1.21.0",
"version": "1.22.0",
"description": "My solutions for LeetCode problems.",
"main": "index.js",
"scripts": {
Expand Down
33 changes: 33 additions & 0 deletions problems/best-time-to-buy-and-sell-stock/README.md
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.
```
24 changes: 24 additions & 0 deletions problems/best-time-to-buy-and-sell-stock/maxProfit.js
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 problems/best-time-to-buy-and-sell-stock/maxProfit.test.js
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)
})
36 changes: 36 additions & 0 deletions problems/climbing-stairs/README.md
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
```
22 changes: 22 additions & 0 deletions problems/climbing-stairs/climbStairs.js
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
41 changes: 41 additions & 0 deletions problems/climbing-stairs/climbStairs.test.js
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)
})
45 changes: 45 additions & 0 deletions problems/divisor-game/README.md
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)
9 changes: 9 additions & 0 deletions problems/divisor-game/divisorGame.js
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
17 changes: 17 additions & 0 deletions problems/divisor-game/divisorGame.test.js
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)
})
33 changes: 33 additions & 0 deletions problems/house-robber/README.md
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.
```
21 changes: 21 additions & 0 deletions problems/house-robber/rob.js
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
Loading

0 comments on commit 2556c09

Please sign in to comment.