当前位置:网站首页>Sword finger offer II 092 Flip character / Sword finger offer II 093 Longest Fibonacci sequence
Sword finger offer II 092 Flip character / Sword finger offer II 093 Longest Fibonacci sequence
2022-06-23 18:25:00 【Biqiliang】
The finger of the sword Offer II 092. Flip the character 【 Medium question 】
Ideas :【 Dynamic programming 】
Second order dp Array dp[i][0] It means that the i Bit flip to 0 after , The minimum number of flips the array keeps increasing dp[i][1] It means that the i Bit flip to 1 after , The minimum number of flips the array keeps increasing
The initial state :dp[0][0] = s.charAt(0) == '0' ? 0 : 1dp[0][1] = s.charAt(0) == '1' ? 0 : 1
Transfer equation :
dp[i][0] = dp[i-1][0]+s.charAt(i) == '0' ? 0:1dp[i][1] = Math.min(dp[i-1][0],dp[i-1][1])+s.charAt(i) == '1' ? 0:1
Code :【dp Array 】
class Solution {
public int minFlipsMonoIncr(String s) {
// Dynamic programming problem solving
int n = s.length();
//dp Two dimensional array
// dp[i][0] It means that the i Bit flip to 0 when , Make the string s Monotonically increasing minimum number of flips , here i-1 The character at must be 0
// dp[i][1] It means that the i Bit flip to 1 when , Make the string s Monotonically increasing minimum number of flips , here i-1 The character at can be 0 It can also be for 1
int[][] dp = new int[n][2];
dp[0][0] = s.charAt(0) == '0' ? 0 : 1;// The first 0 The bit character is 0, be dp[0][0]=0, Otherwise 1
dp[0][1] = s.charAt(0) == '1' ? 0 : 1;// The first 0 The bit character is 1, be dp[0][1]=0, Otherwise 1
for (int i = 1; i < n; i++) {
int k1 = 0,k2 = 0;
if (s.charAt(i) == '0'){
k2++;
}else {
k1++;
}
// Will be the first i Bit flip to 0 Minimum number of flips by dp[i-1][0] + (s[i] == ‘1’ ? 1 : 0)
dp[i][0] = dp[i-1][0] + k1;
// Will be the first i Bit flip to 1 Minimum number of flips by Math.min(dp[i-1][0],dp[i-1[1]) + (s[i] == '0' ? 1 : 0)
dp[i][1] = Math.min(dp[i-1][0],dp[i-1][1]) + k2;
}
// Finally, return to the n-1 Bit flip to 0 Or flip to 1 The lesser of
return Math.min(dp[n-1][0],dp[n-1][1]);
}
}
Code :【 Scrolling array 】
class Solution {
public int minFlipsMonoIncr(String s) {
// Dynamic programming problem solving
int n = s.length();
// Scrolling array emulation dp Array
// dp0 It means that the i Bit flip to 0 when , Make the string s Monotonically increasing minimum number of flips , here i-1 The character at must be 0
// dp1 It means that the i Bit flip to 1 when , Make the string s Monotonically increasing minimum number of flips , here i-1 The character at can be 0 It can also be for 1
int dp0 = s.charAt(0) == '0' ? 0 : 1;// The first 0 The bit character is 0, be dp[0][0]=0, Otherwise 1
int dp1 = s.charAt(0) == '1' ? 0 : 1;// The first 0 The bit character is 1, be dp[0][1]=0, Otherwise 1
for (int i = 1; i < n; i++) {
int k1 = 0,k2 = 0;
if (s.charAt(i) == '0'){
k2++;
}else {
k1++;
}
// Will be the first i Bit flip to 0 Minimum number of flips by dp[i-1][0] + (s[i] == ‘1’ ? 1 : 0)
k1 += dp0;
// Will be the first i Bit flip to 1 Minimum number of flips by Math.min(dp[i-1][0],dp[i-1[1]) + (s[i] == '0' ? 1 : 0)
k2 += Math.min(dp0,dp1);
dp0 = k1;
dp1 = k2;
}
// Finally, return to the n-1 Bit flip to 0 Or flip to 1 The lesser of
return Math.min(dp0,dp1);
}
}
The finger of the sword Offer II 093. The longest Fibonacci sequence 【 Medium question 】
Ideas :【 Dynamic programming 】【 Double pointer 】
Refer to the explanation of the question
Dynamic programming + Double pointer , It is fast !
Code :
class Solution {
public int lenLongestFibSubseq(int[] arr) {
int n = arr.length,max = 0;
//dp[i][j] by arr Subscript in array i j The position number is the legal Fibonacci sequence at the end ,dp[i][j] The value of represents the length of the Fibonacci sequence
int[][] dp = new int[n][n];
// Traversal array , To traverse the number arr[k] Is the end position of the subsequence of the target legal Fibonacci number sequence
for (int k = 2; k < n; k++) {
// Define double pointer i and j among i Indicates the starting position of the target legal Fibonacci number sequence subsequence , The initial value is 0,j Represents the first digit of the end position of the target legal Fibonacci number sequence subsequence , The initial value is k-1
int i = 0, j = k-1;
// When i < j when , stay [i,j] Whether the in window filter exists Objective legitimate Fibonacci number sequence subsequence
while (i < j){
// When arr[i] + arr[j] == arr[k] when , It means that we have found a j k Legal Fibonacci sequence with position number ending ( Here is abbreviated as jk The goal is )
if (arr[i] + arr[j] == arr[k]){
// If dp[i][j] == 0 Said to i j All subsequences ending with position numbers cannot form a legal Fibonacci sequence , therefore jk The length of the target can only be 3
if (dp[i][j] == 0){
dp[j][k] = 3;
}else {
// otherwise jk The length of the target is equal to ij The length of the target +1 And jk The length of the target The maximum between the two
dp[j][k] = Math.max(dp[i][j]+1,dp[j][k]);
}
// Update at this time max by max and jk Target length The maximum between the two
max = Math.max(max,dp[j][k]);
//i Move the pointer to the right. j Move the pointer to the left Continue to search for legal Fibonacci subsequences in the window
i++;
j--;
}else if (arr[i] + arr[j] < arr[k]){
// When arr[i] + arr[j] < arr[k] when , according to arr The property of increasing , In order to make arr[i] + arr[j] ==> arr[k], We should try to shift right i, And then judge again
i++;
}else {
// When arr[i] + arr[j] > arr[k] when , according to arr The property of increasing , In order to make arr[i] + arr[j] ==> arr[k], We should try to shift left j, And then judge again
j--;
}
}
}
// Program execution ,max Keep always yes The maximum length of the legal Fibonacci sequence , Therefore, according to the meaning of the question, the program will return to max that will do
return max;
}
}
边栏推荐
- 视频异常检测数据集 (ShanghaiTech)
- 论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks
- 第十三届蓝桥杯单片机国赛真题
- 程序员未来会成为非常内卷的职业么?
- Remote connection raspberry pie in VNC Viewer Mode
- 实用电路分析3
- Leetcode 1218. 最长定差子序列(提供一种思路)
- Which securities company is good for opening a mobile account? Is online account opening safe?
- 五星认证!知道创宇通过中国信通院内容审核服务系统评测
- After the Computer College changed its examination, the College of Cyberspace Security also changed its examination! Nanjing University of technology computer postgraduate entrance examination
猜你喜欢

计算机学院改考后,网络空间安全学院也改考了!南京理工大学计算机考研

基于FPGA的电磁超声脉冲压缩检测系统 论文+源文件
![[unity] instructions for beginners of textanimator plug-in](/img/aa/a406c70a28931ac138e65787a0aabd.png)
[unity] instructions for beginners of textanimator plug-in

深入理解 padding

【Wwise】Wwise嵌入Unity后打包出现没有声音问题

论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..

Reading papers (51):integration of a holonic organizational control architecture and multiobjective

SimpleDateFormat在多线程环境下存在线程安全问题。

VNC Viewer方式的远程连接树莓派

After the Computer College changed its examination, the College of Cyberspace Security also changed its examination! Nanjing University of technology computer postgraduate entrance examination
随机推荐
Baidu AI Cloud product upgrade Observatory in May
[failure announcement] there is a problem with the redis that replaces memcached, causing the website to fail
[esp8266-01s] get weather, city, Beijing time
Landing of global organizational structure control
Detailed explanation of ssl/tls principle and packet capturing
[sword finger offer] 46 Translate numbers into strings
深入理解 padding
【ESP8266-01s】获取天气,城市,北京时间
Univariate quadratic equation to gauge field
科技互动沙盘是凭借什么收获人气的
【Unity】插件TextAnimator 新手使用说明
How to use R language to draw scatter diagram
Alien world, real presentation, how does the alien version of Pokemon go achieve?
一,数组--滑动窗口问题--长度最小的子数组--水果成篮问题
实用电路分析3
How to make validity table
Imeta | Nannong shenqirong team released microbial network analysis and visualization R package ggclusternet
【Wwise】Wwise嵌入Unity后打包出现没有声音问题
论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..
Self taught programming introduction, what language to learn first?