Find the largest span between the same number in a

2020-08-05 10:08发布


Merry Christmas and hope you are in great Spirits,I have a Question in Java-Arrays as shown below.Im stuck up with this struggling to get it rite.

Consider the leftmost and righmost appearances of some value in an array. We'll say that the "span" is the number of elements between the two inclusive. A single value has a span of 1. Write a **Java Function** that returns the largest span found in the given array.

**Example:
maxSpan({1, 2, 1, 1, 3}) → 4,answer is 4 coz MaxSpan between 1 to 1 is 4
maxSpan({1, 4, 2, 1, 4, 1, 4}) → 6,answer is 6 coz MaxSpan between 4 to 4 is 6
maxSpan({1, 4, 2, 1, 4, 4, 4}) → 6,answer is 6 coz Maxspan between 4 to 4 is 6 which is greater than MaxSpan between 1 and 1 which is 4,Hence 6>4 answer is 6.

I have the code which is not working,it includes all the Spans for a given element,im unable to find the MaxSpan for a given element.

Please help me out.

Results of the above Program are as shown below

Expected This Run
maxSpan({1, 2, 1, 1, 3}) → 4 5 X
maxSpan({1, 4, 2, 1, 4, 1, 4}) → 6 8 X
maxSpan({1, 4, 2, 1, 4, 4, 4}) → 6 9 X
maxSpan({3, 3, 3}) → 3 5 X
maxSpan({3, 9, 3}) → 3 3 OK
maxSpan({3, 9, 9}) → 2 3 X
maxSpan({3, 9}) → 1 1 OK
maxSpan({3, 3}) → 2 3 X
maxSpan({}) → 0 1 X
maxSpan({1}) → 1 1 OK

::Code::

public int maxSpan(int[] nums) {    
    int count=1;//keep an intial count of maxspan=1    
    int maxspan=0;//initialize maxspan=0    
    for(int i=0;i<nums.length;i++){    
        for(int j=i+1;j<nums.length;j++){    
              if(nums[i] == nums[j]){
                 //check to see if "i" index contents == "j" index contents    
                 count++;    //increment count
                 maxspan=count;  //make maxspan as your final count  
                 int number = nums[i]; //number=actual number for maxspan               
              }                            
        }       
    }    
  return maxspan+1; //return maxspan        
}    

标签: java arrays
23条回答
一夜七次
2楼-- · 2020-08-05 10:11
public int maxSpan(int[] nums) {
  int max_span=0, j;
  for (int i=0; i<nums.length; i++){
    j=nums.length-1;
    while(nums[i]!=nums[j]) j--;
    if (j-i+1>max_span) max_span=j-i+1;
  }
  return max_span;
}
查看更多
beautiful°
3楼-- · 2020-08-05 10:13
public int maxSpan(int[] nums) {
  Stack stack = new Stack();
  int count = 1;
  int value = 0;
  int temp = 0;
  if(nums.length < 1) {
    return value;
  }
  for(int i = 0; i < nums.length; i++) {
    for(int j = nums.length - 1; j >= i; j--) {
      if(nums[i] == nums[j]) {
        count = (j - i) + 1;
        stack.push(count);
        count = 1;
        break;
      }
    }
  }
  if(stack.peek() != null) {
  while(stack.size() != 0) {
    temp = (Integer) stack.pop();
    if(value <= temp) {
      value = temp;
    } else {
      value = value;
    }
  }
  }
  return value;
}
查看更多
爷、活的狠高调
4楼-- · 2020-08-05 10:16

Here is the solution of this problem:

public int maxSpan(int[] nums) {
        int maxSpan=0;
        int tempSpan=0;

        if(nums.length==0){
            return 0;
        }

        for(int i=0;i<nums.length;i++){   
            for(int j=nums.length-1;j>i;j--){ 
                if(nums[i]==nums[j]){
                    tempSpan=j-i;
                    break;
                } 
            } 
            if(tempSpan>maxSpan){
                maxSpan=tempSpan;
            } 
        } 
        return maxSpan+1;
    }
查看更多
走好不送
5楼-- · 2020-08-05 10:17

I am not sure, if I have to use 2 for-loops... or any loop at all?

If not, this version functions without any loop.

At first you check, if the length of the array is > 0. If not, you simply return the length of the array, which will correspond to the correct answer.

If it is longer than 0, you check if the first and last position in the array have the same value.

If yes, you return the length of the array as the maxSpan.

If not, you substract a 1, since the value appears twice in the array.

Done.

public int maxSpan(int[] nums) {
  if(nums.length > 0){
    if(nums[0] == nums[nums.length - 1]){
      return nums.length;
    }
    else{
      return nums.length - 1;  
    }   
  }
  return nums.length;
}
查看更多
闹够了就滚
6楼-- · 2020-08-05 10:17

Linear solution with a Map storing the first occurrence, and calculating the distance from it for the next occurrences:

public int maxSpan(int[] nums) {
  int span = 0;
  Map<Integer, Integer> first = new HashMap<Integer, Integer>();
  for (int i = 0; i < nums.length; i++) {
    if (!first.containsKey(nums[i])) 
      first.put(nums[i], i);
    span = Math.max(span, (i - first.get(nums[i])) + 1);
  }
  return span;
}
查看更多
地球回转人心会变
7楼-- · 2020-08-05 10:18

Here is the solution -

public int maxSpan(int[] nums) {
  int span = 0;
  for (int i = 0; i < nums.length; i++) {
    for(int j = i; j < nums.length; j++) {
      if(nums[i] == nums[j]) {
        if(span < (j - i + 1)) {
          span = j -i + 1;
        }
      }
    }
  }
  return span;
}
查看更多
登录 后发表回答