Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
You have n gardens, labeled from 1 to n, and an array paths where paths[i] = [xi, yi] describes a bidirectional path between garden xi to garden yi. In each garden, you want to plant one of 4 types of flowers.
All gardens have at most 3 paths coming into or leaving it.
Your task is to choose a flower type for each garden such that, for any two gardens connected by a path, they have different types of flowers.
Return any such a choice as an array answer, where answer[i] is the type of flower planted in the (i+1)th garden. The flower types are denoted 1, 2, 3, or 4. It is guaranteed an answer exists.
Example 1:
Input: n = 3, paths = [[1,2],[2,3],[3,1]] Output: [1,2,3] Explanation: Gardens 1 and 2 have different types. Gardens 2 and 3 have different types. Gardens 3 and 1 have different types. Hence, [1,2,3] is a valid answer. Other valid answers include [1,2,4], [1,4,2], and [3,2,1].
Example 2:
Input: n = 4, paths = [[1,2],[3,4]] Output: [1,2,1,2]
Example 3:
Input: n = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]] Output: [1,2,3,4]
Constraints:
1 <= n <= 1040 <= paths.length <= 2 * 104paths[i].length == 21 <= xi, yi <= nxi != yiProblem summary: You have n gardens, labeled from 1 to n, and an array paths where paths[i] = [xi, yi] describes a bidirectional path between garden xi to garden yi. In each garden, you want to plant one of 4 types of flowers. All gardens have at most 3 paths coming into or leaving it. Your task is to choose a flower type for each garden such that, for any two gardens connected by a path, they have different types of flowers. Return any such a choice as an array answer, where answer[i] is the type of flower planted in the (i+1)th garden. The flower types are denoted 1, 2, 3, or 4. It is guaranteed an answer exists.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
3 [[1,2],[2,3],[3,1]]
4 [[1,2],[3,4]]
4 [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1042: Flower Planting With No Adjacent
class Solution {
public int[] gardenNoAdj(int n, int[][] paths) {
List<Integer>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (var p : paths) {
int x = p[0] - 1, y = p[1] - 1;
g[x].add(y);
g[y].add(x);
}
int[] ans = new int[n];
boolean[] used = new boolean[5];
for (int x = 0; x < n; ++x) {
Arrays.fill(used, false);
for (int y : g[x]) {
used[ans[y]] = true;
}
for (int c = 1; c < 5; ++c) {
if (!used[c]) {
ans[x] = c;
break;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #1042: Flower Planting With No Adjacent
func gardenNoAdj(n int, paths [][]int) []int {
g := make([][]int, n)
for _, p := range paths {
x, y := p[0]-1, p[1]-1
g[x] = append(g[x], y)
g[y] = append(g[y], x)
}
ans := make([]int, n)
for x := 0; x < n; x++ {
used := [5]bool{}
for _, y := range g[x] {
used[ans[y]] = true
}
for c := 1; c < 5; c++ {
if !used[c] {
ans[x] = c
break
}
}
}
return ans
}
# Accepted solution for LeetCode #1042: Flower Planting With No Adjacent
class Solution:
def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:
g = defaultdict(list)
for x, y in paths:
x, y = x - 1, y - 1
g[x].append(y)
g[y].append(x)
ans = [0] * n
for x in range(n):
used = {ans[y] for y in g[x]}
for c in range(1, 5):
if c not in used:
ans[x] = c
break
return ans
// Accepted solution for LeetCode #1042: Flower Planting With No Adjacent
impl Solution {
pub fn garden_no_adj(n: i32, paths: Vec<Vec<i32>>) -> Vec<i32> {
let n = n as usize;
let mut g = vec![vec![]; n];
for path in paths {
let (x, y) = (path[0] as usize - 1, path[1] as usize - 1);
g[x].push(y);
g[y].push(x);
}
let mut ans = vec![0; n];
for x in 0..n {
let mut used = [false; 5];
for &y in &g[x] {
used[ans[y] as usize] = true;
}
for c in 1..5 {
if !used[c] {
ans[x] = c as i32;
break;
}
}
}
ans
}
}
// Accepted solution for LeetCode #1042: Flower Planting With No Adjacent
function gardenNoAdj(n: number, paths: number[][]): number[] {
const g: number[][] = Array.from({ length: n }, () => []);
for (const [x, y] of paths) {
g[x - 1].push(y - 1);
g[y - 1].push(x - 1);
}
const ans: number[] = Array(n).fill(0);
for (let x = 0; x < n; ++x) {
const used: boolean[] = Array(5).fill(false);
for (const y of g[x]) {
used[ans[y]] = true;
}
for (let c = 1; c < 5; ++c) {
if (!used[c]) {
ans[x] = c;
break;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.