Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 最大相容性理解_C#_Algorithm - Fatal编程技术网

C# 最大相容性理解

C# 最大相容性理解,c#,algorithm,C#,Algorithm,我想从一开始就尝试一些关于可变性的挑战。所有的作业都相对容易,直到一个叫做MaxCounters的作业。我不认为这是一个特别困难,虽然它是第一个标记为非无痛的 我已经阅读并开始使用C语言编写代码: public static int[] maxPart(int N, int[] A){ int[] counters = new int[N]; for(int i = 0; i < A.Length; i++){ for(int j = 0; j < c

我想从一开始就尝试一些关于可变性的挑战。所有的作业都相对容易,直到一个叫做MaxCounters的作业。我不认为这是一个特别困难,虽然它是第一个标记为非无痛的

我已经阅读并开始使用C语言编写代码:

public static int[] maxPart(int N, int[] A){
    int[] counters = new int[N];
    for(int i = 0; i < A.Length; i++){
        for(int j = 0; j < counters.Length; j++){
            if(A[i] == counters[j] && (counters[j] >= 1 && counters[j] <= N )){
                counters [j] = counters [j] + 1;
            }
            if(A[i] == N + 1 ){
                int tmpMax = counters.Max ();
                for(int h = 0; h < counters.Length; h++){
                    counters [h] = tmpMax;
                }
            }
        }
    }
    return counters;
}
公共静态int[]maxPart(int N,int[]A){
int[]计数器=新的int[N];
for(int i=0;i如果(A[i]==counters[j]&(counters[j]>=1&&counters[j]在我看来,你不知何故把计数器的索引(A
A
中的值)和计数器的值(A
counter
中的值)混在了一起。因此使用
A[i]-1
并没有什么神奇之处——这是问题描述中的值
X
(调整为基于0的索引)

我的天真方法是,以我理解问题的方式(我希望它清楚地表明,您的代码做错了什么):

最高级别的解决方案以惰性的方式解决问题,并且不会在每次遇到
max counter
操作时显式更新所有值,而是在
resetLimit
中记住此操作后所有计数器必须具有的最小值。因此,每次他必须增加计数器时,他都会查看其由于以前的
max counter
操作,必须更新该值,并弥补他没有在此计数器上执行的所有
max counter
操作

if(result[A[K] - 1] < resetLimit) {
                result[A[K] - 1] = resetLimit + 1;
            }
if(结果[A[K]-1]

他的解决方案运行在
O(n)
中,速度足够快。

这是我的JavaScript解决方案

const maxCounters = (N, A) => {
  for (let t = 0; t < A.length; t++) {
    if (A[t] < 1 || A[t] > N + 1) {
      throw new Error('Invalid input array A');
    }
  }
  let lastMaxCounter = 0; // save the last max counter is applied to all counters
  let counters = []; // counters result
  // init values by 0
  for (let i = 0; i < N; i++) {
    counters[i] = 0;
  }
  let currentMaxCounter = 0; // save the current max counter each time any counter is increased
  let maxApplied = false;
  for (let j = 0; j < A.length; j++) {
    const val = A[j];
    if (1 <= val && val <= N) {
      if (maxApplied && counters[val - 1] < lastMaxCounter) {
        counters[val - 1] = lastMaxCounter;
      }
      counters[val - 1] = counters[val - 1] + 1;
      if (currentMaxCounter < counters[val - 1]) {
        currentMaxCounter = counters[val - 1];
      }
    } else if (val === N + 1) {
      maxApplied = true;
      lastMaxCounter = currentMaxCounter;
    }
  }
  // apply the lastMaxCounter to all counters
  for (let k = 0; k < counters.length; k++) {
    counters[k] = counters[k] < lastMaxCounter ? lastMaxCounter : counters[k];
  }
  return counters;
};
const maxCounters=(N,A)=>{
for(设t=0;tN+1){
抛出新错误(“无效的输入数组A”);
}
}
设lastMaxCounter=0;//保存最后一个最大计数器应用于所有计数器
let counters=[];//计数器结果
//按0初始化值
for(设i=0;i如果(1在Swift中有一个非常优雅的解决方案:

public func solution(_ N : Int, _ A : inout [Int]) -> [Int] {
    var globalMax = 0
    var currentMax = 0
    var maximums: [Int: Int] = [:]

    for x in A {
        if x > N {
            globalMax = currentMax
            continue
        }
        let newValue = max(maximums[x] ?? globalMax, globalMax) + 1
        currentMax = max(newValue, currentMax)
        maximums[x] = newValue
    }

    var result: [Int] = []
    for i in 1...N {
        result.append(max(maximums[i] ?? globalMax, globalMax))
    }

    return result
}

试试这个Java代码段。它更可读、更整洁,您不需要担心边界检查,并且可能会放弃与您找到的更有效方法相关的第一个发现,顺便说一句,最大值位于主forloop上,不会造成任何开销

公共最终int[]解决方案(int N,int[]A)
{
int条件=N+1;
int currentMax=0;
int lastUpdate=0;
int[]计数器=新的int[N];
for(int i=0;i当前最大值)
{
currentMax=计数器[位置];
}
}
}
对于(int i=0;i
这是C解决方案给我100%的分数

public int[] solution(int N, int[] A) {
        int[] operation = new int[N];
            int max = 0, globalMax = 0;
            foreach (var item in A)
            {
                if (item > N)
                {
                    globalMax = max;
                }
                else
                {
                    if (operation[item - 1] < globalMax)
                    {
                        operation[item - 1] = globalMax;
                    }
                    operation[item - 1]++;
                    if (max < operation[item - 1])
                    {
                        max = operation[item - 1];
                    }
                }
            }
            for (int i = 0; i < operation.Length; i++)
            {
                if (operation[i] < globalMax)
                {
                    operation[i] = globalMax;
                }
            }

            return operation;
    }
public int[]解决方案(int N,int[]A){
int[]操作=新的int[N];
int max=0,globalMax=0;
foreach(A中的var项目)
{
如果(项目>N)
{
globalMax=最大值;
}
其他的
{
if(操作[项目-1]
你能说得更具体些吗?你的问题到底是什么?@Amit请检查我的编辑我的问题是我没有也仍然不理解这个问题。有人能帮我解释一下吗?
const maxCounters = (N, A) => {
  for (let t = 0; t < A.length; t++) {
    if (A[t] < 1 || A[t] > N + 1) {
      throw new Error('Invalid input array A');
    }
  }
  let lastMaxCounter = 0; // save the last max counter is applied to all counters
  let counters = []; // counters result
  // init values by 0
  for (let i = 0; i < N; i++) {
    counters[i] = 0;
  }
  let currentMaxCounter = 0; // save the current max counter each time any counter is increased
  let maxApplied = false;
  for (let j = 0; j < A.length; j++) {
    const val = A[j];
    if (1 <= val && val <= N) {
      if (maxApplied && counters[val - 1] < lastMaxCounter) {
        counters[val - 1] = lastMaxCounter;
      }
      counters[val - 1] = counters[val - 1] + 1;
      if (currentMaxCounter < counters[val - 1]) {
        currentMaxCounter = counters[val - 1];
      }
    } else if (val === N + 1) {
      maxApplied = true;
      lastMaxCounter = currentMaxCounter;
    }
  }
  // apply the lastMaxCounter to all counters
  for (let k = 0; k < counters.length; k++) {
    counters[k] = counters[k] < lastMaxCounter ? lastMaxCounter : counters[k];
  }
  return counters;
};
public func solution(_ N : Int, _ A : inout [Int]) -> [Int] {
    var globalMax = 0
    var currentMax = 0
    var maximums: [Int: Int] = [:]

    for x in A {
        if x > N {
            globalMax = currentMax
            continue
        }
        let newValue = max(maximums[x] ?? globalMax, globalMax) + 1
        currentMax = max(newValue, currentMax)
        maximums[x] = newValue
    }

    var result: [Int] = []
    for i in 1...N {
        result.append(max(maximums[i] ?? globalMax, globalMax))
    }

    return result
}
public int[] solution(int N, int[] A) {
        int[] operation = new int[N];
            int max = 0, globalMax = 0;
            foreach (var item in A)
            {
                if (item > N)
                {
                    globalMax = max;
                }
                else
                {
                    if (operation[item - 1] < globalMax)
                    {
                        operation[item - 1] = globalMax;
                    }
                    operation[item - 1]++;
                    if (max < operation[item - 1])
                    {
                        max = operation[item - 1];
                    }
                }
            }
            for (int i = 0; i < operation.Length; i++)
            {
                if (operation[i] < globalMax)
                {
                    operation[i] = globalMax;
                }
            }

            return operation;
    }