Skip to content

Commit

Permalink
[pull] main from krahets:main (#1)
Browse files Browse the repository at this point in the history
* Update the EN figures for the chapter of preface, introduction, complexity analysis, data structure, array and linked list, stack and queue (krahets#1335)

* Add intial translation of the figures for the rest of the chapters (krahets#1340)

* translation: Add Python and Java code for EN version (krahets#1345)

* Add the intial translation of code of all the languages

* test

* revert

* Remove

* Add Python and Java code for EN version

* Update heap.md (krahets#1344)

* Add the initial EN translation for C++ code (krahets#1346)

* Bug fixes and improvements (krahets#1348)

* Add "reference" for EN version. Bug fixes.

* Unify the figure reference as "the figure below" and "the figure above".
Bug fixes.

* Format the EN markdown files.

* Replace "" with <u></u> for EN version and bug fixes

* Fix biary_tree_dfs.png

* Fix biary_tree_dfs.png

* Fix zh-hant/biary_tree_dfs.png

* Fix heap_sort_step1.png

* Sync zh and zh-hant versions.

* Bug fixes

* Fix EN figures

* Bug fixes

* Fix the figure labels for EN version

* cargo fmt code (krahets#1349)

* feat: add ruby codes - chapter greedy (krahets#1350)

---------

Co-authored-by: Yudong Jin <[email protected]>
Co-authored-by: popozhu <[email protected]>
Co-authored-by: rongyi <[email protected]>
Co-authored-by: khoaxuantu <[email protected]>
  • Loading branch information
5 people authored May 11, 2024
1 parent cac10b0 commit d395a31
Show file tree
Hide file tree
Showing 858 changed files with 20,117 additions and 262 deletions.
2 changes: 1 addition & 1 deletion codes/c/chapter_hashing/simple_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int rotHash(char *key) {

/* Driver Code */
int main() {
char *key = "Hello dsad3241241dsa算123法";
char *key = "Hello 算法";

int hash = addHash(key);
printf("加法哈希值为 %d\n", hash);
Expand Down
2 changes: 1 addition & 1 deletion codes/cpp/chapter_hashing/simple_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int rotHash(string key) {

/* Driver Code */
int main() {
string key = "Hello dsad3241241dsa算123法";
string key = "Hello 算法";

int hash = addHash(key);
cout << "加法哈希值为 " << hash << endl;
Expand Down
4 changes: 2 additions & 2 deletions codes/javascript/chapter_dynamic_programming/coin_change.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function coinChangeDP(coins, amt) {
return dp[n][amt] !== MAX ? dp[n][amt] : -1;
}

/* 零钱兑换:状态压缩后的动态规划 */
/* 零钱兑换:空间优化后的动态规划 */
function coinChangeDPComp(coins, amt) {
const n = coins.length;
const MAX = amt + 1;
Expand Down Expand Up @@ -61,6 +61,6 @@ const amt = 4;
let res = coinChangeDP(coins, amt);
console.log(`凑到目标金额所需的最少硬币数量为 ${res}`);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = coinChangeDPComp(coins, amt);
console.log(`凑到目标金额所需的最少硬币数量为 ${res}`);
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function coinChangeIIDP(coins, amt) {
return dp[n][amt];
}

/* 零钱兑换 II:状态压缩后的动态规划 */
/* 零钱兑换 II:空间优化后的动态规划 */
function coinChangeIIDPComp(coins, amt) {
const n = coins.length;
// 初始化 dp 表
Expand Down Expand Up @@ -59,6 +59,6 @@ const amt = 5;
let res = coinChangeIIDP(coins, amt);
console.log(`凑出目标金额的硬币组合数量为 ${res}`);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = coinChangeIIDPComp(coins, amt);
console.log(`凑出目标金额的硬币组合数量为 ${res}`);
4 changes: 2 additions & 2 deletions codes/javascript/chapter_dynamic_programming/edit_distance.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function editDistanceDP(s, t) {
return dp[n][m];
}

/* 编辑距离:状态压缩后的动态规划 */
/* 编辑距离:空间优化后的动态规划 */
function editDistanceDPComp(s, t) {
const n = s.length,
m = t.length;
Expand Down Expand Up @@ -130,6 +130,6 @@ console.log(`将 ${s} 更改为 ${t} 最少需要编辑 ${res} 步`);
res = editDistanceDP(s, t);
console.log(`将 ${s} 更改为 ${t} 最少需要编辑 ${res} 步`);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = editDistanceDPComp(s, t);
console.log(`将 ${s} 更改为 ${t} 最少需要编辑 ${res} 步`);
4 changes: 2 additions & 2 deletions codes/javascript/chapter_dynamic_programming/knapsack.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function knapsackDP(wgt, val, cap) {
return dp[n][cap];
}

/* 0-1 背包:状态压缩后的动态规划 */
/* 0-1 背包:空间优化后的动态规划 */
function knapsackDPComp(wgt, val, cap) {
const n = wgt.length;
// 初始化 dp 表
Expand Down Expand Up @@ -108,6 +108,6 @@ console.log(`不超过背包容量的最大物品价值为 ${res}`);
res = knapsackDP(wgt, val, cap);
console.log(`不超过背包容量的最大物品价值为 ${res}`);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = knapsackDPComp(wgt, val, cap);
console.log(`不超过背包容量的最大物品价值为 ${res}`);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function minCostClimbingStairsDP(cost) {
return dp[n];
}

/* 爬楼梯最小代价:状态压缩后的动态规划 */
/* 爬楼梯最小代价:空间优化后的动态规划 */
function minCostClimbingStairsDPComp(cost) {
const n = cost.length - 1;
if (n === 1 || n === 2) {
Expand Down
4 changes: 2 additions & 2 deletions codes/javascript/chapter_dynamic_programming/min_path_sum.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function minPathSumDP(grid) {
return dp[n - 1][m - 1];
}

/* 最小路径和:状态压缩后的动态规划 */
/* 最小路径和:空间优化后的动态规划 */
function minPathSumDPComp(grid) {
const n = grid.length,
m = grid[0].length;
Expand Down Expand Up @@ -116,6 +116,6 @@ console.log(`从左上角到右下角的最小路径和为 ${res}`);
res = minPathSumDP(grid);
console.log(`从左上角到右下角的最小路径和为 ${res}`);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = minPathSumDPComp(grid);
console.log(`从左上角到右下角的最小路径和为 ${res}`);
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function unboundedKnapsackDP(wgt, val, cap) {
return dp[n][cap];
}

/* 完全背包:状态压缩后的动态规划 */
/* 完全背包:空间优化后的动态规划 */
function unboundedKnapsackDPComp(wgt, val, cap) {
const n = wgt.length;
// 初始化 dp 表
Expand Down Expand Up @@ -58,6 +58,6 @@ const cap = 4;
let res = unboundedKnapsackDP(wgt, val, cap);
console.log(`不超过背包容量的最大物品价值为 ${res}`);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = unboundedKnapsackDPComp(wgt, val, cap);
console.log(`不超过背包容量的最大物品价值为 ${res}`);
50 changes: 50 additions & 0 deletions codes/ruby/chapter_greedy/coin_change_greedy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
=begin
File: coin_change_greedy.rb
Created Time: 2024-05-07
Author: Xuan Khoa Tu Nguyen ([email protected])
=end

### 零钱兑换:贪心 ###
def coin_change_greedy(coins, amt)
# 假设 coins 列表有序
i = coins.length - 1
count = 0
# 循环进行贪心选择,直到无剩余金额
while amt > 0
# 找到小于且最接近剩余金额的硬币
while i > 0 && coins[i] > amt
i -= 1
end
# 选择 coins[i]
amt -= coins[i]
count += 1
end
# 若未找到可行方案, 则返回 -1
amt == 0 ? count : -1
end

### Driver Code ###
if __FILE__ == $0
# 贪心:能够保证找到全局最优解
coins = [1, 5, 10, 20, 50, 100]
amt = 186
res = coin_change_greedy(coins, amt)
puts "\ncoins = #{coins}, amt = #{amt}"
puts "凑到 #{amt} 所需的最少硬币数量为 #{res}"

# 贪心:无法保证找到全局最优解
coins = [1, 20, 50]
amt = 60
res = coin_change_greedy(coins, amt)
puts "\ncoins = #{coins}, amt = #{amt}"
puts "凑到 #{amt} 所需的最少硬币数量为 #{res}"
puts "实际上需要的最少数量为 3 , 即 20 + 20 + 20"

# 贪心:无法保证找到全局最优解
coins = [1, 49, 50]
amt = 98
res = coin_change_greedy(coins, amt)
puts "\ncoins = #{coins}, amt = #{amt}"
puts "凑到 #{amt} 所需的最少硬币数量为 #{res}"
puts "实际上需要的最少数量为 2 , 即 49 + 49"
end
51 changes: 51 additions & 0 deletions codes/ruby/chapter_greedy/fractional_knapsack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
=begin
File: fractional_knapsack.rb
Created Time: 2024-05-07
Author: Xuan Khoa Tu Nguyen ([email protected])
=end

### 物品 ###
class Item
attr_accessor :w # 物品重量
attr_accessor :v # 物品价值

def initialize(w, v)
@w = w
@v = v
end
end

### 分数背包:贪心 ###
def fractional_knapsack(wgt, val, cap)
# 创建物品列表,包含两个属性:重量,价值
items = wgt.each_with_index.map { |w, i| Item.new(w, val[i]) }
# 按照单位价值 item.v / item.w 从高到低进行排序
items.sort! { |a, b| (b.v.to_f / b.w) <=> (a.v.to_f / a.w) }
# 循环贪心选择
res = 0
for item in items
if item.w <= cap
# 若剩余容量充足,则将当前物品整个装进背包
res += item.v
cap -= item.w
else
# 若剩余容量不足,则将当前物品的一部分装进背包
res += (item.v.to_f / item.w) * cap
# 已无剩余容量,因此跳出循环
break
end
end
res
end

### Driver Code ###
if __FILE__ == $0
wgt = [10, 20, 30, 40, 50]
val = [50, 120, 150, 210, 240]
cap = 50
n = wgt.length

# 贪心算法
res = fractional_knapsack(wgt, val, cap)
puts "不超过背包容量的最大物品价值为 #{res}"
end
37 changes: 37 additions & 0 deletions codes/ruby/chapter_greedy/max_capacity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
=begin
File: max_capacity.rb
Created Time: 2024-05-07
Author: Xuan Khoa Tu Nguyen ([email protected])
=end

### 最大容量:贪心 ###
def max_capacity(ht)
# 初始化 i, j,使其分列数组两端
i, j = 0, ht.length - 1
# 初始最大容量为 0
res = 0

# 循环贪心选择,直至两板相遇
while i < j
# 更新最大容量
cap = [ht[i], ht[j]].min * (j - i)
res = [res, cap].max
# 向内移动短板
if ht[i] < ht[j]
i += 1
else
j -= 1
end
end

res
end

### Driver Code ###
if __FILE__ == $0
ht = [3, 8, 5, 2, 7, 7, 3, 4]

# 贪心算法
res = max_capacity(ht)
puts "最大容量为 #{res}"
end
28 changes: 28 additions & 0 deletions codes/ruby/chapter_greedy/max_product_cutting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=begin
File: max_product_cutting.rb
Created Time: 2024-05-07
Author: Xuan Khoa Tu Nguyen ([email protected])
=end

### 最大切分乘积:贪心 ###
def max_product_cutting(n)
# 当 n <= 3 时,必须切分出一个 1
return 1 * (n - 1) if n <= 3
# 贪心地切分出 3 ,a 为 3 的个数,b 为余数
a, b = n / 3, n % 3
# 当余数为 1 时,将一对 1 * 3 转化为 2 * 2
return (3.pow(a - 1) * 2 * 2).to_i if b == 1
# 当余数为 2 时,不做处理
return (3.pow(a) * 2).to_i if b == 2
# 当余数为 0 时,不做处理
3.pow(a).to_i
end

### Driver Code ###
if __FILE__ == $0
n = 58

# 贪心算法
res = max_product_cutting(n)
puts "最大切分乘积为 #{res}"
end
3 changes: 1 addition & 2 deletions codes/ruby/chapter_sorting/quick_sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class QuickSort
class << self
### 哨兵划分 ###
def partition(nums, left, right)

# 以 nums[left] 为基准数
i, j = left, right
while i < j
Expand Down Expand Up @@ -116,7 +115,7 @@ def partition(nums, left, right)
i # 返回基准数的索引
end

### 快速排序(尾递归优化)
### 快速排序(尾递归优化)###
def quick_sort(nums, left, right)
# 子数组长度不为 1 时递归
while left < right
Expand Down
4 changes: 2 additions & 2 deletions codes/rust/include/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/* 顶点类型 */
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub struct Vertex {
pub val: i32
pub val: i32,
}

/* 输入值列表 vals ,返回顶点列表 vets */
Expand All @@ -18,4 +18,4 @@ pub fn vals_to_vets(vals: Vec<i32>) -> Vec<Vertex> {
/* 输入顶点列表 vets ,返回值列表 vals */
pub fn vets_to_vals(vets: Vec<Vertex>) -> Vec<i32> {
vets.into_iter().map(|vet| vet.val).collect()
}
}
4 changes: 2 additions & 2 deletions codes/typescript/chapter_dynamic_programming/coin_change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function coinChangeDP(coins: Array<number>, amt: number): number {
return dp[n][amt] !== MAX ? dp[n][amt] : -1;
}

/* 零钱兑换:状态压缩后的动态规划 */
/* 零钱兑换:空间优化后的动态规划 */
function coinChangeDPComp(coins: Array<number>, amt: number): number {
const n = coins.length;
const MAX = amt + 1;
Expand Down Expand Up @@ -61,7 +61,7 @@ const amt = 4;
let res = coinChangeDP(coins, amt);
console.log(`凑到目标金额所需的最少硬币数量为 ${res}`);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = coinChangeDPComp(coins, amt);
console.log(`凑到目标金额所需的最少硬币数量为 ${res}`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function coinChangeIIDP(coins: Array<number>, amt: number): number {
return dp[n][amt];
}

/* 零钱兑换 II:状态压缩后的动态规划 */
/* 零钱兑换 II:空间优化后的动态规划 */
function coinChangeIIDPComp(coins: Array<number>, amt: number): number {
const n = coins.length;
// 初始化 dp 表
Expand Down Expand Up @@ -59,7 +59,7 @@ const amt = 5;
let res = coinChangeIIDP(coins, amt);
console.log(`凑出目标金额的硬币组合数量为 ${res}`);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = coinChangeIIDPComp(coins, amt);
console.log(`凑出目标金额的硬币组合数量为 ${res}`);

Expand Down
4 changes: 2 additions & 2 deletions codes/typescript/chapter_dynamic_programming/edit_distance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function editDistanceDP(s: string, t: string): number {
return dp[n][m];
}

/* 编辑距离:状态压缩后的动态规划 */
/* 编辑距离:空间优化后的动态规划 */
function editDistanceDPComp(s: string, t: string): number {
const n = s.length,
m = t.length;
Expand Down Expand Up @@ -141,7 +141,7 @@ console.log(`将 ${s} 更改为 ${t} 最少需要编辑 ${res} 步`);
res = editDistanceDP(s, t);
console.log(`将 ${s} 更改为 ${t} 最少需要编辑 ${res} 步`);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = editDistanceDPComp(s, t);
console.log(`将 ${s} 更改为 ${t} 最少需要编辑 ${res} 步`);

Expand Down
Loading

0 comments on commit d395a31

Please sign in to comment.