Question Definition
Given a non-negative integer N, find the largest number that is less than or equal to N with monotone increasing digits.
(Recall that an integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.)
Example 1:
Example 2:
Input: N = 1234
Output: 1234
Example 3:
Input: N = 332
Output: 299
Note: N is an integer in the range [0, 10^9]
.
Java Solution
public int monotoneIncreasingDigits(int N) {
if(N < 10) return N;
String s = Integer.toString(N);
StringBuilder result = new StringBuilder();
int i = 1;
for(; i < s.length(); i++){
if(s.charAt(i) < s.charAt(i - 1)){
break;
}
result.append(s.charAt(i - 1));
}
if(i == s.length()) return N;
i--;
while(i >= 1 && s.charAt(i - 1) == s.charAt(i)) {
i--;
result.deleteCharAt(result.length() - 1);
}
if(s.charAt(i) != '0')
result.append(s.charAt(i) - '0' - 1);
for(int j = i + 1; j < s.length(); j++)
result.append('9');
return Integer.parseInt(result.toString());
}
Question Definition
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231.
Find the maximum result of ai XOR aj, where 0 ≤ i, j < n.
Could you do this in O(n) runtime?
More …
Question Definition
Sort a linked list using insertion sort.
More …
Question Definition
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]
.
You have a car with an unlimited gas tank and it costs cost[i]
of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station’s index if you can travel around the circuit once, otherwise return -1.
More …
Question Definition
In the world of Dota2, there are two parties: the Radiant and the Dire.
The Dota2 senate consists of senators coming from two parties. Now the senate wants to make a decision about a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:
Ban one senator’s right:
A senator can make another senator lose all his rights in this and all the following rounds.
Announce the victory:
If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and make the decision about the change in the game.
Given a string representing each senator’s party belonging. The character ‘R’ and ‘D’ represent the Radiant party and the Dire party respectively. Then if there are n senators, the size of the given string will be n.
The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.
Suppose every senator is smart enough and will play the best strategy for his own party, you need to predict which party will finally announce the victory and make the change in the Dota2 game. The output should be Radiant or Dire.
Example 1:
Input: "RD"
Output: "Radiant"
Explanation: The first senator comes from Radiant and he can just ban the next senator's right in the round 1.
And the second senator can't exercise any rights any more since his right has been banned.
And in the round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
Example 2:
Input: "RDD"
Output: "Dire"
Explanation:
The first senator comes from Radiant and he can just ban the next senator's right in the round 1.
And the second senator can't exercise any rights anymore since his right has been banned.
And the third senator comes from Dire and he can ban the first senator's right in the round 1.
And in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
Note:
- The length of the given string will in the range
[1, 10,000]
.
Java Solution
public String predictPartyVictory(String senate) {
char[] chars = senate.toCharArray();
int rCount = 0;
int dCount = 0;
while (true) {
boolean changed = false;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (c == 'D') {
if (rCount > 0) {
rCount--;
changed = true;
continue;
} else {
sb.append(c);
dCount++;
}
} else {
if (dCount > 0) {
dCount--;
changed = true;
continue;
} else {
sb.append(c);
rCount++;
}
}
}
if (changed) {
chars = sb.toString().toCharArray();
} else {
return sb.charAt(0) == 'D' ? "Dire" : "Radiant";
}
}
}