Citrix 挂经思考

【calculate region:占据长度的总和】

思路:我自己想的是存map,但是元素的个数未知。别人写的是用stack,我没想到。说明stack在排序的时候很好用啊。

心得:多给点时间查答案的话,还是能查到的。

参考:https://leetcode.com/discuss/general-discussion/534205/interview-question

fun calculateRegion(arr: IntArray): Int {
    val left = getNeightbourSum(arr)
    val right = getNeightbourSum(arr.reverse())
    return left + right + arr.size
}

fun getNeightbourSum(arr: IntArray): Int {
    val s = Stack<IntArray>()
    var ans = 0
    for(i in 0 until arr.size) {
       var count = 0 
       while(stack.isEmpty().not() && stack.peek()[0] <= arr[i]) {
           count += stack.peek()[1] + 1
           stack.pop()
      //小的pop出来,大的add进去 } ans
+= count stack.add(intArrayOf(arr[i], count)) } return ans } // [1, 1, 1, 1] // left = 0 + 1 + 2 + 3 // right = 0 + 1 + 2 + 3 // ans = 6 + 6 + 4(size of a array) = 16

 

【双频共振信号的最大值】

 学过信号的人,这都不懂,该死……经验:oa的时候,题长不长都用谷歌翻译看

要求输出的是被更新的次数,不是具体的最大值

 注意是第二个list:commonValues中的元素被拿出来比较

1.平等水平
1.Equal Levels 
 
两个信号作为模拟的一部分被产生。一个程序监视信号。每当这两个信号同时相等时,就会注意到频率。记录的最大同时频率到目前为止。每次注意到更高的同时频率时,这个变量(mnaxequa/被更新为更高的频率)。
Two signals are being generated as part ofa simulation.Aprogram monitors the signals. Whenever the two signals become equal at the same time, the frequencyis noted. Arecord is maintained forthe maximum simultaneous frequency seen so far. Each time a higher simultaneous frequency is noted, this variable (mnaxequa/is updated to the higher frequency 
 
注意。
Note. 
 
这两个信号在t-O时开始,但它们的持续时间可能不同.在这种情况下,等式的比较只在给定时间两个信号都具有一定频率的较短信号结束时进行,但频率小于或小于当前最大频率axeoUal的频率没有更新。
Both signals start at time t-O but their durations might be different. In this case, the comparison of equality is performed onlyuntl the endofthe shorter signal f both signals have cqual frequencies at a given time but the frequencyisless than or coualo the currentmaximum freouency axeoUal, is not Updated 
 
给出了这两种信号的运行时间,分别表示为oynn和m。持续的过程中,有多少个锡-3i的/aXeouavaraDlupoated?
The running times of .both signals are given, denoted oynand m respectively. Duringth CoUrse of tne sirulatior, how Many tin-3I the /aXxeouavaraDleupoated?
题目
package equalLevels;
import java.util.*;

public class Solution {
    public int updateTimes(List<Integer> signalOne, List<Integer> signalTwo) {
        // Write your code here
        int res = 0;
        boolean isMax = true;

        for (int i = 0; i < signalOne.size(); i++) {
            isMax = true;

            for (int j = 0; j < i; j++) {
                //判断条件不对,是false,所以后面Res无法++
                if ((signalOne.get(i) < signalOne.get(j)) || (signalTwo.get(i) < signalTwo.get(j))) {
                    System.out.println("signalOne.get(i) = " + signalOne.get(i));
                    System.out.println("signalOne.get(j) = " + signalOne.get(j));
                    System.out.println("signalTwo.get(i) = " + signalTwo.get(i));
                    System.out.println("signalTwo.get(j) = " + signalTwo.get(j));
                    System.out.println(" ");

                    isMax = false;
                    //break;
                    continue;
                }else if (isMax && (i != 0)) {
                    res++;
                }
            }

//            isMax = true;
        }

        return res;
    }
}
原来的草稿
package equalLevels;
import java.util.*;

public class Solution {
    public int updateTimes(List<Integer> signalOne, List<Integer> signalTwo) {
        // Write your code here
        int updateTimes = 0;
        
        //count each's time duration, get the min 获得共同时间的最小值
        int t1 = signalOne.size();
        int t2 = signalTwo.size();
        int t = Math.min(t1, t2);

        List<Integer> commonValues = new LinkedList<>();
        //for loop the min time, put common values into a list 把共同时间值存到一个list
        for (int i = 0; i < t; i++) {
            if (signalOne.get(i) == signalTwo.get(i)) {                
                commonValues.add(signalOne.get(i));             
                System.out.println("this element = " + signalOne.get(i));
                System.out.println("last index = " + (commonValues.size() - 1));
                System.out.println("last element = " + commonValues.get(commonValues.size() - 1));
                System.out.println(" ");
                
              //每次如果common value比上一次的更大,就更新
                if (commonValues.size() == 1 || 
                   (commonValues.size() == 2 && commonValues.get(1) > commonValues.get(0)) ||
                   (commonValues.size() > 2 && signalOne.get(i) > commonValues.get(commonValues.size() - 2))
                   ) {
                    updateTimes++;
                }
                
                
            }
        }

        return updateTimes;
    }
}
修改后的答案
package equalLevels;

import java.util.*;

public class Test {
    public static void main(String[] args) {
        Solution solution = new Solution(); 
        
//        List<Integer> signalOne = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 3, 3, 5, 4));
//        List<Integer> signalTwo = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 3, 5, 4));
        //4
        
//        List<Integer> signalOne = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 1));
//        List<Integer> signalTwo = new ArrayList<Integer>(Arrays.asList(5, 4, 3, 4, 1));
        //2
        
        List<Integer> signalOne = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5));
        List<Integer> signalTwo = new ArrayList<Integer>(Arrays.asList(5, 4, 3, 2, 1));
        //1
        
        System.out.println(solution.updateTimes(signalOne, signalTwo));
    }
}
测试代码

 

posted @ 2020-12-28 14:03  苗妙苗  阅读(87)  评论(0编辑  收藏  举报