Skip to content

Commit e48be29

Browse files
authored
Merge pull request #1 from wangzheng0822/master
同步
2 parents aed62d9 + 80fa474 commit e48be29

File tree

10 files changed

+647
-17
lines changed

10 files changed

+647
-17
lines changed

c-cpp/06_linkedlist/single_list.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ struct single_list* del(struct single_list **prev)
4747

4848
if (!prev)
4949
return NULL;
50-
50+
if (*prev == null)
51+
return NULL;
5152
tmp = *prev;
5253
*prev = (*prev)->next;
5354
tmp->next = NULL;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package backtracking
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
const intMax = int(^uint(0) >> 1)
8+
9+
var Cnt int
10+
11+
// LeastCoins find least number of coins of which the total values are equals a given one
12+
func LeastCoins(targetTotal int, coinOptions []int) int {
13+
minNum := intMax
14+
15+
memo := make([][]int, targetTotal+1)
16+
for i := range memo {
17+
memo[i] = make([]int, len(coinOptions))
18+
}
19+
fmt.Println("start")
20+
leastCoins(&minNum, 0, targetTotal, len(coinOptions)-1, coinOptions, memo)
21+
fmt.Println("end")
22+
23+
return minNum
24+
25+
}
26+
27+
func leastCoins(minNum *int, cNum, totalValue, opIndex int, coinOptions []int, memo [][]int) {
28+
Cnt++
29+
if 0 == totalValue {
30+
if cNum < *minNum {
31+
*minNum = cNum
32+
}
33+
34+
return
35+
}
36+
37+
if opIndex < 0 {
38+
return
39+
}
40+
41+
num4Option := 0
42+
remaining := totalValue - coinOptions[opIndex]*num4Option
43+
for remaining >= 0 {
44+
45+
if opIndex != 0 {
46+
if shouldSkip(memo, remaining, opIndex-1, cNum+num4Option) {
47+
goto Next
48+
}
49+
}
50+
leastCoins(minNum, cNum+num4Option, remaining, opIndex-1, coinOptions, memo)
51+
52+
Next:
53+
num4Option++
54+
remaining = totalValue - coinOptions[opIndex]*num4Option
55+
56+
}
57+
58+
}
59+
60+
func shouldSkip(memo [][]int, totalValue, nextOpIdex, cNum int) bool {
61+
if memo[totalValue][nextOpIdex] > 0 && memo[totalValue][nextOpIdex] <= cNum {
62+
fmt.Printf("skip,%d, %d as %d <= %d \n", totalValue, nextOpIdex, memo[totalValue][nextOpIdex], cNum)
63+
return true
64+
}
65+
if memo[totalValue][nextOpIdex] == 0 || memo[totalValue][nextOpIdex] > cNum {
66+
memo[totalValue][nextOpIdex] = cNum
67+
}
68+
return false
69+
}
70+
71+
func LeastCoins2(targetTotal int, coinOptions []int) int {
72+
73+
minNum := intMax
74+
memo := make([][]bool, targetTotal)
75+
for i := range memo {
76+
memo[i] = make([]bool, targetTotal/coinOptions[0])
77+
}
78+
79+
fmt.Println("start")
80+
leastCoins2(&minNum, targetTotal, coinOptions, 0, 0, memo)
81+
fmt.Println("end")
82+
83+
return minNum
84+
85+
}
86+
87+
func leastCoins2(minNum *int, targetTotal int, coinOptions []int, cNum, cValue int, memo [][]bool) {
88+
Cnt++
89+
if cValue == targetTotal {
90+
if *minNum > cNum {
91+
*minNum = cNum
92+
}
93+
return
94+
}
95+
96+
for _, coin := range coinOptions {
97+
if coin+cValue <= targetTotal && !memo[cValue+coin-1][cNum] {
98+
memo[cValue+coin-1][cNum] = true
99+
leastCoins2(minNum, targetTotal, coinOptions, cNum+1, cValue+coin, memo)
100+
}
101+
}
102+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package backtracking
2+
3+
import "testing"
4+
5+
func TestFindLeastCoins(t *testing.T) {
6+
7+
coinOptions := []int{1, 3, 5, 10, 50}
8+
9+
Cnt = 0
10+
result := LeastCoins(9, coinOptions)
11+
12+
t.Log("test 1 =====================")
13+
if result != 3 {
14+
t.Logf("least coins %d", result)
15+
t.Error("failed")
16+
}
17+
t.Logf("cnt===%d", Cnt)
18+
19+
Cnt = 0
20+
t.Log("test 2 =====================")
21+
result = LeastCoins(36, coinOptions)
22+
23+
if result != 5 {
24+
t.Logf("least coins %d", result)
25+
t.Error("failed")
26+
}
27+
t.Logf("cnt===%d", Cnt)
28+
29+
}
30+
31+
func TestFindLeastCoins2(t *testing.T) {
32+
33+
coinOptions := []int{1, 3, 5, 10, 50}
34+
35+
Cnt = 0
36+
result := LeastCoins2(9, coinOptions)
37+
38+
t.Log("test 1 =====================")
39+
if result != 3 {
40+
t.Logf("least coins %d", result)
41+
t.Error("failed")
42+
}
43+
44+
t.Logf("cnt===%d", Cnt)
45+
46+
Cnt = 0
47+
t.Log("test 2 =====================")
48+
result = LeastCoins2(36, coinOptions)
49+
50+
if result != 5 {
51+
t.Logf("least coins %d", result)
52+
t.Error("failed")
53+
}
54+
t.Logf("cnt===%d", Cnt)
55+
56+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package dp
2+
3+
var Cnt int
4+
5+
// minNum(v) = 1 + min(minNum(v-i))
6+
func LeastCoins(targetTotal int, coinOptions []int) int {
7+
memo := make([]int, targetTotal)
8+
9+
cMinNum := leastCoins(targetTotal, coinOptions, memo)
10+
11+
return cMinNum
12+
13+
}
14+
15+
// State Transition Table
16+
func LeastCoins2(targetTotal int, coinOptions []int) int {
17+
memo := make([]int, targetTotal)
18+
19+
for i := 1; i <= targetTotal; i++ {
20+
21+
minNum := -1
22+
for _, coin := range coinOptions {
23+
Cnt++
24+
if i < coin {
25+
break
26+
}
27+
if i == coin {
28+
minNum = 1
29+
break
30+
}
31+
if minNum == -1 || minNum > memo[i-coin-1]+1 {
32+
minNum = memo[i-coin-1] + 1
33+
}
34+
}
35+
memo[i-1] = minNum
36+
}
37+
38+
return memo[targetTotal-1]
39+
40+
}
41+
42+
func leastCoins(targetTotal int, coinOptions, memo []int) int {
43+
Cnt++
44+
if targetTotal == 0 {
45+
return 0
46+
}
47+
if memo[targetTotal-1] != 0 {
48+
return memo[targetTotal-1]
49+
}
50+
cMinNum := -1
51+
for _, coin := range coinOptions {
52+
if targetTotal-coin < 0 {
53+
continue
54+
}
55+
cNum := 1 + leastCoins(targetTotal-coin, coinOptions, memo)
56+
if cMinNum == -1 || cNum < cMinNum {
57+
cMinNum = cNum
58+
}
59+
60+
}
61+
memo[targetTotal-1] = cMinNum
62+
return cMinNum
63+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package dp
2+
3+
import "testing"
4+
5+
func TestFindLeastCoins(t *testing.T) {
6+
7+
coinOptions := []int{1, 3, 5, 10, 50}
8+
9+
Cnt = 0
10+
result := LeastCoins(9, coinOptions)
11+
12+
t.Log("test 1 =====================")
13+
if result != 3 {
14+
t.Logf("least coins %d", result)
15+
t.Error("failed")
16+
}
17+
t.Logf("cnt===%d", Cnt)
18+
19+
Cnt = 0
20+
t.Log("test 2 =====================")
21+
result = LeastCoins(36, coinOptions)
22+
23+
if result != 5 {
24+
t.Logf("least coins %d", result)
25+
t.Error("failed")
26+
}
27+
t.Logf("cnt===%d", Cnt)
28+
29+
}
30+
31+
func TestFindLeastCoins2(t *testing.T) {
32+
33+
coinOptions := []int{1, 3, 5, 10, 50}
34+
35+
Cnt = 0
36+
result := LeastCoins2(9, coinOptions)
37+
38+
t.Log("test 1 =====================")
39+
if result != 3 {
40+
t.Logf("least coins %d", result)
41+
t.Error("failed")
42+
}
43+
t.Logf("cnt===%d", Cnt)
44+
45+
Cnt = 0
46+
t.Log("test 2 =====================")
47+
result = LeastCoins2(36, coinOptions)
48+
49+
if result != 5 {
50+
t.Logf("least coins %d", result)
51+
t.Error("failed")
52+
}
53+
t.Logf("cnt===%d", Cnt)
54+
55+
}

javascript/16_binary/binary-find.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ const biaryFindLastSmall = (sortedArr, target) => {
6868
let high = sortedArr.length - 1
6969
while (low <= high) {
7070
const mid = Math.floor((low + high) / 2)
71-
if (sortedArr[mid] > target) {
71+
if (target < sortedArr[mid]) {
7272
high = mid - 1
7373
} else {
74-
if (mid === sortedArr.length - 1 || sortedArr[mid + 1] > target) return mid
74+
if (mid === sortedArr.length - 1 || sortedArr[mid + 1] >= target) return mid
7575
low = mid + 1
7676
}
7777
}
@@ -87,4 +87,4 @@ console.log(`FindLast: ${last}`)
8787
const FisrtBig = biaryFindFistBig(arr, 5)
8888
console.log(`FindFisrtBig: ${FisrtBig}`)
8989
const LastSmall = biaryFindLastSmall(arr, 4)
90-
console.log(`FindLastSmall: ${LastSmall}`)
90+
console.log(`FindLastSmall: ${LastSmall}`)

javascript/17_skiplist/SkipList.js

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,34 @@
55

66
const MAX_LEVEL = 16;
77

8-
class Node{
9-
data = -1;
10-
maxLevel = 0;
11-
refer = new Array(MAX_LEVEL);
8+
class Node {
9+
constructor({
10+
data = -1,
11+
maxLevel = 0,
12+
refer = new Array(MAX_LEVEL)
13+
} = {}) {
14+
this.data = data;
15+
this.maxLevel = maxLevel;
16+
this.refer = refer
17+
}
1218
}
1319

1420
class SkipList{
15-
levelCount = 1;
16-
head = new Node();
17-
18-
static randomLevel() {
21+
constructor() {
22+
this.head = new Node();
23+
this.levelCount = 1;
24+
}
25+
randomLevel() {
1926
let level = 1;
20-
for(let i = 1; i < MAX_LEVEL; i++) {
21-
if(Math.random() < 0.5) {
27+
for (let i = 1; i < MAX_LEVEL; i++) {
28+
if (Math.random() < 0.5) {
2229
level++;
2330
}
2431
}
2532
return level;
2633
}
27-
2834
insert(value) {
29-
const level = SkipList.randomLevel();
35+
const level = this.randomLevel();
3036
const newNode = new Node();
3137
newNode.data = value;
3238
newNode.maxLevel = level;
@@ -88,8 +94,40 @@ class SkipList{
8894
printAll() {
8995
let p = this.head;
9096
while(p.refer[0] !== undefined) {
91-
// console.log(p.refer[0].data)
97+
console.log(p.refer[0].data)
9298
p = p.refer[0];
9399
}
94100
}
95101
}
102+
103+
test();
104+
function test() {
105+
let list = new SkipList();
106+
let length = 20000;
107+
//顺序插入
108+
for (let i = 1; i <= 10; i++) {
109+
list.insert(i);
110+
}
111+
//输出一次
112+
list.printAll();
113+
console.time('create length-10')
114+
//插入剩下的
115+
for (let i = 11; i <= length - 10; i++) {
116+
list.insert(i);
117+
}
118+
console.timeEnd('create length-10')
119+
//搜索 10次
120+
for (let j = 0; j < 10; j++) {
121+
let key = Math.floor(Math.random() * length + 1);
122+
console.log(key, list.find(key))
123+
}
124+
//搜索不存在的值
125+
console.log('null:', list.find(length + 1));
126+
//搜索5000次统计时间
127+
console.time('search 5000');
128+
for (let j = 0; j < 5000; j++) {
129+
let key = Math.floor(Math.random() * length + 1);
130+
}
131+
console.timeEnd('search 5000');
132+
}
133+

0 commit comments

Comments
 (0)