Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e35687a

Browse files
author
C5141506
committed
Leetcode problems
1 parent 99c341b commit e35687a

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package java_problem.array;
2+
3+
4+
import java.util.Comparator;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.PriorityQueue;
8+
9+
public class TopKFrequentElements347 {
10+
public static void main(String args[]) {
11+
int stones[] = new int[]{2, 7, 4, 1, 8, 1};
12+
System.out.println(topKFrequent(stones,3));
13+
}
14+
15+
public static int[] topKFrequent(int[] nums, int k) {
16+
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
17+
for (int n : nums) {
18+
map.put(n, map.getOrDefault(n,0) + 1);
19+
}
20+
21+
PriorityQueue<MyPair> heap = new PriorityQueue<MyPair>(k, new Comparator<MyPair>() {
22+
@Override
23+
public int compare(MyPair a, MyPair b) {
24+
if (a.count < b.count) {
25+
return -1;
26+
} else if (a.count == b.count) {
27+
return 0;
28+
}
29+
return 1;
30+
}
31+
32+
});
33+
34+
for (Map.Entry<Integer, Integer> keyVal : map.entrySet()) {
35+
if (heap.size() < k) {
36+
MyPair p = new MyPair(keyVal.getKey(), keyVal.getValue());
37+
heap.add(p);
38+
} else {
39+
if (heap.peek().value < keyVal.getValue()) {
40+
heap.poll();
41+
MyPair p = new MyPair(keyVal.getKey(), keyVal.getValue());
42+
heap.add(p);
43+
}
44+
}
45+
}
46+
47+
48+
int[] topK=new int[k];
49+
int i=0;
50+
while (!heap.isEmpty()) {
51+
MyPair myPair=heap.poll();
52+
topK[i]=myPair.value;
53+
i++;
54+
}
55+
56+
return topK;
57+
}
58+
59+
60+
}
61+
62+
class MyPair {
63+
int value;
64+
int count;
65+
66+
public MyPair(int key, int value) {
67+
this.value = key;
68+
this.count = value;
69+
}
70+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package java_problem.heap;
2+
3+
import java.util.Collection;
4+
import java.util.Collections;
5+
import java.util.PriorityQueue;
6+
7+
public class LastStoneWeight1046 {
8+
public static void main(String args[]) {
9+
int stones[] = new int[]{2, 7, 4, 1, 8, 1};
10+
System.out.println(lastStoneWeight(stones));
11+
}
12+
13+
public static int lastStoneWeight(int[] stones) {
14+
if(stones.length==1) return stones[0];
15+
PriorityQueue<Integer> heap = new PriorityQueue<Integer>(Collections.reverseOrder());
16+
for (int weight : stones) {
17+
heap.add(weight);
18+
}
19+
while (heap.size()!=1){
20+
int x=heap.poll();
21+
22+
int y=heap.poll();
23+
24+
if(x>y){
25+
heap.add(x-y);
26+
}else if (x==y){
27+
heap.add(0);
28+
}
29+
}
30+
return heap.poll();
31+
}
32+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package java_problem.slidewindow;
2+
3+
import java.util.HashMap;
4+
5+
public class LongestRepeatedCharacterSubString {
6+
public static void main(String args[]){
7+
System.out.println("Longest sub string length="+characterReplacement( "AABABBA ",1));
8+
}
9+
public static int characterReplacement(String s, int k) {
10+
int n=s.length();
11+
HashMap<Character,Integer> hs=new HashMap<Character,Integer>();
12+
int i=0;
13+
int subStrSize=0;
14+
int maxFreq=0;
15+
for(int j=0;j<n;j++){
16+
int freq=1;
17+
if(hs.containsKey(s.charAt(j))){
18+
freq=hs.get(s.charAt(j));
19+
freq=freq+1;
20+
hs.put(s.charAt(j),freq);
21+
}else{
22+
hs.put(s.charAt(j),freq);
23+
}
24+
maxFreq=Math.max(maxFreq,freq);
25+
int windowSize=j-i+1;
26+
int ops=windowSize-maxFreq;
27+
if(ops>k){
28+
char pre=s.charAt(i);
29+
freq=hs.get(pre)-1;
30+
hs.put(pre,freq);
31+
i++;
32+
windowSize=j-i+1;
33+
}
34+
35+
subStrSize=Math.max(windowSize,subStrSize);
36+
}
37+
return subStrSize;
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /