-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
058850a
commit 7fcf57f
Showing
5 changed files
with
116 additions
and
4 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
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 |
---|---|---|
@@ -1,3 +1,41 @@ | ||
# 貪婪 (Greedy) | ||
|
||
## 分數背包問題 (Fractional Knapsack Problem) | ||
|
||
給定一個背包,背包的容量是有限的,你可以選擇將物品裝入背包。每個物品有一個重量和價值,但你可以選擇將物品切割,部分裝入背包。目標是最大化裝入背包中的物品的總價值。 | ||
|
||
物品: | ||
|
||
```ts | ||
const items = [ | ||
{ weight: 1, value: 5 }, | ||
{ weight: 2, value: 11 }, | ||
{ weight: 3, value: 15 }, | ||
]; | ||
``` | ||
|
||
單位價值:計算每個物品的單位重量價值 (價值/重量) | ||
|
||
每個物品的編號 (`i`) 及其對應的重量和價值: | ||
|
||
| i | weights[i - 1] | values[i - 1] | values[i - 1] / weights[i - 1] | | ||
| --: | -------------: | ------------: | -----------------------------: | | ||
| 1 | 1 | 5 | 5 | | ||
| 2 | 2 | 11 | 5.5 | | ||
| 3 | 3 | 15 | 5 | | ||
|
||
```ts | ||
values[i - 1] / weights[i - 1]; | ||
``` | ||
|
||
單位價值高低:根據單位價值將物品按從高到低排序 | ||
|
||
| i | weights[i - 1] | values[i - 1] | values[i - 1] / weights[i - 1] | | ||
| --: | -------------: | ------------: | -----------------------------: | | ||
| 2 | 2 | 11 | 5.5 | | ||
| 1 | 1 | 5 | 5 | | ||
| 3 | 3 | 15 | 5 | | ||
|
||
```ts | ||
items.sort((a, b) => b.value / b.weight - a.value / a.weight); | ||
``` |
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 |
---|---|---|
@@ -1 +1,26 @@ | ||
# 位元操作 (Bit Manipulation) | ||
|
||
二進位制 | ||
|
||
- 0001 -> 1 | ||
- 0010 -> 2 | ||
- 0011 -> 3 | ||
- 0100 -> 4 | ||
- 0101 -> 5 | ||
- 0110 -> 6 | ||
- 0111 -> 7 | ||
- 1000 -> 8 | ||
|
||
左移: | ||
|
||
```ts | ||
const x = 1; // 0001 | ||
x << 2; // 0100 -> 4 | ||
``` | ||
|
||
右移: | ||
|
||
```ts | ||
const x = 4; // 0100 | ||
x >> 2; // 0001 -> 1 | ||
``` |
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