兩道面試題(英文版)

今天下午小陳給我發了兩道面試題,我花了點時間研究了一下。
現在在這裏總結:
題目一:
Problem Statement # 1

Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint sets A1 and A2 of n1 and n2 numbers, respectively. Let S1 and S2 denote the sums of all the numbers in A1 and A2, respectively. You are supposed to make the partition so that |n1 - n2| is minimized first, and then |S1 - S2| is maximized.

Input Specification:
Each input file contains one test case. For each case, the first line gives an integer N (2 <= N <= 10^5), and then N positive integers follow in the next line, separated by spaces. It is guaranteed that all the integers and their sum are less than 2^31.

Output Specification:
For each case, print in a line two numbers: |n1 - n2| and |S1 - S2|, separated by exactly one space.

Sample Input 1:
10
23 8 10 99 46 2333 46 1 666 555
Sample Output 1:
0 3611

Sample Input 2:
13
110 79 218 69 3721 100 29 135 2 6 13 5188 85
Sample Output 2:
1 9359

沒錯!當初看到是英文版的時候我也是懵逼的,但是誰叫我有金山詞霸呢!這點小困難還是打不倒我的。
題目大意是說:給定一組數組,讓我們寫一個方法將它分爲兩個集合A1和A2,它們分別有n1和n2個元素,集合所有元素值加起來是S1和S2,,要求方法輸出|n1-n2|和|S1-S2|的值,且n1-n2要儘可能的小,S1-S2要儘可能的大。over。

我的思路是這樣的:既然n1-n2要儘可能的小,那麼最小的結果是n1=n2,即|n1-n2 |=0;所以說要儘可能地將這組數組等分,但考慮到傳入的數組中元素個數爲奇數個的時候,是取不到n1=n2的,最多能取到n1-n2=1;
然後直接上代碼理解吧!

package com.xiaochen;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/*
 * @author sjia
 * @Date 2017年4月16日--下午6:05:04
 */
public class Problem {
    public void ouput(int num,String str)
    {
        int s1=0;
        int s2=0;
        String[] strs=str.split(" ");
        List<Integer> list=new ArrayList<Integer>();
        List<Integer> a1=new ArrayList<Integer>();
        List<Integer> a2=new ArrayList<Integer>();
        for(String s:strs)
        {
            list.add(Integer.parseInt(s));
        }
        list.sort(new Comparator<Integer>() {

            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        //分兩種情況,num爲奇數和num爲偶數
        if(num%2==0)
        {
            //System.out.println("偶數");
            for(int i=0;i<(num/2);i++)
            {
                s1+=list.get(i);
                a1.add(list.get(i));
            }
            for(int i=list.size()-1;i>num/2-1;i--)
            {
                s2+=list.get(i);
                a2.add(list.get(i));
            }


        }else if(num%2!=0)
        {
            //System.out.println("奇數");
            for(int i=0;i<(num+1)/2;i++)
            {
                s1+=list.get(i);
                a1.add(list.get(i));
            }
            for(int i=list.size()-1;i>(num-1)/2;i--)
            {
                s2+=list.get(i);
                a2.add(list.get(i));
            }
        }
        int nr=a1.size()-a2.size();
        System.out.println("|n1-n2|="+nr);
        int sr=s1-s2;
        System.out.println("|s1-s2|="+sr);
    }
    public static void main(String[] args) {
        Problem pro=new Problem();
        pro.ouput(13, "110 79 218 69 3721 100 29 135 2 6 13 5188 85");
    }
}

題目二:
Problem Statement # 2

You are to write a program that takes a list of strings containing integers and words and returns a sorted version of the list.

The goal is to sort this list in such a way that all words are in alphabetical order and all integers are in numerical order. Furthermore, if the nth element in the list is an integer it must remain an integer, and if it is a word it must remain a word.

Input Specification:
The input will contain a single, possibly empty, line containing a space-separated list of strings to be sorted. Words will not contain spaces, will contain only the lower-case letters a-z. Integers will be in the range -999999 to 999999, inclusive. The line will be at most 1000 characters long.

Output Specification:
The program must output the list of strings, sorted per the requirements above. Strings must be separated by a single space, with no leading space at the beginning of the line or trailing space at the end of the line.

Sample Input 1:
1
Sample Output 1:
1

Sample Input 2:
car truck bus
Sample Output 2:
bus car truck

Sample Input 3:
8 4 6 1 -2 9 5
Sample Output 3:
-2 1 4 5 6 8 9

Sample Input 4:
car truck 8 4 bus 6 1
Sample Output 4:
bus car 1 4 truck 6 8

個人感覺第二題就是第一題的升級加強版,就是考排序嘛。
這題在思路上是很簡潔明瞭的,但是在實現的過程中略顯繁瑣。
也是直接上代碼吧!

package com.xiaochen;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @author sjia
 * @Date 2017年4月16日--下午6:21:08
 */
public class Problem2 {


    //先將這個字符串中的字母和數字都分隔出來,put進一個map中
    public static Map<String, List<String>> separate(String str) {
        List<String> alp = new ArrayList<String>();
        List<String> num = new ArrayList<String>();
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        //用來臨時存放時字母的string
        String temp = null;
        String[] strs = str.split(" ");
        for (int i = 0; i < strs.length; i++) {
            try {
                temp = strs[i] + "+" + i;
                int result = Integer.parseInt(strs[i]);
                num.add(strs[i] + "+" + i);
            } catch (Exception e) {
                alp.add(temp);
            }
        }
        map.put("alp", alp);
        map.put("num", num);
        return map;
    }
    // letter & number sort
    public void InSort(String str)
    {
        Map<String,List<String>> map=separate(str);
        //alpplus代表該集合中的元素是 "abc+1"這樣的形式
        List<String> alpplus=map.get("alp");
        List<String> numplus=map.get("num");
        List<String> alp=new ArrayList<String>();
        List<String> num=new ArrayList<String>();
        //這兩個list用來存放對應list的index  即alpindex 存放alp的 index
        List<String> alpindex=new ArrayList<String>();
        List<String> numindex=new ArrayList<String>();
        for(String s1:alpplus)
        {
            //注意,之前我是直接用"+"split的,沒加轉義的話會報錯
            String[] strs=s1.split("\\+");
            alp.add(strs[0]);
            alpindex.add(strs[1]);
        }
        for(String s2:numplus)
        {
            String[] strs=s2.split("\\+");
            num.add(strs[0]);
            numindex.add(strs[1]);
        }
        Comparator<String> comparator=new Comparator<String>() {

            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        };
        alpplus.sort(comparator);
        numplus.sort(comparator);
        alpindex.sort(comparator);
        numindex.sort(comparator);

        Map<String,String> result=new HashMap<String,String>();

        for(int i=0;i<alp.size();i++)
        {
            result.put(alpindex.get(i), alp.get(i));
        }
        for(int i=0;i<num.size();i++)
        {
            result.put(numindex.get(i), num.get(i));
        }
        String res="";
        for(int i=0;i<alp.size()+num.size();i++)
        {
            res=res+result.get(i+"")+" ";
        }
        System.out.println(res);
    }
    public static void main(String[] args) {
        Problem2 problem2=new Problem2();
        problem2.InSort("car truck 8 4 bus 6 1");
    }
}

吐槽一下小陳,用兩道題打斷了正在學習spring boot的我….
好了,我現在要滾去看我的spring boot 了.

最後,如果你有更好的做法,或者發現我有什麼地方有錯誤的話,歡迎在評論裏交流

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章