이번 과제는 진짜 오랜만에 푸는 알고리즘 문제 ~.~
public class Solution
{
static int[] dx = { -1, 1, 0, 0 };
static int[] dy = { 0, 0, -1, 1 };
static Queue<int[]> queue = new Queue<int[]>();
static bool[,] visited;
public static int[][] FloodFill(int[][] image, int sr, int sc, int color)
{
visited = new bool[image.Length, image[0].Length];
int curColor = image[sr][sc];
image[sr][sc] = color;
visited[sr, sc] = true;
queue.Enqueue(new int[] { sr, sc });
while (queue.Count > 0)
{
int[] curPos = queue.Dequeue();
int curX = curPos[0];
int curY = curPos[1];
for (int i = 0; i < 4; i++)
{
int nextX = curX + dx[i];
int nextY = curY + dy[i];
if (nextX < 0 || nextY < 0 || nextX >= image.Length || nextY >= image[0].Length) continue;
if (!visited[nextX, nextY] && image[nextX][nextY] == curColor)
{
visited[nextX, nextY] = true;
image[nextX][nextY] = color;
queue.Enqueue(new int[] { nextX, nextY });
}
}
}
return image;
}
}
public class Solution
{
public static int LengthOfLIS(int[] nums)
{
int[] longestSub = new int[nums.Length];
for (int i = 0; i < nums.Length; i++)
{
longestSub[i] = 1;
for (int j = 0; j <= i - 1; j++)
{
if ((nums[j] < nums[i]) && (longestSub[j] + 1 > longestSub[i]))
{
longestSub[i] = longestSub[j] + 1;
}
}
}
return longestSub.Max();
}
}
오늘은 여기까지!!!
끗~