Skip to content

Commit 4b4088d

Browse files
committed
Java solution 274 & 299
1 parent 2a71961 commit 4b4088d

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

java/_274H_Index.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
3+
*
4+
* According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
5+
*
6+
* For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.
7+
*
8+
* Note: If there are several possible values for h, the maximum one is taken as the h-index.
9+
*/
10+
public class _274H_Index {
11+
public int hIndex(int[] citations) {
12+
if (citations == null || citations.length == 0) {
13+
return 0;
14+
}
15+
int n = citations.length;
16+
int[] cnts = new int[n + 1];
17+
for (int citation : citations) {
18+
if (citation >= n) {
19+
cnts[n]++;
20+
} else {
21+
cnts[citation]++;
22+
}
23+
}
24+
int count = 0;
25+
for (int i = n; i >= 0; i--) {
26+
count += cnts[i];
27+
if (count >= i) {
28+
return i;
29+
}
30+
}
31+
return 0;
32+
}
33+
}

java/_299BullsandCows.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number.
3+
*
4+
* For example:
5+
*
6+
* Secret number: "1807"
7+
* Friend's guess: "7810"
8+
* Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.)
9+
* Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return "1A3B".
10+
*
11+
* Please note that both secret number and friend's guess may contain duplicate digits, for example:
12+
*
13+
* Secret number: "1123"
14+
* Friend's guess: "0111"
15+
* In this case, the 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return "1A1B".
16+
* You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.
17+
*/
18+
public class _299BullsandCows {
19+
public String getHint(String secret, String guess) {
20+
int a = 0;
21+
int b = 0;
22+
int[] array1 = new int[10];
23+
int[] array2 = new int[10];
24+
for (int i = 0; i < secret.length(); i++) {
25+
if (secret.charAt(i) == guess.charAt(i)) {
26+
a++;
27+
} else {
28+
array1[secret.charAt(i) - '0']++;
29+
array2[guess.charAt(i) - '0']++;
30+
}
31+
}
32+
for (int i = 0; i < 10; i++) {
33+
b += Math.min(array1[i], array2[i]);
34+
}
35+
String result = a + "A" + b + "B";
36+
return result;
37+
}
38+
}

0 commit comments

Comments
 (0)