ARTS punch Week 13

A:Beautiful Arrangement II    Medium

题目:Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requirement: 

Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly k distinct integers.

If there are multiple answers, print any of them.  The n and k are in the range 1 <= k < n <= 10^4.

The meaning of problems: Given two integers n and k, we return requirements as an array of length n, the different elements are integers from 1 to n, the absolute value of the difference required to satisfy two adjacent elements of K kinds of situations, There are a variety result, one output,

n and k satisfy 1 <= k <n <= 10 ^ 4.

Ideas:

One is the difference between the standard value of k 1 to k, nk priority confirmed front array elements, 1,2,3,4, ...., nk, the remaining elements of the k-1 difference values ​​of k satisfies , k-1, k-2, .... 1, element is n, nk-1, n-1, nk-2, n-2, ...

One is the difference from the standard value n is n-1, n-2, n-2, ... difference between the last element of nk-1 is 1, the beat element is 1, n, 2, n- 1,3, n-2 ...

Program: k is the difference in value of the standard: https://leetcode.com/submissions/detail/236091557/

  N is in the difference value criterion:https://leetcode.com/submissions/detail/236091899/

R: http://open.163.com/movie/2010/12/6/U/M6UTT5U0I_M6V2TGB6U.html   << >> binary search tree, binary search tree has sort of comparable to the fast speed, while the completion of insertion , delete, and search and other dynamic operation,

Efficient and easy to implement. Generating a binary tree corresponding to the same time complexity nlogn and the quick sort, only when the input array is an ordered array, and the depth corresponding to the generated binary time complexity would be n ^ 2.

Using a random algorithm and other tree root node confirmation helps avoid such extreme conditions, so that the time complexity can be stabilized nlogn.

T: recording a two-dimensional array of processing technique of the border situation, no case of the current node of eight nodes around one by one judge, the code is more concise and clear, direct exclusion of four kinds of illegal location:

/ **
* processing data inside the
* @param nums array
* @param x rows
* @param y columns
* @return
* /
Private GetSum int (int [] [] the nums, int X, Y int) {
  int NX nums.length =;
  int = NY the nums [0] .length;
  int SUM = 0;
  // elements present on the periphery of the number of two-dimensional array
  for (int I = -1; I <=. 1; I ++) {
    for (int J = -1; J <=. 1; J ++) {
      // data is not valid
      if (x + i <0 || x + i> = nx || y + j <0 || y + j> = ny ) {
        Continue;
      }
      SUM + the nums = [I + X] [Y + J];
    }
  }
  return SUM;
}

S: Share Wang Zheng "How to weigh the choice of which to use data structures and algorithms," is not blind pursuit of the fastest advanced algorithms, the data need to scale and business scenarios,

Maintainability and so on to carry out a comprehensive overall consideration, the usual business scenarios, small scale data, advanced algorithms and brute force efficiency is close,

And of course maintenance simple brute force algorithm will be higher, based on the need to discuss the situation, not blindly be done quickly.

Guess you like

Origin www.cnblogs.com/wujunjie-Blog/p/11033033.html