生活随笔
收集整理的這篇文章主要介紹了
2019提前批——拼多多笔试题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一(85)
給定兩個數組A和B。其中數組A是幾乎嚴格升序排列的,幾乎的定義是只需改變其
中一個數,即可滿足完全升序排列。
你的任務是從數組A中找到這個數字,并從數組B中選取1數將其替換,使得數
組A是完全嚴格升序排列的嚴格升序排列,即不允許相鄰兩個為相同的數>
請找出數組B中滿足要求的最大數字,并輸出最終有序的數組。如果不存在就輸出
輸入描述:
共兩行,第一行是數組A,第二行是數組3,元素之間用空格分隔
(數組A的長度,數組3的長度< 100)
輸出描述:
共一行,為最終有序的數組。不存在則輸出NO
入:
1 5 4 9
1 2 3 4 5 6 7 8 9
出:
1 5 8 9
需要考慮的情況
長度為1長度為2 ,相同 或 亂序不同。長度大于等于3
長度大于等于3的有三種情況:
3 3 5 前兩個相同,改第一個第二個都行3 3 4 前兩個相同,只能改第一個2 4 4 末尾兩個相同,改第一個第二個都行3 4 4 末尾兩個相同,只能改第二個4 2 6 改中間
其中12可以合并,34可以合并。
因為遍歷數組B,所以情況2改第二個與情況4改第一個,肯定找不到。
import java
.util
.Arrays
;
import java
.util
.Scanner
;public class Test1 {public static void main(String
[] args
) {Scanner scanner
= new Scanner(System
.in
);String line1
= scanner
.nextLine();String line2
= scanner
.nextLine();String
[] line1split
= line1
.split(" ");String
[] line2split
= line2
.split(" ");int[] arr1
= new int[line1split
.length
];int i
= 0;for (String s
: line1split
) {arr1
[i
++] = Integer
.valueOf(s
);}int[] arr2
= new int[line2split
.length
];i
= 0;for (String s
: line2split
) {arr2
[i
++] = Integer
.valueOf(s
);}if (arr1
.length
== 0 || arr2
.length
== 0) {System
.out
.println("NO");return;}boolean res
= findNumberIndex(arr1
, arr2
);if (res
) {for (int k
= 0; k
< arr1
.length
- 1; k
++) {System
.out
.print(arr1
[k
] + " ");}System
.out
.println(arr1
[arr1
.length
- 1]);} else {System
.out
.println("NO");}}public static boolean findNumberIndex(int[] arr1
, int[] arr2
) {if (arr1
.length
>= 3) {for (int i
= 1; i
< arr1
.length
- 1; i
++) {if (arr1
[i
- 1] >= arr1
[i
] && arr1
[i
+ 1] >= arr1
[i
]) {int max
= Integer
.MIN_VALUE
;if (arr1
[i
- 1] == arr1
[i
]) {for (int j
= 0; j
< arr2
.length
; j
++) {if (arr2
[j
] > arr1
[i
- 1] && arr2
[j
] < arr1
[i
+ 1]) {max
= Math
.max(max
, arr2
[j
]);}}if (max
!= Integer
.MIN_VALUE
) {arr1
[i
] = max
;return true;}for (int j
= 0; j
< arr2
.length
; j
++) {if (i
- 2 >= 0) {if (arr2
[j
] > arr1
[i
- 2] && arr2
[j
] < arr1
[i
]) {max
= Math
.max(max
, arr2
[j
]);}} else {if (arr2
[j
] < arr1
[i
]) {max
= Math
.max(max
, arr2
[j
]);}}}if (max
!= Integer
.MIN_VALUE
) {arr1
[i
- 1] = max
;return true;}} else {for (int j
= 0; j
< arr2
.length
; j
++) {if (arr2
[j
] > arr1
[i
- 1] && arr2
[j
] < arr1
[i
+ 1]) {max
= Math
.max(max
, arr2
[j
]);}}}if (max
!= Integer
.MIN_VALUE
) {arr1
[i
] = max
;return true;} else {return false;}}}if (arr1
[arr1
.length
- 2] >= arr1
[arr1
.length
- 1]) {int max
= Integer
.MIN_VALUE
;for (int j
= 0; j
< arr2
.length
; j
++) {if (arr2
[j
] > arr1
[arr1
.length
- 2]) {max
= Math
.max(max
, arr2
[j
]);}}if (max
!= Integer
.MIN_VALUE
) {arr1
[arr1
.length
- 1] = max
;return true;} else {return false;}}} else if (arr1
.length
== 2) {if (arr1
[0] >= arr1
[1]) {int max
= Integer
.MIN_VALUE
;for (int j
= 0; j
< arr2
.length
; j
++) {if (arr2
[j
] < arr1
[1]) {max
= Math
.max(max
, arr2
[j
]);}}if (max
!= Integer
.MIN_VALUE
) {arr1
[0] = max
;return true;} else {return false;}} else {return true;}}return true;}
}
二(100)
給定一個字符串數組(字符串長度和數組的長度均大于1且小于1024),
所有字符均為大寫字母。請問,給定的字符串數組是否能通過更換數組中
元素的順序,從而首尾相連,形成一個環,環上相鄰字符串首尾銜接的字
符相同。
輸入:
一行輸入,空格分隔,表示字符串數組
輸出:
是否可以 true or false
示例
1.
CAT TAB BADC
true
2.
CAT TAB
false
import java
.util
.HashMap
;
import java
.util
.Map
;
import java
.util
.Scanner
;public class Test2 {public static void main(String
[] args
) {Scanner scanner
= new Scanner(System
.in
);String line
= scanner
.nextLine();String
[] lineSplit
= line
.split(" ");Map
<Character, Integer> map
= new HashMap<>();int i
= 0;for (String s
: lineSplit
) {map
.put(s
.charAt(0), map
.getOrDefault(s
.charAt(0), 0) + 1);map
.put(s
.charAt(s
.length() - 1), map
.getOrDefault(s
.charAt(s
.length() - 1), 0) + 1);}boolean res
= true;for (Map
.Entry
<Character, Integer> entry
: map
.entrySet()) {if (entry
.getValue() % 2 == 1){res
= false;}}System
.out
.println(res
);}
}
三(100)
單線程完成任務,要求平均時長最短,返回時長為完成任務時刻-接收任務時刻。假設,零時,接收全部任務。
任務有依賴關系。
輸入:
N M n個任務耗費,m個依賴關系
X X X… N個耗費
X Y Y依賴于X, X需要先完成
輸出:
任務完成序列 (字典序最小)
示例輸入:
5 6
1 2 1 1 1
1 2
1 3
1 4
2 5
3 5
4 5
import java
.util
.ArrayList
;
import java
.util
.Comparator
;
import java
.util
.List
;
import java
.util
.Scanner
;public class Test3 {public static void main(String
[] args
) {Scanner sc
= new Scanner(System
.in
);int n
= sc
.nextInt(); int m
= sc
.nextInt(); sc
.nextLine();Node
[] nodes
= new Node[n
];for (int i
= 0; i
< n
; i
++) {nodes
[i
] = new Node();nodes
[i
].cost
= sc
.nextInt();nodes
[i
].index
= i
+ 1; }sc
.nextLine();for (int i
= 0; i
< m
; i
++) {int from
= sc
.nextInt() - 1;int to
= sc
.nextInt() - 1;sc
.nextLine();nodes
[from
].nexts
.add(nodes
[to
]);nodes
[to
].rudu
++;}int deleteCount
= 0;List
<Integer> res
= new ArrayList<>();while (deleteCount
< n
) {boolean isHaveRudu0
= false;ArrayList
<Node> deleteNodeSet
= new ArrayList<>();for (int i
= 0; i
< n
; i
++) {Node node
= nodes
[i
];if (node
!= null
) {if (node
.rudu
== 0) {isHaveRudu0
= true;deleteNodeSet
.add(node
);}}}deleteNodeSet
.sort(new Comparator<Node>() {@Overridepublic int compare(Node o1
, Node o2
) {int res1
= Integer
.compare(o1
.cost
, o2
.cost
);if (res1
== 0) {return Integer
.compare(o1
.index
, o2
.index
);}return res1
;}});if (isHaveRudu0
) {Node node
= deleteNodeSet
.get(0);nodes
[node
.index
- 1] = null
;res
.add(node
.index
);deleteCount
++;for (Node nn
: node
.nexts
) {nn
.rudu
--;}} else {return;}}for (int i
= 0; i
< res
.size() - 1; i
++) {System
.out
.print(res
.get(i
) + " ");}System
.out
.println(res
.get(res
.size() - 1));}static class Node {int cost
= 0;int rudu
= 0;int index
= -1;List
<Node> nexts
= new ArrayList<>();}
}
四(5)
多多雞寶寶在玩搭積木游戲。有N個長方體積木,每個積木的高都是
1,長寬都為Li ,重量為 Wi。
現在多多雞寶寶想要用這些積木搭一個高高的金字塔。他打算金字塔的每
一層是由且僅由一塊積木組成,同時每一層的積木邊長都嚴格比在其下方
的積木小。
在多次嘗試之后,多多雞寶寶發現每塊積木只能承受自身重量的7倍重量
一若超過7倍自重,搭建的金字塔會因此變得不穩定。具體來說即:對于
每一塊積木,在其上方的積木重量之和必須小于等于其自重的7倍。
多多雞寶寶想請你幫他計算一下最高可以搭一個多高的金字塔?
數據范圍:
1 <= N <= 100
1 <= Li <= 1000
1 <= Wi <= 1000
入:
10
1 2 3 4 5 6 7 8 9 10
1 1 1 1 1 1 1 1 1 10
出:
9
package shen
.leetcode
.solution
.bishi
.pinduoduo
;import java
.util
.ArrayList
;
import java
.util
.Arrays
;
import java
.util
.List
;
import java
.util
.Scanner
;public class Test4 {public static void main(String
[] args
) {Scanner sc
= new Scanner(System
.in
);int n
= sc
.nextInt();sc
.nextLine();Jm
[] arr
= new Jm[n
];for (int i
= 0; i
< n
; i
++) {arr
[i
] = new Jm();arr
[i
].length
= sc
.nextInt();arr
[i
].index
= i
;}sc
.nextLine();for (int i
= 0; i
< n
; i
++) {arr
[i
].weight
= sc
.nextInt();}Arrays
.sort(arr
, (o1
, o2
) -> Integer
.compare(o2
.length
, o1
.length
));if (arr
.length
== 0) return;System
.out
.println(putFirst(arr
));}public static int putFirst(Jm
[] jms
) {List
<Jm> jmList
= new ArrayList<>();int maxLength
= jms
[0].length
;int nextLengthIndex
= 0;for (int i
= 0; i
< jms
.length
; i
++) {if (jms
[i
].length
== maxLength
) {jmList
.add(jms
[i
]);nextLengthIndex
= i
;} else {break;}}nextLengthIndex
++;int maxHeight
= 0;jmList
.sort((o1
, o2
) -> Integer
.compare(o2
.weight
, o1
.weight
));maxHeight
= Math
.max(maxHeight
, putJM(jms
, nextLengthIndex
, jmList
.get(0).weight
* 7));return maxHeight
+ 1;}public static int putJM(Jm
[] jms
, int curLengthIndex
, int currentMinWeight
) {if (curLengthIndex
>= jms
.length
|| currentMinWeight
<= 0) {return 0;}List
<Jm> jmList
= new ArrayList<>();int curMaxLength
= jms
[curLengthIndex
].length
;int nextLengthIndex
= 0;for (int i
= curLengthIndex
; i
< jms
.length
; i
++) {if (jms
[i
].length
== curMaxLength
) {jmList
.add(jms
[i
]);nextLengthIndex
= i
;} else {break;}}nextLengthIndex
++;int maxHeight
= 0;boolean isCurChaoZhong
= true;for (Jm jm
: jmList
) {if (jm
.weight
<= currentMinWeight
) {isCurChaoZhong
= false;currentMinWeight
= Math
.min(currentMinWeight
- jm
.weight
, jm
.weight
* 7);maxHeight
= Math
.max(maxHeight
, putJM(jms
, nextLengthIndex
, currentMinWeight
));}}if (!isCurChaoZhong
) {maxHeight
++;}return maxHeight
;}static class Jm {int weight
;int length
;int index
;}
}
總結
以上是生活随笔為你收集整理的2019提前批——拼多多笔试题的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。