当前位置:网站首页>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;
}
}
边栏推荐
- The battlefield of live broadcast e-commerce is not in the live broadcast room
- How to make validity table
- leetcode刷题:哈希表02 (两个数组的交集)
- 【故障公告】取代 memcached 的 redis 出现问题造成网站故障
- org. apache. ibatis. binding. BindingException: Invalid bound statement (not found):...
- Wiley- Open Science Joint Symposium of the documentation and information center of the Chinese Academy of Sciences, lecture 2: open access journal selection and paper submission
- Leetcode: hash table 07 (sum of three numbers)
- TT 语音落地 Zadig:开源共创 Helm 接入场景,环境治理搞得定!
- 论文阅读 (58):Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based...
- 《致敬百年巨匠 , 数藏袖珍书票》
猜你喜欢

Paper reading (56):muti features predction of protein translational modification sites (task)

iMeta | 南农沈其荣团队发布微生物网络分析和可视化R包ggClusterNet

论文阅读 (48):A Library of Optimization Algorithms for Organizational Design

Rancher2.6全新Monitoring快速入门

org. apache. ibatis. binding. BindingException: Invalid bound statement (not found):...

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

README

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

What does the science and technology interactive sand table gain popularity by virtue of

客服系统搭建教程_宝塔面板下安装使用方式_可对接公众号_支持APP/h5多租户运营...
随机推荐
论文阅读 (51):Integration of a Holonic Organizational Control Architecture and Multiobjective...
Rancher2.6全新Monitoring快速入门
科技互动沙盘是凭借什么收获人气的
Paper reading (54):deepfool: a simple and accurate method to four deep neural networks
Paper reading (58):research and implementation of global path planning for unmanned surface vehicle based
leetcode刷题:哈希表04 (两数之和)
[esp8266 - 01s] obtenir la météo, Ville, heure de Beijing
torch学习(一):环境配置
Counter attack and defense (1): counter sample generation in image domain
基于FPGA的电磁超声脉冲压缩检测系统 论文+源文件
TT 语音落地 Zadig:开源共创 Helm 接入场景,环境治理搞得定!
How to make good use of daily time to review efficiently?
[sword finger offer] 46 Translate numbers into strings
[learning notes] tidb learning notes (III)
Which securities company is good for opening a mobile account? Is online account opening safe?
Is it safe to open a stock account online in 2022?
微信小程序报错[ app.json 文件内容错误] app.json: app.json 未找到
信用卡产品开发周期从23周缩短至9周,银行运维组织如何转向敏捷?
一,数组--滑动窗口问题--长度最小的子数组--水果成篮问题
[sword finger offer] 45 Arrange the array into the smallest number