绠楁硶鍔ㄦ€佽鍒掞紙浜旓級-鑳屽寘闂2
Posted Coding鐨勫摂鍝斿彣鍙?/a>
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绠楁硶鍔ㄦ€佽鍒掞紙浜旓級-鑳屽寘闂2相关的知识,希望对你有一定的参考价值。
鈿狅笍浠婂ぉ缁х画鎴戜滑鐨?-1鑳屽寘闂銆?/span>
馃憠馃憖馃挰浠婃棩缁冧範锛堜竴锛?span class="mq-9">涓€鍜岄浂锛圠eetCode-474锛夈€?/span>
缁欏畾m涓?銆乶涓?鍜屼竴涓彧鐢?銆?鏋勬垚瀛楃涓茬殑鏁扮粍锛屼娇鐢ㄧ粰瀹氱殑m涓?鍜宯涓?鎷兼垚鏁扮粍涓殑瀛楃涓茬殑鏈€澶ф暟閲忥紝姣忎釜0鍜?鏈€澶氫娇鐢ㄤ竴娆?/span>銆備妇渚嬶細
杈撳叆: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
杈撳嚭: 4
瑙i噴: 鎬诲叡 4 涓瓧绗︿覆鍙互閫氳繃 5 涓?nbsp;0 鍜?nbsp;3 涓?nbsp;1 鎷煎嚭锛屽嵆 "10","0001","1","0" 銆?/span>
杈撳叆: Array = {"10", "0", "1"}, m = 1, n = 1
杈撳嚭: 2
瑙i噴: 鎴戜滑鍙互鎷煎嚭 "10"锛屼絾涔嬪悗灏辨病鏈夊墿浣欐暟瀛椾簡銆傛洿濂界殑閫夋嫨鏄嫾鍑?nbsp;"0" 鍜?nbsp;"1" 銆?/span>
馃檱鎬濊矾锛?/span>
姝ら渚濈劧鏄?-1鑳屽寘闂銆?br>
鐘舵€佸畾涔夛細dp[i][j][k]琛ㄧず锛屽瓧绗︿覆鏁扮粍[0 , i]鍖洪棿鍐呯殑瀛楃涓诧紝鍙互鐢╦涓?銆乲涓?琛ㄧず鐨勬渶澶ф暟閲忋€?/span>
杞€佽浆绉?/span>锛氭暟缁勪腑绗琲涓厓绱犻€夋嫨鎴栦笉閫夋嫨涓ょ鐘舵€併€?/span>
涓嶉€夈€?span class="mq-32">dp[i][j][k] = dp[i-1][j][k] 鍓峣-1涓暟缁勫厓绱犲彲浠ョ敤j銆乲涓?鎴?鍙互琛ㄧず鐨勬渶澶ф暟閲忋€?/p>
閫夋嫨銆傜敤cn[]鏁扮粍鏉ヨ〃绀虹i涓瓧绗︿覆涓?銆?鐨勪釜鏁帮紝鐘舵€佽浆绉绘柟绋嬩负锛?span class="mq-49">dp[i][j][k] = dp[i-1][j-cn[0]][k-cn[1]] + 1 鍓峣-1涓暟缁勫厓绱狅紝鍙兘鐢ㄥ墿涓嬬殑(j-cn[0])涓?鍜?span class="mq-68">(k-cn[1])涓?鍙互琛ㄧず鍑虹殑鏈€澶ф暟閲忋€?/p>
缁煎悎锛屾渶鍚庣殑鐘舵€佽浆绉绘柟绋嬭〃绀轰负锛?br>dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j-cn[0]][k-cn[1]] + 1)
鏈€缁堣緭鍑?/span>锛歞p[len-1][m][n]
浠g爜锛?br>
public int findMaxForm(String[] strs, int m, int n) {
int len = strs.length;
int[][][] dp = new int[len+1][m+1][n+1];
// i 浠?寮€濮嬶紝淇濊瘉瑙勫垝涓笂琛屾槸瀛樺湪鐨勩€?/span>
for(int i=1;i<=len;i++){
int[] cn = count(strs[i-1]);
for(int j=0;j<=m;j++){
for(int k=0;k<=n;k++){
// 涓嶈涔嬪悗鐨勫垎鏋愭槸鍟ワ紝鑷冲皯鏄笂涓€琛岀殑澶撮《鐨勮繖涓暟閲忋€?/span>
dp[i][j][k] = dp[i-1][j][k];
if(j>=cn[0] && k>=cn[1]){
dp[i][j][k]=Math.max(dp[i-1][j][k],dp[i-1][j-cn[0]][k-cn[1]]+1);
}
}
}
}
return dp[len][m][n];
}
private int[] count(String str){
int[] cn = new int[2];
for(char c:str.toCharArray()){
cn[c-'0']++;
}
return cn;
}
馃檵绌洪棿浼樺寲
馃檱鎬濊矾锛?/span>
鍦ㄤ笂闈㈣В娉曚腑锛屾垜浠緷鏃ф槸鍙弬鑰冧簡褰撳墠琛岀殑涓婁竴琛岋紝鎵€浠ヨ繖鍎挎垜浠浜庣┖闂寸殑浼樺寲灏辨槸涓€涓€濊矾闄嶇淮銆傞噰鐢ㄤ簩缁存暟缁勬潵杩涜閫嗗悜濉〃銆?/span>
浠g爜锛?/span>
public int findMaxForm(String[] strs, int m, int n) {
int len = strs.length;
int[][] dp = new int[m+1][n+1];
// i 浠?寮€濮嬶紝淇濊瘉瑙勫垝涓笂琛屾槸瀛樺湪鐨勩€?/span>
for(int i=1;i<=len;i++){
int[] cn = count(strs[i-1]);
for(int j=m;j>=cn[0];j--){
for(int k=n;k>=cn[1];k--){
dp[j][k]= Math.max(dp[j][k],dp[j-cn[0]][k-cn[1]]+1);
}
}
}
return dp[m][n];
}
private int[] count(String str){
int[] cn = new int[2];
for(char c:str.toCharArray()){
cn[c-'0']++;
}
return cn;
}
馃憠馃憖馃挰浠婃棩缁冧範锛堜簩锛?span>鐩爣鍜?/strong>锛圠eetCode-494锛夈€?/span>
缁欏畾涓€涓潪璐熸暣鏁版暟缁刵ums锛屽拰涓€涓洰鏍囨暟S锛屾暟缁勪腑姣忎竴涓暟鍓嶉兘鍙互娣诲姞+鎴?绗﹀彿锛屾坊鍔犲畬鍚庝娇寰楁暟缁勭殑鍜屼负S锛屾壘鍑哄彲浠ユ坊鍔犵鍙风殑缁勫悎鏁般€?/span>涓句緥锛?/span>
杈撳叆锛?span class="code-snippet__selector-tag">nums[1,2,3,4,5] S=3
杈撳嚭锛?
瑙i噴锛?/span>
鍙互娣诲姞绗﹀彿鐨勬儏鍐垫湁浠ヤ笅3绉?/span>
-1+2+3+4-5=3
-1-2-3+4+5=3
1-2+3-4+5=3
dp[i][j]=dp[i-1][j-nums[i]]+dp[i-1][j+nums[i]]
馃檱鎬濊矾锛?/span>
閿欒鎬濊矾锛?br>
涓€寮€濮嬫垜鐨勬€濊矾鏄i涓暟閫夋嫨-鎴栬€呴€夋嫨+锛岀姸鎬佹柟绋嬩负锛?span class="mq-134">dp[i][j]=dp[i-1][j-nums[i]]+dp[i-1][j+nums[i]]涓嶉毦鐪嬪嚭锛岀姸鎬佹柟绋嬩腑j+nums[i]鐨勮竟鐣屾垜浠棤娉曡幏鐭ヤ簡銆?/span>
姝g‘鐨勬€濊矾锛?br>
浠旂粏鐪侀锛屾垜浠笉闅惧彂鐜伴鐩腑鏃犻潪灏辨槸鏈塵涓鏁板姞n涓礋鏁帮紝鏈€鍚庣殑缁撴灉涓篠锛岃繖閬撻鍏跺疄鍜屾垜浠箣鍓嶅仛杩囩殑鍒嗗壊绛夊拰瀛愰泦锛圠eetCode-416锛?/span>寰堝儚銆?16鏄鎵惧嚭涓や釜绛夊拰瀛愰泦锛岃€岃繖閬撻鎹釜鎬濊矾灏辨槸锛屾壘鍑轰袱涓瓙闆嗕娇寰楀拰涓篠銆傛墍浠ワ紝杩欏効鎴戜滑灏嗙鍙蜂负+鐨勫閲忓畾涓篨锛岀鍙蜂负璐熺殑瀹归噺瀹氫负Y锛屾暟缁勭殑鎬诲拰涓簊um锛屾垜浠彲浠ュ緱鍒?/span>X+Y=sum锛沊-Y=S銆傚熀浜庤繖涓や釜绛夊紡锛屾垜浠彲浠ュ緱鍒癤=(S+sum)/2锛涗笖S+sum蹇呴』涓哄伓鏁般€?/span>
閫氳繃棰樼洰鐨勬€濊矾杞崲锛屾渶鍚庢垜浠氨灏嗘湰棰樿浆鎹㈡垚涓€涓畝鍗曠殑0-1鑳屽寘闂锛屽嵆浠庢暟缁刵ums涓壘鍑轰竴浜涙暟锛屼娇寰楀拰涓篨锛屾壘鍑烘墍鏈夌殑缁勫悎鏂瑰紡銆?br>
鐘舵€佸畾涔夛細dp[i][j]琛ㄧず锛屾暟缁刐0 , i]鍖洪棿鍐呭彲浠ユ壘鍒板拰涓簀鐨勭粍鍚堟暟銆?/span>
杞€佽浆绉?/span>锛氭暟缁勪腑绗琲涓厓绱犻€夋嫨鎴栦笉閫夋嫨涓ょ鐘舵€併€?/span>
涓嶉€夈€?span class="mq-163">dp[i][j] = dp[i-1][j]鍓峣-1涓暟鍙互鎵惧埌鍜屼负j鐨勬暟閲忋€?/p>
閫夋嫨銆?span class="mq-175">dp[i][j] = dp[i-1][j-nums[i]]銆傚墠i-1涓暟鍙互鎵惧埌鍜屼负(j-nums[i])鐨勬暟閲忋€?/p>
缁煎悎锛屾渶鍚庣殑鐘舵€佽浆绉绘柟绋嬭〃绀轰负锛?br>dp[i][j] = dp[i-1][j]+dp[i-1][j-nums[i]]銆?/span>
鏈€缁堣緭鍑?/span>锛歞p[len][X]銆?/span>
浠g爜锛堟垜浠繖鍎垮氨鐩存帴灏遍€夋嫨绌洪棿浼樺寲鐨勫仛娉曪級锛?/span>
public int findTargetSumWays(int[] nums, int S) {
int len = nums.length;
int sum = 0;
for(int num:nums){
sum+=num;
}
if(((S+sum)&1) == 1){
return 0;
}
// 鐩爣鍜屼笉鍙兘澶т簬鎬诲拰
if (S>sum){
return 0;
}
int X = (S+sum)/2;
int[] dp = new int[X+1];
dp[0]=1;
for(int i=0;i<len;i++){
for(int j=X;j>=nums[i];j--){
dp[j]+=dp[j-nums[i]];
}
}
return dp[X];
}
涓嶇Н璺锛屾棤浠ヨ嚦鍗冮噷銆?/strong>
鏂囩珷鏈夊府鍔╃殑璇濓紝鐐逛釜杞彂銆佸湪鐪嬪憲銆?/strong>
璋㈣阿鏀寔鍝?(*^__^*)
END
馃憞
以上是关于绠楁硶鍔ㄦ€佽鍒掞紙浜旓級-鑳屽寘闂2的主要内容,如果未能解决你的问题,请参考以下文章