let 112. Path Sum

主题思想: 判断是否存在一条根节点到叶子节点的路径,路径上的和等于指定数值。

AC 代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {

        if(root==null) return false;

        return findPathSum(root,0,sum);
    }

    public boolean findPathSum(TreeNode root,int tmp, int sum){

        if(root==null) return false;

         if(root.left==null&&root.right==null){
             return (tmp+root.val)==sum;
         }

        return findPathSum(root.left,tmp+root.val,sum)||findPathSum(root.right,tmp+root.val,sum);

    }

}
Sure, I can help you with that. Here is an implementation of the UCB algorithm in Python: ```python import numpy as np def ucb(t, arm_counts, rewards): c = 2 # exploration parameter n_arms = len(arm_counts) ucb_vals = np.zeros(n_arms) total_counts = np.sum(arm_counts) for i in range(n_arms): if arm_counts[i] == 0: ucb_vals[i] = np.inf else: ucb_vals[i] = rewards[i] + c * np.sqrt(np.log(total_counts) / arm_counts[i]) return np.argmax(ucb_vals) # Define parameters T = 2000 p_values = np.arange(0, 20) n_arms = len(p_values) rewards = [10 - 0.5 * p + np.random.normal(0, 0.04) for p in p_values] # Run simulations regret_ucb = np.zeros(T) regret_greedy = np.zeros(T) arm_counts_ucb = np.zeros(n_arms) arm_counts_greedy = np.zeros(n_arms) total_reward_ucb = 0 total_reward_greedy = 0 chosen_arms_ucb = [] chosen_arms_greedy = [] for t in range(T): # UCB algorithm arm_ucb = ucb(t, arm_counts_ucb, rewards) reward_ucb = rewards[arm_ucb] arm_counts_ucb[arm_ucb] += 1 total_reward_ucb += reward_ucb chosen_arms_ucb.append(arm_ucb) regret_ucb[t] = max(rewards) * (t+1) - total_reward_ucb # Greedy algorithm arm_greedy = np.argmax(rewards) reward_greedy = rewards[arm_greedy] arm_counts_greedy[arm_greedy] += 1 total_reward_greedy += reward_greedy chosen_arms_greedy.append(arm_greedy) regret_greedy[t] = max(rewards) * (t+1) - total_reward_greedy # Plot results import matplotlib.pyplot as plt plt.plot(regret_ucb, label="UCB") plt.plot(regret_greedy, label="Greedy") plt.legend() plt.xlabel("Time") plt.ylabel("Expected Regret") plt.show() ``` This code simulates the UCB algorithm and the greedy algorithm for 2000 periods and plots the expected regret as a function of time. It uses 1000 sample paths by default. Note that the UCB algorithm uses an exploration parameter `c` that determines how much to explore versus exploit. In this implementation, `c` is set to 2. The expected regret is calculated as the difference between the maximum possible reward (i.e., the reward of the best arm at each time step) and the total reward obtained by the algorithm up to that time step. You can run this code to see the results for yourself. Let me know if you have any questions!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值