-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathbigo.java
More file actions
101 lines (87 loc) · 2.46 KB
/
bigo.java
File metadata and controls
101 lines (87 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
public class BigO {
public static void main(String[] args) {
BigO bigO = new BigO();
bigO.bigOn();
bigO.bigOn2();
bigO.bigOlogn();
}
public void bigOn() {
int n = 10;
for (int i = 0; i < n; i++) {
System.out.println(i);
}
}
public void bigOn2() {
int n = 10;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.println(i + ", " + j);
}
}
}
public void bigOlogn() {
int n = 8;
while (n > 1) {
n = Math.floorDiv(n, 2);
System.out.println(n);
}
}
}
/*
Contains Duplicate
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> numSet = new HashSet<>();
for (int num : nums) {
if (!numSet.add(num)) {
// If the add method returns false, it means the element was already in the set
return true;
}
}
// If we've added all elements to the set without duplicates, return false
return false;
}
}
Two Sum
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int difference = target - nums[i];
if (map.containsKey(difference)) {
return new int[] {map.get(difference), i};
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
}
MaxFrequencyStack
public class FreqStack {
private Map<Integer, Integer> countMap;
private Map<Integer, Stack<Integer>> stackMap;
private int maxCount;
public FreqStack() {
countMap = new HashMap<>();
stackMap = new HashMap<>();
maxCount = 0;
}
public void push(int val) {
int newCountOfVal = countMap.getOrDefault(val, 0) + 1;
countMap.put(val, newCountOfVal);
if (newCountOfVal > maxCount) {
maxCount = newCountOfVal;
stackMap.put(newCountOfVal, new Stack<Integer>());
}
stackMap.get(newCountOfVal).push(val);
}
public int pop() {
int valueForPop = stackMap.get(maxCount).pop();
countMap.put(valueForPop, countMap.get(valueForPop) - 1);
if (stackMap.get(maxCount).isEmpty()) {
maxCount--;
}
return valueForPop;
}
}
*/