LeetCode 838. Push Dominoes Shortest Distance to a Character

原题链接在这里:https://leetcode.com/problems/push-dominoes/

题目:

There are N dominoes in a line, and we place each domino vertically upright.

In the beginning, we simultaneously push some of the dominoes either to the left or to the right.

After each second, each domino that is falling to the left pushes the adjacent domino on the left.

Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.

When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.

For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.

Given a string "S" representing the initial state. S[i] = 'L', if the i-th domino has been pushed to the left; S[i] = 'R', if the i-th domino has been pushed to the right; S[i] = '.', if the i-th domino has not been pushed.

Return a string representing the final state. 

Example 1:

Input: ".L.R...LR..L.."
Output: "LL.RR.LLRRLL.."

Example 2:

Input: "RR.L"
Output: "RR.L"
Explanation: The first domino expends no additional force on the second domino.

Note:

  1. 0 <= N <= 10^5
  2. String dominoes contains only 'L', 'R' and '.'

题解:

Try to find the cloest L or R on both sides.

If left side and right side are the same, then middle part should be filled the same, too.

If left is L and right is R, then middle part '.' should not change.

If left is R and right is L, then both sides push to middle.

Use two pointers i, and j to find out both sides cloest L or R.

If it is '.' , then j move until it is finds L or R.

Add a L at the beginning and R at the end to the original string to handle starting and ending '.'

When got the j, calcualte the middle count. Fill the i character. Then the middle part. Then i = j, j++.

Time Compelxity: O(dominoes.length()).

Space: O(1). regardless res.

AC Java:

 1 class Solution {
 2     public String pushDominoes(String dominoes) {
 3         if(dominoes == null || dominoes.length() == 0){
 4             return dominoes;
 5         }
 6         
 7         StringBuilder sb = new StringBuilder();
 8         String d = "L"+dominoes+"R";
 9         int i = 0;
10         int j = 1;
11         while(j < d.length()){
12             if(d.charAt(j) == '.'){
13                 j++;
14                 continue;
15             }
16             
17             int middleCount = j-i-1;
18             if(i>0){
19                 sb.append(d.charAt(i));
20             }
21             
22             // Both sides, cloest point values are the same,
23             // so append all middle part with the same value
24             if(d.charAt(i) == d.charAt(j)){
25                 for(int k = 0; k<middleCount; k++){
26                     sb.append(d.charAt(j));
27                 }
28             }
29             
30             // Cloest left side is L, cloest right side is R, 
31             // then middle part should still be .
32             if(d.charAt(i)=='L' && d.charAt(j)=='R'){
33                 for(int k = 0; k<middleCount; k++){
34                     sb.append('.');
35                 }
36             }
37             
38             // Cloest left side is R, cloest right side is L,
39             // then both sides push to middle, half and half
40             if(d.charAt(i)=='R' && d.charAt(j)=='L'){
41                 for(int k = 0; k<middleCount/2; k++){
42                     sb.append('R');
43                 }
44                 
45                 if(middleCount%2 == 1){
46                     sb.append('.');
47                 }
48                 
49                 for(int k = 0; k<middleCount/2; k++){
50                     sb.append('L');
51                 }
52             }
53             
54             i=j;
55             j++;
56         }
57         
58         return sb.toString();
59     }
60 }

类似Shortest Distance to a Character.

猜你喜欢

转载自www.cnblogs.com/Dylan-Java-NYC/p/11404838.html
今日推荐