122. Best Time to Buy and Sell Stock II
You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Total profit is 4 + 3 = 7.
Example 2:
Input: prices = [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Total profit is 4.
Example 3:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
Constraints:
1 0 Original Page
Wrong Code
public int maxProfit(int[] prices) { int profit = 0; int buy = Integer.MAX_VALUE; int sum = 0; int peek = 0; for(int i=0; i<prices.length i int num="prices[i];" if> buy && num > peek){ profit = num - buy; peek = num; } else if((num>buy && num<peek num buy sum profit peek="num;" return> <p>I init buy to int MAX_VALUE and forget to update this may lead to some error.</p> <p>fix this and truncate the codes</p> <h2> Fine Code </h2> <pre class="brush:php;toolbar:false"> public int maxProfit(int[] prices) { if(prices.length peek){ profit = num - buy; peek = num; } else if(num<peek sum profit buy="num;" peek="num;" return> <h2> 1005. Maximize Sum Of Array After K Negations </h2> <p>Given an integer array nums and an integer k, modify the array in the following way:</p> <p>choose an index i and replace nums[i] with -nums[i].<br> You should apply this process exactly k times. You may choose the same index i multiple times.</p> <p>Return the largest possible sum of the array after modifying it in this way.</p> <p>Example 1:</p> <p>Input: nums = [4,2,3], k = 1<br> Output: 5<br> Explanation: Choose index 1 and nums becomes [4,-2,3].<br> Example 2:</p> <p>Input: nums = [3,-1,0,2], k = 3<br> Output: 6<br> Explanation: Choose indices (1, 2, 2) and nums becomes [3,1,0,2].<br> Example 3:</p> <p>Input: nums = [2,-3,-1,5,-4], k = 2<br> Output: 13<br> Explanation: Choose indices (1, 4) and nums becomes [2,3,-1,5,4].</p> <p>Constraints:</p> <p>1 -100 1 Original Page<br> </p> <pre class="brush:php;toolbar:false"> public int largestSumAfterKNegations(int[] nums, int k) { Arrays.sort(nums); int change = nums[nums.length-1] >=0?0:nums.length-1; int sum = 0; for(int i=0; i<nums.length i if k>0){ sum -= nums[i]; k--; }else{ sum += nums[i]; } // find the cross over if(i>0 && nums[i-1]0){ if(-nums[i-1]>nums[i]){ change = i; }else{ change = i-1; } } } // k>nums.length so we need to run out of these k if(k>0){ if(k%2!=0){ //find the min abs value sum -= 2*Math.abs(nums[change]); } } return sum; } </nums.length>
55. Jump Game
You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.
Example 1:
Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
Constraints:
1 0
Wrong Code
public boolean canJump(int[] nums) { //find whether achive the last element so that we only can see whether can reach the second last element for(int i=0; i<nums.length-1 int size="nums[i];" next="0;" nextval="0;" if return false>= nums.length){ return true; } //find the max steps among the current step for(int j=0; jnext){ next = i+j+nums[i+j]; } } i = next; } return true; } </nums.length-1>
Wrong Code 2
public boolean canJump(int[] nums) { if(nums.length==1){ return true; } if(nums[0] == 0){ return false; } for(int i=0; i<nums.length-1 i if>=nums.length-1){ return true; } } return false; } </nums.length-1>
public boolean canJump(int[] nums) { if(nums.length==1){ return true; } int range = 0; for(int i=0; i= nums.length-1){ return true; } } return false; }
45. Jump Game II
You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].
Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:
0 i + j Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].
Example 1:
Input: nums = [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [2,3,0,1,4]
Output: 2
Constraints:
1
0
It's guaranteed that you can reach nums[n - 1].
public int jump(int[] nums) { if(nums.length == 1){ return 0; } int step = 0; int range = 0; int preRange = 0; for(int i=0; i<nums.length-1 i range="Math.max(range," if>= nums.length-1){ step++; break; } if(i == preRange){ preRange = range; step++; } } return step; } </nums.length-1>
The above is the detailed content of LeetCode DayGreedy Algorithms Part 2. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

This article explains Java's Remote Method Invocation (RMI) for building distributed applications. It details interface definition, implementation, registry setup, and client-side invocation, addressing challenges like network issues and security.

This article details Java's socket API for network communication, covering client-server setup, data handling, and crucial considerations like resource management, error handling, and security. It also explores performance optimization techniques, i

This article details creating custom Java networking protocols. It covers protocol definition (data structure, framing, error handling, versioning), implementation (using sockets), data serialization, and best practices (efficiency, security, mainta


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use