zoukankan      html  css  js  c++  java
  • 爱健身的小王, 修改矩阵,最长上升子串 -美团2019笔试题

    爱健身的小王-美团2019笔试题

    一、暴力解法:
    ((n+1) *k) % 4n = n+1 ,k>= 2, 枚举出最小符合条件的k。

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int t = sc.nextInt();
            while(t-- > 0) {
                int n = sc.nextInt();
                int sq = 4 * n;
                int step = n + 1;
                for(int i=2; ; i++) {
                    if((step*i) % sq == step) {
                        System.out.println(i);
                        break;
                    }
                }
            }
        }
    }
    

    解法二、最大公约数解法

    [(n+1)x = 4n y ag {1} ]

    [x =dfrac{[n+1, 4n]}{n+1} = dfrac{(n+1)(4n)}{(4n,n+1)(n+1)}= dfrac{4n}{(n+1, 4n)} ag{2} ]

    标记x次,刚好跑了y圈,则答案为x+1。根据公式(2)可以推导出最小x的值(x >= 2)。

    import java.util.*;
    public class Main {
        static int gcd(int a, int b) {
            return b == 0 ? a : gcd(b, a%b);
        }
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int t = sc.nextInt();
            while(t-- > 0) {
                int n = sc.nextInt();
                int sq = 4 * n;
                int step = n + 1;
                int res = sq / gcd(sq,step) + 1;
                System.out.println(res);
            }
        }
    }
    

    修改矩阵

    细节要爆炸!!!

    import java.util.*;
    public class Main{
        static int[][] getTopTwo(Map<Integer, Integer> map) {
            int[][] res = new int[map.size()][2];
            int idx = 0;
            for(int k : map.keySet()) {
                res[idx][0] = k; res[idx][1] = map.get(k); idx ++; 
            }
            Arrays.sort(res, (o1, o2)->o2[1]-o1[1]);
            return res;
        }
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt(), m = sc.nextInt();
            Map<Integer, Integer> map1 = new HashMap<>();
            Map<Integer, Integer> map2 = new HashMap<>();
            
            for(int i=0; i < n; i++) {
                for(int j=0; j < m; j++) {
                    int t = sc.nextInt();
                    if((i+j) % 2 == 0) 
                        map1.put(t, map1.getOrDefault(t, 0) + 1);
                    else
                        map2.put(t, map2.getOrDefault(t, 0) + 1);
                }
            }
            int[][] a = getTopTwo(map1);
            int[][] b = getTopTwo(map2);
            int res = 0;
            for(int i=0; i < 2 && i < map1.size(); i++)
                for(int j=0; j < 2 && j < map2.size(); j++) 
                    if(a[i][0] == b[j][0]) 
                        res = Math.max(res, Math.max(a[i][1], b[j][1]));
                    else 
                        res = Math.max(res, a[i][1] + b[j][1]);
            
            if(a[0][0] == 0) res = b[0][1];
            if(b[0][0] == 0) res = a[0][1];
            // System.out.println(Arrays.deepToString(a));
            // System.out.println(Arrays.deepToString(b));
            System.out.println(m*n - res);
        }
    }
    

    最长上升子串

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int[] nums = new int[n];
            List<Integer> re = new ArrayList<>();
            for(int i=0; i < n; i++) {
                nums[i] = sc.nextInt();
                if(i >= 1 && nums[i-1] >= nums[i]) re.add(i); 
            }
            if(re.size() == 0) {
                System.out.println(n-1); 
            } else {
                int res = 0;
                int s1 = 0;
                for(int i=0; i < re.size(); i++) {
                    int s2 = re.get(i);
                    int l1 = s2 - s1;
                    int s3 = i+1 < re.size() ? re.get(i+1) : n;
                    int l2 = s3 - s2;
                    int t = Math.max(l1, l2);
                    if((l2 > 1 && nums[s2-1] < nums[s2+1]) 
                        || (l1 > 1 && nums[s2-2] < nums[s2])) {
                            t = l1 + l2 - 1;
                    }
                    if(t > res) res = t;
                    s1 = s2;
                }
                System.out.println(res);
            }
        }
    }
    
  • 相关阅读:
    js get set访问器及日期扩展?
    js中加“var”和不加“var”的区别
    面试题
    ajax复习
    artTemplate使用
    Angular2组件开发—属性与事件(一)
    Angular2组件开发—为模板应用样式(三)
    Angular2组件开发—为模板应用样式(二)
    Angular2组件开发—为模板应用样式(一)
    Angular2组件开发—模板的逻辑控制(三)
  • 原文地址:https://www.cnblogs.com/lixyuan/p/12975397.html
Copyright © 2011-2022 走看看