Skip to content

Commit b2bbaa2

Browse files
author
digitspace
committed
569-Week 07
1 parent b744e3e commit b2bbaa2

File tree

8 files changed

+306
-0
lines changed

8 files changed

+306
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* 1122. Relative Sort Array
3+
* 数组的相对排序
4+
*/
5+
public class LeetCode_1122_569 {
6+
7+
public static void main(String[] args) {
8+
// TODO Auto-generated method stub
9+
10+
}
11+
12+
class Solution {
13+
public int[] relativeSortArray(int[] arr1, int[] arr2) {
14+
int[] ca = new int[1001];
15+
int[] ra = new int[arr1.length];
16+
int ri = 0;
17+
for( int i : arr1 ) {
18+
ca[i]++;
19+
}
20+
for( int j : arr2 ) {
21+
while( ca[j]-- > 0 ) {
22+
ra[ri++] = j;
23+
}
24+
}
25+
for( int i = 0; i < 1000; i++ ) {
26+
while( ca[i]-- > 0 ) {
27+
ra[ri++] = i;
28+
}
29+
}
30+
return ra;
31+
}
32+
}
33+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import java.util.HashMap;
2+
import java.util.Iterator;
3+
import java.util.Map;
4+
import java.util.TreeMap;
5+
6+
/*
7+
* 1244. Design A Leaderboard
8+
* 力扣排行榜
9+
10+
*/
11+
public class LeetCode_1244_569 {
12+
13+
public static void main(String[] args) {
14+
// TODO Auto-generated method stub
15+
Leaderboard leaderboard = new LeetCode_1244_569().new Leaderboard();
16+
leaderboard.addScore(1,73); // leaderboard = [[1,73]];
17+
leaderboard.addScore(2,56); // leaderboard = [[1,73],[2,56]];
18+
leaderboard.addScore(3,39); // leaderboard = [[1,73],[2,56],[3,39]];
19+
leaderboard.addScore(4,51); // leaderboard = [[1,73],[2,56],[3,39],[4,51]];
20+
leaderboard.addScore(5,4); // leaderboard = [[1,73],[2,56],[3,39],[4,51],[5,4]];
21+
System.out.println(leaderboard.top(1)); // returns 73;
22+
leaderboard.reset(1); // leaderboard = [[2,56],[3,39],[4,51],[5,4]];
23+
leaderboard.reset(2); // leaderboard = [[3,39],[4,51],[5,4]];
24+
leaderboard.addScore(2,51); // leaderboard = [[2,51],[3,39],[4,51],[5,4]];
25+
leaderboard.top(3); // returns 141 = 51 + 51 + 39;
26+
27+
}
28+
29+
class Leaderboard {
30+
Map<Integer,Integer> playerMap;
31+
TreeMap<Integer,Integer> scoreMap;
32+
33+
public Leaderboard() {
34+
playerMap = new HashMap<Integer,Integer>();
35+
scoreMap = new TreeMap<Integer,Integer>();
36+
}
37+
38+
public void addScore(int playerId, int score) {
39+
Integer preScore = playerMap.get(playerId);
40+
if( preScore!= null ) {
41+
Integer count = scoreMap.get(preScore);
42+
if( count <= 1 )
43+
scoreMap.remove(preScore);
44+
else
45+
scoreMap.put(preScore, count-1);
46+
score += preScore;
47+
}
48+
playerMap.put(playerId, score);
49+
scoreMap.put(score, scoreMap.getOrDefault(score,0) + 1);
50+
}
51+
52+
public int top(int K) {
53+
int sum = 0;
54+
Iterator<Integer> iter = scoreMap.descendingKeySet().iterator();
55+
while( K > 0 && iter.hasNext() ) {
56+
int score = iter.next();
57+
int num = scoreMap.get(score);
58+
sum += Math.min(K, num) * score;
59+
K -= Math.min(K, num);
60+
}
61+
return sum;
62+
}
63+
64+
public void reset(int playerId) {
65+
Integer score = playerMap.get(playerId);
66+
playerMap.remove(playerId);
67+
if( score != null ) {
68+
Integer count = scoreMap.get(score);
69+
if( count <= 1 )
70+
scoreMap.remove(score);
71+
else
72+
scoreMap.put(score, count-1);
73+
}
74+
}
75+
}
76+
77+
/**
78+
* Your Leaderboard object will be instantiated and called as such:
79+
* Leaderboard obj = new Leaderboard();
80+
* obj.addScore(playerId,score);
81+
* int param_2 = obj.top(K);
82+
* obj.reset(playerId);
83+
*/
84+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.LinkedHashMap;
2+
import java.util.Map;
3+
4+
/*
5+
* 146. LRU Cache
6+
* LRU缓存机制
7+
*/
8+
public class LeetCode_146_569 {
9+
10+
public static void main(String[] args) {
11+
// TODO Auto-generated method stub
12+
}
13+
14+
class LRUCache extends LinkedHashMap<Integer, Integer>{
15+
int capacity;
16+
17+
public LRUCache(int capacity) {
18+
super(capacity, 0.75F, true);
19+
this.capacity = capacity;
20+
}
21+
22+
public int get(int key) {
23+
return super.getOrDefault(key, -1);
24+
}
25+
26+
public void put(int key, int value) {
27+
super.put(key, value);
28+
}
29+
30+
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
31+
return size() > capacity;
32+
}
33+
}
34+
35+
/**
36+
* Your LRUCache object will be instantiated and called as such:
37+
* LRUCache obj = new LRUCache(capacity);
38+
* int param_1 = obj.get(key);
39+
* obj.put(key,value);
40+
*/
41+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
public class LeetCode_190_569 {
3+
4+
public static void main(String[] args) {
5+
// TODO Auto-generated method stub
6+
7+
}
8+
9+
public class Solution {
10+
// you need treat n as an unsigned value
11+
public int reverseBits(int n) {
12+
int r = 0;
13+
for( int i = 0; i < 32; i-- ) {
14+
r += (n&1) << (31-i);
15+
n = n >> 1;
16+
}
17+
return r;
18+
}
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* 191. Number of 1 Bits
3+
* 位1的个数
4+
*/
5+
public class LeetCode_191_569 {
6+
7+
public static void main(String[] args) {
8+
// TODO Auto-generated method stub
9+
10+
}
11+
12+
public class Solution {
13+
// you need to treat n as an unsigned value
14+
public int hammingWeight(int n) {
15+
int count = 0;
16+
for( int i = 0; i < 32; i++ ) {
17+
if( ((n >> i) & 1) == 1 )
18+
count++;
19+
}
20+
return count;
21+
}
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* 231. Power of Two
3+
* 2的幂
4+
*/
5+
public class LeetCode_231_569 {
6+
7+
public static void main(String[] args) {
8+
// TODO Auto-generated method stub
9+
10+
}
11+
12+
class Solution {
13+
public boolean isPowerOfTwo(int n) {
14+
return n > 0 && ((n & (n-1)) == 0);
15+
}
16+
}
17+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* 242. Valid Anagram
3+
* 有效的字母异位词
4+
*
5+
* 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
6+
7+
示例 1:
8+
9+
输入: s = "anagram", t = "nagaram"
10+
输出: true
11+
示例 2:
12+
13+
输入: s = "rat", t = "car"
14+
输出: false
15+
16+
17+
*/
18+
public class LeetCode_242_569 {
19+
20+
public static void main(String[] args) {
21+
// TODO Auto-generated method stub
22+
23+
}
24+
25+
class Solution {
26+
public boolean isAnagram(String s, String t) {
27+
if( s.length() != t.length() )
28+
return false;
29+
30+
int[] a = new int[26];
31+
for( int i = 0; i < s.length(); i++ ) {
32+
a[s.charAt(i)-'a']++;
33+
a[t.charAt(i)-'a']--;
34+
}
35+
for( int i : a ) {
36+
if(i > 0)
37+
return false;
38+
}
39+
return true;
40+
}
41+
}
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.Arrays;
2+
import java.util.Comparator;
3+
4+
/*
5+
* 56. Merge Intervals
6+
* 合并区间
7+
* 给出一个区间的集合,请合并所有重叠的区间。
8+
9+
示例 1:
10+
11+
输入: [[1,3],[2,6],[8,10],[15,18]]
12+
输出: [[1,6],[8,10],[15,18]]
13+
解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
14+
示例 2:
15+
16+
输入: [[1,4],[4,5]]
17+
输出: [[1,5]]
18+
解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
19+
20+
*/
21+
public class LeetCode_56_569 {
22+
23+
public static void main(String[] args) {
24+
// TODO Auto-generated method stub
25+
int[][] ar = { {2,6},{8,10},{15,18},{1,3} };
26+
new LeetCode_56_569().new Solution().merge(ar);
27+
}
28+
29+
class Solution {
30+
public int[][] merge(int[][] intervals) {
31+
if( intervals.length <= 1 )
32+
return intervals;
33+
Arrays.sort(intervals, Comparator.comparingInt(a -> a[0]));
34+
int length = 1;
35+
for (int i = 1; i < intervals.length; i++ ) {
36+
if (intervals[length-1][1] < intervals[i][0])
37+
intervals[length++] = intervals[i];
38+
else
39+
intervals[length-1][1] = Math.max(intervals[length-1][1], intervals[i][1]);
40+
}
41+
int[][] res = new int[length][];
42+
System.arraycopy(intervals, 0, res, 0, length);
43+
return res;
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)