After finalizing team project on yesterday, I have received the new personal project with the new lecture. Even the personal project should be done by myself, I participated in the team to enhance my skill by learning and sharing the progress. Note that this is not finalized S.A but the draft of the S.A
S.A page
To repeat the algorithm with the practice, the all student including me had received the assignments.
First of all, I have to calculate the largest rectangular area of histogram. The description is that
Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.
In order to caculate the area, I approached this question with the three variables; width, starting index and common heights for width. My answer sheet can be seen in below code block
public class Solution {
public int LargestRectangleArea(int[] heights) {
int area =0;
for(int w=1; w<heights.Length; w++)
{
for(int idx=0; idx<heights.Length; idx++)
{
int com_h = heights[idx];
for(int t=idx; t<(idx+w>heights.Length+1?heights.Length:idx+w); t++)
{
if(t+w > heights.Length+1)
{
com_h = 0;
break;
}
if( com_h > heights[t])
com_h = heights[t];
}
if(area < w*com_h)
area = w*com_h;
}
}
return area;
}
}
This script flows from the initial common heights of starting index, gave the condition to compare the one which is the smallest height in the width. Finally, the area which is wider than the previous one is stored in the area
and retrieved it.
I have been seen this method from the perfume. the perfume spead its aromatic scent spherically. Imaging this tactics, I read the description of the problem.
An image is represented by an
m x n
integer grid image whereimage[i][j]
represents the pixel value of the image.
You are also given three integerssr
,sc
, andcolor
. You should perform a flood fill on the image starting from the pixel image[sr]
[sc]
.
To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with color.
Return the modified image after performing the flood fill.
Before to address the problem, I thought the method of recursion due to the spead method. However, we have to be carefull when you try to use recursion. To avoid the infinite loop, the strict condition should be inserted in the function. Considering the condition, I write the answer like below code block
public class Solution {
public int[][] FloodFill(int[][] image, int sr, int sc, int color) {
if(image[sr][sc] != color)
{
int[][] t = new int[image.Length][];
for(int a=0; a<image.Length; a++)
t[a] = new int[image.Length];
int prev = image[sr][sc];
image[sr][sc] = color;
if(sr > 0 && image[sr-1][sc] == prev)
t = FloodFill(image, sr-1,sc,color);
if(sr < image.Length-1 && image[sr+1][sc] == prev)
t = FloodFill(image, sr+1,sc,color);
if(sc > 0 && image[sr][sc-1] == prev)
t = FloodFill(image, sr,sc-1,color);
if(sc <image[0].Length-1 && image[sr][sc+1] == prev)
t = FloodFill(image, sr,sc+1,color);
}
return image;
}
}
Before to start the personal project, we have to awake the unity skills which was forgotton by last console project. Beginning from the easy games, we reminded the basics of unity structure. The first simple project is the 'ping pong' games. With the basic square sprites, we made the two of player's object with the collider. The text UI also used for implementing score system. the sample play can be seen in the bottom.