当前位置:网站首页>Maximum exchange [greedy thought & monotonic stack implementation]
Maximum exchange [greedy thought & monotonic stack implementation]
2022-07-28 20:12:00 【REN_ Linsen】
Preface
greedy /DP It's practice / A good topic to examine logical thinking , The half step is to simulate and find the law on paper , Then find a suitable data structure , Then use code to realize your idea .
One 、 Maximum exchange

Two 、 greedy & Monotonic stack
/* target: Exchange at most once to maximize the value . Obviously , If the value does not monotonically decrease from high to low , Just swap the largest possible value from the back to the front . greedy 1: Exchange the big one in the back with the small one in the front ; greedy 2: As far as possible , For the front , Try to find the small one as far forward as possible , When equal, it is also necessary to take the left ; For the back , Try to find a big one later , When you want to wait, you also need to take the right . summary : Greedy thoughts , Monotonic stack implementation . */
public int maximumSwap(int num) {
char[] arr = String.valueOf(num).toCharArray();
// step1: Determine non single mediation , In order to start looking for the maximum value later .
int i = 1;
while (i < arr.length && arr[i] <= arr[i - 1]) ++i;
if (i == arr.length) return num;
// step2: Find the maximum value .
int idx = i;
while (i < arr.length) {
if (arr[idx] <= arr[i]) idx = i;
++i;
}
// step3: Find the front position as far as possible .
i = 0;
while (i < arr.length && arr[i] >= arr[idx]) ++i;
// step4: Swap places .
swap(arr, i, idx);
return Integer.parseInt(String.valueOf(arr));
}
private void swap(char[] arr, int i, int j) {
char c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
summary
1) Practice greed more / Dynamic planning topic , It is helpful to improve the ability of logical analysis .
2) Facing problems , Find the law , Select the appropriate data structure according to the characteristics of the data structure , Then use the branch of the matching code / Cycle to solve the problem , It is often necessary to transform the problem , Go to data structure & Branch / loop / Recursive up .
reference
边栏推荐
- The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
- Const pointer of C language and parameter passing of main function
- Scene thread allocation in MMO real-time combat games
- Andorid system layout, values, drawable adaptation
- [C language] header file of complex number four operations and complex number operations
- [C language] Fibonacci sequence [recursion and iteration]
- 【NPP安装插件】
- 爬取IP
- 河北邯郸:拓展基层就业空间 助力高校毕业生就业
- Leetcode day4 the highest paid employee in the Department
猜你喜欢

利用STM32的HAL库驱动1.54寸 TFT屏(240*240 ST7789V)

跨区域网络的通信学习静态路由

Can China make a breakthrough in the future development of the meta universe and occupy the highland?

Kubeedge releases white paper on cloud native edge computing threat model and security protection technology

党员故事|李青艾用漫画带动农民增收致富

Cdga | how can the industrial Internet industry do a good job in data governance?

9. Pointer of C language (1) what is pointer and how to define pointer variables
![[NPP installation plug-in]](/img/6f/97e53116ec4ebc6a6338d125ddad8b.png)
[NPP installation plug-in]
![[C language] print pattern summary](/img/48/d8ff17453e810fcd9269f56eda4d47.png)
[C language] print pattern summary

Know small and medium LAN WLAN
随机推荐
Basic mathematical knowledge (update)
C+ + core programming
In the second half of 2022, the system integration project management engineer certification starts on August 20
最大交换[贪心思想&单调栈实现]
9. Pointer of C language (3) classic program, exchange the value of two numbers for deep analysis, (easy to understand), are formal parameters and arguments a variable?
What is the process of swing event processing?
为什么客户支持对SaaS公司很重要?
跨区域网络的通信学习静态路由
[C language] function
[C language] guessing numbers game [function]
Zfoo adds routes similar to mydog
KPMG China: insights into information technology audit projects of securities fund management institutions
[C language] initial C language reflection and summary
The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
JVM (24) -- performance monitoring and tuning (5) -- Analyzing GC logs
Leetcode day3 find duplicate email addresses
9. Pointer of C language (5) how many bytes does the pointer variable occupy
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
Handan, Hebei: expand grassroots employment space and help college graduates obtain employment