生活随笔
收集整理的這篇文章主要介紹了
LeetCode:输出整体轮廓线和最长子数组长度
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
LeetCode:描述輪廓線和最長(zhǎng)子數(shù)組長(zhǎng)度
目錄:
輸出整體輪廓線返回最長(zhǎng)子數(shù)組長(zhǎng)度求數(shù)組中奇數(shù)和偶數(shù)個(gè)數(shù)相同的最長(zhǎng)子數(shù)組
1. 輸出整體輪廓線
給定一個(gè)N行3列二維數(shù)組, 每一行表示有一座大樓, 一共有N座大樓。 所有大樓的底部都坐落在X軸上, 每一行的三個(gè)值(a,b,c)代表每座大樓的從(a,0)點(diǎn)開(kāi)始, 到 (b,0)點(diǎn)結(jié)束, 高度為c。 輸入的數(shù)據(jù)可以保證a<b,且a, b, c均為正數(shù)。 大樓之間可以有重合。 請(qǐng)輸出整體的輪廓線。
例子: 給定一個(gè)二維數(shù)組 [ [1, 3, 3], [2, 4, 4], [5, 6,1] ]
輸出為輪廓線 [ [1, 2, 3], [2, 4, 4], [5, 6, 1] ]
public static class Node {public boolean isUp
;public int posi
;public int h
;public Node(boolean bORe
, int position
, int height
) {isUp
= bORe
;posi
= position
;h
= height
;}}public static class NodeComparator implements Comparator<Node> {@Overridepublic int compare(Node o1
, Node o2
) {if (o1
.posi
!= o2
.posi
) { return o1
.posi
- o2
.posi
;}if (o1
.isUp
!= o2
.isUp
) {return o1
.isUp
? -1 : 1;}return 0;}}public static List
<List
<Integer>> buildingOutline(int[][] buildings
) {Node
[] nodes
= new Node[buildings
.length
* 2];for (int i
= 0; i
< buildings
.length
; i
++) {nodes
[i
* 2] = new Node(true, buildings
[i
][0], buildings
[i
][2]);nodes
[i
* 2 + 1] = new Node(false, buildings
[i
][1], buildings
[i
][2]);}Arrays
.sort(nodes
, new NodeComparator());TreeMap
<Integer, Integer> htMap
= new TreeMap<>();TreeMap
<Integer, Integer> pmMap
= new TreeMap<>();for (int i
= 0; i
< nodes
.length
; i
++) {if (nodes
[i
].isUp
) {if (!htMap
.containsKey(nodes
[i
].h
)) {htMap
.put(nodes
[i
].h
, 1);} else {htMap
.put(nodes
[i
].h
, htMap
.get(nodes
[i
].h
) + 1);}} else {if (htMap
.containsKey(nodes
[i
].h
)) {if (htMap
.get(nodes
[i
].h
) == 1) {htMap
.remove(nodes
[i
].h
);} else {htMap
.put(nodes
[i
].h
, htMap
.get(nodes
[i
].h
) - 1);}}}if (htMap
.isEmpty()) {pmMap
.put(nodes
[i
].posi
, 0);} else {pmMap
.put(nodes
[i
].posi
, htMap
.lastKey());}}List
<List
<Integer>> res
= new ArrayList<>();int start
= 0;int height
= 0;for (Entry
<Integer, Integer> entry
: pmMap
.entrySet()) {int curPosition
= entry
.getKey();int curMaxHeight
= entry
.getValue();if (height
!= curMaxHeight
) {if (height
!= 0) {List
<Integer> newRecord
= new ArrayList<Integer>();newRecord
.add(start
);newRecord
.add(curPosition
);newRecord
.add(height
);res
.add(newRecord
);}start
= curPosition
;height
= curMaxHeight
;}}return res
;}
2. 返回最長(zhǎng)子數(shù)組長(zhǎng)度
題目描述:給定一個(gè)數(shù)組arr, 和一個(gè)整數(shù)num, 求在arr中, 累加和等于num的最長(zhǎng)子數(shù)組的長(zhǎng)度
例子:
arr = {7,3,2,1,1,7,7,7} num = 7
其中有很多的子數(shù)組累加和等于7, 但是最長(zhǎng)的子數(shù)組是{3,2,1,1}, 所以返回其長(zhǎng)度4
題解:創(chuàng)建一個(gè)HashMap,key保存從0位置開(kāi)始到每個(gè)位置的長(zhǎng)度,value 保存出現(xiàn)長(zhǎng)度的位置。如果sum-aim的結(jié)果出現(xiàn)在map中,則說(shuō)明有這個(gè)長(zhǎng)度,獲取長(zhǎng)度即可。如果沒(méi)有出現(xiàn)過(guò),保存在 map 中即可。
public static int maxLength(int[] arr
, int aim
) {if (arr
== null
|| arr
.length
== 0) {return 0;}HashMap
<Integer, Integer> map
= new HashMap<Integer, Integer>();map
.put(0, -1); int len
= 0;int sum
= 0;for (int i
= 0; i
< arr
.length
; i
++) {sum
+= arr
[i
];if (map
.containsKey(sum
- aim
)) {len
= Math
.max(i
- map
.get(sum
- aim
), len
);}if (!map
.containsKey(sum
)) {map
.put(sum
, i
);}}return len
;}
3. 求數(shù)組中奇數(shù)和偶數(shù)個(gè)數(shù)相同的最長(zhǎng)子數(shù)組
用動(dòng)態(tài)劃分思想,最后一個(gè)位置的數(shù)肯定會(huì)被劃分,那么就有兩種可能
最后一個(gè)數(shù)所在部分不是異或和為 0 的子數(shù)組,那么 dp[i] = dp[i-1]最后一個(gè)數(shù)所在部分是異或和為 0 的子數(shù)組,那么 dp[i] = dp[k-1] +1
public static int mostEOR(int[] arr
) {int ans
= 0;int xor
= 0;int[] dp
= new int[arr
.length
];HashMap
<Integer, Integer> map
= new HashMap<>();map
.put(0, -1);for (int i
= 0; i
< arr
.length
; i
++) {xor
^= arr
[i
];if (map
.containsKey(xor
)) {int pre
= map
.get(xor
);dp
[i
] = pre
== -1 ? 1 : (dp
[pre
] + 1);}if (i
> 0) {dp
[i
] = Math
.max(dp
[i
- 1], dp
[i
]);}map
.put(xor
, i
);ans
= Math
.max(ans
, dp
[i
]);}return ans
;}
總結(jié)
以上是生活随笔為你收集整理的LeetCode:输出整体轮廓线和最长子数组长度的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。