In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to apply the C++ greedy algorithm. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
Select sort
The sort of choice that we are familiar with adopts the strategy of greed. Its greedy strategy is to select the minimum value from each unsorted data and put the minimum value at the beginning of the unsorted data until the unsorted data is 0.
Void swap (int* arr, int I, int j) {int tmp = arr [I]; arr [I] = arr [j]; arr [j] = tmp;} void selectSort (int* arr, int n) {/ / descending for (int I = 0; I
< n; i++) { int maxIndex = i; for (int j = i+1; j < n; j++) { if (arr[j] >= arr [maxIndex]) maxIndex = j;} swap (arr, I, maxIndex);}} balance string
Problem description:
In a balanced string, the number of'L' and'R' characters is the same.
To give you a balanced string s, please split it into as many balanced strings as possible.
Note: each string split must be a balanced string, and the segmented balanced string is a consecutive substring of the original balanced string.
Returns the maximum number of balanced strings that can be obtained by splitting.
Greedy strategy: scan from left to right, as soon as the balance is reached, immediately split, there is no nested balance.
Therefore, a variable balance can be defined, which changes in different directions when different characters are encountered. When balance is 0, the balance is achieved and the number of divisions is updated.
For example: scan the string s from left to right, encounter LMagnebalanceLiu1, encounter RMagnebalanceLife1. When balance is 0, update cnt++. If the final cnt==0, s only needs to remain as it is. Return 1.
Code implementation:
Class Solution {public: int balancedStringSplit (string s) {if (s.size () = = 1) return 0; int cnt = 0; int balance = 0; for (int I = 0; I
< s.size(); i++) { if(s[i] == 'R') --balance; else ++balance; if(balance == 0) cnt++; } if(cnt == 0) return 1; return cnt; }};买股票的最佳时机 问题描述: 给定一个数组 prices ,其中 prices[i] 是一支给定股票第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 贪心策略: 连续上涨交易日:第一天买最后一天卖收益最大,等价于每天都买卖。 连续下降交易日:不买卖收益最大,即不会亏钱。 故可以遍历整个股票交易日价格列表,在所有上涨交易日都买卖(赚到所有利润),所有下降交易日都不买卖(永不亏钱)For example, from 10 to 50 is a five-day rise, which can be bought on the first day and sold on the last day, with a profit of 40%, which is equivalent to buying on the first day, selling on the second day and buying again on the second day. and so on
Code implementation:
Class Solution {public: int maxProfit (vector& prices) {int profit = 0; for (int I = 0; I
< prices.size() - 1; i++) { if(prices[i] = y,那么位置y也可以到达。即对于每一个可以到达的位置x,它使得x+1,x+2,…,x+nums[x] >= y, then position y can also be reached. Iterate through every location in the array at once and update the farthest possible location in real time. For the current traversal position x, if it is within the farthest reachable position, then we can reach that position through several hops from the starting point, so we can update the farthest reachable position with x+nums [x].
During traversal, if the farthest reachable position is greater than or equal to the last position in the array, the last position is reachable and true is returned directly. At the end of the traversal, the last location is still unreachable and returns false.
For example: [2,3,1,1,4]
Initially at position 0, the maximum length that can jump is 2, so the farthest reachable position is updated to 2; continue traversing to position 1, due to 1a2 [0];}; int solve (vector& moneyCount, int money) {int num = 0; sort (moneyCount.begin (), moneyCount.end (), cmp ()); / / first select the banknote with the maximum denomination for (int I = 0; I)
< moneyCount.size() - 1; i++) { //选择需要的当前面值和该面值有的数量中的较小值 int c = min(money / moneyCount[i][0], moneyCount[i][1]); money -= c * moneyCount[i][0]; num += c; } if (money >0) num =-1; return num;} Multi-machine scheduling problem
Problem description:
A factory has n independent jobs, which are processed by m identical machines. The processing time required for job I is ti, and any job cannot be interrupted or split when it is processed. The current factory director asks you to write a program for him to work out the shortest time for n jobs to be processed by m machines.
Enter: first line T (1
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.