当前位置:网站首页>324. swing sort II / Sword finger offer II 102 Target value of addition and subtraction
324. swing sort II / Sword finger offer II 102 Target value of addition and subtraction
2022-06-29 08:23:00 【Biqiliang】
324. Swing sort II【 Medium question 】【 A daily topic 】
Ideas :
Learning problem solving .
【Nick~Hot topic 】 Relatively simple double pointer idea !
Code :
class Solution {
public void wiggleSort(int[] nums) {
int n = nums.length;
// Copy nums The array is copy
int[] copy = Arrays.copyOf(nums,n);
// Yes copy Array to sort
Arrays.sort(copy);
// take copy The array is divided into two parts , Both the left side and the right side increase in turn ( Possible equality ), The left side is all smaller than the right side , Definition left Is the right endpoint pointer on the left ,right Is the right endpoint pointer on the right
int left = (n - 1) / 2,right = n - 1;
// Arrange the left and right elements in reverse order , Then cross update the elements of the array , Even subscripts place the left elements in reverse order , Odd subscripts place the right elements in reverse order
for (int i = 0; i < n; i++) {
if (i % 2 == 0){
// Even subscripts are placed in reverse order on the left element
nums[i] = copy[left--];
}else {
// Odd subscripts are placed in reverse order on the right element
nums[i] = copy[right--];
}
}
}
}
The finger of the sword Offer II 102. The target value of addition and subtraction 【 Medium question 】
Ideas :【 Dynamic programming 】
First of all, we need to re model the problem , Transform the problem into a dynamic programming problem .
Write the first order according to the solution dp And second order dp as follows .
Code :【 Second order dp】
class Solution {
public int findTargetSumWays(int[] nums, int target) {
/** * According to the solution to the problem , We need to change the direction of solving this problem first , Then use dynamic programming to solve . * Set the sum of the elements of the array to sum, Set the sum of the elements with a minus sign before the element to neg , Then the sum of the other elements with the plus sign is sum - neg, therefore * sum - neg - neg = target ==> neg = (sum-target)/2 * Because the elements in the array are non negative integers , therefore neg Must also be a non negative integer therefore sum - target Must be Nonnegative even number , If this condition is not satisfied Then return directly 0, Indicates that there is no expression that meets the requirements * After excluding special circumstances , because sum only , And you can find out ,target Given , So the question turns to Select any element in the array , Find the sum of the selected elements equal to sum-target Number of alternatives */
int sum = Arrays.stream(nums).sum();
if (sum - target < 0 || (sum - target) % 2 != 0){
return 0;
}
int n = nums.length,neg = (sum - target) / 2;
// Definition dp Array dp[i][j] Express Before the array i Elements Select any number of elements and by j Number of alternatives
int[][] dp = new int[n+1][neg+1];
// The boundary conditions Don't select element , Then the element and must be 0, Then when the required sum is 0 when , This is a plan , When required and not for 0 when , No scheme meets the requirements dp[0][j] = 0(j!=0) Keep the default value
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= neg; j++) {
// If j >= nums[i-1] So the current element nums[i-1] No choice , Both cases should be considered
if (j >= nums[i-1]){
// No election The current number of schemes depends on dp[i-1][j]
// choose The current number of schemes depends on dp[i-1][j-nums[i]]
dp[i][j] = dp[i-1][j] + dp[i-1][j-nums[i-1]];
}else {
// If j < nums[i-1] So the current element nums[i-1] You must not choose , Because I have chosen and must surpass j
dp[i][j] = dp[i-1][j];
}
}
}
//dp[n][neg] That is to say All elements of the array And for neg Number of alternatives
return dp[n][neg];
}
}
Code :【 First order dp】
class Solution {
public int findTargetSumWays(int[] nums, int target) {
/** * According to the solution to the problem , We need to change the direction of solving this problem first , Then use dynamic programming to solve . * Set the sum of the elements of the array to sum, Set the sum of the elements with a minus sign before the element to neg , Then the sum of the other elements with the plus sign is sum - neg, therefore * sum - neg - neg = target ==> neg = (sum-target)/2 * Because the elements in the array are non negative integers , therefore neg Must also be a non negative integer therefore sum - target Must be Nonnegative even number , If this condition is not satisfied Then return directly 0, Indicates that there is no expression that meets the requirements * After excluding special circumstances , because sum only , And you can find out ,target Given , So the question turns to Select any element in the array , Find the sum of the selected elements equal to sum-target Number of alternatives */
int sum = Arrays.stream(nums).sum();
if (sum - target < 0 || (sum - target) % 2 != 0){
return 0;
}
int n = nums.length,neg = (sum - target) / 2;
// Definition dp Array dp[j] Express Before the array i(i Dynamically adjust according to the actual situation , For the initial 0, And then from 1 Gradually increasing ) Elements Take the sum of any number of elements as j Number of alternatives
int[] dp = new int[neg+1];
// The boundary conditions And for 0 Before 0 The number of schemes with elements is 1
dp[0] = 1;
for (int i = 0; i < n; i++) {
// Gradually expand the window of optional elements
for (int j = neg; j >= nums[i]; j--) {
// In order to prevent dp[j-nums[i]] It is updated during calculation , So we traverse in reverse order dp Array
// Traversal in reverse order j == nums[i] The deadline is because j<nums[i] when ,dp[j] = dp[j]
// The transfer equation is as follows :
dp[j] = dp[j] + dp[j-nums[i]];
}
}
// At the end of the loop ,i by n, So at this time dp[neg] That is to say All elements of the array Take the sum of any number of elements as neg Number of alternatives
return dp[neg];
}
}
边栏推荐
- 《乔布斯传》英文原著重点词汇笔记(八)【 chapter six 】
- Improvement direction of programming ability
- In PHP version 7.1.13, it is found that floating-point data passes through JSON during use_ There will be precision problems after encode
- NLP标注工具:Label Studio实现多用户协作打标
- Flutter 文件读写-path_provider
- Taro 介绍
- 324. 摆动排序 II / 剑指 Offer II 102. 加减的目标值
- C compiler - implicit function declaration
- AC automata
- Why are two SQL statements that execute very fast, especially after the Union
猜你喜欢

SizeBalanceTree

Excel中VLOOKUP函数简易使用——精确匹配或近似匹配数据

语音信号处理-基础(一):声学基础知识

After crossing, she said that the multiverse really exists

Hands on deep learning (I) -- linear neural network

Segment tree and use

Simple use of AWS elastic Beanstalk

Binary search tree

Automatic operation and maintenance management platform - construction and daily use of SPuG

Introduction to taro
随机推荐
[6G] collation of white paper on computing network technology
Codeforces Round #799 (Div. 4)
Binary search tree
关于组织2021-2022全国青少年电子信息 智能创新大赛西北赛区(陕西)复赛的通知
js:Array. Reduce cumulative calculation and array consolidation
关于SQL语句的大小写
NOR flash application layer operation
[quantitative investment system]django realizes screening and paging from the database
Voice annotation tool: Praat
Swift中@dynamicMemberLookup和callAsFunction特性实现对象透明代理功能
苹果开发者容易招致调查的若干行为
Un voyage profond d'IA dans Huawei Cloud
PostgreSQL installation: the database cluster initialization failed, stack hbuilder installation
MySQL system keyword summary (official website)
城通网盘仿蓝奏网盘源码 附带视频教程
Paddlenlp general information extraction model: UIE [information extraction {entity relationship extraction, Chinese word segmentation, accurate entity markers, emotion analysis, etc.}, text error cor
VMware vcenter/esxi series vulnerability summary
语音标注工具:Praat
图文详解JVM中的垃圾回收机制(GC)
Stm32 usart+dma usage based on Hal Library