Focus on classic interview questions: Array/String (2)

189. Rotate array

medium

Given an array of integers  nums, rotate the elements in the array to the right by  k positions where they  k are non-negative numbers.

Example 1:

Input: nums = [1,2,3,4,5,6,7], k = 3
 Output:  [5,6,7,1,2,3,4]
Explanation: 
Rotate right 1 step: [7,1,2,3,4,5,6]
Rotate right 2 steps: [6,7,1,2,3,4,5]
Rotate right 3 steps:[5,6,7,1,2,3,4]

Example 2:

Input: nums = [-1,-100,3,99], k = 2
 Output: [3,99,-1,-100]
 Explanation:  
Rotate 1 step to the right: [99,-1,-100,3 ] 
Rotate 2 steps to the right: [3,99,-1,-100]

hint:

  • 1 <= nums.length <= 105
  • -231 <= nums[i] <= 231 - 1
  • 0 <= k <= 105
void rotate(int* nums, int numsSize, int k) {
    k=k%numsSize;
    if(k==0){
        return;
    }
    int a[k],j=0;
    for(int i=numsSize-k;i<numsSize;i++){
        a[j++]=nums[i];
    }
    for(int i=numsSize-k-1;i>=0;i--){
       nums[i+k]=nums[i];
    }
    for(int i=0;i<k;i++){
        nums[i]=a[i];
    }
}

121. Best time to buy and sell stocks

Simple

Given an array  prices , its th  i element  prices[i] represents the price of a given stock on  i day th.

You can only choose  to buy the stock on one day  and   sell the stock on a different day in the future . Design an algorithm to calculate the maximum profit you can make.

Returns the maximum profit you can make from this trade. If you can't make any profit, return  0 .

Example 1:

Input: [7,1,5,3,6,4]
 Output: 5
 Explanation: Buy on day 2 (stock price = 1), sell on day 5 (stock price = 6), Maximum profit = 6-1 = 5. 
     Note that the profit cannot be 7-1 = 6, because the selling price needs to be greater than the buying price; at the same time, you cannot sell the stock before buying.

Example 2:

Input: prices = [7,6,4,3,1]
 Output: 0
 Explanation: In this case, no transaction is completed, so the maximum profit is 0.

hint:

  • 1 <= prices.length <= 105
  • 0 <= prices[i] <= 104
int maxProfit(int* prices, int pricesSize) {
    int min=99999,max=0;
    for(int i=0;i<pricesSize;i++){
        if(prices[i]>min&&max<(prices[i]-min)){
               max=prices[i]-min;
        }
        if(prices[i]<min){
            min=prices[i];
        }
    }
    return max;
   
}

122. Best times to buy and sell stocks II

medium

You are given an array of integers  prices that  prices[i] represents the price of a certain stock on  i day 1.

On each day, you can decide whether to buy and/or sell stocks.  You can only hold  a maximum of one share at any time   . You can also buy first and then sell on  the same day  .

Return  the maximum  profit you can make   .

Example 1:

Input: prices = [7,1,5,3,6,4]
 Output: 7
 Explanation: Buy on day 2 (stock price = 1) and sell on day 3 (stock price = 5) Out, the profit from this transaction = 5 - 1 = 4. 
     Subsequently, if you buy on the 4th day (stock price = 3) and sell on the 5th day (stock price = 6), the profit from this transaction = 6 - 3 = 3. 
     The total profit is 4 + 3 = 7.

Example 2:

Input: prices = [1,2,3,4,5]
 Output: 4
 Explanation: Buy on day 1 (stock price = 1), sell on day 5 (stock price = 5), The profit from this transaction = 5 - 1 = 4. 
     The total profit is 4.

Example 3:

Input: prices = [7,6,4,3,1]
 Output: 0
 Explanation: In this case, the transaction cannot obtain positive profit, so the maximum profit can be obtained by not participating in the transaction, and the maximum profit is 0.

hint:

  • 1 <= prices.length <= 3 * 104
  • 0 <= prices[i] <= 104
int maxProfit(int* prices, int pricesSize) {
    int max=0;
    for(int i=0;i<pricesSize-1;i++){
          if(prices[i]<prices[i+1]){
            max=max+(prices[i+1]-prices[i]);
          }
    }
    return max;
}

55. Jumping Game

medium

Given an array of non-negative integers  nums , you are initially at  the first index of the array  . Each element in the array represents the maximum length you can jump at that position.

Determine whether you can reach the last subscript, and if so, return  true ; otherwise, return  false .

Example 1:

Input: nums = [2,3,1,1,4]
 Output: true
 Explanation: You can jump 1 step first, from subscript 0 to subscript 1, and then jump 3 steps from subscript 1 to the last subscript.

Example 2:

Input: nums = [3,2,1,0,4]
 Output: false
 Explanation: No matter what, the position with index 3 will always be reached. But the maximum jump length of this index is 0, so the last index can never be reached.

hint:

  • 1 <= nums.length <= 104
  • 0 <= nums[i] <= 105
bool canJump(int* nums, int numsSize) {
    int a[10000]={0};
    a[numsSize-1]=1;
    for(int i=numsSize-2;i>=0;i--){
        for(int j=i+1;j<numsSize&&j<=i+nums[i];j++){
             if(a[j]==1){
                 a[i]=1;
                 break;
             }
        }
    }
    if(a[0]==1){
        return true;
    }
    return false;
}

45. Jump Game II

medium

Given a  0-indexed integer array  n of  length . The initial position is  .numsnums[0]

Each element  nums[i] represents  i the maximum length to jump forward from the index. In other words, if you are at  nums[i] , you can jump to anywhere  nums[i + j] :

  • 0 <= j <= nums[i] 
  • i + j < n

Returns  nums[n - 1] the minimum number of hops reached. The generated test cases can be reached  nums[n - 1].

Example 1:

Input: nums = [2,3,1,1,4]
 Output: 2
 Explanation: The minimum number of jumps to the last position is 2. 
     Jump from index 0 to index 1, jump 1 step, and then jump 3 steps to reach the last position of the array.

Example 2:

Input: nums = [2,3,0,1,4]
 Output: 2

hint:

  • 1 <= nums.length <= 104
  • 0 <= nums[i] <= 1000
  • The topic is guaranteed to be reachable nums[n-1]

int jump(int* nums, int numsSize){
   int *dp=(int *)malloc(sizeof(int)*numsSize);
   dp[0]=0;
   for(int i = 1 ; i < numsSize ; i++ )
    {
        dp[i] =  numsSize + 1;
    }
   for(int i =1; i< numsSize; i++)
    {
        for(int j = 0; j < i; j++)
        {
            if(j + nums[j] >= i)
            {
                dp[i] = fmin(dp[i],dp[j]+1);
            }
        }
    }
   return dp[numsSize-1];

}

Guess you like

Origin blog.csdn.net/weixin_63357306/article/details/135104400