1.排队
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 用户购券需耗费的时间
* @param coupons int整型vector 买券的数组
* @param m int整型 位置m
* @return int整型
*/
int timeRequiredToBuyCounpons(vector<int>& coupons, int m) {
int res = 0;
for (int i = 0; i < coupons.size(); i++) {
if (i <= m) {
res += min(coupons[m], coupons[i]);
}
else {
res += min(coupons[m] - 1, coupons[i]);
}
}
return res * 10;
}
};
2.奇数节点之和(类似于leetcode1315)
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 奇数节点之和
* @param root TreeNode类
* @return int整型
*/
int sum(TreeNode* root) {
int res = 0;
if (root->left) {
if (root->val % 2 == 1) {
if (root->left->left) res += root->left->left->val;
if (root->left->right) res += root->left->right->val;
}
res += sum(root->left);
}
if (root->right) {
if (root->val % 2 == 1) {
if (root->right->left) res += root->right->left->val;
if (root->right->right) res += root->right->right->val;
}
res += sum(root->right);
}
return res;
}
};
3.汽车喷漆(类似于剑指offerII091)
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 返回喷漆完所有汽车的最小成本
* @param costs int整型vector<vector<>> 汽车喷漆的成本
* @return int整型
*/
int minVehiclePaintingCost(vector<vector<int> >& costs) {
int carnum = costs.size(), colornum = costs[0].size();
vector<vector<int>> dp(carnum, vector<int>(colornum));
for (int i = 0; i < colornum; i++) dp[0][i] = costs[0][i];
for (int i = 1; i < carnum; i++) {
for (int j = 0; j < colornum; j++) {
int last = INT_MAX;
for (int k = 0; k < colornum; k++) {
if (k != j) last = min(last, dp[i - 1][k]);
}
dp[i][j] = last + costs[i][j];
}
}
int res = INT_MAX;
for (int i = 0; i < colornum; i++) {
res = min(res, dp[carnum - 1][i]);
}
return res;
}
};
#途虎养车##笔试##笔试题目##笔试题型##笔试时间#