Check whether the integer and twice the number 1346. exist
Give you an integer array arr, please check if there are two integers N and M, N satisfying M is twice (i.e., N = 2 * M).
More formally, check if there are two indices i and j are met:
i != j
0 <= i, j < arr.length
arr[i] == 2 * arr[j]
Example 1:
Input: arr = [10,2,5,3]
Output: true
interpretation: N = 10 is M = 5 twice, i.e. 2 * 5 = 10.
Example 2:
Input: arr = [7,1,14,11]
Output: true
interpretation: N = 14 is twice M = 7, i.e., 14 * 2 = 7.
Example 3:
Input: arr = [3,1,7,11]
Output: false
interpretation: in this case the absence of N and M satisfy N = 2 * M.
prompt:
2 <= arr.length <= 500
-10^3 <= arr[i] <= 10^3
Thinking
Note 2 times 0 is still 0 itself, so to count the number of zeros in the array
Code
class Solution {
public boolean checkIfExist(int[] arr) {
int num0 = 0;
HashSet<Integer> set = new HashSet<>();
for (int a: arr) {
if (a != 0) {
set.add(a);
if (set.contains(a * 2)) {
return true;
}
} else {
++num0;
}
}
if (num0 >= 2) {
return true;
}
for (int a: arr) {
if (a != 0) {
if (set.contains(a * 2)) {
return true;
}
}
}
return false;
}
}
Producing step 1347. The minimum number of letters words ectopic
Give you two equal length strings s and t. Each step, you can choose to replace any one character to another character of t.
T be returned so that ectopic letter word s minimum number of steps.
Ectopic word letters refer to the same letters, but arranged in different strings.
Example 1:
Output: s = "bab", t = "aba"
Output: 1
Note: replaced with 'b' t in the first 'a', t = "bba " s is a letter of the word ectopic.
Example 2:
Output: s = "leetcode", t = "practice"
Output: 5
Note: replaced by the appropriate character in t 'p', 'r', 'a', 'i' and 'c', so that t becomes s letter ectopic words.
Example 3:
Output: s = "anagram", t = "mangaar"
Output: 0
Tip: "anagram" and "mangaar" is itself a group of letters ectopic words.
Example 4:
Output: s = "xxyyzz", t = "xxyyzz"
Output: 0
Example 5:
Output: s = "friend", t = "family"
Output: 4
prompt:
. 1 <= s.length <= 50000
s.length t.length ==
S and contain only lowercase letters t
Thinking
HashMap statistics with the number of strings each letter appears
Code
class Solution {
public int minSteps(String s, String t) {
int[] cnts = new int[26];
for (char ch: s.toCharArray()) {
int c = ch - 'a';
++cnts[c];
}
for (char ch: t.toCharArray()) {
int c = ch - 'a';
if (cnts[c] > 0) {
--cnts[c];
}
}
int ans = 0;
for (int c: cnts) {
ans += c;
}
return ans;
}
}
1348. count Tweets
You can support the realization of a tweet count class the following two methods TweetCounts
:
1. recordTweet(string tweetName, int time)
Tweets posted record: The user tweetName
at the time (in seconds) time posted a tweet.
2. getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime)
Returns from the start time startTime
(in seconds) to end time endTime
(in seconds) of each 分 minute
, 时 hour
or a 日 day
specified user (depending freq) the tweetName
total number of packets release push.
freq
The value is always 分 minute
, 时 hour
or 日 day
one, a user retrieves a specified tweetName
time interval the number of tweet.
The first time interval is always from startTime start, the time interval [startTime, startTime + delta*1>, [startTime + delta*1, startTime + delta*2>, [startTime + delta*2, startTime + delta*3>, ... , [startTime + delta*i, min(startTime + delta*(i+1), endTime + 1)>
, wherein i and delta (depending freq) are non-negative integers.
Example:
输入:
[“TweetCounts”,“recordTweet”,“recordTweet”,“recordTweet”,“getTweetCountsPerFrequency”,“getTweetCountsPerFrequency”,“recordTweet”,“getTweetCountsPerFrequency”]
[[],[“tweet3”,0],[“tweet3”,60],[“tweet3”,10],[“minute”,“tweet3”,0,59],[“minute”,“tweet3”,0,60],[“tweet3”,120],[“hour”,“tweet3”,0,210]]
Output:
[null, null, null, null, [2], [2,1], null, [. 4]]
Explanation:
TweetCounts tweetCounts = new TweetCounts();
tweetCounts.recordTweet("tweet3", 0);
tweetCounts.recordTweet("tweet3", 60);
tweetCounts.recordTweet("tweet3", 10); // "tweet3" 发布推文的时间分别是 0, 10 和 60 。
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59); // 返回 [2]。统计频率是每分钟(60 秒),因此只有一个有效时间间隔 [0,60> - > 2 条推文。
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // 返回 [2,1]。统计频率是每分钟(60 秒),因此有两个有效时间间隔 1) [0,60> - > 2 条推文,和 2) [60,61> - > 1 条推文。
tweetCounts.recordTweet("tweet3", 120); // "tweet3" 发布推文的时间分别是 0, 10, 60 和 120 。
tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210); // 返回 [4]。统计频率是每小时(3600 秒),因此只有一个有效时间间隔 [0,211> - > 4 条推文。
prompt:
Taking into account recordTweet and getTweetCountsPerFrequency, there are up to 10,000 operations.
0 <= Time, the startTime, endTime <^ = 10. 9
0 <= endTime - the startTime <= 10. 4 ^
Thinking
Simulation questions, more detail is not easy to do once
Code
class TweetCounts {
private HashMap<String, ArrayList<Integer>> userTweets = new HashMap<>();
private HashMap<String, Integer> TIME_FREQ = new HashMap<>();
public TweetCounts() {
TIME_FREQ.put("minute", 60);
TIME_FREQ.put("hour", 3600);
TIME_FREQ.put("day", 86400);
}
public void recordTweet(String tweetName, int time) {
if (!userTweets.containsKey(tweetName)) {
userTweets.put(tweetName, new ArrayList<Integer>());
}
userTweets.get(tweetName).add(time);
}
public List<Integer> getTweetCountsPerFrequency(String freq, String tweetName, int startTime, int endTime) {
int sep = TIME_FREQ.get(freq), num = (endTime - startTime)/sep+1;
Integer[] ans = new Integer[num];
Arrays.fill(ans, 0);
if (!userTweets.containsKey(tweetName)) {
return Arrays.asList(ans);
}
ArrayList<Integer> tweets = userTweets.get(tweetName);
Collections.sort(tweets);
int left = startTime, right = startTime + sep, cnt = 0, idx = 0, i = 0, n = tweets.size();
for (i=0; i<n; ++i) {
int tweet = tweets.get(i);
if (tweet > endTime) {
break;
}
if (tweet < startTime) {
continue;
}
if (tweet >= left && tweet < right) {
++cnt;
} else {
ans[idx++] = cnt;
cnt = 0;
left = right;
right += sep;
--i;
}
}
ans[idx] = cnt;
return Arrays.asList(ans);
}
}
/**
* Your TweetCounts object will be instantiated and called as such:
* TweetCounts obj = new TweetCounts();
* obj.recordTweet(tweetName,time);
* List<Integer> param_2 = obj.getTweetCountsPerFrequency(freq,tweetName,startTime,endTime);
*/
5335. The maximum number of students taking the exam
Give you a m * n matrix representation seats in a classroom seats distribution. If the seat is bad (not available), use '#' indicates; otherwise, denoted by '.'.
Students can see on the left, right, upper left, upper right next to the four directions of his students answer, but students can not see him sitting directly in front of or behind the answer. Please work and returns the maximum number of students taking the exam together and can not cheat the examination room can hold.
Students must be in good condition sitting on the seat.
Example 1:
Input:
".". "" Seats = [[ "#",, "#", "#",, "#"],
. "" [, "#", "#", "#" ".", "#"],
".". "" [ "#" "#" "#" "#"]]
output: 4
to explain: teachers can make four students sitting available on the seat, so that they will not be able to cheat in exams.
Example 2:
Input:
. "" Seats = [[, "#"],
[ "#", "#"],
. "" [ "#",],
[ "#", "#"],
[ " . "," # "]]
output: 3
explained: let all the students sitting in available seat.
Example 3:
Input:
".". "". "" Seats = [[ "#",,,, "#"],
[, "". "
prompt:
seats only character '.' and '#'
m == seats.length
n-seats == [I] .length
. 1 <= m <=. 8
. 1 <= n-<=. 8
Thinking
State compression dynamic programming. Since the state only on the next line on the line, so you can use one-dimensional dynamic programming, all possible states for each line of compressed binary.
Code
class Solution {
/**
* Check whether student seat state (state0, state1) and seat situation is valid
* @param state0: previous row student seat state
* @param state1: current row student seat state
* @param row: seat situation of current row
*/
private boolean checkState(int state0, int state1, char[] row) {
int col = 0, n = row.length, curLeft = -2;
for (col=0; col<n; ++col) {
if ((state1 & (1<<col)) > 0) {
if (row[col] == '#') {
return false; // student sites on invalid seat
}
if (col - curLeft == 1) {
return false; // left student on same row
}
if (((col-1>=0) && ((state0 & (1<<(col-1))) > 0)) || ((col+1<n) && ((state0 & (1<<(col+1))) > 0))) {
return false; // left/right student on previous row
}
curLeft = col;
}
}
return true;
}
/**
* Calculate number of students from row state number
*/
private int calValue(int state) {
int ret = 0, i = 1;
while (i <= state) {
if ((state & i) > 0) {
++ret;
}
i <<= 1;
}
return ret;
}
public int maxStudents(char[][] seats) {
int m = seats.length, n = seats[0].length, state = 0, pres = 0, j = 0, max_state = (1<<n), val = 0;
int[] dp = new int[max_state], dp1 = new int[max_state];
for (state=0; state<max_state; ++state) {
if (checkState(0, state, seats[0])) {
dp[state] = calValue(state);
}
}
for (j=1; j<m; ++j) {
// for (state=0; state<max_state; ++state) {
// System.out.print(dp[state] + ", ");
// }
// System.out.println();
for (state=0; state<max_state; ++state) {
if (!checkState(0, state, seats[j])) {
continue;
}
val = calValue(state);
for (pres = 0; pres<max_state; ++pres) {
if (checkState(pres, state, seats[j])) {
dp1[state] = Math.max(dp[pres] + val, dp1[state]);
}
}
}
// for (state=0; state<max_state; ++state) {
// System.out.print(dp1[state] + ", ");
// }
// System.out.println();
dp = Arrays.copyOf(dp1, max_state);
Arrays.fill(dp1, 0);
}
val = 0;
for (state=0; state<max_state; ++state) {
val = Math.max(val, dp[state]);
}
return val;
}
}